Replacing gethostbyname, gethostbyname2 and their _r variants with getaddrinfo to...
[strongswan.git] / src / libfreeswan / atoaddr.c
1 /*
2 * conversion from ASCII forms of addresses to internal ones
3 * Copyright (C) 1998, 1999 Henry Spencer.
4 *
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>.
9 *
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.
14 */
15 #include <sys/socket.h>
16
17 #include "internal.h"
18 #include "freeswan.h"
19
20 /*
21 * Define NOLEADINGZEROS to interpret 032 as an error, not as 32. There
22 * is deliberately no way to interpret it as 26 (i.e., as octal).
23 */
24
25 /*
26 * Legal characters in a domain name. Underscore technically is not,
27 * but is a common misunderstanding.
28 */
29 static const char namechars[] = "abcdefghijklmnopqrstuvwxyz0123456789"
30 "ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.";
31
32 static const char *try8hex(const char *, size_t, struct in_addr *);
33 static const char *try8hosthex(const char *, size_t, struct in_addr *);
34 static const char *trydotted(const char *, size_t, struct in_addr *);
35 static const char *getbyte(const char **, const char *, int *);
36
37 /*
38 - atoaddr - convert ASCII name or dotted-decimal address to binary address
39 */
40 const char * /* NULL for success, else string literal */
41 atoaddr(src, srclen, addrp)
42 const char *src;
43 size_t srclen; /* 0 means "apply strlen" */
44 struct in_addr *addrp;
45 {
46 struct addrinfo hints, *res;
47 struct netent *ne = NULL;
48 const char *oops;
49 # define HEXLEN 10 /* strlen("0x11223344") */
50 # ifndef ATOADDRBUF
51 # define ATOADDRBUF 100
52 # endif
53 char namebuf[ATOADDRBUF];
54 char *p = namebuf;
55 char *q;
56 int error;
57
58 if (srclen == 0)
59 srclen = strlen(src);
60 if (srclen == 0)
61 return "empty string";
62
63 /* might it be hex? */
64 if (srclen == HEXLEN && *src == '0' && CIEQ(*(src+1), 'x'))
65 return try8hex(src+2, srclen-2, addrp);
66 if (srclen == HEXLEN && *src == '0' && CIEQ(*(src+1), 'h'))
67 return try8hosthex(src+2, srclen-2, addrp);
68
69 /* try it as dotted decimal */
70 oops = trydotted(src, srclen, addrp);
71 if (oops == NULL)
72 return NULL; /* it worked */
73 if (*oops != '?')
74 return oops; /* it *was* probably meant as a d.q. */
75
76 /* try it as a name -- first, NUL-terminate it */
77 if (srclen > sizeof(namebuf)-1) {
78 p = (char *) MALLOC(srclen+1);
79 if (p == NULL)
80 return "unable to allocate temporary space for name";
81 }
82 p[0] = '\0';
83 strncat(p, src, srclen);
84
85 /* next, check that it's a vaguely legal name */
86 for (q = p; *q != '\0'; q++)
87 if (!isprint(*q))
88 return "unprintable character in name";
89 if (strspn(p, namechars) != srclen)
90 return "illegal (non-DNS-name) character in name";
91
92 /* try as host name, failing that as /etc/networks network name */
93 memset(&hints, 0, sizeof(hints));
94 hints.ai_family = AF_INET;
95 error = getaddrinfo(p, NULL, &hints, &res);
96 if (error != 0)
97 {
98 ne = getnetbyname(p);
99 if (ne == NULL)
100 {
101 if (p != namebuf)
102 {
103 FREE(p);
104 }
105 return "name lookup failed";
106 }
107 addrp->s_addr = htonl(ne->n_net);
108 }
109 else
110 {
111 memcpy(&addrp->s_addr, res->ai_addr->sa_data, sizeof(addrp->s_addr));
112 freeaddrinfo(res);
113 }
114
115 if (p != namebuf)
116 {
117 FREE(p);
118 }
119
120 return NULL;
121 }
122
123 /*
124 - try8hosthex - try conversion as an eight-digit host-order hex number
125 */
126 const char * /* NULL for success, else string literal */
127 try8hosthex(src, srclen, addrp)
128 const char *src;
129 size_t srclen; /* should be 8 */
130 struct in_addr *addrp;
131 {
132 const char *oops;
133 unsigned long addr;
134
135 if (srclen != 8)
136 return "internal error, try8hex called with bad length";
137
138 oops = atoul(src, srclen, 16, &addr);
139 if (oops != NULL)
140 return oops;
141
142 addrp->s_addr = addr;
143 return NULL;
144 }
145
146 /*
147 - try8hex - try conversion as an eight-digit network-order hex number
148 */
149 const char * /* NULL for success, else string literal */
150 try8hex(src, srclen, addrp)
151 const char *src;
152 size_t srclen; /* should be 8 */
153 struct in_addr *addrp;
154 {
155 const char *oops;
156
157 oops = try8hosthex(src, srclen, addrp);
158 if (oops != NULL)
159 return oops;
160
161 addrp->s_addr = htonl(addrp->s_addr);
162 return NULL;
163 }
164
165 /*
166 - trydotted - try conversion as dotted decimal
167 *
168 * If the first char of a complaint is '?', that means "didn't look like
169 * dotted decimal at all".
170 */
171 const char * /* NULL for success, else string literal */
172 trydotted(src, srclen, addrp)
173 const char *src;
174 size_t srclen;
175 struct in_addr *addrp;
176 {
177 const char *stop = src + srclen; /* just past end */
178 int byte;
179 const char *oops;
180 unsigned long addr;
181 int i;
182 # define NBYTES 4
183 # define BYTE 8
184
185 addr = 0;
186 for (i = 0; i < NBYTES && src < stop; i++) {
187 oops = getbyte(&src, stop, &byte);
188 if (oops != NULL) {
189 if (*oops != '?')
190 return oops; /* bad number */
191 if (i > 1)
192 return oops+1; /* failed number */
193 return oops; /* with leading '?' */
194 }
195 addr = (addr << BYTE) | byte;
196 if (i < 3 && src < stop && *src++ != '.') {
197 if (i == 0)
198 return "?syntax error in dotted-decimal address";
199 else
200 return "syntax error in dotted-decimal address";
201 }
202 }
203 addr <<= (NBYTES - i) * BYTE;
204 if (src != stop)
205 return "extra garbage on end of dotted-decimal address";
206
207 addrp->s_addr = htonl(addr);
208 return NULL;
209 }
210
211 /*
212 - getbyte - try to scan a byte in dotted decimal
213 * A subtlety here is that all this arithmetic on ASCII digits really is
214 * highly portable -- ANSI C guarantees that digits 0-9 are contiguous.
215 * It's easier to just do it ourselves than set up for a call to atoul().
216 *
217 * If the first char of a complaint is '?', that means "didn't look like a
218 * number at all".
219 */
220 const char * /* NULL for success, else string literal */
221 getbyte(srcp, stop, retp)
222 const char **srcp; /* *srcp is updated */
223 const char *stop; /* first untouchable char */
224 int *retp; /* return-value pointer */
225 {
226 char c;
227 const char *p;
228 int no;
229
230 if (*srcp >= stop)
231 return "?empty number in dotted-decimal address";
232
233 if (stop - *srcp >= 3 && **srcp == '0' && CIEQ(*(*srcp+1), 'x'))
234 return "hex numbers not supported in dotted-decimal addresses";
235 #ifdef NOLEADINGZEROS
236 if (stop - *srcp >= 2 && **srcp == '0' && isdigit(*(*srcp+1)))
237 return "octal numbers not supported in dotted-decimal addresses";
238 #endif /* NOLEADINGZEROS */
239
240 /* must be decimal, if it's numeric at all */
241 no = 0;
242 p = *srcp;
243 while (p < stop && no <= 255 && (c = *p) >= '0' && c <= '9') {
244 no = no*10 + (c - '0');
245 p++;
246 }
247 if (p == *srcp)
248 return "?non-numeric component in dotted-decimal address";
249 *srcp = p;
250 if (no > 255)
251 return "byte overflow in dotted-decimal address";
252 *retp = no;
253 return NULL;
254 }