-
Notifications
You must be signed in to change notification settings - Fork 0
/
asn.c
109 lines (77 loc) · 2.44 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
/*
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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
Copyright (C) 2010 by Roderick Groesbeek <[email protected]>
Released under GPL, as above.
*/
#include <config.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/nameser.h>
#include <netdb.h>
#include <netinet/in.h>
#include <resolv.h>
#include <string.h>
#include <sys/socket.h>
#include <glib.h>
int PrintAS = 0;
GHashTable * ashash = NULL;
char *asn_lookup(const char *domain)
{
unsigned char answer[PACKETSZ], *pt;
char host[128];
char *txt;
int len, exp, cttl, size, txtlen, type;
if(res_init() < 0) {
fprintf(stderr,"@res_init failed\n");
return NULL;
}
memset(answer, 0, PACKETSZ);
if((len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ)) < 0) {
return "-1";
}
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 */
GETLONG(cttl, pt);
GETSHORT(size, pt);
txtlen = *pt;
if(txtlen >= size || !txtlen) {
printf("@Broken TXT record (txtlen = %d, size = %d)\n", txtlen, size); return NULL;
}
if(!(txt = malloc(txtlen + 1)))
return NULL;
pt++;
strncpy(txt, (char*) pt, txtlen);
txt[txtlen] = 0;
return txt;
}