summaryrefslogtreecommitdiff
path: root/src/libnet_if_addr.c
blob: 241904d7ce35899cb1b23c4e1579fec67181a457 (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
 *  $Id: libnet_if_addr.c,v 1.23 2004/04/13 17:32:28 mike Exp $
 *
 *  libnet
 *  libnet_if_addr.c - interface selection code
 *
 *  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"

#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif

#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif

#include "../include/ifaddrlist.h"

#define MAX_IPADDR 512
static size_t ip_addr_num = MAX_IPADDR;

#if !(__WIN32__)

/*
 * By testing if we can retrieve the FLAGS of an iface
 * we can know if it exists or not and if it is up.
 */
int 
libnet_check_iface(libnet_t *l)
{
    struct ifreq ifr;
    int fd, res;

    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd < 0)
    {
        snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s() socket: %s", __func__, strerror(errno));
        return (-1);
    }

    strncpy(ifr.ifr_name, l->device, sizeof(ifr.ifr_name) -1);
    ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
    
    res = ioctl(fd, SIOCGIFFLAGS, (int8_t *)&ifr);
    if (res < 0)
    {
        snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s() ioctl: %s", __func__, strerror(errno));
    }
    else
    {
        if ((ifr.ifr_flags & IFF_UP) == 0)
        {
            snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): %s is down", __func__, l->device);
	    res = -1;
        }
    }
    close(fd);

    return (res);
}

#endif

#if defined(__OpenBSD__) ||  defined(__linux__)
#include <sys/types.h>
    #ifdef __OpenBSD__
    #include <sys/socket.h>
    #endif
#include <ifaddrs.h>

int
libnet_ifaddrlist(struct libnet_ifaddr_list **ipaddrp, char *dev, char *errbuf)
{
    static struct libnet_ifaddr_list *ifaddrlist = NULL;
    struct ifaddrs *ifap, *ifa;
    int i = 0;

    (void)dev; /* unused */

    if (!ifaddrlist)
    {
        ifaddrlist = calloc(ip_addr_num, sizeof(struct libnet_ifaddr_list));
        if (!ifaddrlist)
        {
            snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): OOM when allocating initial ifaddrlist", __func__);
            return 0;
        }
    }

    if (getifaddrs(&ifap) != 0)
    {
        snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): getifaddrs: %s", __func__, strerror(errno));
        return 0;
    }

    for (ifa = ifap; ifa; ifa = ifa->ifa_next)
    {
        if (ifa->ifa_flags & IFF_LOOPBACK || ifa->ifa_addr == NULL)
            continue;

        if (ifa->ifa_addr->sa_family == AF_INET )
        {
            ifaddrlist[i].device = strdup(ifa->ifa_name);
            if (ifaddrlist[i].device == NULL)
            {
                snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): OOM", __func__);
                continue;
            }
            ifaddrlist[i].addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
            ++i;
        }

        if (i == ip_addr_num) {
            struct libnet_ifaddr_list *tmp;

            /* grow by a factor of 1.5, close enough to golden ratio */
            ip_addr_num += ip_addr_num >> 2;
            tmp = realloc(ifaddrlist, ip_addr_num * sizeof(struct libnet_ifaddr_list));
            if (!tmp)
            {
                snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): OOM reallocating ifaddrlist", __func__);
                break;
            }

            ifaddrlist = tmp;
        }
    }

    freeifaddrs(ifap);
    *ipaddrp = ifaddrlist;
    return (i);
}


#else
#if !(__WIN32__)


/*
 *  Return the interface list
 */

#ifdef HAVE_SOCKADDR_SA_LEN
#define NEXTIFR(i) \
((struct ifreq *)((u_char *)&i->ifr_addr + i->ifr_addr.sa_len))
#else
#define NEXTIFR(i) (i + 1)
#endif

#ifndef BUFSIZE
#define BUFSIZE 2048
#endif

#ifdef HAVE_LINUX_PROCFS
#define PROC_DEV_FILE "/proc/net/dev"
#endif

int
libnet_ifaddrlist(struct libnet_ifaddr_list **ipaddrp, char *dev, char *errbuf)
{
    static struct libnet_ifaddr_list *ifaddrlist = NULL;
    struct ifreq *ifr, *lifr, *pifr, nifr;
    char device[sizeof(nifr.ifr_name)];
    
    char *p;
    struct ifconf ifc;
    struct ifreq ibuf[MAX_IPADDR];
    int fd, nipaddr;
    
#ifdef HAVE_LINUX_PROCFS
    FILE *fp;
    char buf[2048];
#endif
    
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd < 0)
    {
	snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): socket error: %s", __func__, strerror(errno));
	return (-1);
    }

#ifdef HAVE_LINUX_PROCFS
    fp = fopen(PROC_DEV_FILE, "r");
    if (!fp)
    {
	snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): fopen(proc_dev_file) failed: %s",  __func__, strerror(errno));
	goto bad;
    }
#endif

    memset(&ifc, 0, sizeof(ifc));
    ifc.ifc_len = sizeof(ibuf);
    ifc.ifc_buf = (caddr_t)ibuf;

    if (ioctl(fd, SIOCGIFCONF, &ifc) < 0)
    {
	snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): ioctl(SIOCGIFCONF) error: %s", __func__, strerror(errno));
	goto bad;
    }

    pifr = NULL;
    lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
    
    if (!ifaddrlist)
    {
        ifaddrlist = calloc(ip_addr_num, sizeof(struct libnet_ifaddr_list));
        if (!ifaddrlist)
        {
            snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): OOM when allocating initial ifaddrlist", __func__);
            return 0;
        }
    }

    nipaddr = 0;

#ifdef HAVE_LINUX_PROCFS
    while (fgets(buf, sizeof(buf), fp))
    {
        p = strchr(buf, ':');
	if (!p)
            continue;

        *p = '\0';
        for (p = buf; *p == ' '; p++)
	    ;
	
        strncpy(nifr.ifr_name, p, sizeof(nifr.ifr_name) - 1);
        nifr.ifr_name[sizeof(nifr.ifr_name) - 1] = '\0';
	
#else /* !HAVE_LINUX_PROCFS */

    for (ifr = ifc.ifc_req; ifr < lifr; ifr = NEXTIFR(ifr))
    {
        struct libnet_ifaddr_list *al = &ifaddrlist[nipaddr];

	/* XXX LINUX SOLARIS ifalias */
        p = strchr(ifr->ifr_name, ':');
	if (p)
            *p = '\0';

	if (pifr && strcmp(ifr->ifr_name, pifr->ifr_name) == 0)
            continue;

	strncpy(nifr.ifr_name, ifr->ifr_name, sizeof(nifr.ifr_name) - 1);
	nifr.ifr_name[sizeof(nifr.ifr_name) - 1] = '\0';
#endif

        /* save device name */
        strncpy(device, nifr.ifr_name, sizeof(device) - 1);
        device[sizeof(device) - 1] = '\0';

        if (ioctl(fd, SIOCGIFFLAGS, &nifr) < 0)
        {
            pifr = ifr;
            continue;
	}
        if ((nifr.ifr_flags & IFF_UP) == 0)
	{
            pifr = ifr;
            continue;	
	}

        if (dev == NULL && LIBNET_ISLOOPBACK(&nifr))
	{
            pifr = ifr;
            continue;
	}
	
        strncpy(nifr.ifr_name, device, sizeof(device) - 1);
        nifr.ifr_name[sizeof(nifr.ifr_name) - 1] = '\0';
        if (ioctl(fd, SIOCGIFADDR, (int8_t *)&nifr) < 0)
        {
            if (errno != EADDRNOTAVAIL)
            {
                snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): SIOCGIFADDR: dev=%s: %s", __func__, device, strerror(errno));
                goto bad;
	    }

            /* device has no IP address => set to 0 */
            al->addr = 0;
        }
        else
        {
            al->addr = ((struct sockaddr_in *)&nifr.ifr_addr)->sin_addr.s_addr;
        }
        
        free(al->device);

        al->device = strdup(device);
        if (al->device == NULL)
        {
            snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): strdup not enough memory", __func__);
            goto bad;
        }

        ++nipaddr;
        if (nipaddr == ip_addr_num) {
            struct libnet_ifaddr_list *tmp;

            /* grow by a factor of 1.5, close enough to golden ratio */
            ip_addr_num += ip_addr_num >> 2;
            tmp = realloc(ifaddrlist, ip_addr_num * sizeof(struct libnet_ifaddr_list));
            if (!tmp) {
                snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): OOM reallocating ifaddrlist", __func__);
                break;
            }

            ifaddrlist = tmp;
        }

#ifndef HAVE_LINUX_PROCFS
        pifr = ifr;
#endif
    }
	
#ifdef HAVE_LINUX_PROCFS
    if (ferror(fp))
    {
        snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): ferror: %s", __func__, strerror(errno));
	goto bad;
    }
    fclose(fp);
#endif

    close(fd);
    *ipaddrp = ifaddrlist;

    return (nipaddr);

bad:
#ifdef HAVE_LINUX_PROCFS
    if (fp)
	fclose(fp);
#endif
    close(fd);
    return (-1);
}
#else
/* WIN32 support *
 * TODO move win32 support into win32 specific source file */

/* From tcptraceroute, convert a numeric IP address to a string */
#define IPTOSBUFFERS    12
static int8_t *iptos(uint32_t in)
{
    static int8_t output[IPTOSBUFFERS][ 3 * 4 + 3 + 1];
    static int16_t which;
    uint8_t *p;

    p = (uint8_t *)&in;
    which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
    snprintf(output[which], IPTOSBUFFERS, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);

    return output[which];
}

int
libnet_ifaddrlist(struct libnet_ifaddr_list **ipaddrp, char *dev_unused, char *errbuf)
{
    static struct libnet_ifaddr_list *ifaddrlist = NULL;
    int8_t err[PCAP_ERRBUF_SIZE];
    pcap_if_t *devlist = NULL;
    pcap_if_t *dev = NULL;
    int nipaddr = 0;
    int i = 0;

    /* Retrieve the interfaces list */
    if (pcap_findalldevs(&devlist, err) == -1)
    {
        snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): error in pcap_findalldevs: %s", __func__, err);
        return (-1);
    }

    if (!ifaddrlist)
    {
        ifaddrlist = calloc(ip_addr_num, sizeof(struct libnet_ifaddr_list));
        if (!ifaddrlist)
        {
            snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): OOM when allocating initial ifaddrlist", __func__);
            return 0;
        }
    }

    for (dev = devlist; dev; dev = dev->next)
    {
        struct pcap_addr* pcapaddr;

        for (pcapaddr = dev->addresses; pcapaddr; pcapaddr = pcapaddr->next)
        {
            struct sockaddr* addr = pcapaddr->addr;
#if 0
            printf("if name '%s' description '%s' loop? %d\n", dev->name, dev->description, dev->flags);
            {
                char p[NI_MAXHOST] = "";
                int sz = sizeof(struct sockaddr_storage);
                int r;
                r = getnameinfo(addr, sz, p, sizeof(p), NULL,0, NI_NUMERICHOST);
                printf("  addr %s\n", r ? gai_strerror(r) : p);
            }
#endif

            if (dev->flags & PCAP_IF_LOOPBACK)
                continue;

            /* this code ignores IPv6 addresses, a limitation of the libnet_ifaddr_list struct */

            if (addr->sa_family != AF_INET)
                continue;

            ifaddrlist[i].device = strdup(dev->name);
            ifaddrlist[i].addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
            ++i;
            ++nipaddr;

            if (nipaddr == ip_addr_num)
            {
                struct libnet_ifaddr_list *tmp;

                /* grow by a factor of 1.5, close enough to golden ratio */
                ip_addr_num += ip_addr_num >> 2;
                tmp = realloc(ifaddrlist, ip_addr_num * sizeof(struct libnet_ifaddr_list));
                if (!tmp)
                {
                    snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): OOM reallocating ifaddrlist", __func__);
                    break;
                }

                ifaddrlist = tmp;
            }
        }
    }

    pcap_freealldevs(devlist);

    *ipaddrp = ifaddrlist;

    return nipaddr;
}
#endif /* __WIN32__ */

#endif /* __OpenBSD__ */

int
libnet_select_device(libnet_t *l)
{
    struct libnet_ifaddr_list *address_list = NULL, *al;
    uint32_t addr;
    int c, i, rc;

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

    if (l->device && !isdigit(l->device[0]))
    {
#if !(__WIN32__)
	if (libnet_check_iface(l) < 0)
	{
            /* err msg set in libnet_check_iface() */
	    return (-1);
	}
#endif
	return (1);
    }

    /*
     *  Number of interfaces.
     */
    c = libnet_ifaddrlist(&address_list, l->device, l->err_buf);
    if (c < 0)
    {
        return (-1);
    }
    else if (c == 0)
    {
        snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): no network interface found", __func__);
        return (-1);
    }

    al = address_list;
    if (l->device)
    {
        addr = libnet_name2addr4(l, l->device, LIBNET_DONT_RESOLVE);

        for (i = c; i; --i, ++address_list)
        {
            if (
                    0 == strcmp(l->device, address_list->device)
                    || 
                    address_list->addr == addr
               )
            {
                /* free the "user supplied device" - see libnet_init() */
                free(l->device);
                l->device =  strdup(address_list->device);
                goto good;
            }
        }
        if (i <= 0)
        {
            snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): can't find interface for IP %s", __func__, l->device);
	    goto bad;
        }
    }
    else
    {
        l->device = strdup(address_list->device);
    }

good:
    rc = 1;
    goto end;
bad:
    rc = -1;
end:
    if (al) {
        for (i = 0; i < c; i++)
        {
            free(al[i].device);
            al[i].device = NULL;
        }
        free(al);
    }

    return rc;
}

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