summaryrefslogtreecommitdiff
path: root/src/libnet_resolve.c
blob: 21fa06959a47ba13b394a8db9793c70762675b3a (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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 *  libnet
 *  libnet_resolve.c - various name resolution type routines
 *
 *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
 *  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include "common.h"

#ifndef HAVE_GETHOSTBYNAME2
struct hostent *
gethostbyname2(const char *name, int af)
{
        return gethostbyname(name);
}
#endif

char *
libnet_addr2name4(uint32_t in, uint8_t use_name)
{
	#define HOSTNAME_SIZE 512
    static char hostname[HOSTNAME_SIZE+1], hostname2[HOSTNAME_SIZE+1];
    static uint16_t which;
    uint8_t *p;

    struct hostent *host_ent = NULL;
    struct in_addr addr;

    /*
     *  Swap to the other buffer.  We swap static buffers to avoid having to
     *  pass in a int8_t *.  This makes the code that calls this function more
     *  intuitive, but makes this function ugly.  This function is seriously
     *  non-reentrant.  For threaded applications (or for signal handler code)
     *  use host_lookup_r().
     */
    which++;
    
    if (use_name == LIBNET_RESOLVE)
    {
		addr.s_addr = in;
        host_ent = gethostbyaddr((int8_t *)&addr, sizeof(struct in_addr), AF_INET);
        /* if this fails, we silently ignore the error and move to plan b! */
    }
    if (!host_ent)
    {

        p = (uint8_t *)&in;
   		snprintf(((which % 2) ? hostname : hostname2), HOSTNAME_SIZE,
                 "%d.%d.%d.%d",
                 (p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));
    }
    else if (use_name == LIBNET_RESOLVE)
    {
		char *ptr = ((which % 2) ? hostname : hostname2);
		strncpy(ptr, host_ent->h_name, HOSTNAME_SIZE);
		ptr[HOSTNAME_SIZE] = '\0';
	
    }
    return (which % 2) ? (hostname) : (hostname2);
}

void
libnet_addr2name4_r(uint32_t in, uint8_t use_name, char *hostname,
        int hostname_len)
{
    uint8_t *p;
    struct hostent *host_ent = NULL;
    struct in_addr addr;

    if (use_name == LIBNET_RESOLVE)
    {   
		addr.s_addr = in;
        host_ent = gethostbyaddr((int8_t *)&addr, sizeof(struct in_addr),
                AF_INET);
    }
    if (!host_ent)
    {
        p = (uint8_t *)&in;
        snprintf(hostname, hostname_len, "%d.%d.%d.%d",
                (p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));
    }
    else
    {
        strncpy(hostname, host_ent->h_name, hostname_len - 1);
		hostname[sizeof(hostname) - 1] = '\0';
    }
}

uint32_t
libnet_name2addr4(libnet_t *l, const char *host_name, uint8_t use_name)
{
    struct in_addr addr;
    struct hostent *host_ent; 
    uint32_t m;
    uint32_t val;
    int i;

    if (use_name == LIBNET_RESOLVE)
    {
        if ((addr.s_addr = inet_addr(host_name)) == INADDR_NONE)
        {
            if (!(host_ent = gethostbyname(host_name)))
            {
                snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                        "%s(): %s", __func__,
#if (_WIN32)
                            "gethostbyname failure"
#else
                            /* FIXME doesn't exist on windows, needs WSAGetLastError()/FormatMessage */
                            hstrerror(h_errno)
#endif
                        );
                /* XXX - this is actually 255.255.255.255 */
                return (-1);
            }
            memcpy(&addr.s_addr, host_ent->h_addr, host_ent->h_length);
        }
        /* network byte order */
        return (addr.s_addr);
    }
    else
    {
        /*
         *  We only want dots 'n decimals.
         */
        if (!isdigit(host_name[0]))
        {
            if (l)
            {
                snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                    "%s(): expecting dots and decimals", __func__);
            }
            /* XXX - this is actually 255.255.255.255 */
            return (-1);
        }

        m = 0;
        for (i = 0; i < 4; i++)
        {
            m <<= 8;
            if (*host_name)
            {
                val = 0;
                while (*host_name && *host_name != '.')
                {   
                    val *= 10;
                    val += *host_name - '0';
                    if (val > 255)
                    {
                        if (l)
                        {
                            snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                            "%s(): value greater than 255", __func__);
                        }
                        /* XXX - this is actually 255.255.255.255 */
                        return (-1);
                    }
                    host_name++;
                }
                m |= val;
                if (*host_name)
                {
                    host_name++;
                }
            }
        }
        /* host byte order */
       return (ntohl(m));
    }
}

void
libnet_addr2name6_r(struct libnet_in6_addr addr, uint8_t use_name,
            char *host_name, int host_name_len)
{
    struct hostent *host_ent = NULL;

    if (use_name == LIBNET_RESOLVE)
    {    
#ifdef HAVE_SOLARIS 
#ifdef HAVE_SOLARIS_IPV6
        host_ent = getipnodebyaddr((int8_t *)&addr, sizeof(struct in_addr),
                AF_INET6, NULL);
#else
        /* XXX - Gah!  Can't report error! */
        host_ent = NULL;
#endif
#else
        host_ent = gethostbyaddr((int8_t *)&addr, sizeof(struct in_addr),
                AF_INET6);
#endif
    }
    if (!host_ent)
    {
#if !defined(__WIN32__) /* Silence Win32 warning */
        inet_ntop(AF_INET6, &addr, host_name, host_name_len);
#endif
    }
    else
    {
        strncpy(host_name, host_ent->h_name, host_name_len -1);
		host_name[sizeof(host_name) - 1] = '\0';
    }
}

const struct libnet_in6_addr in6addr_error = IN6ADDR_ERROR_INIT;

int
libnet_in6_is_error(struct libnet_in6_addr addr)
{
    return 0 == memcmp(&addr, &in6addr_error, sizeof(addr));
}

struct libnet_in6_addr
libnet_name2addr6(libnet_t *l, const char *host_name, uint8_t use_name)
{
#if !defined (__WIN32__)
    struct libnet_in6_addr addr;
    struct hostent *host_ent; 
#endif
   
    if (use_name == LIBNET_RESOLVE)
    {
#ifdef __WIN32__
        /* XXX - we don't support this yet */
        if (l)
        {        
            snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                "%s(): can't resolve IPv6 addresses", __func__);
        }
        return (in6addr_error);
#else
#ifdef HAVE_SOLARIS 
#ifdef HAVE_SOLARIS_IPV6
        if (!(host_ent = getipnodebyname((int8_t *)&addr,
                sizeof(struct in_addr), AF_INET6, NULL)))
#else
        snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, 
                "%s(): %s", __func__, strerror(errno));
        return (in6addr_error);
#endif
#else
        if (!(host_ent = gethostbyname2(host_name, AF_INET6)))
#endif
        {
            snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                    "%s(): %s", __func__, strerror(errno));
            return (in6addr_error);
        }
        memcpy(&addr, host_ent->h_addr, host_ent->h_length);
        return (addr);
#endif  /* !__WIN32__ */
    }
    else
    {
#if defined(__WIN32__) /* Silence Win32 warning */
        if (l)
        {        
               snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                "%s(): can't resolve IPv6 addresses.", __func__);
        }
        return (in6addr_error);
#else
        if(!inet_pton(AF_INET6, host_name, &addr))
        {
            if (l)
            {
                snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                    "%s(): invalid IPv6 address", __func__);
            }
            return (in6addr_error);
        }
        return (addr);
#endif
    }
}

#ifdef HAVE_GETIFADDRS

#include <ifaddrs.h>

struct libnet_in6_addr
libnet_get_ipaddr6(libnet_t *l)
{
    struct ifaddrs *ifaddr, *p;
    struct libnet_in6_addr addr;

    if (l == NULL)
    {
        return (in6addr_error);
    }

    if (getifaddrs(&ifaddr) != 0)
    {
        snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                "%s(): getifaddrs(): %s", __func__, strerror(errno));
        return (in6addr_error);
    }

    if (l->device == NULL)
    {
        if (libnet_select_device(l) == -1)
        {
            /* error msg set in libnet_select_device() */
            return (in6addr_error);
        }
    }

    for (p = ifaddr; p != NULL; p = p->ifa_next)
    {
        if ((strcmp(p->ifa_name, l->device) == 0) && (p->ifa_addr != NULL) &&
                (p->ifa_addr->sa_family == AF_INET6))
        {
            memcpy(&addr.__u6_addr,
                    ((struct sockaddr_in6*)p->ifa_addr)->sin6_addr.s6_addr, 16);
            freeifaddrs(ifaddr);
            return (addr);
        }
    }

    freeifaddrs(ifaddr);
    return (in6addr_error);
}
#else
struct libnet_in6_addr
libnet_get_ipaddr6(libnet_t *l)
{
    snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
           "%s(): not yet Implemented", __func__);
    return (in6addr_error);
}
#endif /* WIN32 */

#if !defined(__WIN32__)
uint32_t
libnet_get_ipaddr4(libnet_t *l)
{
    struct ifreq ifr;
    struct sockaddr_in *sin;
    int fd;

    if (l == NULL)
    {
        return (-1);
    }

    /* create dummy socket to perform an ioctl upon */
    fd = socket(PF_INET, SOCK_DGRAM, 0);
    if (fd == -1)
    {
        snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                "%s(): socket(): %s", __func__, strerror(errno));
        return (-1);
    }

    sin = (struct sockaddr_in *)&ifr.ifr_addr;

    if (l->device == NULL)
    {
        if (libnet_select_device(l) == -1)
        {
            /* error msg set in libnet_select_device() */
            close(fd);
            return (-1);
        }
    }
    strncpy(ifr.ifr_name, l->device, sizeof(ifr.ifr_name) -1);
	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
	
    ifr.ifr_addr.sa_family = AF_INET;

    if (ioctl(fd, SIOCGIFADDR, (int8_t*) &ifr) < 0)
    {
        snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                "%s(): ioctl(): %s", __func__, strerror(errno));
        close(fd);
        return (-1);
    }
    close(fd);
    return (sin->sin_addr.s_addr);
}
#else
#include <Packet32.h>
uint32_t
libnet_get_ipaddr4(libnet_t *l)
{
    long npflen = 1;
    struct sockaddr_in sin;
    struct npf_if_addr ipbuff;

    memset(&sin,0,sizeof(sin));
    memset(&ipbuff,0,sizeof(ipbuff));

    if (PacketGetNetInfoEx(l->device, &ipbuff, &npflen))
    {
        sin = *(struct sockaddr_in *)&ipbuff.IPAddress;
    }
    return (sin.sin_addr.s_addr);
}
#endif /* WIN32 */

uint8_t *
libnet_hex_aton(const char *s, int *len)
{
    uint8_t *buf;
    int i;
    int32_t l;
    char *pp;
        
    while (isspace(*s))
    {
        s++;
    }
    for (i = 0, *len = 0; s[i]; i++)
    {
        if (s[i] == ':')
        {
            (*len)++;
        }
    }
    buf = malloc(*len + 1);
    if (buf == NULL)
    {
        return (NULL);
    }
    /* expect len hex octets separated by ':' */
    for (i = 0; i < *len + 1; i++)
    {
        l = strtol(s, &pp, 16);
        if (pp == s || l > 0xff || l < 0)
        {
            *len = 0;
            free(buf);
            return (NULL);
        }
        if (!(*pp == ':' || (i == *len && (isspace(*pp) || *pp == '\0'))))
        {
            *len = 0;
            free(buf);
            return (NULL);
        }
        buf[i] = (uint8_t)l;
        s = pp + 1;
    }
    /* return int8_tacter after the octets ala strtol(3) */
    (*len)++;
    return (buf);
}

/**
 * Local Variables:
 *  indent-tabs-mode: nil
 *  c-file-style: "stroustrup"
 * End:
 */