summaryrefslogtreecommitdiff
path: root/usr/src/lib/libslp/clib/slp_net_utils.c
blob: 7f7ee732282ba9bf0abe89ece3c56065b6fa8b7b (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1999 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <unistd.h>
#include <netdb.h>
#include <nss_dbdefs.h>
#include <slp-internal.h>
#include <slp_net_utils.h>

typedef struct slp_ifinfo {
	struct sockaddr_in addr;
	struct sockaddr_in netmask;
	struct sockaddr_in bc_addr;
	short flags;
} slp_ifinfo_t;

typedef struct slp_handle_ifinfo {
	slp_ifinfo_t *all_ifs;
	int numifs;
} slp_handle_ifinfo_t;


static SLPError get_all_interfaces(slp_handle_ifinfo_t *info);

/*
 * Obtains the broadcast addresses for all local interfaces given in
 * addrs.
 *
 * hp		IN / OUT holds cached-per-handle if info
 * given_ifs	IN	an array of local interfaces
 * num_givenifs	IN	number of addresses in given_ifs
 * bc_addrs	OUT	an array of broadcast addresses for local interfaces
 * num_addrs	OUT	number of addrs returned in bc_addrs
 *
 * Returns SLP_OK if at least one broadcast address was found; if none
 * were found, returns err != SLP_OK and *bc_addrs = NULL;
 * Caller must free *bc_addrs when done.
 */
SLPError slp_broadcast_addrs(slp_handle_impl_t *hp, struct in_addr *given_ifs,
				int num_givenifs,
				struct sockaddr_in *bc_addrs[],
				int *num_addrs) {

	SLPError err;
	int i, j;
	slp_ifinfo_t *all_ifs;
	slp_handle_ifinfo_t *ifinfo;
	int numifs;

	if (!hp->ifinfo) {
		if (!(ifinfo = malloc(sizeof (*ifinfo)))) {
			slp_err(LOG_CRIT, 0, "slp_broadcast_addrs",
				"out of memory");
			return (SLP_MEMORY_ALLOC_FAILED);
		}
		if ((err = get_all_interfaces(ifinfo)) != SLP_OK) {
			free(ifinfo);
			return (err);
		}
		hp->ifinfo = ifinfo;
	}
	all_ifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->all_ifs;
	numifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->numifs;

	/* allocate memory for reply */
	if (!(*bc_addrs = calloc(num_givenifs, sizeof (**bc_addrs)))) {
		slp_err(LOG_CRIT, 0, "slp_broadcast_addrs", "out of memory");
		return (SLP_MEMORY_ALLOC_FAILED);
	}

	/* copy bc addrs for all desired interfaces which are bc-enabled */
	*num_addrs = 0;
	for (j = 0; j < num_givenifs; j++) {
	    for (i = 0; i < numifs; i++) {

		if (!(all_ifs[i].flags & IFF_BROADCAST)) {
			continue;
		}

		if (memcmp(&(all_ifs[i].addr.sin_addr.s_addr),
			    &(given_ifs[j].s_addr),
			    sizeof (given_ifs[j].s_addr)) == 0 &&
		    all_ifs[i].bc_addr.sin_addr.s_addr != 0) {

		    /* got it, so copy it to bc_addrs */
		    (void) memcpy(
				    *bc_addrs + *num_addrs,
				    &(all_ifs[i].bc_addr),
				    sizeof (all_ifs[i].bc_addr));
		    (*num_addrs)++;

		    break;
		}
	    }
	}

	if (*num_addrs == 0) {
		/* none found */
		free (*bc_addrs);
		bc_addrs = NULL;
		return (SLP_INTERNAL_SYSTEM_ERROR);
	}
	return (SLP_OK);
}

/*
 * Returns true if addr is on a subnet local to the local host.
 */
SLPBoolean slp_on_subnet(slp_handle_impl_t *hp, struct in_addr addr) {
	int i;
	struct in_addr netmask, net_addr, masked_addr;
	slp_ifinfo_t *all_ifs;
	slp_handle_ifinfo_t *ifinfo;
	int numifs;

	if (!hp->ifinfo) {
		if (!(ifinfo = malloc(sizeof (*ifinfo)))) {
			slp_err(LOG_CRIT, 0, "slp_broadcast_addrs",
				"out of memory");
			return (SLP_FALSE);
		}
		if (get_all_interfaces(ifinfo) != SLP_OK) {
			free(ifinfo);
			return (SLP_FALSE);
		}
		hp->ifinfo = ifinfo;
	}
	all_ifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->all_ifs;
	numifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->numifs;

	for (i = 0; i < numifs; i++) {
		/* get netmask */
		netmask.s_addr = all_ifs[i].netmask.sin_addr.s_addr;
		/* get network address */
		net_addr.s_addr =
			all_ifs[i].addr.sin_addr.s_addr & netmask.s_addr;
		/* apply netmask to input addr */
		masked_addr.s_addr = addr.s_addr & netmask.s_addr;

		if (memcmp(&(masked_addr.s_addr), &(net_addr.s_addr),
				sizeof (net_addr.s_addr)) == 0) {
			return (SLP_TRUE);
		}
	}

	return (SLP_FALSE);
}

/*
 * Returns true if any local interface if configured with addr.
 */
SLPBoolean slp_on_localhost(slp_handle_impl_t *hp, struct in_addr addr) {
	int i;
	slp_ifinfo_t *all_ifs;
	slp_handle_ifinfo_t *ifinfo;
	int numifs;

	if (!hp->ifinfo) {
		if (!(ifinfo = malloc(sizeof (*ifinfo)))) {
			slp_err(LOG_CRIT, 0, "slp_broadcast_addrs",
				"out of memory");
			return (SLP_FALSE);
		}
		if (get_all_interfaces(ifinfo) != SLP_OK) {
			free(ifinfo);
			return (SLP_FALSE);
		}
		hp->ifinfo = ifinfo;
	}
	all_ifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->all_ifs;
	numifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->numifs;

	for (i = 0; i < numifs; i++) {
		if (memcmp(&(addr.s_addr), &(all_ifs[i].addr.sin_addr.s_addr),
				sizeof (addr)) == 0) {

			return (SLP_TRUE);
		}
	}

	return (SLP_FALSE);
}

void slp_free_ifinfo(void *hi) {
	free(((slp_handle_ifinfo_t *)hi)->all_ifs);
}

/*
 * Populates all_ifs.
 */
static SLPError get_all_interfaces(slp_handle_ifinfo_t *info) {
	int i, n, s = 0;
	int numifs;
	char *buf = NULL;
	size_t bufsize;
	struct ifconf ifc;
	struct ifreq *ifrp, ifr;
	slp_ifinfo_t *all_ifs = NULL;
	SLPError err = SLP_OK;

	/* create a socket with which to get interface info */
	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		goto cleanup;
	}

	/* how many interfaces are configured? */
	if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
		goto cleanup;
	}

	/* allocate memory for ifinfo_t array */
	if (!(all_ifs = calloc(numifs, sizeof (*all_ifs)))) {
		slp_err(LOG_CRIT, 0, "get_all_interfaces", "out of memory");
		err = SLP_MEMORY_ALLOC_FAILED;
		goto cleanup;
	}

	/* allocate memory for interface info */
	bufsize = numifs * sizeof (struct ifreq);
	if (!(buf = malloc(bufsize))) {
		slp_err(LOG_CRIT, 0, "get_all_interfaces", "out of memory");
		err = SLP_MEMORY_ALLOC_FAILED;
		goto cleanup;
	}

	/* get if info */
	ifc.ifc_len = bufsize;
	ifc.ifc_buf = buf;
	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
		goto cleanup;
	}

	ifrp = ifc.ifc_req;
	i = 0;
	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifrp++) {

	    /* ignore if interface is not up */
	    (void) memset((char *)&ifr, 0, sizeof (ifr));
	    (void) strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof (ifr.ifr_name));
	    if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
		continue;
	    }
	    if (!(ifr.ifr_flags & IFF_UP)) {
		continue;
	    }

	    all_ifs[i].flags = ifr.ifr_flags;

	    /* get the interface's address */
	    if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
		continue;
	    }

	    (void) memcpy(&(all_ifs[i].addr), &ifr.ifr_addr,
				sizeof (all_ifs[i].addr));

	    /* get the interface's broadcast address */
	    if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
		(void) memset(&(all_ifs[i].bc_addr), 0,
				sizeof (all_ifs[i].bc_addr));
	    } else {
		(void) memcpy(&(all_ifs[i].bc_addr), &ifr.ifr_addr,
				sizeof (all_ifs[i].bc_addr));
	    }

	    /* get the interface's subnet mask */
	    if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
		(void) memset(&(all_ifs[i].netmask), 0,
				sizeof (all_ifs[i].netmask));
	    } else {
		(void) memcpy(&(all_ifs[i].netmask), &ifr.ifr_addr,
				sizeof (all_ifs[i].netmask));
	    }

	    i++;
	}

	/* i contains the number we actually got info on */
	info->numifs = i;
	info->all_ifs = all_ifs;

	if (i == 0) {
		err = SLP_INTERNAL_SYSTEM_ERROR;
		free(all_ifs);
		info->all_ifs = NULL;
	}

cleanup:
	if (s) (void) close(s);
	if (buf) free(buf);

	return (err);
}

/*
 * Converts a SLPSrvURL to a network address. 'sa' must have been
 * allocated by the caller.
 * Assumes that addresses are given as specified in the protocol spec,
 * i.e. as IP addresses and not host names.
 */
SLPError slp_surl2sin(SLPSrvURL *surl, struct sockaddr_in *sa) {
	struct sockaddr_in *sin = (struct sockaddr_in *)sa;

	if (slp_pton(surl->s_pcHost, &(sin->sin_addr)) < 1)
		return (SLP_PARAMETER_BAD);
	sin->sin_family = AF_INET;
	/* port number */
	sin->sin_port = htons(
		(surl->s_iPort == 0 ? SLP_PORT : surl->s_iPort));

	return (SLP_OK);
}

/*
 * A wrapper around gethostbyaddr_r. This checks the useGetXXXbyYYY
 * property first to determine whether a name service lookup should
 * be used. If not, it converts the address in 'addr' to a string
 * and just returns that.
 *
 * The core functionality herein will be replaced with getaddrinfo
 * when it becomes available.
 */

char *slp_gethostbyaddr(const char *addr, int size) {
	char storebuf[SLP_NETDB_BUFSZ], addrbuf[INET6_ADDRSTRLEN], *cname;
	const char *use_xbyy;
	struct hostent namestruct[1], *name;
	int herrno;

	/* default: copy in the IP address */
	cname = slp_ntop(addrbuf, INET6_ADDRSTRLEN, (const void *) addr);
	if (cname && !(cname = strdup(cname))) {
		slp_err(LOG_CRIT, 0, "slp_gethostbyaddr", "out of memory");
		return (NULL);
	}

	if ((use_xbyy = SLPGetProperty(SLP_CONFIG_USEGETXXXBYYYY)) != NULL &&
	    strcasecmp(use_xbyy, "false") == 0) {
		return (cname);
	}

	while (!(name = gethostbyaddr_r(addr, size,
					AF_INET,
					namestruct,
					storebuf,
					SLP_NETDB_BUFSZ,
					&herrno))) {
		switch (herrno) {
		case NO_RECOVERY:
		case NO_DATA:
			return (cname);
		case TRY_AGAIN:
			continue;
		default:
			return (cname);	/* IP address */
		}
	}

	free(cname);
	if (!(cname = strdup(name->h_name))) {
		slp_err(LOG_CRIT, 0, "slp_gethostbyaddr", "out of memory");
		return (NULL);
	}

	return (cname);
}

/* @@@ currently getting these from libresolv2 -> change? */

/*
 * Converts the address pointed to by 'addr' to a string. Currently
 * just calls inet_ntoa, but is structured to be a wrapper to
 * inet_ntop. Returns NULL on failure.
 *
 * This wrapper allows callers to be protocol agnostic. Right now it
 * only handles IPv4.
 */
/*ARGSUSED*/
char *slp_ntop(char *buf, int buflen, const void *addr) {
	return (inet_ntoa(*(struct in_addr *)addr));
}

/*
 * convert from presentation format (which usually means ASCII printable)
 * to network format (which is usually some kind of binary format).
 * return:
 *	1 if the address was valid for the specified address family
 *	0 if the address wasn't valid (`dst' is untouched in this case)
 *	-1 if some other error occurred (`dst' is untouched in this case, too)
 *
 * This wrapper allows callers to be protocol agnostic. Right now it
 * only handles IPv4.
 */
int slp_pton(const char *addrstr, void *addr) {
	return (inet_pton(AF_INET, addrstr, addr));
}