1 /* 2 * eth.h 3 * 4 * Ethernet. 5 * 6 * Copyright (c) 2000 Dug Song <dugsong@monkey.org> 7 * 8 * $Id: eth.h 547 2005-01-25 21:30:40Z dugsong $ 9 */ 10 11 #ifndef DNET_ETH_H 12 #define DNET_ETH_H 13 14 #define ETH_ADDR_LEN 6 15 #define ETH_ADDR_BITS 48 16 #define ETH_TYPE_LEN 2 17 #define ETH_CRC_LEN 4 18 #define ETH_HDR_LEN 14 19 20 #define ETH_LEN_MIN 64 /* minimum frame length with CRC */ 21 #define ETH_LEN_MAX 1518 /* maximum frame length with CRC */ 22 23 #define ETH_MTU (ETH_LEN_MAX - ETH_HDR_LEN - ETH_CRC_LEN) 24 #define ETH_MIN (ETH_LEN_MIN - ETH_HDR_LEN - ETH_CRC_LEN) 25 26 typedef struct eth_addr { 27 uint8_t data[ETH_ADDR_LEN]; 28 } eth_addr_t; 29 30 struct eth_hdr { 31 eth_addr_t eth_dst; /* destination address */ 32 eth_addr_t eth_src; /* source address */ 33 uint16_t eth_type; /* payload type */ 34 }; 35 36 /* 37 * Ethernet payload types - http://standards.ieee.org/regauth/ethertype 38 */ 39 #define ETH_TYPE_PUP 0x0200 /* PUP protocol */ 40 #define ETH_TYPE_IP 0x0800 /* IP protocol */ 41 #define ETH_TYPE_ARP 0x0806 /* address resolution protocol */ 42 #define ETH_TYPE_REVARP 0x8035 /* reverse addr resolution protocol */ 43 #define ETH_TYPE_8021Q 0x8100 /* IEEE 802.1Q VLAN tagging */ 44 #define ETH_TYPE_IPV6 0x86DD /* IPv6 protocol */ 45 #define ETH_TYPE_MPLS 0x8847 /* MPLS */ 46 #define ETH_TYPE_MPLS_MCAST 0x8848 /* MPLS Multicast */ 47 #define ETH_TYPE_PPPOEDISC 0x8863 /* PPP Over Ethernet Discovery Stage */ 48 #define ETH_TYPE_PPPOE 0x8864 /* PPP Over Ethernet Session Stage */ 49 #define ETH_TYPE_LOOPBACK 0x9000 /* used to test interfaces */ 50 51 #define ETH_IS_MULTICAST(ea) (*(ea) & 0x01) /* is address mcast/bcast? */ 52 53 #define ETH_ADDR_BROADCAST "\xff\xff\xff\xff\xff\xff" 54 55 #define eth_pack_hdr(h, dst, src, type) do { \ 56 struct eth_hdr *eth_pack_p = (struct eth_hdr *)(h); \ 57 memmove(ð_pack_p->eth_dst, &(dst), ETH_ADDR_LEN); \ 58 memmove(ð_pack_p->eth_src, &(src), ETH_ADDR_LEN); \ 59 eth_pack_p->eth_type = htons(type); \ 60 } while (0) 61 62 typedef struct eth_handle eth_t; 63 64 __BEGIN_DECLS 65 eth_t *eth_open(const char *device); 66 int eth_get(eth_t *e, eth_addr_t *ea); 67 int eth_set(eth_t *e, const eth_addr_t *ea); 68 ssize_t eth_send(eth_t *e, const void *buf, size_t len); 69 eth_t *eth_close(eth_t *e); 70 71 int eth_get_pcap_devname(const char *ifname, char *pcapdev, int pcapdevlen); 72 73 char *eth_ntop(const eth_addr_t *eth, char *dst, size_t len); 74 int eth_pton(const char *src, eth_addr_t *dst); 75 char *eth_ntoa(const eth_addr_t *eth); 76 #define eth_aton eth_pton 77 __END_DECLS 78 79 #endif /* DNET_ETH_H */ 80