-
Notifications
You must be signed in to change notification settings - Fork 336
/
asn.c
350 lines (296 loc) · 8.4 KB
/
asn.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
mtr -- a network diagnostic tool
Copyright (C) 1997,1998 Matt Kimball
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#ifdef HAVE_ERROR_H
#include <error.h>
#else
#include "portability/error.h"
#endif
#include <errno.h>
#ifdef __APPLE__
#define BIND_8_COMPAT
#endif
#include <arpa/nameser.h>
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
#include <arpa/nameser_compat.h>
#endif
#include <netdb.h>
#include <netinet/in.h>
#include <resolv.h>
#include <string.h>
#include <sys/socket.h>
#include <search.h>
#include "mtr.h"
#include "asn.h"
#include "utils.h"
/* #define IIDEBUG */
#ifdef IIDEBUG
#include <syslog.h>
#define DEB_syslog syslog
#else
#define DEB_syslog(...) do {} while (0)
#endif
#define IIHASH_HI 128
#define ITEMSMAX 15
#define ITEMSEP '|'
#define NAMELEN 127
#define UNKN "???"
static int iihash = 0;
static char fmtinfo[32];
/* items width: ASN, Route, Country, Registry, Allocated */
static const int iiwidth[] = { 12, 19, 4, 8, 11 }; /* item len + space */
typedef char *items_t[ITEMSMAX + 1];
static items_t items_a; /* without hash: items */
static char txtrec[NAMELEN + 1]; /* without hash: txtrec */
static items_t *items = &items_a;
static char *ipinfo_lookup(
const char *domain)
{
unsigned char answer[PACKETSZ], *pt;
char host[128];
char *txt;
int len, exp, size, txtlen, type;
if (res_init() < 0) {
error(0, 0, "@res_init failed");
return NULL;
}
memset(answer, 0, PACKETSZ);
if ((len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ)) < 0) {
if (iihash)
DEB_syslog(LOG_INFO, "Malloc-txt: %s", UNKN);
return xstrdup(UNKN);
}
pt = answer + sizeof(HEADER);
if ((exp =
dn_expand(answer, answer + len, pt, host, sizeof(host))) < 0) {
printf("@dn_expand failed\n");
return NULL;
}
pt += exp;
GETSHORT(type, pt);
if (type != T_TXT) {
printf("@Broken DNS reply.\n");
return NULL;
}
pt += INT16SZ; /* class */
if ((exp =
dn_expand(answer, answer + len, pt, host, sizeof(host))) < 0) {
printf("@second dn_expand failed\n");
return NULL;
}
pt += exp;
GETSHORT(type, pt);
if (type != T_TXT) {
printf("@Not a TXT record\n");
return NULL;
}
pt += INT16SZ; /* class */
pt += INT32SZ; /* ttl */
GETSHORT(size, pt);
txtlen = *pt;
if (txtlen >= size || !txtlen) {
printf("@Broken TXT record (txtlen = %d, size = %d)\n", txtlen,
size);
return NULL;
}
if (txtlen > NAMELEN)
txtlen = NAMELEN;
if (iihash) {
txt = xmalloc(txtlen + 1);
} else
txt = (char *) txtrec;
pt++;
xstrncpy(txt, (char *) pt, txtlen + 1);
if (iihash)
DEB_syslog(LOG_INFO, "Malloc-txt(%p): %s", txt, txt);
return txt;
}
/* originX.asn.cymru.com txtrec: ASN | Route | Country | Registry | Allocated */
static char *split_txtrec(
struct mtr_ctl *ctl,
char *txt_rec)
{
char *prev;
char *next;
int i = 0, j;
if (!txt_rec)
return NULL;
if (iihash) {
DEB_syslog(LOG_INFO, "Malloc-tbl: %s", txt_rec);
if (!(items = malloc(sizeof(*items)))) {
DEB_syslog(LOG_INFO, "Free-txt(%p)", txt_rec);
free(txt_rec);
return NULL;
}
}
prev = txt_rec;
while ((next = strchr(prev, ITEMSEP)) && (i < ITEMSMAX)) {
*next = '\0';
next++;
(*items)[i] = trim(prev, ITEMSEP);
prev = next;
i++;
}
(*items)[i] = trim(prev, ITEMSEP);
if (i < ITEMSMAX)
i++;
for (j = i; j <= ITEMSMAX; j++)
(*items)[j] = NULL;
if (i > ctl->ipinfo_max)
ctl->ipinfo_max = i;
if (ctl->ipinfo_no >= i) {
return (*items)[0];
} else
return (*items)[ctl->ipinfo_no];
}
#ifdef ENABLE_IPV6
/* from dns.c:addr2ip6arpa() */
static void reverse_host6(
struct in6_addr *addr,
char *buff,
int buff_length)
{
int i;
char *b = buff;
// We need to process the top 64 bits, or 8 bytes.
for (i = 8-1; i >= 0; i--, b += 4, buff_length -= 4)
snprintf(b, buff_length,
"%x.%x.", addr->s6_addr[i] & 0xf, addr->s6_addr[i] >> 4);
*--b = 0;
}
#endif
#ifdef ENABLE_IPV6
static bool is_well_known_nat64(struct in6_addr *addr){
// 64:ff9b::
return addr->s6_addr[0] == 0x00 && addr->s6_addr[1] == 0x64 && addr->s6_addr[2] == 0xff && addr->s6_addr[3] == 0x9b;
}
#endif
static char *get_ipinfo(
struct mtr_ctl *ctl,
ip_t * addr)
{
char key[NAMELEN];
char lookup_key[NAMELEN];
char *val = NULL;
ENTRY item;
if (!addr)
return NULL;
if (ctl->af == AF_INET6) {
#ifdef ENABLE_IPV6
if (is_well_known_nat64(addr)) {
// Treats the final 4 bytes as IPv4 address
unsigned char buff[4];
memcpy(buff, addr->s6_addr + 12, 4);
if (snprintf
(key, NAMELEN, "%d.%d.%d.%d", buff[3], buff[2], buff[1],
buff[0]) >= NAMELEN)
return NULL;
if (snprintf(lookup_key, NAMELEN, "%s.%s", key, ctl->ipinfo_provider4)
>= NAMELEN)
return NULL;
} else {
reverse_host6(addr, key, NAMELEN);
if (snprintf(lookup_key, NAMELEN, "%s.%s", key, ctl->ipinfo_provider6)
>= NAMELEN)
return NULL;
}
#else
return NULL;
#endif
} else {
unsigned char buff[4];
memcpy(buff, addr, 4);
if (snprintf
(key, NAMELEN, "%d.%d.%d.%d", buff[3], buff[2], buff[1],
buff[0]) >= NAMELEN)
return NULL;
if (snprintf(lookup_key, NAMELEN, "%s.%s", key, ctl->ipinfo_provider4)
>= NAMELEN)
return NULL;
}
if (iihash) {
ENTRY *found_item;
DEB_syslog(LOG_INFO, ">> Search: %s", key);
item.key = key;;
if ((found_item = hsearch(item, FIND))) {
if (!(val = (*((items_t *) found_item->data))[ctl->ipinfo_no]))
val = (*((items_t *) found_item->data))[0];
DEB_syslog(LOG_INFO, "Found (hashed): %s", val);
}
}
if (!val) {
DEB_syslog(LOG_INFO, "Lookup: %s", key);
if ((val = split_txtrec(ctl, ipinfo_lookup(lookup_key)))) {
DEB_syslog(LOG_INFO, "Looked up: %s", key);
if (iihash)
if ((item.key = xstrdup(key))) {
item.data = (void *) items;
hsearch(item, ENTER);
DEB_syslog(LOG_INFO, "Insert into hash: %s", key);
}
}
}
return val;
}
ATTRIBUTE_CONST size_t get_iiwidth_len(
void)
{
return (sizeof(iiwidth) / sizeof((iiwidth)[0]));
}
ATTRIBUTE_CONST int get_iiwidth(
int ipinfo_no)
{
static const int len = (sizeof(iiwidth) / sizeof((iiwidth)[0]));
if (ipinfo_no < len)
return iiwidth[ipinfo_no];
return iiwidth[ipinfo_no % len];
}
char *fmt_ipinfo(
struct mtr_ctl *ctl,
ip_t * addr)
{
char *ipinfo = get_ipinfo(ctl, addr);
char fmt[8];
snprintf(fmt, sizeof(fmt), "%s%%-%ds", ctl->ipinfo_no ? "" : "AS",
get_iiwidth(ctl->ipinfo_no));
snprintf(fmtinfo, sizeof(fmtinfo), fmt, ipinfo ? ipinfo : UNKN);
return fmtinfo;
}
int is_printii(
struct mtr_ctl *ctl)
{
return (ctl->ipinfo_no >= 0);
}
void asn_open(
struct mtr_ctl *ctl)
{
DEB_syslog(LOG_INFO, "hcreate(%d)", IIHASH_HI);
if (!(iihash = hcreate(IIHASH_HI)))
error(0, errno, "ipinfo hash");
}
void asn_close(
struct mtr_ctl *ctl)
{
if (iihash) {
DEB_syslog(LOG_INFO, "hdestroy()");
hdestroy();
iihash = 0;
}
}