ring: Merge common ring_{rx,tx} initialization into own function
[netsniff-ng.git] / proto_ipv6.c
bloba38bec3f44bdd62bd97925cb943f76c790e85239
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 "csum.h"
15 #include "dissector_eth.h"
16 #include "ipv6.h"
17 #include "geoip.h"
18 #include "pkt_buff.h"
20 extern void ipv6(struct pkt_buff *pkt);
21 extern void ipv6_less(struct pkt_buff *pkt);
23 void ipv6(struct pkt_buff *pkt)
25 uint8_t traffic_class;
26 uint32_t flow_label;
27 char src_ip[INET6_ADDRSTRLEN];
28 char dst_ip[INET6_ADDRSTRLEN];
29 struct ipv6hdr *ip = (struct ipv6hdr *) pkt_pull(pkt, sizeof(*ip));
30 struct sockaddr_in6 sas, sad;
31 const char *city, *region, *country;
33 if (ip == NULL)
34 return;
36 traffic_class = (ip->priority << 4) |
37 ((ip->flow_lbl[0] & 0xF0) >> 4);
38 flow_label = ((ip->flow_lbl[0] & 0x0F) << 8) |
39 (ip->flow_lbl[1] << 4) | ip->flow_lbl[2];
41 inet_ntop(AF_INET6, &ip->saddr, src_ip, sizeof(src_ip));
42 inet_ntop(AF_INET6, &ip->daddr, dst_ip, sizeof(dst_ip));
44 tprintf(" [ IPv6 ");
45 tprintf("Addr (%s => %s), ", src_ip, dst_ip);
46 tprintf("Version (%u), ", ip->version);
47 tprintf("TrafficClass (%u), ", traffic_class);
48 tprintf("FlowLabel (%u), ", flow_label);
49 tprintf("Len (%u), ", ntohs(ip->payload_len));
50 tprintf("NextHdr (%u), ", ip->nexthdr);
51 tprintf("HopLimit (%u)", ip->hop_limit);
52 tprintf(" ]\n");
54 memset(&sas, 0, sizeof(sas));
55 sas.sin6_family = PF_INET6;
56 memcpy(&sas.sin6_addr, &ip->saddr, sizeof(ip->saddr));
58 memset(&sad, 0, sizeof(sad));
59 sad.sin6_family = PF_INET6;
60 memcpy(&sad.sin6_addr, &ip->daddr, sizeof(ip->daddr));
62 if (geoip_working()) {
63 tprintf("\t[ Geo (");
64 if ((country = geoip6_country_name(sas))) {
65 tprintf("%s", country);
66 if ((region = geoip6_region_name(sas)))
67 tprintf(" / %s", region);
68 if ((city = geoip6_city_name(sas)))
69 tprintf(" / %s", city);
70 } else {
71 tprintf("local");
73 tprintf(" => ");
74 if ((country = geoip6_country_name(sad))) {
75 tprintf("%s", country);
76 if ((region = geoip6_region_name(sad)))
77 tprintf(" / %s", region);
78 if ((city = geoip6_city_name(sad)))
79 tprintf(" / %s", city);
80 } else {
81 tprintf("local");
83 tprintf(") ]\n");
86 pkt_set_proto(pkt, &eth_lay3, ip->nexthdr);
89 void ipv6_less(struct pkt_buff *pkt)
91 char src_ip[INET6_ADDRSTRLEN];
92 char dst_ip[INET6_ADDRSTRLEN];
93 struct ipv6hdr *ip = (struct ipv6hdr *) pkt_pull(pkt, sizeof(*ip));
95 if (ip == NULL)
96 return;
98 inet_ntop(AF_INET6, &ip->saddr, src_ip, sizeof(src_ip));
99 inet_ntop(AF_INET6, &ip->daddr, dst_ip, sizeof(dst_ip));
101 tprintf(" %s/%s Len %u", src_ip, dst_ip,
102 ntohs(ip->payload_len));
104 pkt_set_proto(pkt, &eth_lay3, ip->nexthdr);
107 struct protocol ipv6_ops = {
108 .key = 0x86DD,
109 .print_full = ipv6,
110 .print_less = ipv6_less,