Skip to content

File syn_eth.c

FileList > net > syn_eth.c

Go to the source code of this file

Zero-Heap Native Ethernet II & ARP Protocol Engine Implementation.

  • #include "syntropic/net/syn_eth.h"
  • #include "syntropic/net/syn_icmp.h"
  • #include "syntropic/net/syn_tcp.h"
  • #include "syntropic/net/syn_transport_udp.h"
  • #include "syntropic/net/syn_udp.h"
  • #include "syntropic/port/syn_port_system.h"
  • #include "syntropic/util/syn_crc.h"
  • #include <string.h>

Public Static Attributes

Type Name
const uint8_t MAC_BROADCAST = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}

Public Functions

Type Name
SYN_Status syn_eth_arp_lookup (SYN_ETH * eth, uint32_t ip, uint8_t mac_out)
Lookup MAC address in local ARP cache table.
SYN_Status syn_eth_arp_update (SYN_ETH * eth, uint32_t ip, const uint8_t mac)
Update or insert an entry into the local ARP cache table.
SYN_Status syn_eth_build_frame (SYN_ETH * eth, const uint8_t dst_mac, uint16_t ethertype, const uint8_t * payload, size_t payload_len, uint8_t * frame_out, size_t * frame_len)
Build and transmit an outbound Ethernet II frame.
SYN_Status syn_eth_generate_mac (const void * uid_bytes, size_t uid_len, uint8_t mac_out)
Generate a deterministic Locally Administered MAC address from MCU Unique ID.
SYN_WEAK struct SYN_ICMP * syn_eth_get_icmp_instance (void)
Weak hook — override to inject an ICMP engine into the Ethernet dispatcher.
SYN_WEAK struct SYN_TCP * syn_eth_get_tcp_instance (void)
Weak hook — override to inject a TCP engine into the Ethernet dispatcher.
SYN_Status syn_eth_init (SYN_ETH * eth, const uint8_t mac_addr, uint32_t ip_addr)
Initialize Native Ethernet Engine.
size_t syn_eth_pack_header (uint8_t * buf, size_t buf_size, const uint8_t dst_mac, const uint8_t src_mac, uint16_t ethertype)
Pack Ethernet II Header (14 bytes) into target buffer.
SYN_Status syn_eth_process_frame (SYN_ETH * eth, const uint8_t * frame, size_t len, uint8_t * tx_buf, size_t * tx_len)
Process incoming Ethernet II frame.
uint16_t syn_ip_checksum (const void * buf, size_t len)
Calculate 16-bit Internet Checksum (RFC 1071) over buffer.
size_t syn_ip_pack_header (uint8_t * buf, size_t buf_size, uint32_t src_ip, uint32_t dst_ip, uint8_t proto, uint16_t payload_len, uint16_t id)
Pack standard 20-byte IPv4 Header into target buffer.
SYN_WEAK SYN_UDP * syn_transport_udp_get_instance (void)
Weak hook to obtain UDP transport instance.

Public Static Attributes Documentation

variable MAC_BROADCAST

const uint8_t MAC_BROADCAST[6];

Broadcast MAC address (FF:FF:FF:FF:FF:FF)


Public Functions Documentation

function syn_eth_arp_lookup

Lookup MAC address in local ARP cache table.

SYN_Status syn_eth_arp_lookup (
    SYN_ETH * eth,
    uint32_t ip,
    uint8_t mac_out
) 

Parameters:

  • eth Pointer to Ethernet instance.
  • ip 32-bit IPv4 address to lookup.
  • mac_out Output buffer (6 bytes) to receive resolved MAC.

Returns:

SYN_OK if IP found in cache, SYN_NOT_FOUND if absent.


function syn_eth_arp_update

Update or insert an entry into the local ARP cache table.

SYN_Status syn_eth_arp_update (
    SYN_ETH * eth,
    uint32_t ip,
    const uint8_t mac
) 

Parameters:

  • eth Pointer to Ethernet instance.
  • ip 32-bit IPv4 address.
  • mac Resolved 6-byte MAC address.

Returns:

SYN_OK on success.


function syn_eth_build_frame

Build and transmit an outbound Ethernet II frame.

SYN_Status syn_eth_build_frame (
    SYN_ETH * eth,
    const uint8_t dst_mac,
    uint16_t ethertype,
    const uint8_t * payload,
    size_t payload_len,
    uint8_t * frame_out,
    size_t * frame_len
) 

Parameters:

  • eth Pointer to Ethernet instance.
  • dst_mac Destination MAC address (6 bytes).
  • ethertype EtherType value (e.g. SYN_ETHTYPE_IPV4).
  • payload Pointer to payload buffer.
  • payload_len Payload byte length.
  • frame_out Output frame buffer (must hold at least 1514 bytes).
  • frame_len Pointer to receive final raw Ethernet frame length.

Returns:

SYN_OK on success.


function syn_eth_generate_mac

Generate a deterministic Locally Administered MAC address from MCU Unique ID.

SYN_Status syn_eth_generate_mac (
    const void * uid_bytes,
    size_t uid_len,
    uint8_t mac_out
) 

Parameters:

  • uid_bytes Pointer to hardware unique ID bytes (e.g. STM32 96-bit UID).
  • uid_len Length of unique ID in bytes.
  • mac_out Output buffer to receive generated 6-byte MAC address.

Returns:

SYN_OK on success.


function syn_eth_get_icmp_instance

Weak hook — override to inject an ICMP engine into the Ethernet dispatcher.

SYN_WEAK struct SYN_ICMP * syn_eth_get_icmp_instance (
    void
) 

When syn_eth_process_frame receives an IPv4/ICMP packet (protocol 1), it calls this to obtain the ICMP instance. Return NULL to silently drop.

Returns:

Pointer to SYN_ICMP instance, or NULL.


function syn_eth_get_tcp_instance

Weak hook — override to inject a TCP engine into the Ethernet dispatcher.

SYN_WEAK struct SYN_TCP * syn_eth_get_tcp_instance (
    void
) 

When syn_eth_process_frame receives an IPv4/TCP packet (protocol 6), it calls this to obtain the TCP instance. Return NULL to silently drop.

Returns:

Pointer to SYN_TCP instance, or NULL.


function syn_eth_init

Initialize Native Ethernet Engine.

SYN_Status syn_eth_init (
    SYN_ETH * eth,
    const uint8_t mac_addr,
    uint32_t ip_addr
) 

Parameters:

  • eth Pointer to Ethernet instance.
  • mac_addr 6-byte hardware MAC address.
  • ip_addr 32-bit IPv4 address (host byte order).

Returns:

SYN_OK on success.


function syn_eth_pack_header

Pack Ethernet II Header (14 bytes) into target buffer.

size_t syn_eth_pack_header (
    uint8_t * buf,
    size_t buf_size,
    const uint8_t dst_mac,
    const uint8_t src_mac,
    uint16_t ethertype
) 

Parameters:

  • buf Output destination buffer.
  • buf_size Destination buffer capacity.
  • dst_mac Destination 6-byte MAC address.
  • src_mac Source 6-byte MAC address.
  • ethertype 16-bit EtherType (host byte order).

Returns:

Bytes written (14) on success, 0 on invalid param / insufficient buffer.


function syn_eth_process_frame

Process incoming Ethernet II frame.

SYN_Status syn_eth_process_frame (
    SYN_ETH * eth,
    const uint8_t * frame,
    size_t len,
    uint8_t * tx_buf,
    size_t * tx_len
) 

Parameters:

  • eth Pointer to Ethernet instance.
  • frame Pointer to raw frame bytes.
  • len Frame byte length (60..1514 bytes).
  • tx_buf Output buffer to receive immediate ARP reply if generated.
  • tx_len Pointer to receive ARP reply byte length (0 if no reply needed).

Returns:

SYN_OK on success.


function syn_ip_checksum

Calculate 16-bit Internet Checksum (RFC 1071) over buffer.

uint16_t syn_ip_checksum (
    const void * buf,
    size_t len
) 

Parameters:

  • buf Pointer to buffer.
  • len Buffer byte length.

Returns:

16-bit one's complement checksum.


function syn_ip_pack_header

Pack standard 20-byte IPv4 Header into target buffer.

size_t syn_ip_pack_header (
    uint8_t * buf,
    size_t buf_size,
    uint32_t src_ip,
    uint32_t dst_ip,
    uint8_t proto,
    uint16_t payload_len,
    uint16_t id
) 

Parameters:

  • buf Output destination buffer.
  • buf_size Destination buffer capacity.
  • src_ip Source IPv4 address (host byte order).
  • dst_ip Destination IPv4 address (host byte order).
  • proto IP Protocol number (1=ICMP, 2=IGMP, 6=TCP, 17=UDP).
  • payload_len Payload byte length after IPv4 header.
  • id 16-bit IP identification field.

Returns:

Bytes written (20) on success, 0 on invalid param / insufficient buffer.


function syn_transport_udp_get_instance

Weak hook to obtain UDP transport instance.

SYN_WEAK  SYN_UDP * syn_transport_udp_get_instance (
    void
) 

Get the active native SYN_UDP stack instance.

Returns:

Pointer to global SYN_UDP instance, or NULL if unavailable.



The documentation for this class was generated from the following file src/syntropic/net/syn_eth.c