trafgen: fix packet socket initialization with multiple CPUs
[netsniff-ng.git] / proto_ipv6.c
blobe3e8f68771cd8bcebef53b308e1e7f697e1799ff
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 char *city, *region;
31 const char *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 xfree(region);
70 if ((city = geoip6_city_name(&sas))) {
71 tprintf(" / %s", city);
72 xfree(city);
74 } else {
75 tprintf("local");
77 tprintf(" => ");
78 if ((country = geoip6_country_name(&sad))) {
79 tprintf("%s", country);
80 if ((region = geoip6_region_name(&sad))) {
81 tprintf(" / %s", region);
82 xfree(region);
84 if ((city = geoip6_city_name(&sad))) {
85 tprintf(" / %s", city);
86 xfree(city);
88 } else {
89 tprintf("local");
91 tprintf(") ]\n");
94 pkt_set_dissector(pkt, &eth_lay3, ip->nexthdr);
97 void ipv6_less(struct pkt_buff *pkt)
99 char src_ip[INET6_ADDRSTRLEN];
100 char dst_ip[INET6_ADDRSTRLEN];
101 struct ipv6hdr *ip = (struct ipv6hdr *) pkt_pull(pkt, sizeof(*ip));
103 if (ip == NULL)
104 return;
106 inet_ntop(AF_INET6, &ip->saddr, src_ip, sizeof(src_ip));
107 inet_ntop(AF_INET6, &ip->daddr, dst_ip, sizeof(dst_ip));
109 tprintf(" %s/%s Len %u", src_ip, dst_ip,
110 ntohs(ip->payload_len));
112 pkt_set_dissector(pkt, &eth_lay3, ip->nexthdr);
115 struct protocol ipv6_ops = {
116 .key = 0x86DD,
117 .print_full = ipv6,
118 .print_less = ipv6_less,