~ubuntu-branches/debian/experimental/openchange/experimental

« back to all changes in this revision

Viewing changes to libmapi/socket/netif.c

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2008-08-06 18:32:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080806183245-nauuj71be5mm03w7
Tags: upstream-1.0~svn679
ImportĀ upstreamĀ versionĀ 1.0~svn679

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   Unix SMB/CIFS implementation.
 
3
   return a list of network interfaces
 
4
   Copyright (C) Andrew Tridgell 1998
 
5
   
 
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.
 
10
   
 
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.
 
15
   
 
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/>.
 
18
*/
 
19
 
 
20
 
 
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
 
24
 
 
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.
 
29
 
 
30
*/
 
31
 
 
32
#include <libmapi/libmapi.h>
 
33
 
 
34
#ifdef __COMPAR_FN_T
 
35
#define QSORT_CAST (__compar_fn_t)
 
36
#endif
 
37
 
 
38
#ifndef QSORT_CAST
 
39
#define QSORT_CAST (int (*)(const void *, const void *))
 
40
#endif
 
41
 
 
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.
 
44
 
 
45
   It probably also works on any BSD style system.  */
 
46
 
 
47
/****************************************************************************
 
48
  get the netmask address for a local interface
 
49
****************************************************************************/
 
50
static int _get_interfaces(struct iface_struct *ifaces, int max_interfaces)
 
51
{  
 
52
        struct ifconf ifc;
 
53
        char buff[8192];
 
54
        int fd, i, n;
 
55
        struct ifreq *ifr=NULL;
 
56
        int total = 0;
 
57
        struct in_addr ipaddr;
 
58
        struct in_addr nmask;
 
59
        char *iname;
 
60
 
 
61
        if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
 
62
                return -1;
 
63
        }
 
64
  
 
65
        ifc.ifc_len = sizeof(buff);
 
66
        ifc.ifc_buf = buff;
 
67
 
 
68
        if (ioctl(fd, SIOCGIFCONF, &ifc) != 0) {
 
69
                close(fd);
 
70
                return -1;
 
71
        } 
 
72
 
 
73
        ifr = ifc.ifc_req;
 
74
  
 
75
        n = ifc.ifc_len / sizeof(struct ifreq);
 
76
 
 
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) {
 
80
                        continue;
 
81
                }
 
82
 
 
83
                iname = ifr[i].ifr_name;
 
84
                ipaddr = (*(struct sockaddr_in *)&ifr[i].ifr_addr).sin_addr;
 
85
 
 
86
                if (ioctl(fd, SIOCGIFFLAGS, &ifr[i]) != 0) {
 
87
                        continue;
 
88
                }  
 
89
 
 
90
                if (!(ifr[i].ifr_flags & IFF_UP)) {
 
91
                        continue;
 
92
                }
 
93
 
 
94
                if (ioctl(fd, SIOCGIFNETMASK, &ifr[i]) != 0) {
 
95
                        continue;
 
96
                }  
 
97
 
 
98
                nmask = ((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr;
 
99
 
 
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;
 
104
                total++;
 
105
        }
 
106
 
 
107
        close(fd);
 
108
 
 
109
        return total;
 
110
}  
 
111
 
 
112
static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
 
113
{
 
114
        int r;
 
115
        r = strcmp(i1->name, i2->name);
 
116
        if (r) return r;
 
117
        r = ntohl(i1->ip.s_addr) - ntohl(i2->ip.s_addr);
 
118
        if (r) return r;
 
119
        r = ntohl(i1->netmask.s_addr) - ntohl(i2->netmask.s_addr);
 
120
        return r;
 
121
}
 
122
 
 
123
/* this wrapper is used to remove duplicates from the interface list generated
 
124
   above */
 
125
_PUBLIC_ int get_interfaces(struct iface_struct *ifaces, int max_interfaces)
 
126
{
 
127
        int total, i, j;
 
128
 
 
129
        total = _get_interfaces(ifaces, max_interfaces);
 
130
        if (total <= 0) return total;
 
131
 
 
132
        /* now we need to remove duplicates */
 
133
        qsort(ifaces, total, sizeof(ifaces[0]), QSORT_CAST iface_comp);
 
134
 
 
135
        for (i=1;i<total;) {
 
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];
 
139
                        }
 
140
                        total--;
 
141
                } else {
 
142
                        i++;
 
143
                }
 
144
        }
 
145
 
 
146
        return total;
 
147
}