EDGESEC  0.1.0-alpha.0+sha.ca29a8277b72f80785649ea9ef9cd7edf642d939
Secure router - reference implementation
packet_decoder.h
Go to the documentation of this file.
1 
11 #ifndef PACKET_DECODER_H
12 #define PACKET_DECODER_H
13 
14 #include <net/if.h>
15 #include <pcap.h>
16 
17 #include <utarray.h>
18 #include "../../../utils/allocs.h"
19 #include "../../../utils/attributes.h"
20 #include "../../../utils/net.h"
21 #include "../../../utils/os.h"
22 
23 #define MAX_QUESTION_LEN 255
24 
25 typedef enum packet_types {
39 
40 struct tuple_packet {
41  uint8_t *packet;
43 };
44 
49 struct eth_schema {
50  uint64_t timestamp;
52  uint32_t caplen;
53  uint32_t length;
54  char ifname[IF_NAMESIZE];
57  uint16_t ether_type;
58 };
59 
64 struct arp_schema {
66  uint16_t ar_hrd;
67  uint16_t ar_pro;
68  uint8_t ar_hln;
69  uint8_t ar_pln;
70  uint16_t ar_op;
75 };
76 
81 struct ip4_schema {
86  uint8_t ip_hl;
87  uint8_t ip_v;
88  uint8_t ip_tos;
89  uint16_t ip_len;
90  uint16_t ip_id;
91  uint16_t ip_off;
92  uint8_t ip_ttl;
93  uint8_t ip_p;
94  uint16_t ip_sum;
95 };
96 
101 struct ip6_schema {
103  uint32_t
105  uint16_t ip6_un1_plen;
106  uint8_t ip6_un1_nxt;
107  uint8_t ip6_un1_hlim;
108  uint8_t ip6_un2_vfc;
111 };
112 
117 struct tcp_schema {
119  uint16_t source;
120  uint16_t dest;
121  uint32_t seq;
122  uint32_t ack_seq;
123  uint16_t res1;
124  uint16_t doff;
125  uint16_t fin;
126  uint16_t syn;
127  uint16_t rst;
128  uint16_t psh;
129  uint16_t ack;
130  uint16_t urg;
131  uint16_t window;
132  uint16_t check_p;
133  uint16_t urg_ptr;
134 };
135 
140 struct udp_schema {
142  uint16_t source;
143  uint16_t dest;
144  uint16_t len;
145  uint16_t check_p;
146 };
147 
152 struct icmp4_schema {
154  uint8_t type;
155  uint8_t code;
156  uint16_t checksum;
157  uint32_t gateway;
158 };
159 
164 struct icmp6_schema {
166  uint8_t icmp6_type;
167  uint8_t icmp6_code;
168  uint16_t icmp6_cksum;
169  uint32_t icmp6_un_data32;
170 };
171 
176 struct dns_schema {
178  uint16_t tid;
179  uint16_t flags;
180  uint16_t nqueries;
181  uint16_t nanswers;
182  uint16_t nauth;
183  uint16_t nother;
185 };
186 
191 struct mdns_schema {
193  uint16_t tid;
194  uint16_t flags;
195  uint16_t nqueries;
196  uint16_t nanswers;
197  uint16_t nauth;
198  uint16_t nother;
200 };
201 
206 struct dhcp_schema {
208  uint8_t op;
209  uint8_t htype;
211  uint8_t hlen;
212  uint8_t hops;
213  uint32_t
214  xid;
215  uint16_t secs;
216  uint16_t flags;
224 };
225 
230 struct dns_header {
231  uint16_t tid;
232  uint16_t flags;
233  uint16_t nqueries;
234  uint16_t nanswers;
235  uint16_t nauth;
236  uint16_t nother;
237 };
238 
243 struct mdns_header {
244  uint16_t tid;
245  uint16_t flags;
246  uint16_t nqueries;
247  uint16_t nanswers;
248  uint16_t nauth;
249  uint16_t nother;
250 };
251 
257  uint16_t qtype;
259  uint16_t uresponse : 1;
261  uint16_t qclass : 15;
264 
270  uint16_t rrtype;
271  uint16_t cflush : 1;
273  uint16_t rrclass : 15;
275  uint32_t ttl;
276  uint16_t rdlength;
278 } STRUCT_PACKED;
279 
284 struct dhcp_header {
285  uint8_t op;
286  uint8_t
288  uint8_t hlen;
289  uint8_t hops;
290  uint32_t xid;
291  uint16_t secs;
292  uint16_t flags;
293  uint32_t ciaddr;
294  uint32_t
296  uint32_t siaddr;
297  uint32_t giaddr;
298  uint8_t chaddr[16];
299  uint8_t legacy[192];
300 } STRUCT_PACKED;
301 
307  struct ether_header *ethh;
308  struct ether_arp *arph;
309  struct ip *ip4h;
310  struct ip6_hdr *ip6h;
311  struct tcphdr *tcph;
312  struct udphdr *udph;
313  struct icmp *icmp4h;
314  struct icmp6_hdr *icmp6h;
315  struct dns_header *dnsh;
318  struct eth_schema eths;
319  struct arp_schema arps;
320  struct ip4_schema ip4s;
321  struct ip6_schema ip6s;
322  struct tcp_schema tcps;
323  struct udp_schema udps;
324  struct icmp4_schema icmp4s;
325  struct icmp6_schema icmp6s;
326  struct dns_schema dnss;
327  struct mdns_schema mdnss;
328  struct dhcp_schema dhcps;
329  uint64_t timestamp;
330  uint32_t caplen;
331  uint32_t length;
332  char ifname[IF_NAMESIZE];
334 };
335 
346 int extract_packets(const char *ltype, const struct pcap_pkthdr *header,
347  const uint8_t *packet, char *interface, UT_array *tp_array);
348 
349 #endif
#define OS_INET_ADDRSTRLEN
Definition: net.h:25
#define OS_INET6_ADDRSTRLEN
Definition: net.h:26
#define MACSTR_LEN
Definition: net.h:31
#define MAX_RANDOM_UUID_LEN
Definition: os.h:31
int extract_packets(const char *ltype, const struct pcap_pkthdr *header, const uint8_t *packet, char *interface, UT_array *tp_array)
Extract packets from pcap packet data.
Definition: packet_decoder.c:349
#define MAX_QUESTION_LEN
Definition: packet_decoder.h:23
packet_types
Definition: packet_decoder.h:25
@ PACKET_UDP
Definition: packet_decoder.h:32
@ PACKET_DHCP
Definition: packet_decoder.h:37
@ PACKET_NONE
Definition: packet_decoder.h:26
@ PACKET_ETHERNET
Definition: packet_decoder.h:27
@ PACKET_DNS
Definition: packet_decoder.h:35
@ PACKET_ARP
Definition: packet_decoder.h:28
@ PACKET_MDNS
Definition: packet_decoder.h:36
@ PACKET_ICMP6
Definition: packet_decoder.h:34
@ PACKET_IP4
Definition: packet_decoder.h:29
@ PACKET_IP6
Definition: packet_decoder.h:30
@ PACKET_ICMP4
Definition: packet_decoder.h:33
@ PACKET_TCP
Definition: packet_decoder.h:31
struct mdns_query_meta STRUCT_PACKED
enum packet_types PACKET_TYPES
ARP protocol schema definition.
Definition: packet_decoder.h:64
char arp_spa[OS_INET_ADDRSTRLEN]
Definition: packet_decoder.h:72
uint16_t ar_op
Definition: packet_decoder.h:70
uint8_t ar_hln
Definition: packet_decoder.h:68
uint16_t ar_pro
Definition: packet_decoder.h:67
uint16_t ar_hrd
Definition: packet_decoder.h:66
char arp_sha[MACSTR_LEN]
Definition: packet_decoder.h:71
char arp_tha[MACSTR_LEN]
Definition: packet_decoder.h:73
char arp_tpa[OS_INET_ADDRSTRLEN]
Definition: packet_decoder.h:74
uint8_t ar_pln
Definition: packet_decoder.h:69
Capture structure definition.
Definition: packet_decoder.h:306
struct dns_header * dnsh
Definition: packet_decoder.h:315
struct mdns_schema mdnss
Definition: packet_decoder.h:327
struct dhcp_header * dhcph
Definition: packet_decoder.h:317
struct tcp_schema tcps
Definition: packet_decoder.h:322
struct tcphdr * tcph
Definition: packet_decoder.h:311
char ifname[IF_NAMESIZE]
Definition: packet_decoder.h:332
struct mdns_header * mdnsh
Definition: packet_decoder.h:316
struct icmp * icmp4h
Definition: packet_decoder.h:313
struct udphdr * udph
Definition: packet_decoder.h:312
struct ether_header * ethh
Definition: packet_decoder.h:307
struct icmp6_hdr * icmp6h
Definition: packet_decoder.h:314
struct icmp6_schema icmp6s
Definition: packet_decoder.h:325
struct udp_schema udps
Definition: packet_decoder.h:323
uint32_t caplen
Definition: packet_decoder.h:330
struct dhcp_schema dhcps
Definition: packet_decoder.h:328
struct ip * ip4h
Definition: packet_decoder.h:309
struct arp_schema arps
Definition: packet_decoder.h:319
struct ip6_schema ip6s
Definition: packet_decoder.h:321
struct ether_arp * arph
Definition: packet_decoder.h:308
struct icmp4_schema icmp4s
Definition: packet_decoder.h:324
uint32_t length
Definition: packet_decoder.h:331
struct eth_schema eths
Definition: packet_decoder.h:318
struct ip4_schema ip4s
Definition: packet_decoder.h:320
struct dns_schema dnss
Definition: packet_decoder.h:326
uint64_t timestamp
Definition: packet_decoder.h:329
struct ip6_hdr * ip6h
Definition: packet_decoder.h:310
DHCP header definition (truncated)
Definition: packet_decoder.h:284
uint32_t siaddr
Definition: packet_decoder.h:296
uint16_t secs
Definition: packet_decoder.h:291
uint16_t flags
Definition: packet_decoder.h:292
uint8_t legacy[192]
Definition: packet_decoder.h:299
uint32_t xid
Definition: packet_decoder.h:290
uint32_t ciaddr
Definition: packet_decoder.h:293
uint8_t op
Definition: packet_decoder.h:285
uint8_t hlen
Definition: packet_decoder.h:288
uint8_t hops
Definition: packet_decoder.h:289
uint32_t giaddr
Definition: packet_decoder.h:297
uint32_t yiaddr
Definition: packet_decoder.h:295
uint8_t htype
Definition: packet_decoder.h:287
uint8_t chaddr[16]
Definition: packet_decoder.h:298
DHCP protocol schema definition.
Definition: packet_decoder.h:206
uint8_t hops
Definition: packet_decoder.h:212
uint8_t hlen
Definition: packet_decoder.h:211
uint32_t xid
Definition: packet_decoder.h:214
char ciaddr[OS_INET_ADDRSTRLEN]
Definition: packet_decoder.h:217
uint16_t flags
Definition: packet_decoder.h:216
char yiaddr[OS_INET_ADDRSTRLEN]
Definition: packet_decoder.h:219
char siaddr[OS_INET_ADDRSTRLEN]
Definition: packet_decoder.h:221
uint16_t secs
Definition: packet_decoder.h:215
uint8_t htype
Definition: packet_decoder.h:209
char giaddr[OS_INET_ADDRSTRLEN]
Definition: packet_decoder.h:222
char chaddr[MACSTR_LEN]
Definition: packet_decoder.h:223
uint8_t op
Definition: packet_decoder.h:208
DNS header definition.
Definition: packet_decoder.h:230
uint16_t nother
Definition: packet_decoder.h:236
uint16_t nauth
Definition: packet_decoder.h:235
uint16_t nqueries
Definition: packet_decoder.h:233
uint16_t tid
Definition: packet_decoder.h:231
uint16_t nanswers
Definition: packet_decoder.h:234
uint16_t flags
Definition: packet_decoder.h:232
DNS protocol schema definition.
Definition: packet_decoder.h:176
uint16_t tid
Definition: packet_decoder.h:178
uint16_t nqueries
Definition: packet_decoder.h:180
uint16_t nauth
Definition: packet_decoder.h:182
uint16_t flags
Definition: packet_decoder.h:179
char qname[MAX_QUESTION_LEN]
Definition: packet_decoder.h:184
uint16_t nother
Definition: packet_decoder.h:183
uint16_t nanswers
Definition: packet_decoder.h:181
Ethernet protocol schema definition.
Definition: packet_decoder.h:49
uint64_t timestamp
Definition: packet_decoder.h:50
uint32_t caplen
Definition: packet_decoder.h:52
uint16_t ether_type
Definition: packet_decoder.h:57
char ether_shost[MACSTR_LEN]
Definition: packet_decoder.h:56
char ifname[IF_NAMESIZE]
Definition: packet_decoder.h:54
char ether_dhost[MACSTR_LEN]
Definition: packet_decoder.h:55
uint32_t length
Definition: packet_decoder.h:53
ICMP4 protocol schema definition.
Definition: packet_decoder.h:152
uint8_t code
Definition: packet_decoder.h:155
uint32_t gateway
Definition: packet_decoder.h:157
uint16_t checksum
Definition: packet_decoder.h:156
uint8_t type
Definition: packet_decoder.h:154
ICMP6 protocol schema definition.
Definition: packet_decoder.h:164
uint8_t icmp6_type
Definition: packet_decoder.h:166
uint8_t icmp6_code
Definition: packet_decoder.h:167
uint32_t icmp6_un_data32
Definition: packet_decoder.h:169
uint16_t icmp6_cksum
Definition: packet_decoder.h:168
IP4 protocol schema definition.
Definition: packet_decoder.h:81
uint16_t ip_len
Definition: packet_decoder.h:89
uint8_t ip_v
Definition: packet_decoder.h:87
char ip_src[OS_INET_ADDRSTRLEN]
Definition: packet_decoder.h:83
uint8_t ip_tos
Definition: packet_decoder.h:88
uint16_t ip_off
Definition: packet_decoder.h:91
uint8_t ip_ttl
Definition: packet_decoder.h:92
uint16_t ip_sum
Definition: packet_decoder.h:94
uint8_t ip_p
Definition: packet_decoder.h:93
uint8_t ip_hl
Definition: packet_decoder.h:86
uint16_t ip_id
Definition: packet_decoder.h:90
char ip_dst[OS_INET_ADDRSTRLEN]
Definition: packet_decoder.h:84
IP6 protocol schema definition.
Definition: packet_decoder.h:101
uint8_t ip6_un1_nxt
Definition: packet_decoder.h:106
uint8_t ip6_un1_hlim
Definition: packet_decoder.h:107
uint8_t ip6_un2_vfc
Definition: packet_decoder.h:108
char ip6_src[OS_INET6_ADDRSTRLEN]
Definition: packet_decoder.h:109
char ip6_dst[OS_INET6_ADDRSTRLEN]
Definition: packet_decoder.h:110
uint32_t ip6_un1_flow
Definition: packet_decoder.h:104
uint16_t ip6_un1_plen
Definition: packet_decoder.h:105
mDNS response meta definition
Definition: packet_decoder.h:269
uint16_t rrclass
Definition: packet_decoder.h:273
uint16_t rrtype
Definition: packet_decoder.h:270
uint32_t ttl
Definition: packet_decoder.h:275
uint16_t rdlength
Definition: packet_decoder.h:276
uint16_t cflush
Definition: packet_decoder.h:271
mDNS header definition
Definition: packet_decoder.h:243
uint16_t nother
Definition: packet_decoder.h:249
uint16_t tid
Definition: packet_decoder.h:244
uint16_t flags
Definition: packet_decoder.h:245
uint16_t nqueries
Definition: packet_decoder.h:246
uint16_t nanswers
Definition: packet_decoder.h:247
uint16_t nauth
Definition: packet_decoder.h:248
mDNS query meta definition
Definition: packet_decoder.h:256
uint16_t qclass
Definition: packet_decoder.h:261
uint16_t uresponse
Definition: packet_decoder.h:259
uint16_t qtype
Definition: packet_decoder.h:257
mDNS protocol schema definition
Definition: packet_decoder.h:191
uint16_t flags
Definition: packet_decoder.h:194
uint16_t nother
Definition: packet_decoder.h:198
uint16_t tid
Definition: packet_decoder.h:193
uint16_t nauth
Definition: packet_decoder.h:197
uint16_t nanswers
Definition: packet_decoder.h:196
uint16_t nqueries
Definition: packet_decoder.h:195
char qname[MAX_QUESTION_LEN]
Definition: packet_decoder.h:199
TCP protocol schema definition.
Definition: packet_decoder.h:117
uint16_t ack
Definition: packet_decoder.h:129
uint16_t rst
Definition: packet_decoder.h:127
uint16_t window
Definition: packet_decoder.h:131
uint32_t seq
Definition: packet_decoder.h:121
uint16_t urg
Definition: packet_decoder.h:130
uint16_t doff
Definition: packet_decoder.h:124
uint16_t urg_ptr
Definition: packet_decoder.h:133
uint16_t res1
Definition: packet_decoder.h:123
uint16_t check_p
Definition: packet_decoder.h:132
uint16_t source
Definition: packet_decoder.h:119
uint32_t ack_seq
Definition: packet_decoder.h:122
uint16_t syn
Definition: packet_decoder.h:126
uint16_t fin
Definition: packet_decoder.h:125
uint16_t dest
Definition: packet_decoder.h:120
uint16_t psh
Definition: packet_decoder.h:128
Definition: packet_decoder.h:40
PACKET_TYPES type
Definition: packet_decoder.h:42
uint8_t * packet
Definition: packet_decoder.h:41
UDP protocol schema definition.
Definition: packet_decoder.h:140
uint16_t source
Definition: packet_decoder.h:142
uint16_t dest
Definition: packet_decoder.h:143
uint16_t check_p
Definition: packet_decoder.h:145
uint16_t len
Definition: packet_decoder.h:144