wire.c (82794) | wire.c (119679) |
---|---|
1/* | 1/* |
2 * Copyright (c) 1997-2001 Erez Zadok | 2 * Copyright (c) 1997-2003 Erez Zadok |
3 * Copyright (c) 1990 Jan-Simon Pendry 4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 5 * Copyright (c) 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Jan-Simon Pendry at Imperial College, London. 10 * --- 22 unchanged lines hidden (view full) --- 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * %W% (Berkeley) %G% 40 * | 3 * Copyright (c) 1990 Jan-Simon Pendry 4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 5 * Copyright (c) 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Jan-Simon Pendry at Imperial College, London. 10 * --- 22 unchanged lines hidden (view full) --- 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * %W% (Berkeley) %G% 40 * |
41 * $Id: wire.c,v 1.8.2.5 2001/01/10 03:23:41 ezk Exp $ | 41 * $Id: wire.c,v 1.8.2.9 2002/12/27 22:45:13 ezk Exp $ |
42 * 43 */ 44 45/* 46 * This function returns the subnet (address&netmask) for the primary network 47 * interface. If the resulting address has an entry in the hosts file, the 48 * corresponding name is returned, otherwise the address is returned in 49 * standard internet format. --- 249 unchanged lines hidden (view full) --- 299 * Determine whether a network name is one of the local networks 300 * of a host. 301 */ 302int 303is_network_member(const char *net) 304{ 305 addrlist *al; 306 | 42 * 43 */ 44 45/* 46 * This function returns the subnet (address&netmask) for the primary network 47 * interface. If the resulting address has an entry in the hosts file, the 48 * corresponding name is returned, otherwise the address is returned in 49 * standard internet format. --- 249 unchanged lines hidden (view full) --- 299 * Determine whether a network name is one of the local networks 300 * of a host. 301 */ 302int 303is_network_member(const char *net) 304{ 305 addrlist *al; 306 |
307 for (al = localnets; al; al = al->ip_next) 308 if (STREQ(net, al->ip_net_name) || STREQ(net, al->ip_net_num)) 309 return TRUE; | 307 /* 308 * If the network name string does not contain a '/', use old behavior. 309 * If it does contain a '/' then interpret the string as a network/netmask 310 * pair. If "netmask" doesn't exist, use the interface's own netmask. 311 * Also support fully explicit netmasks such as 255.255.255.0 as well as 312 * bit-length netmask such as /24 (hex formats such 0xffffff00 work too). 313 */ 314 if (strchr(net, '/') == NULL) { 315 for (al = localnets; al; al = al->ip_next) 316 if (STREQ(net, al->ip_net_name) || STREQ(net, al->ip_net_num)) 317 return TRUE; 318 } else { 319 char *netstr = strdup(net), *maskstr; 320 u_long netnum, masknum = 0; 321 maskstr = strchr(netstr, '/'); 322 maskstr++; 323 maskstr[-1] = '\0'; /* null terminate netstr */ 324 if (*maskstr == '\0') /* if empty string, make it NULL */ 325 maskstr = NULL; 326 /* check if netmask uses a dotted-quad or bit-length, or not defined at all */ 327 if (maskstr) { 328 if (strchr(maskstr, '.')) { 329 masknum = inet_addr(maskstr); 330 if (masknum < 0) /* can be invalid (-1) or all-1s */ 331 masknum = 0xffffffff; 332 } else if (NSTRCEQ(maskstr, "0x", 2)) { 333 masknum = strtoul(maskstr, NULL, 16); 334 } else { 335 int bits = atoi(maskstr); 336 if (bits < 0) 337 bits = 0; 338 if (bits > 32) 339 bits = 32; 340 masknum = 0xffffffff << (32-bits); 341 } 342 } 343 netnum = inet_addr(netstr); /* not checking return value, b/c -1 (0xffffffff) is valid */ 344 XFREE(netstr); /* netstr not needed any longer */ |
310 | 345 |
346 /* now check against each local interface */ 347 for (al = localnets; al; al = al->ip_next) { 348 if ((al->ip_addr & (maskstr ? masknum : al->ip_mask)) == netnum) 349 return TRUE; 350 } 351 } 352 |
|
311 return FALSE; 312} 313 314 315#ifdef HAVE_GETIFADDRS 316void 317getwire(char **name1, char **number1) 318{ 319 addrlist *al = NULL, *tail = NULL; 320 struct ifaddrs *ifaddrs, *ifap; | 353 return FALSE; 354} 355 356 357#ifdef HAVE_GETIFADDRS 358void 359getwire(char **name1, char **number1) 360{ 361 addrlist *al = NULL, *tail = NULL; 362 struct ifaddrs *ifaddrs, *ifap; |
321#ifndef HAVE_FIELD_STRUCT_IFADDRS_IFA_NEXT | 363#ifndef HAVE_STRUCT_IFADDRS_IFA_NEXT |
322 int count = 0, i; | 364 int count = 0, i; |
323#endif /* not HAVE_FIELD_STRUCT_IFADDRS_IFA_NEXT */ | 365#endif /* not HAVE_STRUCT_IFADDRS_IFA_NEXT */ |
324 325 ifaddrs = NULL; | 366 367 ifaddrs = NULL; |
326#ifdef HAVE_FIELD_STRUCT_IFADDRS_IFA_NEXT | 368#ifdef HAVE_STRUCT_IFADDRS_IFA_NEXT |
327 if (getifaddrs(&ifaddrs) < 0) 328 goto out; 329 330 for (ifap = ifaddrs; ifap != NULL; ifap = ifap->ifa_next) { | 369 if (getifaddrs(&ifaddrs) < 0) 370 goto out; 371 372 for (ifap = ifaddrs; ifap != NULL; ifap = ifap->ifa_next) { |
331#else /* not HAVE_FIELD_STRUCT_IFADDRS_IFA_NEXT */ | 373#else /* not HAVE_STRUCT_IFADDRS_IFA_NEXT */ |
332 if (getifaddrs(&ifaddrs, &count) < 0) 333 goto out; 334 335 for (i = 0,ifap = ifaddrs; i < count; ifap++, i++) { | 374 if (getifaddrs(&ifaddrs, &count) < 0) 375 goto out; 376 377 for (i = 0,ifap = ifaddrs; i < count; ifap++, i++) { |
336#endif /* HAVE_FIELD_STRUCT_IFADDRS_IFA_NEXT */ | 378#endif /* HAVE_STRUCT_IFADDRS_IFA_NEXT */ |
337 338 if (!ifap || !ifap->ifa_addr || ifap->ifa_addr->sa_family != AF_INET) 339 continue; 340 341 /* 342 * If the interface is a loopback, or its not running 343 * then ignore it. 344 */ --- 27 unchanged lines hidden (view full) --- 372 } else { 373 *name1 = NO_SUBNET; 374 *number1 = "0.0.0.0"; 375 } 376} 377 378#else /* not HAVE_GETIFADDRS */ 379 | 379 380 if (!ifap || !ifap->ifa_addr || ifap->ifa_addr->sa_family != AF_INET) 381 continue; 382 383 /* 384 * If the interface is a loopback, or its not running 385 * then ignore it. 386 */ --- 27 unchanged lines hidden (view full) --- 414 } else { 415 *name1 = NO_SUBNET; 416 *number1 = "0.0.0.0"; 417 } 418} 419 420#else /* not HAVE_GETIFADDRS */ 421 |
380#if defined(HAVE_FIELD_STRUCT_IFREQ_IFR_ADDR) && defined(HAVE_FIELD_STRUCT_SOCKADDR_SA_LEN) | 422#if defined(HAVE_STRUCT_IFREQ_IFR_ADDR) && defined(HAVE_STRUCT_SOCKADDR_SA_LEN) |
381# define SIZE(ifr) (MAX((ifr)->ifr_addr.sa_len, sizeof((ifr)->ifr_addr)) + sizeof(ifr->ifr_name)) | 423# define SIZE(ifr) (MAX((ifr)->ifr_addr.sa_len, sizeof((ifr)->ifr_addr)) + sizeof(ifr->ifr_name)) |
382#else /* not defined(HAVE_FIELD_STRUCT_IFREQ_IFR_ADDR) && defined(HAVE_FIELD_STRUCT_SOCKADDR_SA_LEN) */ | 424#else /* not defined(HAVE_STRUCT_IFREQ_IFR_ADDR) && defined(HAVE_STRUCT_SOCKADDR_SA_LEN) */ |
383# define SIZE(ifr) sizeof(struct ifreq) | 425# define SIZE(ifr) sizeof(struct ifreq) |
384#endif /* not defined(HAVE_FIELD_STRUCT_IFREQ_IFR_ADDR) && defined(HAVE_FIELD_STRUCT_SOCKADDR_SA_LEN) */ | 426#endif /* not defined(HAVE_STRUCT_IFREQ_IFR_ADDR) && defined(HAVE_STRUCT_SOCKADDR_SA_LEN) */ |
385 386#define clist (ifc.ifc_ifcu.ifcu_req) 387#define count (ifc.ifc_len/sizeof(struct ifreq)) 388 389 390void 391getwire(char **name1, char **number1) 392{ --- 126 unchanged lines hidden --- | 427 428#define clist (ifc.ifc_ifcu.ifcu_req) 429#define count (ifc.ifc_len/sizeof(struct ifreq)) 430 431 432void 433getwire(char **name1, char **number1) 434{ --- 126 unchanged lines hidden --- |