File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / arping / src / findif_getifaddrs.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Sun Jun 15 16:26:43 2014 UTC (10 years ago) by misho
Branches: arping, MAIN
CVS tags: v2_13, HEAD
arping 2.13

    1: /* arping/src/findif_getifaddrs.c
    2:  *
    3:  *  Copyright (C) 2000-2011 Thomas Habets <thomas@habets.se>
    4:  *
    5:  *  This library is free software; you can redistribute it and/or
    6:  *  modify it under the terms of the GNU General Public
    7:  *  License as published by the Free Software Foundation; either
    8:  *  version 2 of the License, or (at your option) any later version.
    9:  *
   10:  *  This library 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 GNU
   13:  *  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: #include "arping.h"
   37: 
   38: const char *
   39: arping_lookupdev(uint32_t srcip,
   40:                  uint32_t dstip,
   41:                  char *ebuf)
   42: {
   43:         struct ifaddrs *ifa = NULL;
   44:         struct ifaddrs *cur;
   45:         const char *ret = NULL;
   46:         int match_count = 0;     /* Matching interfaces */
   47: 
   48:         /* best match */
   49:         in_addr_t best_mask = 0;
   50:         in_addr_t best_addr;
   51: 
   52:         /* Results */
   53:         static char ifname[IFNAMSIZ];
   54: 
   55:         if (getifaddrs(&ifa)) {
   56:                 if (verbose) {
   57:                         printf("getifaddrs(): %s\n", strerror(errno));
   58:                 }
   59:                 goto out;
   60:         }
   61:         for (cur = ifa; cur; cur = cur->ifa_next) {
   62:                 in_addr_t addr, mask;
   63: 
   64:                 if (!(cur->ifa_flags & IFF_UP)) {
   65:                         continue;
   66:                 }
   67:                 if (!cur->ifa_addr
   68:                     || !cur->ifa_netmask
   69:                     || !cur->ifa_name) {
   70:                         continue;
   71:                 }
   72:                 if (cur->ifa_addr->sa_family != AF_INET) {
   73:                         continue;
   74:                 }
   75:                 if (cur->ifa_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) {
   76:                         continue;
   77:                 }
   78:                 addr =((struct sockaddr_in*)cur->ifa_addr)->sin_addr.s_addr;
   79:                 mask =((struct sockaddr_in*)cur->ifa_netmask)->sin_addr.s_addr;
   80:                 if ((addr & mask) != (dstip & mask)) {
   81:                         continue;
   82:                 }
   83:                 match_count++;
   84:                 if (ntohl(mask) > ntohl(best_mask)) {
   85:                         memset(ifname, 0, sizeof(ifname));
   86:                         strncpy(ifname, cur->ifa_name, sizeof(ifname)-1);
   87:                         best_addr = addr;
   88:                         best_mask = mask;
   89:                 }
   90:         }
   91:         if (match_count) {
   92:                 ret = ifname;
   93:                 if (verbose) {
   94:                         printf("Autodetected interface %s\n", ret);
   95:                 }
   96:         } else {
   97:                 if (verbose) {
   98:                         printf("Failed to find iface using getifaddrs().\n");
   99:                 }
  100:         }
  101:  out:
  102:         if (ifa) {
  103:                 freeifaddrs(ifa);
  104:         }
  105:         return ret;
  106: }
  107: /* ---- Emacs Variables ----
  108:  * Local Variables:
  109:  * c-basic-offset: 8
  110:  * indent-tabs-mode: nil
  111:  * End:
  112:  */

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