1 | /** PCAPNG META PACKET */ |
---|
2 | #include "libtrace.h" |
---|
3 | #include "libtrace_int.h" |
---|
4 | #include "libpacketdump.h" |
---|
5 | #include "format_pcapng.h" |
---|
6 | #include "byteswap.c" |
---|
7 | |
---|
8 | #include <stdio.h> |
---|
9 | #include <stdlib.h> |
---|
10 | #include <string.h> |
---|
11 | #include <netinet/in.h> |
---|
12 | #include <inttypes.h> |
---|
13 | |
---|
14 | DLLEXPORT void decode(int link_type UNUSED,const char *packet UNUSED,unsigned len UNUSED) { |
---|
15 | }; |
---|
16 | |
---|
17 | static void print_section_type(libtrace_meta_t *r) { |
---|
18 | int i; |
---|
19 | printf(" PCAPNG Section Header Block\n"); |
---|
20 | |
---|
21 | if (r == NULL) { return; } |
---|
22 | |
---|
23 | for (i=0; i<r->num; i++) { |
---|
24 | switch(r->items[i].option) { |
---|
25 | case(PCAPNG_META_SHB_HARDWARE): |
---|
26 | printf(" shb_hardware: %s\n", |
---|
27 | (char *)r->items[i].data); |
---|
28 | break; |
---|
29 | case(PCAPNG_META_SHB_OS): |
---|
30 | printf(" shb_os: %s\n", |
---|
31 | (char *)r->items[i].data); |
---|
32 | break; |
---|
33 | case(PCAPNG_META_SHB_USERAPPL): |
---|
34 | printf(" shb_userappl: %s\n", |
---|
35 | (char *)r->items[i].data); |
---|
36 | break; |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|
40 | static void print_interface_type(libtrace_meta_t *r, libtrace_packet_t *packet) { |
---|
41 | int i; |
---|
42 | struct in_addr ip; |
---|
43 | unsigned char *tmp; |
---|
44 | char *ip6, *ptr UNUSED; |
---|
45 | printf(" PCAPNG Interface Description Block\n"); |
---|
46 | |
---|
47 | if (r == NULL) { return; } |
---|
48 | |
---|
49 | for (i=0; i<r->num; i++) { |
---|
50 | switch(r->items[i].option) { |
---|
51 | case(PCAPNG_META_IF_NAME): |
---|
52 | printf(" if_name: %s\n", |
---|
53 | (char *)r->items[i].data); |
---|
54 | break; |
---|
55 | case(PCAPNG_META_IF_DESCR): |
---|
56 | printf(" if_description: %s\n", |
---|
57 | (char *)r->items[i].data); |
---|
58 | break; |
---|
59 | case(PCAPNG_META_IF_IP4): |
---|
60 | ip.s_addr = *(uint32_t *)r->items[i].data; |
---|
61 | printf(" if_IPv4addr: %s", inet_ntoa(ip)); |
---|
62 | break; |
---|
63 | case(PCAPNG_META_IF_IP6): |
---|
64 | ip6 = calloc(1, INET6_ADDRSTRLEN); |
---|
65 | ptr = trace_get_interface_ipv6_string(packet, ip6, |
---|
66 | INET6_ADDRSTRLEN, 0); |
---|
67 | printf(" if_IPv6addr: %s\n", ip6); |
---|
68 | free(ip6); |
---|
69 | break; |
---|
70 | case(PCAPNG_META_IF_MAC): |
---|
71 | tmp = r->items[i].data; |
---|
72 | printf(" if_MACaddr: %02x:%02x:%02x:%02x:%02x:%02x\n", |
---|
73 | tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5]); |
---|
74 | break; |
---|
75 | case(PCAPNG_META_IF_EUI): |
---|
76 | tmp = r->items[i].data; |
---|
77 | printf(" if_EUIaddr: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", |
---|
78 | tmp[0], tmp[1], tmp[2], tmp[3], |
---|
79 | tmp[4], tmp[5], tmp[6], tmp[7]); |
---|
80 | break; |
---|
81 | case(PCAPNG_META_IF_SPEED): |
---|
82 | printf(" if_speed: %" PRIu64 "\n", |
---|
83 | *(uint64_t *)r->items[i].data); |
---|
84 | break; |
---|
85 | case(PCAPNG_META_IF_TSRESOL): |
---|
86 | printf(" if_tsresol: %" PRIu8 "\n", |
---|
87 | *(uint8_t *)r->items[i].data); |
---|
88 | break; |
---|
89 | case(PCAPNG_META_IF_TZONE): |
---|
90 | /* Waiting for specification to specify */ |
---|
91 | break; |
---|
92 | case(PCAPNG_META_IF_FILTER): |
---|
93 | printf(" if_filter: %" PRIu8 "", |
---|
94 | *(uint8_t *)r->items[i].data); |
---|
95 | printf(" %s\n", |
---|
96 | (char *)r->items[i].data+sizeof(uint8_t)); |
---|
97 | break; |
---|
98 | case(PCAPNG_META_IF_OS): |
---|
99 | printf(" if_os: %s\n", |
---|
100 | (char *)r->items[i].data); |
---|
101 | break; |
---|
102 | case(PCAPNG_META_IF_FCSLEN): |
---|
103 | printf(" if_fcslen: %" PRIu8 "\n", |
---|
104 | *(uint8_t *)r->items[i].data); |
---|
105 | break; |
---|
106 | case(PCAPNG_META_IF_TSOFFSET): |
---|
107 | printf(" if_tsoffset: %" PRIu64 "\n", |
---|
108 | *(uint64_t *)r->items[i].data); |
---|
109 | break; |
---|
110 | case(PCAPNG_META_IF_HARDWARE): |
---|
111 | printf(" if_hardware: %s\n", |
---|
112 | (char *)r->items[i].data); |
---|
113 | break; |
---|
114 | default: |
---|
115 | break; |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | static void print_name_resolution_type(libtrace_meta_t *r) { |
---|
121 | int i; |
---|
122 | struct in_addr ip; |
---|
123 | printf(" PCAPNG Name Resolution\n"); |
---|
124 | if (r == NULL) { return; } |
---|
125 | |
---|
126 | for (i=0; i<r->num; i++) { |
---|
127 | switch(r->items[i].option) { |
---|
128 | case(PCAPNG_META_NRB_RECORD_IP4): |
---|
129 | ip.s_addr = *(uint32_t *)r->items[i].data; |
---|
130 | printf(" nrb_record_ipv4: %s dns_name: %s\n", |
---|
131 | inet_ntoa(ip), |
---|
132 | (char *)(r->items[i].data+sizeof(uint32_t))); |
---|
133 | break; |
---|
134 | case(PCAPNG_META_NRB_RECORD_IP6): |
---|
135 | /* todo - need to find an example */ |
---|
136 | break; |
---|
137 | //case(PCAPNG_META_NS_DNSNAME): |
---|
138 | // printf(" ns_dnsname: %s\n", |
---|
139 | // (char *)r->items[i].data); |
---|
140 | // break; |
---|
141 | //case(PCAPNG_META_NS_DNS_IP4_ADDR): |
---|
142 | // printf(" ns_dnsIP4addr: %u.%u.%u.%u\n", |
---|
143 | // *(uint8_t *)r->items[i].data, |
---|
144 | // *(uint8_t *)r->items[i].data+8, |
---|
145 | // *(uint8_t *)r->items[i].data+16, |
---|
146 | // *(uint8_t *)r->items[i].data+24); |
---|
147 | // break; |
---|
148 | //case(PCAPNG_META_NS_DNS_IP6_ADDR): |
---|
149 | // /* todo - need to find an example */ |
---|
150 | // break; |
---|
151 | default: |
---|
152 | break; |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | static void print_interface_statistics_type(libtrace_meta_t *r) { |
---|
158 | int i; |
---|
159 | printf(" PCAPNG Interface Statistics\n"); |
---|
160 | |
---|
161 | if (r == NULL) { return; } |
---|
162 | |
---|
163 | for (i=0; i<r->num; i++) { |
---|
164 | switch(r->items[i].option) { |
---|
165 | case(PCAPNG_META_ISB_STARTTIME): |
---|
166 | /* Need to split into 4 octets */ |
---|
167 | printf(" isb_starttime: %" PRIu64 "\n", |
---|
168 | *(uint64_t *)r->items[i].data); |
---|
169 | break; |
---|
170 | case(PCAPNG_META_ISB_ENDTIME): |
---|
171 | printf(" isb_endtime: %" PRIu64 "\n", |
---|
172 | *(uint64_t *)r->items[i].data); |
---|
173 | break; |
---|
174 | case(PCAPNG_META_ISB_IFRECV): |
---|
175 | printf(" isb_ifrecv: %" PRIu64 "\n", |
---|
176 | *(uint64_t *)r->items[i].data); |
---|
177 | break; |
---|
178 | case(PCAPNG_META_ISB_IFDROP): |
---|
179 | printf(" isb_ifdrop: %" PRIu64 "\n", |
---|
180 | *(uint64_t *)r->items[i].data); |
---|
181 | break; |
---|
182 | case(PCAPNG_META_ISB_FILTERACCEPT): |
---|
183 | printf(" isb_filteraccept: %" PRIu64 "\n", |
---|
184 | *(uint64_t *)r->items[i].data); |
---|
185 | break; |
---|
186 | case(PCAPNG_META_ISB_OSDROP): |
---|
187 | printf(" isb_osdrop: %" PRIu64 "\n", |
---|
188 | *(uint64_t *)r->items[i].data); |
---|
189 | break; |
---|
190 | case(PCAPNG_META_ISB_USRDELIV): |
---|
191 | printf(" isb_usrdeliv: %" PRIu64 "\n", |
---|
192 | *(uint64_t *)r->items[i].data); |
---|
193 | break; |
---|
194 | default: |
---|
195 | break; |
---|
196 | } |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | static void print_custom_type(libtrace_meta_t *r) { |
---|
201 | int i, k; |
---|
202 | printf(" PCAPNG Custom Block\n"); |
---|
203 | |
---|
204 | if (r == NULL) { return; } |
---|
205 | |
---|
206 | /* print the custom data */ |
---|
207 | for (i=0; i<r->num; i++) { |
---|
208 | printf(" Private Enterprise Number (PEN): %" PRIu32 "\n", |
---|
209 | *(uint32_t *)r->items[i].data); |
---|
210 | printf(" Data: "); |
---|
211 | char *ptr = r->items[i].data+sizeof(uint32_t); |
---|
212 | uint16_t length = r->items[i].len-sizeof(uint32_t); |
---|
213 | for (k=0; k<length; k++) { |
---|
214 | printf("%02x ", ptr[k]); |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | static void print_secrets_type(libtrace_meta_t *r UNUSED) { |
---|
220 | /* todo */ |
---|
221 | } |
---|
222 | |
---|
223 | DLLEXPORT void decode_meta(int link_type UNUSED,const char *packet UNUSED,unsigned len UNUSED, |
---|
224 | libtrace_packet_t *p) { |
---|
225 | |
---|
226 | struct pcapng_peeker *pkthdr; |
---|
227 | uint32_t section; |
---|
228 | |
---|
229 | /* get the section header ID */ |
---|
230 | pkthdr = (struct pcapng_peeker *)p->header; |
---|
231 | if (DATA(p->trace)->byteswapped) { |
---|
232 | section = byteswap32(pkthdr->blocktype); |
---|
233 | } else { |
---|
234 | section = pkthdr->blocktype; |
---|
235 | } |
---|
236 | |
---|
237 | /* Get the entire section of whatever type of meta packet this is from the meta api */ |
---|
238 | libtrace_meta_t *r = trace_get_section(p, section); |
---|
239 | |
---|
240 | switch(section) { |
---|
241 | case PCAPNG_SECTION_TYPE: |
---|
242 | print_section_type(r); |
---|
243 | break; |
---|
244 | case PCAPNG_INTERFACE_TYPE: |
---|
245 | print_interface_type(r, p); |
---|
246 | break; |
---|
247 | case PCAPNG_OLD_PACKET_TYPE: |
---|
248 | /* We will never make it here */ |
---|
249 | break; |
---|
250 | case PCAPNG_SIMPLE_PACKET_TYPE: |
---|
251 | /* We will never make it here */ |
---|
252 | break; |
---|
253 | case PCAPNG_NAME_RESOLUTION_TYPE: |
---|
254 | print_name_resolution_type(r); |
---|
255 | break; |
---|
256 | case PCAPNG_INTERFACE_STATS_TYPE: |
---|
257 | print_interface_statistics_type(r); |
---|
258 | break; |
---|
259 | case PCAPNG_ENHANCED_PACKET_TYPE: |
---|
260 | /* We will never make it here */ |
---|
261 | break; |
---|
262 | case PCAPNG_CUSTOM_TYPE: |
---|
263 | print_custom_type(r); |
---|
264 | break; |
---|
265 | case PCAPNG_CUSTOM_NONCOPY_TYPE: |
---|
266 | print_custom_type(r); |
---|
267 | break; |
---|
268 | case PCAPNG_DECRYPTION_SECRETS_TYPE: |
---|
269 | /* specification does not define options for this |
---|
270 | * however we can still print the secrets data */ |
---|
271 | print_secrets_type(r); |
---|
272 | break; |
---|
273 | default: |
---|
274 | printf("Unknown Type/Block\n"); |
---|
275 | |
---|
276 | } |
---|
277 | |
---|
278 | /* destroy the meta result */ |
---|
279 | trace_destroy_meta(r); |
---|
280 | |
---|
281 | } |
---|