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