Annotation of embedaddon/arping/src/findif_getifaddrs.c, revision 1.1.1.3

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: 
1.1.1.3 ! misho      42: #define UNUSED(x) (void)(x)
        !            43: 
1.1       misho      44: const char *
                     45: arping_lookupdev(uint32_t srcip,
                     46:                  uint32_t dstip,
                     47:                  char *ebuf)
                     48: {
                     49:         struct ifaddrs *ifa = NULL;
                     50:         struct ifaddrs *cur;
                     51:         const char *ret = NULL;
                     52:         int match_count = 0;     /* Matching interfaces */
1.1.1.3 ! misho      53:         UNUSED(srcip);
1.1       misho      54: 
                     55:         /* best match */
                     56:         in_addr_t best_mask = 0;
                     57: 
                     58:         /* Results */
                     59:         static char ifname[IFNAMSIZ];
                     60: 
1.1.1.2   misho      61:         *ebuf = 0;
                     62: 
1.1       misho      63:         if (getifaddrs(&ifa)) {
                     64:                 if (verbose) {
1.1.1.2   misho      65:                         printf("arping: getifaddrs(): %s\n", strerror(errno));
1.1       misho      66:                 }
1.1.1.2   misho      67:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
                     68:                          "getifaddrs(): %s", strerror(errno));
1.1       misho      69:                 goto out;
                     70:         }
                     71:         for (cur = ifa; cur; cur = cur->ifa_next) {
                     72:                 in_addr_t addr, mask;
                     73: 
                     74:                 if (!(cur->ifa_flags & IFF_UP)) {
                     75:                         continue;
                     76:                 }
                     77:                 if (!cur->ifa_addr
                     78:                     || !cur->ifa_netmask
                     79:                     || !cur->ifa_name) {
                     80:                         continue;
                     81:                 }
                     82:                 if (cur->ifa_addr->sa_family != AF_INET) {
                     83:                         continue;
                     84:                 }
                     85:                 if (cur->ifa_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) {
                     86:                         continue;
                     87:                 }
                     88:                 addr =((struct sockaddr_in*)cur->ifa_addr)->sin_addr.s_addr;
                     89:                 mask =((struct sockaddr_in*)cur->ifa_netmask)->sin_addr.s_addr;
                     90:                 if ((addr & mask) != (dstip & mask)) {
                     91:                         continue;
                     92:                 }
                     93:                 match_count++;
                     94:                 if (ntohl(mask) > ntohl(best_mask)) {
                     95:                         memset(ifname, 0, sizeof(ifname));
                     96:                         strncpy(ifname, cur->ifa_name, sizeof(ifname)-1);
                     97:                         best_mask = mask;
                     98:                 }
                     99:         }
                    100:         if (match_count) {
                    101:                 ret = ifname;
                    102:                 if (verbose) {
1.1.1.2   misho     103:                         printf("arping: Autodetected interface %s\n", ret);
1.1       misho     104:                 }
                    105:         } else {
1.1.1.2   misho     106:                 if (verbose > 1) {
                    107:                         printf("arping: Failed to find iface using"
                    108:                                " getifaddrs().\n");
1.1       misho     109:                 }
1.1.1.2   misho     110:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
                    111:                          "No matching interface found using getifaddrs().");
1.1       misho     112:         }
                    113:  out:
                    114:         if (ifa) {
                    115:                 freeifaddrs(ifa);
                    116:         }
                    117:         return ret;
                    118: }
                    119: /* ---- Emacs Variables ----
                    120:  * Local Variables:
                    121:  * c-basic-offset: 8
                    122:  * indent-tabs-mode: nil
                    123:  * End:
                    124:  */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>