2 * Copyright (c) 2010 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>
21 #include <sys/param.h>
30 #endif /* STDC_HEADERS */
32 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
36 #endif /* HAVE_STRING_H */
39 #endif /* HAVE_STRINGS_H */
42 #endif /* HAVE_UNISTD_H */
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
49 #include "interfaces.h"
52 # define INADDR_NONE ((unsigned int)-1)
56 * Parse a space-delimited list of IP address/netmask pairs and
57 * store in a list of interface structures.
60 set_interfaces(const char *ai)
62 char *addrinfo, *addr, *mask;
63 struct interface *ifp;
64 debug_decl(set_interfaces, SUDO_DEBUG_NETIF)
66 addrinfo = estrdup(ai);
67 for (addr = strtok(addrinfo, " \t"); addr != NULL; addr = strtok(NULL, " \t")) {
68 /* Separate addr and mask. */
69 if ((mask = strchr(addr, '/')) == NULL)
73 /* Parse addr and store in list. */
74 ifp = ecalloc(1, sizeof(*ifp));
75 if (strchr(addr, ':')) {
77 #ifdef HAVE_STRUCT_IN6_ADDR
78 ifp->family = AF_INET6;
79 if (inet_pton(AF_INET6, addr, &ifp->addr.ip6) != 1 ||
80 inet_pton(AF_INET6, mask, &ifp->netmask.ip6) != 1)
88 ifp->family = AF_INET;
89 ifp->addr.ip4.s_addr = inet_addr(addr);
90 ifp->netmask.ip4.s_addr = inet_addr(mask);
91 if (ifp->addr.ip4.s_addr == INADDR_NONE ||
92 ifp->netmask.ip4.s_addr == INADDR_NONE) {
97 ifp->next = interfaces;
105 dump_interfaces(const char *ai)
108 debug_decl(set_interfaces, SUDO_DEBUG_NETIF)
110 addrinfo = estrdup(ai);
112 sudo_printf(SUDO_CONV_INFO_MSG, _("Local IP address and netmask pairs:\n"));
113 for (cp = strtok(addrinfo, " \t"); cp != NULL; cp = strtok(NULL, " \t"))
114 sudo_printf(SUDO_CONV_INFO_MSG, "\t%s\n", cp);