2 * Copyright (c) 2010-2012 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/socket.h>
29 #endif /* STDC_HEADERS */
31 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
35 #endif /* HAVE_STRING_H */
38 #endif /* HAVE_STRINGS_H */
41 #endif /* HAVE_UNISTD_H */
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
48 #include "interfaces.h"
51 # define INADDR_NONE ((unsigned int)-1)
54 static struct interface *interfaces;
57 * Parse a space-delimited list of IP address/netmask pairs and
58 * store in a list of interface structures.
61 set_interfaces(const char *ai)
63 char *addrinfo, *addr, *mask;
64 struct interface *ifp;
65 debug_decl(set_interfaces, SUDO_DEBUG_NETIF)
67 addrinfo = estrdup(ai);
68 for (addr = strtok(addrinfo, " \t"); addr != NULL; addr = strtok(NULL, " \t")) {
69 /* Separate addr and mask. */
70 if ((mask = strchr(addr, '/')) == NULL)
74 /* Parse addr and store in list. */
75 ifp = ecalloc(1, sizeof(*ifp));
76 if (strchr(addr, ':')) {
78 #ifdef HAVE_STRUCT_IN6_ADDR
79 ifp->family = AF_INET6;
80 if (inet_pton(AF_INET6, addr, &ifp->addr.ip6) != 1 ||
81 inet_pton(AF_INET6, mask, &ifp->netmask.ip6) != 1)
89 ifp->family = AF_INET;
90 ifp->addr.ip4.s_addr = inet_addr(addr);
91 ifp->netmask.ip4.s_addr = inet_addr(mask);
92 if (ifp->addr.ip4.s_addr == INADDR_NONE ||
93 ifp->netmask.ip4.s_addr == INADDR_NONE) {
98 ifp->next = interfaces;
112 dump_interfaces(const char *ai)
115 debug_decl(set_interfaces, SUDO_DEBUG_NETIF)
117 addrinfo = estrdup(ai);
119 sudo_printf(SUDO_CONV_INFO_MSG, _("Local IP address and netmask pairs:\n"));
120 for (cp = strtok(addrinfo, " \t"); cp != NULL; cp = strtok(NULL, " \t"))
121 sudo_printf(SUDO_CONV_INFO_MSG, "\t%s\n", cp);