1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate * All rights reserved.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <syslog.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
33*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
34*7c478bd9Sstevel@tonic-gate #include <net/if.h>
35*7c478bd9Sstevel@tonic-gate #include <unistd.h>
36*7c478bd9Sstevel@tonic-gate #include <netdb.h>
37*7c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
38*7c478bd9Sstevel@tonic-gate #include <slp-internal.h>
39*7c478bd9Sstevel@tonic-gate #include <slp_net_utils.h>
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate typedef struct slp_ifinfo {
42*7c478bd9Sstevel@tonic-gate struct sockaddr_in addr;
43*7c478bd9Sstevel@tonic-gate struct sockaddr_in netmask;
44*7c478bd9Sstevel@tonic-gate struct sockaddr_in bc_addr;
45*7c478bd9Sstevel@tonic-gate short flags;
46*7c478bd9Sstevel@tonic-gate } slp_ifinfo_t;
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate typedef struct slp_handle_ifinfo {
49*7c478bd9Sstevel@tonic-gate slp_ifinfo_t *all_ifs;
50*7c478bd9Sstevel@tonic-gate int numifs;
51*7c478bd9Sstevel@tonic-gate } slp_handle_ifinfo_t;
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate static SLPError get_all_interfaces(slp_handle_ifinfo_t *info);
55*7c478bd9Sstevel@tonic-gate
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate * Obtains the broadcast addresses for all local interfaces given in
58*7c478bd9Sstevel@tonic-gate * addrs.
59*7c478bd9Sstevel@tonic-gate *
60*7c478bd9Sstevel@tonic-gate * hp IN / OUT holds cached-per-handle if info
61*7c478bd9Sstevel@tonic-gate * given_ifs IN an array of local interfaces
62*7c478bd9Sstevel@tonic-gate * num_givenifs IN number of addresses in given_ifs
63*7c478bd9Sstevel@tonic-gate * bc_addrs OUT an array of broadcast addresses for local interfaces
64*7c478bd9Sstevel@tonic-gate * num_addrs OUT number of addrs returned in bc_addrs
65*7c478bd9Sstevel@tonic-gate *
66*7c478bd9Sstevel@tonic-gate * Returns SLP_OK if at least one broadcast address was found; if none
67*7c478bd9Sstevel@tonic-gate * were found, returns err != SLP_OK and *bc_addrs = NULL;
68*7c478bd9Sstevel@tonic-gate * Caller must free *bc_addrs when done.
69*7c478bd9Sstevel@tonic-gate */
slp_broadcast_addrs(slp_handle_impl_t * hp,struct in_addr * given_ifs,int num_givenifs,struct sockaddr_in * bc_addrs[],int * num_addrs)70*7c478bd9Sstevel@tonic-gate SLPError slp_broadcast_addrs(slp_handle_impl_t *hp, struct in_addr *given_ifs,
71*7c478bd9Sstevel@tonic-gate int num_givenifs,
72*7c478bd9Sstevel@tonic-gate struct sockaddr_in *bc_addrs[],
73*7c478bd9Sstevel@tonic-gate int *num_addrs) {
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate SLPError err;
76*7c478bd9Sstevel@tonic-gate int i, j;
77*7c478bd9Sstevel@tonic-gate slp_ifinfo_t *all_ifs;
78*7c478bd9Sstevel@tonic-gate slp_handle_ifinfo_t *ifinfo;
79*7c478bd9Sstevel@tonic-gate int numifs;
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate if (!hp->ifinfo) {
82*7c478bd9Sstevel@tonic-gate if (!(ifinfo = malloc(sizeof (*ifinfo)))) {
83*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_broadcast_addrs",
84*7c478bd9Sstevel@tonic-gate "out of memory");
85*7c478bd9Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate if ((err = get_all_interfaces(ifinfo)) != SLP_OK) {
88*7c478bd9Sstevel@tonic-gate free(ifinfo);
89*7c478bd9Sstevel@tonic-gate return (err);
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate hp->ifinfo = ifinfo;
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate all_ifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->all_ifs;
94*7c478bd9Sstevel@tonic-gate numifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->numifs;
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate /* allocate memory for reply */
97*7c478bd9Sstevel@tonic-gate if (!(*bc_addrs = calloc(num_givenifs, sizeof (**bc_addrs)))) {
98*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_broadcast_addrs", "out of memory");
99*7c478bd9Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate
102*7c478bd9Sstevel@tonic-gate /* copy bc addrs for all desired interfaces which are bc-enabled */
103*7c478bd9Sstevel@tonic-gate *num_addrs = 0;
104*7c478bd9Sstevel@tonic-gate for (j = 0; j < num_givenifs; j++) {
105*7c478bd9Sstevel@tonic-gate for (i = 0; i < numifs; i++) {
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate if (!(all_ifs[i].flags & IFF_BROADCAST)) {
108*7c478bd9Sstevel@tonic-gate continue;
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate
111*7c478bd9Sstevel@tonic-gate if (memcmp(&(all_ifs[i].addr.sin_addr.s_addr),
112*7c478bd9Sstevel@tonic-gate &(given_ifs[j].s_addr),
113*7c478bd9Sstevel@tonic-gate sizeof (given_ifs[j].s_addr)) == 0 &&
114*7c478bd9Sstevel@tonic-gate all_ifs[i].bc_addr.sin_addr.s_addr != 0) {
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate /* got it, so copy it to bc_addrs */
117*7c478bd9Sstevel@tonic-gate (void) memcpy(
118*7c478bd9Sstevel@tonic-gate *bc_addrs + *num_addrs,
119*7c478bd9Sstevel@tonic-gate &(all_ifs[i].bc_addr),
120*7c478bd9Sstevel@tonic-gate sizeof (all_ifs[i].bc_addr));
121*7c478bd9Sstevel@tonic-gate (*num_addrs)++;
122*7c478bd9Sstevel@tonic-gate
123*7c478bd9Sstevel@tonic-gate break;
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate if (*num_addrs == 0) {
129*7c478bd9Sstevel@tonic-gate /* none found */
130*7c478bd9Sstevel@tonic-gate free (*bc_addrs);
131*7c478bd9Sstevel@tonic-gate bc_addrs = NULL;
132*7c478bd9Sstevel@tonic-gate return (SLP_INTERNAL_SYSTEM_ERROR);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate return (SLP_OK);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate /*
138*7c478bd9Sstevel@tonic-gate * Returns true if addr is on a subnet local to the local host.
139*7c478bd9Sstevel@tonic-gate */
slp_on_subnet(slp_handle_impl_t * hp,struct in_addr addr)140*7c478bd9Sstevel@tonic-gate SLPBoolean slp_on_subnet(slp_handle_impl_t *hp, struct in_addr addr) {
141*7c478bd9Sstevel@tonic-gate int i;
142*7c478bd9Sstevel@tonic-gate struct in_addr netmask, net_addr, masked_addr;
143*7c478bd9Sstevel@tonic-gate slp_ifinfo_t *all_ifs;
144*7c478bd9Sstevel@tonic-gate slp_handle_ifinfo_t *ifinfo;
145*7c478bd9Sstevel@tonic-gate int numifs;
146*7c478bd9Sstevel@tonic-gate
147*7c478bd9Sstevel@tonic-gate if (!hp->ifinfo) {
148*7c478bd9Sstevel@tonic-gate if (!(ifinfo = malloc(sizeof (*ifinfo)))) {
149*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_broadcast_addrs",
150*7c478bd9Sstevel@tonic-gate "out of memory");
151*7c478bd9Sstevel@tonic-gate return (SLP_FALSE);
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate if (get_all_interfaces(ifinfo) != SLP_OK) {
154*7c478bd9Sstevel@tonic-gate free(ifinfo);
155*7c478bd9Sstevel@tonic-gate return (SLP_FALSE);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate hp->ifinfo = ifinfo;
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate all_ifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->all_ifs;
160*7c478bd9Sstevel@tonic-gate numifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->numifs;
161*7c478bd9Sstevel@tonic-gate
162*7c478bd9Sstevel@tonic-gate for (i = 0; i < numifs; i++) {
163*7c478bd9Sstevel@tonic-gate /* get netmask */
164*7c478bd9Sstevel@tonic-gate netmask.s_addr = all_ifs[i].netmask.sin_addr.s_addr;
165*7c478bd9Sstevel@tonic-gate /* get network address */
166*7c478bd9Sstevel@tonic-gate net_addr.s_addr =
167*7c478bd9Sstevel@tonic-gate all_ifs[i].addr.sin_addr.s_addr & netmask.s_addr;
168*7c478bd9Sstevel@tonic-gate /* apply netmask to input addr */
169*7c478bd9Sstevel@tonic-gate masked_addr.s_addr = addr.s_addr & netmask.s_addr;
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate if (memcmp(&(masked_addr.s_addr), &(net_addr.s_addr),
172*7c478bd9Sstevel@tonic-gate sizeof (net_addr.s_addr)) == 0) {
173*7c478bd9Sstevel@tonic-gate return (SLP_TRUE);
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate return (SLP_FALSE);
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate
180*7c478bd9Sstevel@tonic-gate /*
181*7c478bd9Sstevel@tonic-gate * Returns true if any local interface if configured with addr.
182*7c478bd9Sstevel@tonic-gate */
slp_on_localhost(slp_handle_impl_t * hp,struct in_addr addr)183*7c478bd9Sstevel@tonic-gate SLPBoolean slp_on_localhost(slp_handle_impl_t *hp, struct in_addr addr) {
184*7c478bd9Sstevel@tonic-gate int i;
185*7c478bd9Sstevel@tonic-gate slp_ifinfo_t *all_ifs;
186*7c478bd9Sstevel@tonic-gate slp_handle_ifinfo_t *ifinfo;
187*7c478bd9Sstevel@tonic-gate int numifs;
188*7c478bd9Sstevel@tonic-gate
189*7c478bd9Sstevel@tonic-gate if (!hp->ifinfo) {
190*7c478bd9Sstevel@tonic-gate if (!(ifinfo = malloc(sizeof (*ifinfo)))) {
191*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_broadcast_addrs",
192*7c478bd9Sstevel@tonic-gate "out of memory");
193*7c478bd9Sstevel@tonic-gate return (SLP_FALSE);
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate if (get_all_interfaces(ifinfo) != SLP_OK) {
196*7c478bd9Sstevel@tonic-gate free(ifinfo);
197*7c478bd9Sstevel@tonic-gate return (SLP_FALSE);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate hp->ifinfo = ifinfo;
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate all_ifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->all_ifs;
202*7c478bd9Sstevel@tonic-gate numifs = ((slp_handle_ifinfo_t *)hp->ifinfo)->numifs;
203*7c478bd9Sstevel@tonic-gate
204*7c478bd9Sstevel@tonic-gate for (i = 0; i < numifs; i++) {
205*7c478bd9Sstevel@tonic-gate if (memcmp(&(addr.s_addr), &(all_ifs[i].addr.sin_addr.s_addr),
206*7c478bd9Sstevel@tonic-gate sizeof (addr)) == 0) {
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate return (SLP_TRUE);
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate }
211*7c478bd9Sstevel@tonic-gate
212*7c478bd9Sstevel@tonic-gate return (SLP_FALSE);
213*7c478bd9Sstevel@tonic-gate }
214*7c478bd9Sstevel@tonic-gate
slp_free_ifinfo(void * hi)215*7c478bd9Sstevel@tonic-gate void slp_free_ifinfo(void *hi) {
216*7c478bd9Sstevel@tonic-gate free(((slp_handle_ifinfo_t *)hi)->all_ifs);
217*7c478bd9Sstevel@tonic-gate }
218*7c478bd9Sstevel@tonic-gate
219*7c478bd9Sstevel@tonic-gate /*
220*7c478bd9Sstevel@tonic-gate * Populates all_ifs.
221*7c478bd9Sstevel@tonic-gate */
get_all_interfaces(slp_handle_ifinfo_t * info)222*7c478bd9Sstevel@tonic-gate static SLPError get_all_interfaces(slp_handle_ifinfo_t *info) {
223*7c478bd9Sstevel@tonic-gate int i, n, s = 0;
224*7c478bd9Sstevel@tonic-gate int numifs;
225*7c478bd9Sstevel@tonic-gate char *buf = NULL;
226*7c478bd9Sstevel@tonic-gate size_t bufsize;
227*7c478bd9Sstevel@tonic-gate struct ifconf ifc;
228*7c478bd9Sstevel@tonic-gate struct ifreq *ifrp, ifr;
229*7c478bd9Sstevel@tonic-gate slp_ifinfo_t *all_ifs = NULL;
230*7c478bd9Sstevel@tonic-gate SLPError err = SLP_OK;
231*7c478bd9Sstevel@tonic-gate
232*7c478bd9Sstevel@tonic-gate /* create a socket with which to get interface info */
233*7c478bd9Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
234*7c478bd9Sstevel@tonic-gate goto cleanup;
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate
237*7c478bd9Sstevel@tonic-gate /* how many interfaces are configured? */
238*7c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
239*7c478bd9Sstevel@tonic-gate goto cleanup;
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate
242*7c478bd9Sstevel@tonic-gate /* allocate memory for ifinfo_t array */
243*7c478bd9Sstevel@tonic-gate if (!(all_ifs = calloc(numifs, sizeof (*all_ifs)))) {
244*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "get_all_interfaces", "out of memory");
245*7c478bd9Sstevel@tonic-gate err = SLP_MEMORY_ALLOC_FAILED;
246*7c478bd9Sstevel@tonic-gate goto cleanup;
247*7c478bd9Sstevel@tonic-gate }
248*7c478bd9Sstevel@tonic-gate
249*7c478bd9Sstevel@tonic-gate /* allocate memory for interface info */
250*7c478bd9Sstevel@tonic-gate bufsize = numifs * sizeof (struct ifreq);
251*7c478bd9Sstevel@tonic-gate if (!(buf = malloc(bufsize))) {
252*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "get_all_interfaces", "out of memory");
253*7c478bd9Sstevel@tonic-gate err = SLP_MEMORY_ALLOC_FAILED;
254*7c478bd9Sstevel@tonic-gate goto cleanup;
255*7c478bd9Sstevel@tonic-gate }
256*7c478bd9Sstevel@tonic-gate
257*7c478bd9Sstevel@tonic-gate /* get if info */
258*7c478bd9Sstevel@tonic-gate ifc.ifc_len = bufsize;
259*7c478bd9Sstevel@tonic-gate ifc.ifc_buf = buf;
260*7c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
261*7c478bd9Sstevel@tonic-gate goto cleanup;
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate
264*7c478bd9Sstevel@tonic-gate ifrp = ifc.ifc_req;
265*7c478bd9Sstevel@tonic-gate i = 0;
266*7c478bd9Sstevel@tonic-gate for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifrp++) {
267*7c478bd9Sstevel@tonic-gate
268*7c478bd9Sstevel@tonic-gate /* ignore if interface is not up */
269*7c478bd9Sstevel@tonic-gate (void) memset((char *)&ifr, 0, sizeof (ifr));
270*7c478bd9Sstevel@tonic-gate (void) strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof (ifr.ifr_name));
271*7c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
272*7c478bd9Sstevel@tonic-gate continue;
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate if (!(ifr.ifr_flags & IFF_UP)) {
275*7c478bd9Sstevel@tonic-gate continue;
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate
278*7c478bd9Sstevel@tonic-gate all_ifs[i].flags = ifr.ifr_flags;
279*7c478bd9Sstevel@tonic-gate
280*7c478bd9Sstevel@tonic-gate /* get the interface's address */
281*7c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
282*7c478bd9Sstevel@tonic-gate continue;
283*7c478bd9Sstevel@tonic-gate }
284*7c478bd9Sstevel@tonic-gate
285*7c478bd9Sstevel@tonic-gate (void) memcpy(&(all_ifs[i].addr), &ifr.ifr_addr,
286*7c478bd9Sstevel@tonic-gate sizeof (all_ifs[i].addr));
287*7c478bd9Sstevel@tonic-gate
288*7c478bd9Sstevel@tonic-gate /* get the interface's broadcast address */
289*7c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
290*7c478bd9Sstevel@tonic-gate (void) memset(&(all_ifs[i].bc_addr), 0,
291*7c478bd9Sstevel@tonic-gate sizeof (all_ifs[i].bc_addr));
292*7c478bd9Sstevel@tonic-gate } else {
293*7c478bd9Sstevel@tonic-gate (void) memcpy(&(all_ifs[i].bc_addr), &ifr.ifr_addr,
294*7c478bd9Sstevel@tonic-gate sizeof (all_ifs[i].bc_addr));
295*7c478bd9Sstevel@tonic-gate }
296*7c478bd9Sstevel@tonic-gate
297*7c478bd9Sstevel@tonic-gate /* get the interface's subnet mask */
298*7c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
299*7c478bd9Sstevel@tonic-gate (void) memset(&(all_ifs[i].netmask), 0,
300*7c478bd9Sstevel@tonic-gate sizeof (all_ifs[i].netmask));
301*7c478bd9Sstevel@tonic-gate } else {
302*7c478bd9Sstevel@tonic-gate (void) memcpy(&(all_ifs[i].netmask), &ifr.ifr_addr,
303*7c478bd9Sstevel@tonic-gate sizeof (all_ifs[i].netmask));
304*7c478bd9Sstevel@tonic-gate }
305*7c478bd9Sstevel@tonic-gate
306*7c478bd9Sstevel@tonic-gate i++;
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate
309*7c478bd9Sstevel@tonic-gate /* i contains the number we actually got info on */
310*7c478bd9Sstevel@tonic-gate info->numifs = i;
311*7c478bd9Sstevel@tonic-gate info->all_ifs = all_ifs;
312*7c478bd9Sstevel@tonic-gate
313*7c478bd9Sstevel@tonic-gate if (i == 0) {
314*7c478bd9Sstevel@tonic-gate err = SLP_INTERNAL_SYSTEM_ERROR;
315*7c478bd9Sstevel@tonic-gate free(all_ifs);
316*7c478bd9Sstevel@tonic-gate info->all_ifs = NULL;
317*7c478bd9Sstevel@tonic-gate }
318*7c478bd9Sstevel@tonic-gate
319*7c478bd9Sstevel@tonic-gate cleanup:
320*7c478bd9Sstevel@tonic-gate if (s) (void) close(s);
321*7c478bd9Sstevel@tonic-gate if (buf) free(buf);
322*7c478bd9Sstevel@tonic-gate
323*7c478bd9Sstevel@tonic-gate return (err);
324*7c478bd9Sstevel@tonic-gate }
325*7c478bd9Sstevel@tonic-gate
326*7c478bd9Sstevel@tonic-gate /*
327*7c478bd9Sstevel@tonic-gate * Converts a SLPSrvURL to a network address. 'sa' must have been
328*7c478bd9Sstevel@tonic-gate * allocated by the caller.
329*7c478bd9Sstevel@tonic-gate * Assumes that addresses are given as specified in the protocol spec,
330*7c478bd9Sstevel@tonic-gate * i.e. as IP addresses and not host names.
331*7c478bd9Sstevel@tonic-gate */
slp_surl2sin(SLPSrvURL * surl,struct sockaddr_in * sa)332*7c478bd9Sstevel@tonic-gate SLPError slp_surl2sin(SLPSrvURL *surl, struct sockaddr_in *sa) {
333*7c478bd9Sstevel@tonic-gate struct sockaddr_in *sin = (struct sockaddr_in *)sa;
334*7c478bd9Sstevel@tonic-gate
335*7c478bd9Sstevel@tonic-gate if (slp_pton(surl->s_pcHost, &(sin->sin_addr)) < 1)
336*7c478bd9Sstevel@tonic-gate return (SLP_PARAMETER_BAD);
337*7c478bd9Sstevel@tonic-gate sin->sin_family = AF_INET;
338*7c478bd9Sstevel@tonic-gate /* port number */
339*7c478bd9Sstevel@tonic-gate sin->sin_port = htons(
340*7c478bd9Sstevel@tonic-gate (surl->s_iPort == 0 ? SLP_PORT : surl->s_iPort));
341*7c478bd9Sstevel@tonic-gate
342*7c478bd9Sstevel@tonic-gate return (SLP_OK);
343*7c478bd9Sstevel@tonic-gate }
344*7c478bd9Sstevel@tonic-gate
345*7c478bd9Sstevel@tonic-gate /*
346*7c478bd9Sstevel@tonic-gate * A wrapper around gethostbyaddr_r. This checks the useGetXXXbyYYY
347*7c478bd9Sstevel@tonic-gate * property first to determine whether a name service lookup should
348*7c478bd9Sstevel@tonic-gate * be used. If not, it converts the address in 'addr' to a string
349*7c478bd9Sstevel@tonic-gate * and just returns that.
350*7c478bd9Sstevel@tonic-gate *
351*7c478bd9Sstevel@tonic-gate * The core functionality herein will be replaced with getaddrinfo
352*7c478bd9Sstevel@tonic-gate * when it becomes available.
353*7c478bd9Sstevel@tonic-gate */
354*7c478bd9Sstevel@tonic-gate
slp_gethostbyaddr(const char * addr,int size)355*7c478bd9Sstevel@tonic-gate char *slp_gethostbyaddr(const char *addr, int size) {
356*7c478bd9Sstevel@tonic-gate char storebuf[SLP_NETDB_BUFSZ], addrbuf[INET6_ADDRSTRLEN], *cname;
357*7c478bd9Sstevel@tonic-gate const char *use_xbyy;
358*7c478bd9Sstevel@tonic-gate struct hostent namestruct[1], *name;
359*7c478bd9Sstevel@tonic-gate int herrno;
360*7c478bd9Sstevel@tonic-gate
361*7c478bd9Sstevel@tonic-gate /* default: copy in the IP address */
362*7c478bd9Sstevel@tonic-gate cname = slp_ntop(addrbuf, INET6_ADDRSTRLEN, (const void *) addr);
363*7c478bd9Sstevel@tonic-gate if (cname && !(cname = strdup(cname))) {
364*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_gethostbyaddr", "out of memory");
365*7c478bd9Sstevel@tonic-gate return (NULL);
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate
368*7c478bd9Sstevel@tonic-gate if ((use_xbyy = SLPGetProperty(SLP_CONFIG_USEGETXXXBYYYY)) != NULL &&
369*7c478bd9Sstevel@tonic-gate strcasecmp(use_xbyy, "false") == 0) {
370*7c478bd9Sstevel@tonic-gate return (cname);
371*7c478bd9Sstevel@tonic-gate }
372*7c478bd9Sstevel@tonic-gate
373*7c478bd9Sstevel@tonic-gate while (!(name = gethostbyaddr_r(addr, size,
374*7c478bd9Sstevel@tonic-gate AF_INET,
375*7c478bd9Sstevel@tonic-gate namestruct,
376*7c478bd9Sstevel@tonic-gate storebuf,
377*7c478bd9Sstevel@tonic-gate SLP_NETDB_BUFSZ,
378*7c478bd9Sstevel@tonic-gate &herrno))) {
379*7c478bd9Sstevel@tonic-gate switch (herrno) {
380*7c478bd9Sstevel@tonic-gate case NO_RECOVERY:
381*7c478bd9Sstevel@tonic-gate case NO_DATA:
382*7c478bd9Sstevel@tonic-gate return (cname);
383*7c478bd9Sstevel@tonic-gate case TRY_AGAIN:
384*7c478bd9Sstevel@tonic-gate continue;
385*7c478bd9Sstevel@tonic-gate default:
386*7c478bd9Sstevel@tonic-gate return (cname); /* IP address */
387*7c478bd9Sstevel@tonic-gate }
388*7c478bd9Sstevel@tonic-gate }
389*7c478bd9Sstevel@tonic-gate
390*7c478bd9Sstevel@tonic-gate free(cname);
391*7c478bd9Sstevel@tonic-gate if (!(cname = strdup(name->h_name))) {
392*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_gethostbyaddr", "out of memory");
393*7c478bd9Sstevel@tonic-gate return (NULL);
394*7c478bd9Sstevel@tonic-gate }
395*7c478bd9Sstevel@tonic-gate
396*7c478bd9Sstevel@tonic-gate return (cname);
397*7c478bd9Sstevel@tonic-gate }
398*7c478bd9Sstevel@tonic-gate
399*7c478bd9Sstevel@tonic-gate /* @@@ currently getting these from libresolv2 -> change? */
400*7c478bd9Sstevel@tonic-gate
401*7c478bd9Sstevel@tonic-gate /*
402*7c478bd9Sstevel@tonic-gate * Converts the address pointed to by 'addr' to a string. Currently
403*7c478bd9Sstevel@tonic-gate * just calls inet_ntoa, but is structured to be a wrapper to
404*7c478bd9Sstevel@tonic-gate * inet_ntop. Returns NULL on failure.
405*7c478bd9Sstevel@tonic-gate *
406*7c478bd9Sstevel@tonic-gate * This wrapper allows callers to be protocol agnostic. Right now it
407*7c478bd9Sstevel@tonic-gate * only handles IPv4.
408*7c478bd9Sstevel@tonic-gate */
409*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
slp_ntop(char * buf,int buflen,const void * addr)410*7c478bd9Sstevel@tonic-gate char *slp_ntop(char *buf, int buflen, const void *addr) {
411*7c478bd9Sstevel@tonic-gate return (inet_ntoa(*(struct in_addr *)addr));
412*7c478bd9Sstevel@tonic-gate }
413*7c478bd9Sstevel@tonic-gate
414*7c478bd9Sstevel@tonic-gate /*
415*7c478bd9Sstevel@tonic-gate * convert from presentation format (which usually means ASCII printable)
416*7c478bd9Sstevel@tonic-gate * to network format (which is usually some kind of binary format).
417*7c478bd9Sstevel@tonic-gate * return:
418*7c478bd9Sstevel@tonic-gate * 1 if the address was valid for the specified address family
419*7c478bd9Sstevel@tonic-gate * 0 if the address wasn't valid (`dst' is untouched in this case)
420*7c478bd9Sstevel@tonic-gate * -1 if some other error occurred (`dst' is untouched in this case, too)
421*7c478bd9Sstevel@tonic-gate *
422*7c478bd9Sstevel@tonic-gate * This wrapper allows callers to be protocol agnostic. Right now it
423*7c478bd9Sstevel@tonic-gate * only handles IPv4.
424*7c478bd9Sstevel@tonic-gate */
slp_pton(const char * addrstr,void * addr)425*7c478bd9Sstevel@tonic-gate int slp_pton(const char *addrstr, void *addr) {
426*7c478bd9Sstevel@tonic-gate return (inet_pton(AF_INET, addrstr, addr));
427*7c478bd9Sstevel@tonic-gate }
428