Merge pull request #59 from electronjoe/graceful-SIGTERM-handling
[netsniff-ng-old.git] / proto_ipv6.c
blob23cf6c40383b4068675cd45c9b350dfeb1541ea7
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 "protos.h"
15 #include "csum.h"
16 #include "dissector_eth.h"
17 #include "ipv6.h"
18 #include "geoip.h"
19 #include "pkt_buff.h"
21 extern void ipv6(struct pkt_buff *pkt);
22 extern void ipv6_less(struct pkt_buff *pkt);
24 void ipv6(struct pkt_buff *pkt)
26 uint8_t traffic_class;
27 uint32_t flow_label;
28 char src_ip[INET6_ADDRSTRLEN];
29 char dst_ip[INET6_ADDRSTRLEN];
30 struct ipv6hdr *ip = (struct ipv6hdr *) pkt_pull(pkt, sizeof(*ip));
31 struct sockaddr_in6 sas, sad;
32 const char *city, *region, *country;
34 if (ip == NULL)
35 return;
37 traffic_class = (ip->priority << 4) |
38 ((ip->flow_lbl[0] & 0xF0) >> 4);
39 flow_label = ((ip->flow_lbl[0] & 0x0F) << 8) |
40 (ip->flow_lbl[1] << 4) | ip->flow_lbl[2];
42 inet_ntop(AF_INET6, &ip->saddr, src_ip, sizeof(src_ip));
43 inet_ntop(AF_INET6, &ip->daddr, dst_ip, sizeof(dst_ip));
45 tprintf(" [ IPv6 ");
46 tprintf("Addr (%s => %s), ", src_ip, dst_ip);
47 tprintf("Version (%u), ", ip->version);
48 tprintf("TrafficClass (%u), ", traffic_class);
49 tprintf("FlowLabel (%u), ", flow_label);
50 tprintf("Len (%u), ", ntohs(ip->payload_len));
51 tprintf("NextHdr (%u), ", ip->nexthdr);
52 tprintf("HopLimit (%u)", ip->hop_limit);
53 tprintf(" ]\n");
55 memset(&sas, 0, sizeof(sas));
56 sas.sin6_family = PF_INET6;
57 memcpy(&sas.sin6_addr, &ip->saddr, sizeof(ip->saddr));
59 memset(&sad, 0, sizeof(sad));
60 sad.sin6_family = PF_INET6;
61 memcpy(&sad.sin6_addr, &ip->daddr, sizeof(ip->daddr));
63 if (geoip_working()) {
64 tprintf("\t[ Geo (");
65 if ((country = geoip6_country_name(sas))) {
66 tprintf("%s", country);
67 if ((region = geoip6_region_name(sas)))
68 tprintf(" / %s", region);
69 if ((city = geoip6_city_name(sas)))
70 tprintf(" / %s", city);
71 } else {
72 tprintf("local");
74 tprintf(" => ");
75 if ((country = geoip6_country_name(sad))) {
76 tprintf("%s", country);
77 if ((region = geoip6_region_name(sad)))
78 tprintf(" / %s", region);
79 if ((city = geoip6_city_name(sad)))
80 tprintf(" / %s", city);
81 } else {
82 tprintf("local");
84 tprintf(") ]\n");
87 pkt_set_proto(pkt, &eth_lay3, ip->nexthdr);
90 void ipv6_less(struct pkt_buff *pkt)
92 char src_ip[INET6_ADDRSTRLEN];
93 char dst_ip[INET6_ADDRSTRLEN];
94 struct ipv6hdr *ip = (struct ipv6hdr *) pkt_pull(pkt, sizeof(*ip));
96 if (ip == NULL)
97 return;
99 inet_ntop(AF_INET6, &ip->saddr, src_ip, sizeof(src_ip));
100 inet_ntop(AF_INET6, &ip->daddr, dst_ip, sizeof(dst_ip));
102 tprintf(" %s/%s Len %u", src_ip, dst_ip,
103 ntohs(ip->payload_len));
105 pkt_set_proto(pkt, &eth_lay3, ip->nexthdr);
108 struct protocol ipv6_ops = {
109 .key = 0x86DD,
110 .print_full = ipv6,
111 .print_less = ipv6_less,