| 1 | /* arping/src/findif_getifaddrs.c |
| 2 | * |
| 3 | * Copyright (C) 2000-2014 Thomas Habets <[email protected]> |
| 4 | * |
| 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. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 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 | #define _GNU_SOURCE |
| 23 | #if HAVE_CONFIG_H |
| 24 | #include "config.h" |
| 25 | #endif |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <ifaddrs.h> |
| 34 | #include <net/if.h> |
| 35 | #include <netinet/in.h> |
| 36 | |
| 37 | #if HAVE_LIBNET_H |
| 38 | #include <libnet.h> |
| 39 | #endif |
| 40 | |
| 41 | #include "arping.h" |
| 42 | |
| 43 | #define UNUSED(x) (void)(x) |
| 44 | |
| 45 | const char * |
| 46 | arping_lookupdev(uint32_t srcip, |
| 47 | uint32_t dstip, |
| 48 | char *ebuf) |
| 49 | { |
| 50 | struct ifaddrs *ifa = NULL; |
| 51 | struct ifaddrs *cur; |
| 52 | const char *ret = NULL; |
| 53 | int match_count = 0; /* Matching interfaces */ |
| 54 | UNUSED(srcip); |
| 55 | |
| 56 | /* best match */ |
| 57 | in_addr_t best_mask = 0; |
| 58 | |
| 59 | /* Results */ |
| 60 | static char ifname[IFNAMSIZ]; |
| 61 | |
| 62 | *ebuf = 0; |
| 63 | |
| 64 | if (getifaddrs(&ifa)) { |
| 65 | if (verbose) { |
| 66 | printf("arping: getifaddrs(): %s\n", strerror(errno)); |
| 67 | } |
| 68 | xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 69 | "getifaddrs(): %s", strerror(errno)); |
| 70 | goto out; |
| 71 | } |
| 72 | for (cur = ifa; cur; cur = cur->ifa_next) { |
| 73 | in_addr_t addr, mask; |
| 74 | |
| 75 | if (!(cur->ifa_flags & IFF_UP)) { |
| 76 | continue; |
| 77 | } |
| 78 | if (!cur->ifa_addr |
| 79 | || !cur->ifa_netmask |
| 80 | || !cur->ifa_name) { |
| 81 | continue; |
| 82 | } |
| 83 | if (cur->ifa_addr->sa_family != AF_INET) { |
| 84 | continue; |
| 85 | } |
| 86 | if (cur->ifa_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) { |
| 87 | continue; |
| 88 | } |
| 89 | addr =((struct sockaddr_in*)cur->ifa_addr)->sin_addr.s_addr; |
| 90 | mask =((struct sockaddr_in*)cur->ifa_netmask)->sin_addr.s_addr; |
| 91 | if ((addr & mask) != (dstip & mask)) { |
| 92 | continue; |
| 93 | } |
| 94 | match_count++; |
| 95 | if (ntohl(mask) > ntohl(best_mask)) { |
| 96 | memset(ifname, 0, sizeof(ifname)); |
| 97 | strncpy(ifname, cur->ifa_name, sizeof(ifname)-1); |
| 98 | best_mask = mask; |
| 99 | } |
| 100 | } |
| 101 | if (match_count) { |
| 102 | ret = ifname; |
| 103 | if (verbose) { |
| 104 | printf("arping: Autodetected interface %s\n", ret); |
| 105 | } |
| 106 | } else { |
| 107 | if (verbose > 1) { |
| 108 | printf("arping: Failed to find iface using" |
| 109 | " getifaddrs().\n"); |
| 110 | } |
| 111 | xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 112 | "No matching interface found using getifaddrs()."); |
| 113 | } |
| 114 | out: |
| 115 | if (ifa) { |
| 116 | freeifaddrs(ifa); |
| 117 | } |
| 118 | return ret; |
| 119 | } |
| 120 | /* ---- Emacs Variables ---- |
| 121 | * Local Variables: |
| 122 | * c-basic-offset: 8 |
| 123 | * indent-tabs-mode: nil |
| 124 | * End: |
| 125 | */ |