summaryrefslogtreecommitdiff
path: root/source3/lib/netmask.c
blob: 138a4413f1005232edb0a29e952f15e5a20d8aba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   code to query kernel netmask
   Copyright (C) Andrew Tridgell 1998
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


/* working out the netmask for an interface is an incredibly non-portable
   thing. We have several possible implementations below, and autoconf
   tries each of them to see what works 

   Note that this file does _not_ include includes.h. That is so this code
   can be called directly from the autoconf tests. That also means
   this code cannot use any of the normal Samba debug stuff or defines.
   This is standalone code.

*/

#ifndef AUTOCONF
#include "config.h"
#endif

#ifdef HAVE_NETMASK_IFCONF

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>

#ifndef SIOCGIFCONF
#include <sys/sockio.h>
#endif


/****************************************************************************
  get the netmask address for a local interface
****************************************************************************/
int get_netmask(struct in_addr *ipaddr, struct in_addr *nmask)
{  
	struct ifconf ifc;
	char buff[2048];
	int fd, i, n;
	struct ifreq *ifr=NULL;

	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
#ifdef DEBUG
		fprintf(stderr,"socket failed\n");
#endif
		return -1;
	}
  
	ifc.ifc_len = sizeof(buff);
	ifc.ifc_buf = buff;
	if (ioctl(fd, SIOCGIFCONF, &ifc) != 0) {
#ifdef DEBUG
		fprintf(stderr,"SIOCGIFCONF failed\n");
#endif
		close(fd);
		return -1;
	} 

	ifr = ifc.ifc_req;
  
	n = ifc.ifc_len / sizeof(struct ifreq);
	
#ifdef DEBUG
	fprintf(stderr,"%d interfaces - looking for %s\n", 
		n, inet_ntoa(*ipaddr));
#endif

	/* Loop through interfaces, looking for given IP address */
	for (i=n-1;i>=0;i--) {
		if (ioctl(fd, SIOCGIFADDR, &ifr[i]) != 0) {
#ifdef DEBUG
			fprintf(stderr,"SIOCGIFADDR failed\n");
#endif
			continue;
		}

#ifdef DEBUG
		fprintf(stderr,"interface %s\n", 
			inet_ntoa((*(struct sockaddr_in *)&ifr[i].ifr_addr).sin_addr));
#endif
		if (ipaddr->s_addr !=
		    (*(struct sockaddr_in *)&ifr[i].ifr_addr).sin_addr.s_addr) {
			continue;
		}

		if (ioctl(fd, SIOCGIFNETMASK, &ifr[i]) != 0) {
#ifdef DEBUG
			fprintf(stderr,"SIOCGIFNETMASK failed\n");
#endif
			close(fd);
			return -1;
		}  
		close(fd);
		(*nmask) = ((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr;
#ifdef DEBUG
		fprintf(stderr,"netmask %s\n", inet_ntoa(*nmask));
#endif
		return 0;
	}

#ifdef DEBUG
	fprintf(stderr,"interface not found\n");
#endif

	close(fd);
	return -1;
}  

#elif defined(HAVE_NETMASK_IFREQ)

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>

#ifndef SIOCGIFCONF
#include <sys/sockio.h>
#endif

#ifndef I_STR
#include <sys/stropts.h>
#endif


/****************************************************************************
this should cover most of the rest of systems
****************************************************************************/
 int get_netmask(struct in_addr *ipaddr, struct in_addr *nmask)
{
	struct ifreq ifreq;
	struct strioctl strioctl;
	struct ifconf *ifc;
	char buff[2048];
	int fd, i, n;
	struct ifreq *ifr=NULL;

	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
#ifdef DEBUG
		fprintf(stderr,"socket failed\n");
#endif
		return -1;
	}
  
	ifc = (struct ifconf *)buff;
	ifc->ifc_len = BUFSIZ - sizeof(struct ifconf);
	strioctl.ic_cmd = SIOCGIFCONF;
	strioctl.ic_dp  = (char *)ifc;
	strioctl.ic_len = sizeof(buff);
	if (ioctl(fd, I_STR, &strioctl) < 0) {
#ifdef DEBUG
		fprintf(stderr,"SIOCGIFCONF failed\n");
#endif
		close(fd);
		return -1;
	} 
	
	ifr = (struct ifreq *)ifc->ifc_req;  

	/* Loop through interfaces, looking for given IP address */
	n = ifc->ifc_len / sizeof(struct ifreq);

	for (i = 0; i<n; i++, ifr++) {
#ifdef DEBUG
		fprintf(stderr,"interface %s\n", 
			inet_ntoa((*(struct sockaddr_in *)&ifr->ifr_addr).sin_addr.s_addr));
#endif
		if (ipaddr->s_addr ==
		    (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) {
			break;
		}
	}

#ifdef DEBUG
	if (i == n) {
		fprintf(stderr,"interface not found\n");
		close(fd);
		return -1;
	}
#endif

	ifreq = *ifr;
  
	strioctl.ic_cmd = SIOCGIFNETMASK;
	strioctl.ic_dp  = (char *)&ifreq;
	strioctl.ic_len = sizeof(struct ifreq);
	if (ioctl(fd, I_STR, &strioctl) != 0) {
#ifdef DEBUG
		fprintf(stderr,"Failed SIOCGIFNETMASK\n");
#endif
		close(fd);
		return -1;
	}

	close(fd);
	*nmask = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
#ifdef DEBUG
	fprintf(stderr,"netmask %s\n", inet_ntoa(*nmask));
#endif
	return 0;
}

#elif defined(HAVE_NETMASK_AIX)

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>

#ifndef SIOCGIFCONF
#include <sys/sockio.h>
#endif

/****************************************************************************
this one is for AIX
****************************************************************************/
 int get_netmask(struct in_addr *ipaddr, struct in_addr *nmask)
{
	char buff[2048];
	int fd, i, n;
	struct ifconf ifc;
	struct ifreq *ifr=NULL;

	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
#ifdef DEBUG
		fprintf(stderr,"socket failed\n");
#endif
		return -1;
	}


	ifc.ifc_len = sizeof(buff);
	ifc.ifc_buf = buff;

	if (ioctl(fd, SIOCGIFCONF, &ifc) != 0) {
#ifdef DEBUG
		fprintf(stderr,"SIOCGIFCONF failed\n");
#endif
		close(fd);
		return -1;
	}

	ifr = ifc.ifc_req;
	/* Loop through interfaces, looking for given IP address */
	i = ifc.ifc_len;
	while (i > 0) {
#ifdef DEBUG
		fprintf(stderr,"interface %s\n", 
			inet_ntoa((*(struct sockaddr_in *)&ifr->ifr_addr).sin_addr));
#endif
		if (ipaddr->s_addr ==
		    (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) {
			break;
		}
		i -= ifr->ifr_addr.sa_len + IFNAMSIZ;
		ifr = (struct ifreq*) ((char*) ifr + ifr->ifr_addr.sa_len + 
				       IFNAMSIZ);
	}
  

#ifdef DEBUG
	if (i <= 0) {
		fprintf(stderr,"interface not found\n");
		close(fd);
		return -1;
	}
#endif

	if (ioctl(fd, SIOCGIFNETMASK, ifr) != 0) {
#ifdef DEBUG
		fprintf(stderr,"SIOCGIFNETMASK failed\n");
#endif
		close(fd);
		return -1;
	}  

	close(fd);

	(*nmask) = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
#ifdef DEBUG
	fprintf(stderr,"netmask %s\n", inet_ntoa(*nmask));
#endif
	return 0;
}

#else /* a dummy version */
 int get_netmask(struct in_addr *ipaddr, struct in_addr *nmask)
{
	return -1;
}
#endif


#ifdef AUTOCONF
/* this is the autoconf driver to test get_netmask() */

 main()
{
	char buf[1024];
	struct hostent *hp;
	struct in_addr ip, nmask;

	if (gethostname(buf, sizeof(buf)-1) != 0) {
		fprintf(stderr,"gethostname failed\n");
		exit(1);
	}

	hp = gethostbyname(buf);

	if (!hp) {
		fprintf(stderr,"gethostbyname failed\n");
		exit(1);
	}

	memcpy((char *)&ip, (char *)hp->h_addr, hp->h_length);

	if (get_netmask(&ip, &nmask) == 0) exit(0);

	fprintf(stderr,"get_netmask failed\n");
	exit(1);
}
#endif