-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathip6.c
162 lines (144 loc) · 3.18 KB
/
ip6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* David Leonard, 2002. Public domain. */
/* $Id$ */
/* Internet protocol version 6 */
#if HAVE_CONFIG_H
# include "config.h"
#endif
#if HAVE_NETINET_IP6_H
#if HAVE_PCAP_H
# include <pcap.h>
#endif
#if HAVE_NETDB_H
# include <netdb.h>
#endif
#if STDC_HEADERS
# include <string.h>
# include <stdlib.h>
#endif
#if HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#if HAVE_NETINET_IN_SYSTM_H
# include <netinet/in_systm.h>
#endif
#if HAVE_NETINET_IP6_H
# include <netinet/ip6.h>
#endif
#if HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#if HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#include "compat.h"
#include "tag.h"
#include "flow.h"
#include "hash.h"
#include "main.h"
#include "display.h"
#ifndef IPV6_GET_VERSION
# define IPV6_GET_VERSION(v) ((*(u_int8_t *)(v) & 0xf0) >> 4)
#endif
/* Compare two IPv6 addresses */
static int
in6addr_cmp(a, b)
const void *a;
const void *b;
{
const struct in6_addr *ia = (const struct in6_addr *)a;
const struct in6_addr *ib = (const struct in6_addr *)b;
return memcmp(ia, ib, sizeof (struct in6_addr));
}
/* Compute a hash value of an IPv6 address */
static unsigned int
in6addr_hash(a)
const void *a;
{
const struct in6_addr *ia = (const struct in6_addr *)a;
return hash_generic(ia, sizeof *ia);
}
static struct hash ip6_hash = {
in6addr_cmp, /* cmp */
in6addr_hash, /* hashfn */
(free_t)free, /* freaky */
(free_t)free /* freedata */
};
/* Look up an IP address */
const char *
ip6_lookup(addr)
const struct in6_addr *addr;
{
const char *result;
static int old_nflag = -1;
static int old_Fflag = -1;
if (old_nflag != nflag || old_Fflag != Fflag) {
hash_clear(&ip6_hash);
old_nflag = nflag;
old_Fflag = Fflag;
}
result = (const char *)hash_lookup(&ip6_hash, addr);
if (result == NULL) {
struct hostent *he;
struct in6_addr *a2;
const char *s;
char *t;
char buf[1024];
if (nflag)
he = NULL;
else {
display_message("resolving %s",
inet_ntop(AF_INET6, addr, buf, sizeof buf));
he = gethostbyaddr((char *)addr, sizeof *addr,
AF_INET6);
display_message("");
}
if (he == NULL)
s = inet_ntop(AF_INET6, addr, buf, sizeof buf);
else {
if (!Fflag) {
t = strchr(he->h_name, '.');
if (t) *t = '\0';
}
s = he->h_name;
}
result = strdup(s);
a2 = (struct in6_addr *)malloc(sizeof *addr);
if (a2 == NULL)
errx(1, "malloc");
memcpy(a2, addr, sizeof *a2);
hash_store(&ip6_hash, a2, result);
}
return result;
}
const char *
ip6_tag(p, end)
const char *p;
const char *end;
{
const struct ip6_hdr *ip6;
static char tag[TAGLEN];
ip6 = (struct ip6_hdr *)p;
if (IPV6_GET_VERSION(ip6) != 6) {
snprintf(tag, sizeof tag, "ip6 version %u",
IPV6_GET_VERSION(ip6));
return tag;
}
switch (ip6->ip6_nxt) {
case IPPROTO_TCP:
return tcp_tag(p + sizeof *ip6, end, NULL, ip6);
case IPPROTO_UDP:
return udp_tag(p + sizeof *ip6, end, NULL, ip6);
case IPPROTO_ICMPV6:
/* XXX */
snprintf(tag, sizeof tag, "icmp6 %s",
tag_combine(ip6_lookup(&ip6->ip6_src),
ip6_lookup(&ip6->ip6_dst)));
return tag;
default:
snprintf(tag, sizeof tag, "ip6 %s proto %u",
tag_combine(ip6_lookup(&ip6->ip6_src),
ip6_lookup(&ip6->ip6_dst)), ip6->ip6_nxt);
return tag;
}
}
#endif