nmsg  0.11.2
nmsg_port_net.h
1 #ifndef NMSG_PORT_NET_H
2 #define NMSG_PORT_NET_H
3 
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <netinet/ip6.h>
8 #include <arpa/inet.h>
9 #include <stdint.h>
10 
11 #ifndef ETHER_HDR_LEN
12 # define ETHER_HDR_LEN 14
13 #endif
14 
15 #ifndef ETH_ALEN
16 # define ETH_ALEN 6
17 #endif
18 
19 #ifndef IP_OFFMASK
20 # define IP_OFFMASK 0x1fff
21 #endif
22 
23 #ifndef IP_DF
24 # define IP_DF 0x4000
25 #endif
26 
27 #ifndef IP_MF
28 # define IP_MF 0x2000
29 #endif
30 
31 #ifndef ETHERTYPE_IP
32 # define ETHERTYPE_IP 0x0800
33 #endif
34 
35 #ifndef ETHERTYPE_VLAN
36 # define ETHERTYPE_VLAN 0x8100
37 #endif
38 
39 #ifndef ETHERTYPE_IPV6
40 # define ETHERTYPE_IPV6 0x86dd
41 #endif
42 
43 #ifndef IPV6_VERSION
44 # define IPV6_VERSION 0x60
45 #endif
46 
47 #ifndef IPV6_VERSION_MASK
48 # define IPV6_VERSION_MASK 0xf0
49 #endif
50 
51 #ifndef IP6F_OFF_MASK
52 # if !defined(WORDS_BIGENDIAN)
53 # define IP6F_OFF_MASK 0xf8ff
54 # else
55 # define IP6F_OFF_MASK 0xfff8
56 # endif
57 #endif
58 
59 /* Macros. */
60 
61 #define load_net16(buf, out) do { \
62  uint16_t _my_16; \
63  memcpy(&_my_16, buf, sizeof(uint16_t)); \
64  _my_16 = ntohs(_my_16); \
65  *(out) = _my_16; \
66 } while (0)
67 
68 #define load_net32(buf, out) do { \
69  uint32_t _my_32; \
70  memcpy(&_my_32, buf, sizeof(uint32_t)); \
71  _my_32 = ntohl(_my_32); \
72  *(out) = _my_32; \
73 } while (0)
74 
75 #define store_net16(buf, in) do { \
76  uint16_t _my_16 = htons(in); \
77  memcpy(buf, &_my_16, sizeof(uint16_t)); \
78 } while (0)
79 
80 #define store_net32(buf, in) do { \
81  uint32_t _my_32 = htonl(in); \
82  memcpy(buf, &_my_32, sizeof(uint32_t)); \
83 } while (0)
84 
85 /* Data types. */
86 
87 struct nmsg_ethhdr {
88  uint8_t ether_dhost[ETH_ALEN];
89  uint8_t ether_shost[ETH_ALEN];
90  uint16_t ether_type;
91 } __attribute__ ((__packed__));
92 
93 struct nmsg_iphdr {
94 #if !defined(WORDS_BIGENDIAN)
95  unsigned int ip_hl:4;
96  unsigned int ip_v:4;
97 #else
98  unsigned int ip_v:4;
99  unsigned int ip_hl:4;
100 #endif
101  uint8_t ip_tos;
102  uint16_t ip_len;
103  uint16_t ip_id;
104  uint16_t ip_off;
105  uint8_t ip_ttl;
106  uint8_t ip_p;
107  uint16_t ip_sum;
108  uint32_t ip_src;
109  uint32_t ip_dst;
110 } __attribute__ ((__packed__));
111 
112 struct nmsg_tcphdr {
113  uint16_t th_sport;
114  uint16_t th_dport;
115  uint32_t th_seq;
116  uint32_t th_ack;
117 #if !defined(WORDS_BIGENDIAN)
118  uint8_t th_x2:4;
119  uint8_t th_off:4;
120 #else
121  uint8_t th_off:4;
122  uint8_t th_x2:4;
123 #endif
124  uint8_t th_flags;
125  uint16_t th_win;
126  uint16_t th_sum;
127  uint16_t th_urp;
128 } __attribute__ ((__packed__));
129 
130 struct nmsg_udphdr {
131  uint16_t uh_sport;
132  uint16_t uh_dport;
133  uint16_t uh_ulen;
134  uint16_t uh_sum;
135 } __attribute__ ((__packed__));
136 
137 struct nmsg_icmphdr {
138  uint8_t icmp_type;
139  uint8_t icmp_code;
140  uint16_t icmp_cksum;
141 } __attribute__ ((__packed__));
142 
143 #endif /* NMSG_PORT_NET_H */