summaryrefslogtreecommitdiffstats
path: root/isys/dns.c
blob: c7a0f596ef32c005f711ad057443221ebaecd181 (plain)
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
#include <alloca.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <stdlib.h>
#include <string.h>

#include "dns.h"

/* This is dumb, but glibc doesn't like to do hostname lookups w/o libc.so */

/*
 * IPv6 DNS extensions documented here:
 * http://tools.ietf.org/html/rfc3596
 */

union dns_response{
    HEADER hdr;
    u_char buf[PACKETSZ];
};

static int doQuery(char * query, int queryType,
                   char ** domainName, void * ipNum, int family) {
    int len, ancount, type;
    u_char * data, * end;
    char name[MAXDNAME];
    union dns_response static_response, *response = &static_response;
    size_t response_len = sizeof(static_response);

    /* Give time to finish ethernet negotiation */
    _res.retry = 3;

    do {
        len = res_search(query, C_IN, queryType, (void*)response, response_len);
        if (len <= 0) return -1;
        if (len < response_len) break;
        if (response != &static_response) free(response);
        if (len > 0x10000) return -1;
        response_len = len + 1024;
        response = malloc(response_len);
        if (response == NULL) return -1;
    } while (1);

    if (len < sizeof(response->hdr)) {
        if (response != &static_response) free(response);
        return -1;
    }

    if (ntohs(response->hdr.rcode) != NOERROR) {
        if (response != &static_response) free(response);
        return -1;
    }

    ancount = ntohs(response->hdr.ancount);

    if (ancount < 1) {
        if (response != &static_response) free(response);
        return -1;
    }

    data = response->buf + sizeof(HEADER);
    end = response->buf + len;
    
    /* skip the question */
    len = dn_skipname(data, end);
    if (len <= 0) {
        if (response != &static_response) free(response);
        return -1;
    }
    data += len + QFIXEDSZ;

    /* parse the answer(s) */
    while (--ancount >= 0 && data < end) {
        /* skip the domain name portion of the RR record */
        data += dn_skipname(data, end);

        /* get RR information */
        if (data + 3 * INT16SZ + INT32SZ > end) {
            if (response != &static_response) free(response);
            return -1;
        }
        GETSHORT(type, data);
        data += INT16SZ; /* skip class */
        data += INT32SZ; /* skip TTL */
        GETSHORT(len,  data);

        if (type == T_PTR) {
            /* we got a pointer */
            len = dn_expand(response->buf, end, data, name, sizeof(name));
            if (len <= 0) {
                if (response != &static_response) free(response);
                return -1;
            }
            if (queryType == T_PTR && domainName) {
                /* we wanted a pointer */
                *domainName = malloc(strlen(name) + 1);
                 strcpy(*domainName, name);
                if (response != &static_response) free(response);
                return 0;
            }
        } else if (type == T_A) {
            /* we have an IPv4 address */
            if (queryType == T_A && ipNum) {
                memcpy(ipNum, data, sizeof(struct in_addr));
                if (response != &static_response)
					free(response);
                return 0;
            }
        } else if (type == T_AAAA) {
			/* we have an IPv6 address */
            if (queryType == T_AAAA && ipNum) {
                memcpy(ipNum, data, sizeof(struct in6_addr));
                if (response != &static_response)
                    free(response);
                return 0;
            }
		}

        /* move ahead to next RR */
        data += len;
    }

    if (response != &static_response) free(response);
    return -1;
}

char * mygethostbyaddr(char * ipnum, int family) {
    int i, j, ret;
    char *buf = NULL;
    char sbuf[5];
    char *result = NULL;
    char *octets[4];
    char *octet = NULL;
    char *parts[8];
    char *partptr = NULL;
    struct in6_addr addr6;

    _res.retry = 1;

    if (ipnum == NULL || (family != AF_INET && family != AF_INET6))
        return NULL;

    if (family == AF_INET) {
        buf = strdup(ipnum);
        octet = strtok(buf, ".");

        i = 0;
        while (octet != NULL) {
            octets[i] = octet;
            i++;
            octet = strtok(NULL, ".");
        }

        if (i == 4) {
            if (asprintf(&ipnum, "%s.%s.%s.%s.in-addr.arpa", octets[3],
                         octets[2], octets[1], octets[0]) == -1)
                return NULL;
        } else {
            return NULL;
        }

        free(buf);
        buf = NULL;
    } else if (family == AF_INET6) {
        if (!inet_pton(AF_INET6, ipnum, &addr6))
            return NULL;

        i = 7;
        while (i >= 0) {
            sprintf(sbuf, "%4x", ntohs(addr6.s6_addr16[i]));
            sbuf[4] = '\0';

            if ((parts[i] = malloc(8)) == NULL)
                return NULL;

            partptr = parts[i];

            for (j = 3; j >= 0; j--) {
                if (sbuf[j] == ' ')
                    *partptr = '0';
                else
                    *partptr = sbuf[j];

                partptr++;

                if (j != 0) {
                    *partptr = '.';
                    partptr++;
                }
            }

            i--;
        }

        if (asprintf(&ipnum, "%s.%s.%s.%s.%s.%s.%s.%s.ip6.arpa", parts[7],
                     parts[6], parts[5], parts[4], parts[3], parts[2],
                     parts[1], parts[0]) == -1)
            return NULL;

        for (j = 0; j < 8; j++) {
            free(parts[j]);
            parts[j] = NULL;
        }
    }

    ret = doQuery(ipnum, T_PTR, &result, NULL, family);
    if (ret)
        ret = doQuery(ipnum, T_PTR, &result, NULL, family);

    if (ret) 
        return NULL;
    else
        return result;
}

int mygethostbyname(char * name, void * addr, int family) {
    int type;

    if (family == AF_INET)
        type = T_A;
    else if (family == AF_INET6)
        type = T_AAAA;
    else
        type = -1;

    return doQuery(name, type, NULL, addr, family);
}

#if 0
int main(int argc, char **argv) {
    struct in_addr addr;
    struct in6_addr addr6;
    char *ret = NULL;

    /* IPv4 tests */
    printf("hostname for %s is %s\n", "152.1.2.22",
           mygethostbyaddr("152.1.2.22", AF_INET));
    if (mygethostbyname("www.redhat.com", &addr, AF_INET) == 0) {
        ret = malloc(48);
        inet_ntop(AF_INET, &addr, ret, INET_ADDRSTRLEN);
        printf("ip for www.redhat.com is %s\n", ret);
        free(ret);
        ret = NULL;
    }

    /* IPv6 tests */
    printf("hostname for %s is %s\n", "fec0:acdc:1::1",
           mygethostbyaddr("fec0:acdc:1::1", AF_INET6));
    if (mygethostbyname("cutlet.ipv6.install.boston.redhat.com", &addr6, AF_INET6) == 0) {
        ret = malloc(48);
        inet_ntop(AF_INET6, &addr6, ret, INET6_ADDRSTRLEN);
        printf("ip for cutlet.ipv6.install.boston.redhat.com is %s\n", ret);
        free(ret);
        ret = NULL;
    }

    return 0;
}
#endif

/* vim:set shiftwidth=4 softtabstop=4: */