Annotation of embedaddon/arping/src/findif_getifaddrs.c, revision 1.1.1.2
1.1 misho 1: /* arping/src/findif_getifaddrs.c
2: *
1.1.1.2 ! misho 3: * Copyright (C) 2000-2014 Thomas Habets <thomas@habets.se>
1.1 misho 4: *
1.1.1.2 ! misho 5: * This program is free software; you can redistribute it and/or modify
! 6: * it under the terms of the GNU General Public License as published by
! 7: * the Free Software Foundation; either version 2 of the License, or
! 8: * (at your option) any later version.
1.1 misho 9: *
1.1.1.2 ! misho 10: * This program is distributed in the hope that it will be useful,
1.1 misho 11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.1.1.2 ! misho 12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 13: * GNU General Public License for more details.
1.1 misho 14: *
15: * You should have received a copy of the GNU General Public License along
16: * with this program; if not, write to the Free Software Foundation, Inc.,
17: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18: */
19: /**
20: * Most modern systems should have getifaddrs().
21: */
22: #if HAVE_CONFIG_H
23: #include "config.h"
24: #endif
25:
26: #include <stdio.h>
27: #include <string.h>
28: #include <errno.h>
29: #include <sys/types.h>
30: #include <sys/socket.h>
31: #include <sys/ioctl.h>
32: #include <ifaddrs.h>
33: #include <net/if.h>
34: #include <netinet/in.h>
35:
1.1.1.2 ! misho 36: #if HAVE_LIBNET_H
! 37: #include <libnet.h>
! 38: #endif
! 39:
1.1 misho 40: #include "arping.h"
41:
42: const char *
43: arping_lookupdev(uint32_t srcip,
44: uint32_t dstip,
45: char *ebuf)
46: {
47: struct ifaddrs *ifa = NULL;
48: struct ifaddrs *cur;
49: const char *ret = NULL;
50: int match_count = 0; /* Matching interfaces */
51:
52: /* best match */
53: in_addr_t best_mask = 0;
54: in_addr_t best_addr;
55:
56: /* Results */
57: static char ifname[IFNAMSIZ];
58:
1.1.1.2 ! misho 59: *ebuf = 0;
! 60:
1.1 misho 61: if (getifaddrs(&ifa)) {
62: if (verbose) {
1.1.1.2 ! misho 63: printf("arping: getifaddrs(): %s\n", strerror(errno));
1.1 misho 64: }
1.1.1.2 ! misho 65: snprintf(ebuf, LIBNET_ERRBUF_SIZE,
! 66: "getifaddrs(): %s", strerror(errno));
1.1 misho 67: goto out;
68: }
69: for (cur = ifa; cur; cur = cur->ifa_next) {
70: in_addr_t addr, mask;
71:
72: if (!(cur->ifa_flags & IFF_UP)) {
73: continue;
74: }
75: if (!cur->ifa_addr
76: || !cur->ifa_netmask
77: || !cur->ifa_name) {
78: continue;
79: }
80: if (cur->ifa_addr->sa_family != AF_INET) {
81: continue;
82: }
83: if (cur->ifa_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) {
84: continue;
85: }
86: addr =((struct sockaddr_in*)cur->ifa_addr)->sin_addr.s_addr;
87: mask =((struct sockaddr_in*)cur->ifa_netmask)->sin_addr.s_addr;
88: if ((addr & mask) != (dstip & mask)) {
89: continue;
90: }
91: match_count++;
92: if (ntohl(mask) > ntohl(best_mask)) {
93: memset(ifname, 0, sizeof(ifname));
94: strncpy(ifname, cur->ifa_name, sizeof(ifname)-1);
95: best_addr = addr;
96: best_mask = mask;
97: }
98: }
99: if (match_count) {
100: ret = ifname;
101: if (verbose) {
1.1.1.2 ! misho 102: printf("arping: Autodetected interface %s\n", ret);
1.1 misho 103: }
104: } else {
1.1.1.2 ! misho 105: if (verbose > 1) {
! 106: printf("arping: Failed to find iface using"
! 107: " getifaddrs().\n");
1.1 misho 108: }
1.1.1.2 ! misho 109: snprintf(ebuf, LIBNET_ERRBUF_SIZE,
! 110: "No matching interface found using getifaddrs().");
1.1 misho 111: }
112: out:
113: if (ifa) {
114: freeifaddrs(ifa);
115: }
116: return ret;
117: }
118: /* ---- Emacs Variables ----
119: * Local Variables:
120: * c-basic-offset: 8
121: * indent-tabs-mode: nil
122: * End:
123: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>