root/source4/lib/socket/netif.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. _get_interfaces
  2. iface_comp
  3. get_interfaces

   1 /* 
   2    Unix SMB/CIFS implementation.
   3    return a list of network interfaces
   4    Copyright (C) Andrew Tridgell 1998
   5    Copyright (C) Jeremy Allison 2007
   6    Copyright (C) Jelmer Vernooij 2007
   7    
   8    This program is free software; you can redistribute it and/or modify
   9    it under the terms of the GNU General Public License as published by
  10    the Free Software Foundation; either version 3 of the License, or
  11    (at your option) any later version.
  12    
  13    This program is distributed in the hope that it will be useful,
  14    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16    GNU General Public License for more details.
  17    
  18    You should have received a copy of the GNU General Public License
  19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20 */
  21 
  22 
  23 /* working out the interfaces for a OS is an incredibly non-portable
  24    thing. We have several possible implementations below, and autoconf
  25    tries each of them to see what works
  26 
  27    Note that this file does _not_ include includes.h. That is so this code
  28    can be called directly from the autoconf tests. That also means
  29    this code cannot use any of the normal Samba debug stuff or defines.
  30    This is standalone code.
  31 
  32 */
  33 
  34 #include "includes.h"
  35 #include "system/network.h"
  36 #include "netif.h"
  37 
  38 /****************************************************************************
  39  Try the "standard" getifaddrs/freeifaddrs interfaces.
  40  Also gets IPv6 interfaces.
  41 ****************************************************************************/
  42 
  43 /****************************************************************************
  44  Get the netmask address for a local interface.
  45 ****************************************************************************/
  46 
  47 static int _get_interfaces(struct iface_struct *ifaces, int max_interfaces)
     /* [<][>][^][v][top][bottom][index][help] */
  48 {
  49         struct ifaddrs *iflist = NULL;
  50         struct ifaddrs *ifptr = NULL;
  51         int total = 0;
  52 
  53         if (getifaddrs(&iflist) < 0) {
  54                 return -1;
  55         }
  56 
  57         /* Loop through interfaces, looking for given IP address */
  58         for (ifptr = iflist, total = 0;
  59                         ifptr != NULL && total < max_interfaces;
  60                         ifptr = ifptr->ifa_next) {
  61 
  62                 memset(&ifaces[total], '\0', sizeof(ifaces[total]));
  63 
  64                 if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
  65                         continue;
  66                 }
  67 
  68                 /* Check the interface is up. */
  69                 if (!(ifptr->ifa_flags & IFF_UP)) {
  70                         continue;
  71                 }
  72 
  73                 /* We don't support IPv6 *yet* */
  74                 if (ifptr->ifa_addr->sa_family != AF_INET) {
  75                         continue;
  76                 }
  77 
  78                 ifaces[total].ip = ((struct sockaddr_in *)ifptr->ifa_addr)->sin_addr;
  79                 ifaces[total].netmask = ((struct sockaddr_in *)ifptr->ifa_netmask)->sin_addr;
  80 
  81                 strlcpy(ifaces[total].name, ifptr->ifa_name,
  82                         sizeof(ifaces[total].name));
  83                 total++;
  84         }
  85 
  86         freeifaddrs(iflist);
  87 
  88         return total;
  89 }
  90 
  91 static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
     /* [<][>][^][v][top][bottom][index][help] */
  92 {
  93         int r;
  94         r = strcmp(i1->name, i2->name);
  95         if (r) return r;
  96         r = ntohl(i1->ip.s_addr) - ntohl(i2->ip.s_addr);
  97         if (r) return r;
  98         r = ntohl(i1->netmask.s_addr) - ntohl(i2->netmask.s_addr);
  99         return r;
 100 }
 101 
 102 /* this wrapper is used to remove duplicates from the interface list generated
 103    above */
 104 int get_interfaces(struct iface_struct *ifaces, int max_interfaces)
     /* [<][>][^][v][top][bottom][index][help] */
 105 {
 106         int total, i, j;
 107 
 108         total = _get_interfaces(ifaces, max_interfaces);
 109         if (total <= 0) return total;
 110 
 111         /* now we need to remove duplicates */
 112         qsort(ifaces, total, sizeof(ifaces[0]), QSORT_CAST iface_comp);
 113 
 114         for (i=1;i<total;) {
 115                 if (iface_comp(&ifaces[i-1], &ifaces[i]) == 0) {
 116                         for (j=i-1;j<total-1;j++) {
 117                                 ifaces[j] = ifaces[j+1];
 118                         }
 119                         total--;
 120                 } else {
 121                         i++;
 122                 }
 123         }
 124 
 125         return total;
 126 }

/* [<][>][^][v][top][bottom][index][help] */