2 * convert from ASCII form of address/subnet/range to binary
3 * Copyright (C) 1998, 1999 Henry Spencer.
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 * License for more details.
15 * RCSID $Id: atoasr.c,v 1.5 1999/12/07 05:00:51 henry Exp $
21 - atoasr - convert ASCII to address, subnet, or range
23 const char * /* NULL for success, else string literal */
24 atoasr(src, srclen, typep, addrsp)
26 size_t srclen; /* 0 means "apply strlen" */
27 char *typep; /* return type code: 'a', 's', 'r' */
28 struct in_addr addrsp[2];
37 return "empty string";
39 /* subnet is easy to spot */
40 punct = memchr(src, '/', srclen);
43 return atosubnet(src, srclen, &addrsp[0], &addrsp[1]);
48 for (punct = src; (punct = memchr(punct, '.', stop - punct)) != NULL;
50 if (stop - punct > 3 && *(punct+1) == '.' && *(punct+2) == '.')
51 break; /* NOTE BREAK OUT */
53 /* didn't find the range delimiter, must be plain address */
55 return atoaddr(src, srclen, &addrsp[0]);
58 /* looks like a range */
60 if (stop - punct > 4 && *(punct+3) == '.')
61 punct++; /* first dot is trailing dot of name */
62 oops = atoaddr(src, punct - src, &addrsp[0]);
65 oops = atoaddr(punct+3, stop - (punct+3), &addrsp[1]);
68 if (ntohl(addrsp[0].s_addr) > ntohl(addrsp[1].s_addr))
69 return "invalid range, begin > end";
93 fprintf(stderr, "Usage: %s {addr|net/mask|begin...end|-r}\n",
98 if (strcmp(argv[1], "-r") == 0) {
100 fprintf(stderr, "regress() returned?!?\n");
104 oops = atoasr(argv[1], 0, &type, a);
106 fprintf(stderr, "%s: conversion failed: %s\n", argv[0], oops);
111 n = addrtoa(a[0], 0, buf, sizeof(buf));
114 n = subnettoa(a[0], a[1], 0, buf, sizeof(buf));
117 n = rangetoa(a, 0, buf, sizeof(buf));
120 fprintf(stderr, "%s: unknown type '%c'\n", argv[0], type);
124 if (n > sizeof(buf)) {
125 fprintf(stderr, "%s: reverse conversion of ", argv[0]);
126 fprintf(stderr, "%s ", inet_ntoa(a[0]));
127 fprintf(stderr, "%s", inet_ntoa(a[1]));
128 fprintf(stderr, " failed: need %ld bytes, have only %ld\n",
129 (long)n, (long)sizeof(buf));
139 char *output; /* NULL means error expected */
141 "1.2.3.0", "1.2.3.0",
142 "1.2.3.0/255.255.255.0", "1.2.3.0/24",
143 "1.2.3.0...1.2.3.5", "1.2.3.0...1.2.3.5",
148 "localhost./32", "127.0.0.1/32",
149 "localhost....127.0.0.3", "127.0.0.1...127.0.0.3",
150 "127.0.0.0...localhost.", "127.0.0.0...127.0.0.1",
151 "127.0.0.3...localhost.", NULL,
168 for (r = rtab; r->input != NULL; r++) {
169 strcpy(in, r->input);
170 oops = atoasr(in, 0, &type, a);
171 if (oops != NULL && r->output == NULL)
172 {} /* okay, error expected */
173 else if (oops != NULL) {
174 printf("`%s' atoasr failed: %s\n", r->input, oops);
176 } else if (r->output == NULL) {
177 printf("`%s' atoasr succeeded unexpectedly '%c'\n",
183 n = addrtoa(a[0], 0, buf, sizeof(buf));
186 n = subnettoa(a[0], a[1], 0, buf, sizeof(buf));
189 n = rangetoa(a, 0, buf, sizeof(buf));
192 fprintf(stderr, "`%s' unknown type '%c'\n",
198 if (n > sizeof(buf)) {
199 printf("`%s' '%c' reverse failed: need %ld\n",
200 r->input, type, (long)n);
202 } else if (n > 0 && strcmp(r->output, buf) != 0) {
203 printf("`%s' '%c' gave `%s', expected `%s'\n",
204 r->input, type, buf, r->output);
212 #endif /* ATOASR_MAIN */