File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / arping / src / findif_getifaddrs.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Mar 16 23:40:57 2021 UTC (3 years, 3 months ago) by misho
Branches: arping, MAIN
CVS tags: v2_21, HEAD
arping 2.21

    1: /* arping/src/findif_getifaddrs.c
    2:  *
    3:  *  Copyright (C) 2000-2014 Thomas Habets <thomas@habets.se>
    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: #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: 
   36: #if HAVE_LIBNET_H
   37: #include <libnet.h>
   38: #endif
   39: 
   40: #include "arping.h"
   41: 
   42: #define UNUSED(x) (void)(x)
   43: 
   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 */
   53:         UNUSED(srcip);
   54: 
   55:         /* best match */
   56:         in_addr_t best_mask = 0;
   57: 
   58:         /* Results */
   59:         static char ifname[IFNAMSIZ];
   60: 
   61:         *ebuf = 0;
   62: 
   63:         if (getifaddrs(&ifa)) {
   64:                 if (verbose) {
   65:                         printf("arping: getifaddrs(): %s\n", strerror(errno));
   66:                 }
   67:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
   68:                          "getifaddrs(): %s", strerror(errno));
   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) {
  103:                         printf("arping: Autodetected interface %s\n", ret);
  104:                 }
  105:         } else {
  106:                 if (verbose > 1) {
  107:                         printf("arping: Failed to find iface using"
  108:                                " getifaddrs().\n");
  109:                 }
  110:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
  111:                          "No matching interface found using getifaddrs().");
  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>