trafgen: parser: Unify proto field value parsing
[netsniff-ng-new.git] / proto_ipv6.c
blobba3f6c85203fa5bce6240dd10ff08c0436faeb6b
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2010 Emmanuel Roullit.
5 * Subject to the GPL, version 2.
6 */
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <netinet/in.h> /* for ntohs() */
11 #include <arpa/inet.h> /* for inet_ntop() */
13 #include "proto.h"
14 #include "dissector_eth.h"
15 #include "ipv6.h"
16 #include "geoip.h"
17 #include "pkt_buff.h"
19 extern void ipv6(struct pkt_buff *pkt);
20 extern void ipv6_less(struct pkt_buff *pkt);
22 void ipv6(struct pkt_buff *pkt)
24 uint8_t traffic_class;
25 uint32_t flow_label;
26 char src_ip[INET6_ADDRSTRLEN];
27 char dst_ip[INET6_ADDRSTRLEN];
28 struct ipv6hdr *ip = (struct ipv6hdr *) pkt_pull(pkt, sizeof(*ip));
29 struct sockaddr_in6 sas, sad;
30 const char *city, *region, *country;
32 if (ip == NULL)
33 return;
35 traffic_class = (ip->priority << 4) |
36 ((ip->flow_lbl[0] & 0xF0) >> 4);
37 flow_label = ((ip->flow_lbl[0] & 0x0F) << 8) |
38 (ip->flow_lbl[1] << 4) | ip->flow_lbl[2];
40 inet_ntop(AF_INET6, &ip->saddr, src_ip, sizeof(src_ip));
41 inet_ntop(AF_INET6, &ip->daddr, dst_ip, sizeof(dst_ip));
43 tprintf(" [ IPv6 ");
44 tprintf("Addr (%s => %s), ", src_ip, dst_ip);
45 tprintf("Version (%u), ", ip->version);
46 tprintf("TrafficClass (%u), ", traffic_class);
47 tprintf("FlowLabel (%u), ", flow_label);
48 tprintf("Len (%u), ", ntohs(ip->payload_len));
49 tprintf("NextHdr (%u), ", ip->nexthdr);
50 tprintf("HopLimit (%u)", ip->hop_limit);
51 tprintf(" ]\n");
53 memset(&sas, 0, sizeof(sas));
54 sas.sin6_family = PF_INET6;
55 memcpy(&sas.sin6_addr, &ip->saddr, sizeof(ip->saddr));
57 memset(&sad, 0, sizeof(sad));
58 sad.sin6_family = PF_INET6;
59 memcpy(&sad.sin6_addr, &ip->daddr, sizeof(ip->daddr));
61 if (geoip_working()) {
62 tprintf("\t[ Geo (");
63 if ((country = geoip6_country_name(&sas))) {
64 tprintf("%s", country);
65 if ((region = geoip6_region_name(&sas)))
66 tprintf(" / %s", region);
67 if ((city = geoip6_city_name(&sas)))
68 tprintf(" / %s", city);
69 } else {
70 tprintf("local");
72 tprintf(" => ");
73 if ((country = geoip6_country_name(&sad))) {
74 tprintf("%s", country);
75 if ((region = geoip6_region_name(&sad)))
76 tprintf(" / %s", region);
77 if ((city = geoip6_city_name(&sad)))
78 tprintf(" / %s", city);
79 } else {
80 tprintf("local");
82 tprintf(") ]\n");
85 pkt_set_dissector(pkt, &eth_lay3, ip->nexthdr);
88 void ipv6_less(struct pkt_buff *pkt)
90 char src_ip[INET6_ADDRSTRLEN];
91 char dst_ip[INET6_ADDRSTRLEN];
92 struct ipv6hdr *ip = (struct ipv6hdr *) pkt_pull(pkt, sizeof(*ip));
94 if (ip == NULL)
95 return;
97 inet_ntop(AF_INET6, &ip->saddr, src_ip, sizeof(src_ip));
98 inet_ntop(AF_INET6, &ip->daddr, dst_ip, sizeof(dst_ip));
100 tprintf(" %s/%s Len %u", src_ip, dst_ip,
101 ntohs(ip->payload_len));
103 pkt_set_dissector(pkt, &eth_lay3, ip->nexthdr);
106 struct protocol ipv6_ops = {
107 .key = 0x86DD,
108 .print_full = ipv6,
109 .print_less = ipv6_less,