1 /* $NetBSD: inet_network.c,v 1.1 2008/01/19 04:12:21 ginsbach Exp $ */
3 * Copyright (c) 2008, The NetBSD Foundation.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <arpa/inet.h>
36 #include <netinet/in.h>
48 #define ISODIGIT(c) ((int)(c) >= '0' && (int)(c) <= '7')
50 char *unescape(char *);
51 char *unquote(char *);
54 main(int argc
, char *argv
[])
56 size_t len
, lineno
= 0;
57 char *ep
, *line
, *input
= NULL
, *result
= NULL
;
61 while ((line
= fparseln(stdin
, &len
, &lineno
, NULL
, 0)) != NULL
) {
62 if (strncmp(line
, "input:", 6) == 0) {
65 input
= &line
[7 + strspn(&line
[7], WS
)];
67 errx(1, "missing input at line %ld",
68 (unsigned long)lineno
);
69 input
= strdup(unescape(unquote(input
)));
70 } else if (strncmp(line
, "result:", 7) == 0) {
73 result
= &line
[8 + strspn(&line
[8], WS
)];
75 errx(1, "missing result at line %ld",
76 (unsigned long)lineno
);
79 "result without input at line %ld");
80 result
= strdup(result
);
82 addr
.s_addr
= inet_network(input
);
85 s_addr
= strtoul(result
, &ep
, 0);
86 if (errno
== ERANGE
|| errno
== EINVAL
)
87 err(1, "line %ld: strtoul(): %s",
88 (unsigned long)lineno
, result
);
90 if (addr
.s_addr
!= s_addr
)
92 warnx("0x%08x != 0x%08x [%s]\tFAILED",
93 addr
.s_addr
, s_addr
, input
);
95 warnx("0x%08x == 0x%08x [%s]\tPASSED",
96 addr
.s_addr
, s_addr
, input
);
98 errx(1, "0x%08x != 0x%08x [%s]",
99 addr
.s_addr
, s_addr
, input
);
102 errx(1, "unknown directive at line %ld",
103 (unsigned long)lineno
);
115 char c
, *cp
, *new = orig
;
118 for (cp
= orig
; (*orig
= *cp
); cp
++) {
119 if (*cp
== '\'' || *cp
== '"')
122 escaped
= (*cp
== '\\');
130 * unescape - handle C escapes in a string
135 char c
, *cp
, *new = orig
;
138 for (cp
= orig
; (*orig
= *cp
); cp
++, orig
++) {
143 case 'a': /* alert (bell) */
146 case 'b': /* backspace */
149 case 'e': /* escape */
152 case 'f': /* formfeed */
155 case 'n': /* newline */
158 case 'r': /* carriage return */
161 case 't': /* horizontal tab */
164 case 'v': /* vertical tab */
167 case '\\': /* backslash */
170 case '\'': /* single quote */
173 case '\"': /* double quote */
179 case '3': /* octal */
183 case '7': /* number */
185 ISODIGIT((unsigned char)*cp
) && i
< 3;
193 case 'x': /* hexidecimal number */
196 isxdigit((unsigned char)*cp
) && i
< 2;
199 if (isdigit((unsigned char)*cp
))
202 c
|= ((toupper((unsigned char)*cp
) -