2 * Copyright (c) 2011-2013 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 */
33 #endif /* HAVE_STRING_H */
36 #endif /* HAVE_STRINGS_H */
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
45 #define SUDO_ERROR_WRAP 0
49 #include "interfaces.h"
51 __dso_public int main(int argc, char *argv[]);
54 check_addr(char *input)
56 int expected, matched;
60 while (isspace((unsigned char)*input))
63 /* input: "addr[/mask] 1/0" */
64 len = strcspn(input, " \t");
66 while (isspace((unsigned char)*cp))
71 matched = addr_matches(input);
72 if (matched != expected) {
73 warningx("%s %smatched: FAIL", input, matched ? "" : "not ");
82 fprintf(stderr, "usage: check_addr datafile\n");
87 main(int argc, char *argv[])
89 int ntests = 0, errors = 0;
94 #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
95 setprogname(argc > 0 ? argv[0] : "check_addr");
101 fp = fopen(argv[1], "r");
103 fatalx("unable to open %s", argv[1]);
106 * Input is in the following format. There are two types of
107 * lines: interfaces, which sets the address and mask of the
108 * locally connected ethernet interfaces for the lines that
109 * follow and, address lines that include and address (with
110 * optional netmask) to match, followed by expected match status
113 * interfaces: addr1/mask addr2/mask ...
114 * address: addr[/mask] 1/0
115 * address: addr[/mask] 1/0
116 * interfaces: addr3/mask addr4/mask ...
117 * address: addr[/mask] 1/0
120 while (fgets(line, sizeof(line), fp) != NULL) {
121 len = strcspn(line, "\n");
124 /* Ignore comments */
125 if ((cp = strchr(line, '#')) != NULL)
128 /* Skip blank lines. */
132 if (strncmp(line, "interfaces:", sizeof("interfaces:") - 1) == 0) {
133 set_interfaces(line + sizeof("interfaces:") - 1);
134 } else if (strncmp(line, "address:", sizeof("address:") - 1) == 0) {
135 errors += check_addr(line + sizeof("address:") - 1);
138 warningx("unexpected data line: %s\n", line);
143 printf("check_addr: %d tests run, %d errors, %d%% success rate\n",
144 ntests, errors, (ntests - errors) * 100 / ntests);