=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/sudo/Attic/interfaces.c,v retrieving revision 1.1.1.1 retrieving revision 1.1.1.1.6.1 diff -u -r1.1.1.1 -r1.1.1.1.6.1 --- src/usr.bin/sudo/Attic/interfaces.c 1999/11/18 16:29:01 1.1.1.1 +++ src/usr.bin/sudo/Attic/interfaces.c 2002/01/18 17:20:23 1.1.1.1.6.1 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 1998, 1999 Todd C. Miller + * Copyright (c) 1996, 1998-2001 Todd C. Miller * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -43,55 +43,68 @@ #include "config.h" -#include -#ifdef STDC_HEADERS -#include -#endif /* STDC_HEADERS */ -#ifdef HAVE_UNISTD_H -#include -#endif /* HAVE_UNISTD_H */ -#ifdef HAVE_STRING_H -#include -#endif /* HAVE_STRING_H */ -#ifdef HAVE_STRINGS_H -#include -#endif /* HAVE_STRINGS_H */ -#include -#include #include #include #include #include #include #if defined(HAVE_SYS_SOCKIO_H) && !defined(SIOCGIFCONF) -#include +# include #endif +#include +#ifdef STDC_HEADERS +# include +# include +#else +# ifdef HAVE_STDLIB_H +# include +# endif +#endif /* STDC_HEADERS */ +#ifdef HAVE_STRING_H +# if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS) +# include +# endif +# include +#else +# ifdef HAVE_STRINGS_H +# include +# endif +#endif /* HAVE_STRING_H */ +#ifdef HAVE_UNISTD_H +# include +#endif /* HAVE_UNISTD_H */ +#include +#include #ifdef _ISC -#include -#include -#include -#include -#define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\ +# include +# include +# include +# include +# define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\ strioctl.ic_dp=(param);\ strioctl.ic_timout=0;\ strioctl.ic_len=(len);} #endif /* _ISC */ #ifdef _MIPS -#include +# include #endif /* _MIPS */ #include #include #include +#ifdef HAVE_GETIFADDRS +# include +#endif #include "sudo.h" #include "interfaces.h" #ifndef lint -static const char rcsid[] = "$Sudo: interfaces.c,v 1.59 1999/08/12 16:24:09 millert Exp $"; +static const char rcsid[] = "$Sudo: interfaces.c,v 1.62 2001/12/14 22:12:39 millert Exp $"; #endif /* lint */ -#if defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES) +#ifdef HAVE_GETIFADDRS + /* * Allocate and fill in the interfaces global variable with the * machine's ip addresses and netmasks. @@ -99,6 +112,63 @@ void load_interfaces() { + struct ifaddrs *ifa, *ifaddrs; + /* XXX - sockaddr_in6 sin6; */ + struct sockaddr_in *sin; + int i; + + if (getifaddrs(&ifaddrs)) + return; + + /* Allocate space for the interfaces list. */ + for (ifa = ifaddrs; ifa -> ifa_next; ifa = ifa -> ifa_next) { + /* Skip interfaces marked "down" and "loopback". */ + if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) || + (ifa->ifa_flags & IFF_LOOPBACK)) + continue; + + switch(ifa->ifa_addr->sa_family) { + /* XXX - AF_INET6 */ + case AF_INET: + num_interfaces++; + break; + } + } + interfaces = + (struct interface *) emalloc(sizeof(struct interface) * num_interfaces); + + /* Store the ip addr / netmask pairs. */ + for (ifa = ifaddrs, i = 0; ifa -> ifa_next; ifa = ifa -> ifa_next) { + /* Skip interfaces marked "down" and "loopback". */ + if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) || + (ifa->ifa_flags & IFF_LOOPBACK)) + continue; + + switch(ifa->ifa_addr->sa_family) { + /* XXX - AF_INET6 */ + case AF_INET: + sin = (struct sockaddr_in *)ifa->ifa_addr; + memcpy(&interfaces[i].addr, &sin->sin_addr, + sizeof(struct in_addr)); + sin = (struct sockaddr_in *)ifa->ifa_netmask; + memcpy(&interfaces[i].netmask, &sin->sin_addr, + sizeof(struct in_addr)); + i++; + break; + } + } + freeifaddrs(ifaddrs); +} + +#elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES) + +/* + * Allocate and fill in the interfaces global variable with the + * machine's ip addresses and netmasks. + */ +void +load_interfaces() +{ struct ifconf *ifconf; struct ifreq *ifr, ifr_tmp; struct sockaddr_in *sin; @@ -238,3 +308,14 @@ } #endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */ + +void +dump_interfaces() +{ + int i; + + puts("Local IP address and netmask pairs:"); + for (i = 0; i < num_interfaces; i++) + printf("\t%s / 0x%x\n", inet_ntoa(interfaces[i].addr), + ntohl(interfaces[i].netmask.s_addr)); +}