2
Unix SMB/CIFS implementation.
3
return a list of network interfaces
4
Copyright (C) Andrew Tridgell 1998
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 3 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program. If not, see <http://www.gnu.org/licenses/>.
21
/* working out the interfaces for a OS is an incredibly non-portable
22
thing. We have several possible implementations below, and autoconf
23
tries each of them to see what works
25
Note that this file does _not_ include includes.h. That is so this code
26
can be called directly from the autoconf tests. That also means
27
this code cannot use any of the normal Samba debug stuff or defines.
28
This is standalone code.
32
#include <libmapi/libmapi.h>
35
#define QSORT_CAST (__compar_fn_t)
39
#define QSORT_CAST (int (*)(const void *, const void *))
42
/* this works for Linux 2.2, Solaris 2.5, SunOS4, HPUX 10.20, OSF1
43
V4.0, Ultrix 4.4, SCO Unix 3.2, IRIX 6.4 and FreeBSD 3.2.
45
It probably also works on any BSD style system. */
47
/****************************************************************************
48
get the netmask address for a local interface
49
****************************************************************************/
50
static int _get_interfaces(struct iface_struct *ifaces, int max_interfaces)
55
struct ifreq *ifr=NULL;
57
struct in_addr ipaddr;
61
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
65
ifc.ifc_len = sizeof(buff);
68
if (ioctl(fd, SIOCGIFCONF, &ifc) != 0) {
75
n = ifc.ifc_len / sizeof(struct ifreq);
77
/* Loop through interfaces, looking for given IP address */
78
for (i=n-1;i>=0 && total < max_interfaces;i--) {
79
if (ioctl(fd, SIOCGIFADDR, &ifr[i]) != 0) {
83
iname = ifr[i].ifr_name;
84
ipaddr = (*(struct sockaddr_in *)&ifr[i].ifr_addr).sin_addr;
86
if (ioctl(fd, SIOCGIFFLAGS, &ifr[i]) != 0) {
90
if (!(ifr[i].ifr_flags & IFF_UP)) {
94
if (ioctl(fd, SIOCGIFNETMASK, &ifr[i]) != 0) {
98
nmask = ((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr;
100
strncpy(ifaces[total].name, iname, sizeof(ifaces[total].name)-1);
101
ifaces[total].name[sizeof(ifaces[total].name)-1] = 0;
102
ifaces[total].ip = ipaddr;
103
ifaces[total].netmask = nmask;
112
static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
115
r = strcmp(i1->name, i2->name);
117
r = ntohl(i1->ip.s_addr) - ntohl(i2->ip.s_addr);
119
r = ntohl(i1->netmask.s_addr) - ntohl(i2->netmask.s_addr);
123
/* this wrapper is used to remove duplicates from the interface list generated
125
_PUBLIC_ int get_interfaces(struct iface_struct *ifaces, int max_interfaces)
129
total = _get_interfaces(ifaces, max_interfaces);
130
if (total <= 0) return total;
132
/* now we need to remove duplicates */
133
qsort(ifaces, total, sizeof(ifaces[0]), QSORT_CAST iface_comp);
136
if (iface_comp(&ifaces[i-1], &ifaces[i]) == 0) {
137
for (j=i-1;j<total-1;j++) {
138
ifaces[j] = ifaces[j+1];