]> git.ipfire.org Git - people/ms/strongswan.git/blob - src/libfreeswan/libfreeswan/ttoaddr.c
- started to rebuild source layout
[people/ms/strongswan.git] / src / libfreeswan / libfreeswan / ttoaddr.c
1 /*
2 * conversion from text forms of addresses to internal ones
3 * Copyright (C) 2000 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 * RCSID $Id: ttoaddr.c,v 1.1 2004/03/15 20:35:26 as Exp $
16 */
17 #include "internal.h"
18 #include "freeswan.h"
19
20 /*
21 * Legal ASCII characters in a domain name. Underscore technically is not,
22 * but is a common misunderstanding. Non-ASCII characters are simply
23 * exempted from checking at the moment, to allow for UTF-8 encoded stuff;
24 * the purpose of this check is merely to catch blatant errors.
25 */
26 static const char namechars[] = "abcdefghijklmnopqrstuvwxyz0123456789"
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.";
28 #define ISASCII(c) (((c) & 0x80) == 0)
29
30 static err_t tryname(const char *, size_t, int, int, ip_address *);
31 static err_t tryhex(const char *, size_t, int, ip_address *);
32 static err_t trydotted(const char *, size_t, ip_address *);
33 static err_t getbyte(const char **, const char *, int *);
34 static err_t colon(const char *, size_t, ip_address *);
35 static err_t getpiece(const char **, const char *, unsigned *);
36
37 /*
38 - ttoaddr - convert text name or dotted-decimal address to binary address
39 */
40 err_t /* NULL for success, else string literal */
41 ttoaddr(src, srclen, af, dst)
42 const char *src;
43 size_t srclen; /* 0 means "apply strlen" */
44 int af; /* address family */
45 ip_address *dst;
46 {
47 err_t oops;
48 # define HEXLEN 10 /* strlen("0x11223344") */
49 int nultermd;
50
51 if (srclen == 0) {
52 srclen = strlen(src);
53 if (srclen == 0)
54 return "empty string";
55 nultermd = 1;
56 } else
57 nultermd = 0; /* at least, not *known* to be terminated */
58
59 switch (af) {
60 case AF_INET:
61 case AF_INET6:
62 case 0: /* guess */
63 break;
64
65 default:
66 return "invalid address family";
67 }
68
69 if (af == AF_INET && srclen == HEXLEN && *src == '0') {
70 if (*(src+1) == 'x' || *(src+1) == 'X')
71 return tryhex(src+2, srclen-2, 'x', dst);
72 if (*(src+1) == 'h' || *(src+1) == 'H')
73 return tryhex(src+2, srclen-2, 'h', dst);
74 }
75
76 if (memchr(src, ':', srclen) != NULL) {
77 if(af == 0)
78 {
79 af = AF_INET6;
80 }
81
82 if (af != AF_INET6)
83 return "non-ipv6 address may not contain `:'";
84 return colon(src, srclen, dst);
85 }
86
87 if (af == 0 || af == AF_INET) {
88 oops = trydotted(src, srclen, dst);
89 if (oops == NULL)
90 return NULL; /* it worked */
91 if (*oops != '?')
92 return oops; /* probably meant as d-d */
93 }
94
95 return tryname(src, srclen, nultermd, af, dst);
96 }
97
98 /*
99 - tnatoaddr - convert text numeric address (only) to binary address
100 */
101 err_t /* NULL for success, else string literal */
102 tnatoaddr(src, srclen, af, dst)
103 const char *src;
104 size_t srclen; /* 0 means "apply strlen" */
105 int af; /* address family */
106 ip_address *dst;
107 {
108 err_t oops;
109
110 if (srclen == 0) {
111 srclen = strlen(src);
112 if (srclen == 0)
113 return "empty string";
114 }
115
116 switch (af) {
117 case 0: /* guess */
118 oops = colon(src, srclen, dst);
119 if(oops == NULL)
120 {
121 return NULL;
122 }
123 oops = trydotted(src, srclen, dst);
124 if(oops == NULL)
125 {
126 return NULL;
127 }
128 return "does not appear to be either IPv4 or IPv6 numeric address";
129 break;
130
131 case AF_INET6:
132 return colon(src, srclen, dst);
133 break;
134 case AF_INET:
135 oops = trydotted(src, srclen, dst);
136 if (oops == NULL)
137 return NULL; /* it worked */
138 if (*oops != '?')
139 return oops; /* probably meant as d-d */
140 return "does not appear to be numeric address";
141 break;
142 default:
143 return "unknown address family in tnatoaddr";
144 break;
145 }
146 }
147
148 /*
149 - tryname - try it as a name
150 * Slightly complicated by lack of reliable NUL termination in source.
151 */
152 static err_t
153 tryname(src, srclen, nultermd, af, dst)
154 const char *src;
155 size_t srclen;
156 int nultermd; /* is it known to be NUL-terminated? */
157 int af;
158 ip_address *dst;
159 {
160 struct hostent *h;
161 struct netent *ne = NULL;
162 char namebuf[100]; /* enough for most DNS names */
163 const char *cp;
164 char *p = namebuf;
165 size_t n;
166
167 for (cp = src, n = srclen; n > 0; cp++, n--)
168 if (ISASCII(*cp) && strchr(namechars, *cp) == NULL)
169 return "illegal (non-DNS-name) character in name";
170
171 if (nultermd)
172 cp = src;
173 else {
174 if (srclen+1 > sizeof(namebuf)) {
175 p = (char *) MALLOC(srclen+1);
176 if (p == NULL)
177 return "unable to get temporary space for name";
178 }
179 p[0] = '\0'; /* strncpy semantics are wrong */
180 strncat(p, src, srclen);
181 cp = (const char *)p;
182 }
183
184 h = gethostbyname2(cp, af);
185 if (h == NULL && af == AF_INET)
186 ne = getnetbyname(cp);
187 if (p != namebuf)
188 FREE(p);
189 if (h == NULL && ne == NULL)
190 return "does not look numeric and name lookup failed";
191
192 if (h != NULL) {
193 if (h->h_addrtype != af)
194 return "address-type mismatch from gethostbyname2!!!";
195 return initaddr((unsigned char *)h->h_addr, h->h_length, af, dst);
196 } else {
197 if (ne->n_addrtype != af)
198 return "address-type mismatch from getnetbyname!!!";
199 ne->n_net = htonl(ne->n_net);
200 return initaddr((unsigned char *)&ne->n_net, sizeof(ne->n_net),
201 af, dst);
202 }
203 }
204
205 /*
206 - tryhex - try conversion as an eight-digit hex number (AF_INET only)
207 */
208 static err_t
209 tryhex(src, srclen, flavor, dst)
210 const char *src;
211 size_t srclen; /* should be 8 */
212 int flavor; /* 'x' for network order, 'h' for host order */
213 ip_address *dst;
214 {
215 err_t oops;
216 unsigned long ul;
217 union {
218 uint32_t addr;
219 unsigned char buf[4];
220 } u;
221
222 if (srclen != 8)
223 return "internal error, tryhex called with bad length";
224
225 oops = ttoul(src, srclen, 16, &ul);
226 if (oops != NULL)
227 return oops;
228
229 u.addr = (flavor == 'h') ? ul : htonl(ul);
230 return initaddr(u.buf, sizeof(u.buf), AF_INET, dst);
231 }
232
233 /*
234 - trydotted - try conversion as dotted decimal (AF_INET only)
235 *
236 * If the first char of a complaint is '?', that means "didn't look like
237 * dotted decimal at all".
238 */
239 static err_t
240 trydotted(src, srclen, dst)
241 const char *src;
242 size_t srclen;
243 ip_address *dst;
244 {
245 const char *stop = src + srclen; /* just past end */
246 int byte;
247 err_t oops;
248 # define NBYTES 4
249 unsigned char buf[NBYTES];
250 int i;
251
252 memset(buf, 0, sizeof(buf));
253 for (i = 0; i < NBYTES && src < stop; i++) {
254 oops = getbyte(&src, stop, &byte);
255 if (oops != NULL) {
256 if (*oops != '?')
257 return oops; /* bad number */
258 if (i > 1)
259 return oops+1; /* failed number */
260 return oops; /* with leading '?' */
261 }
262 buf[i] = byte;
263 if (i < 3 && src < stop && *src++ != '.') {
264 if (i == 0)
265 return "?syntax error in dotted-decimal address";
266 else
267 return "syntax error in dotted-decimal address";
268 }
269 }
270 if (src != stop)
271 return "extra garbage on end of dotted-decimal address";
272
273 return initaddr(buf, sizeof(buf), AF_INET, dst);
274 }
275
276 /*
277 - getbyte - try to scan a byte in dotted decimal
278 * A subtlety here is that all this arithmetic on ASCII digits really is
279 * highly portable -- ANSI C guarantees that digits 0-9 are contiguous.
280 * It's easier to just do it ourselves than set up for a call to ttoul().
281 *
282 * If the first char of a complaint is '?', that means "didn't look like a
283 * number at all".
284 */
285 err_t
286 getbyte(srcp, stop, retp)
287 const char **srcp; /* *srcp is updated */
288 const char *stop; /* first untouchable char */
289 int *retp; /* return-value pointer */
290 {
291 char c;
292 const char *p;
293 int no;
294
295 if (*srcp >= stop)
296 return "?empty number in dotted-decimal address";
297
298 no = 0;
299 p = *srcp;
300 while (p < stop && no <= 255 && (c = *p) >= '0' && c <= '9') {
301 no = no*10 + (c - '0');
302 p++;
303 }
304 if (p == *srcp)
305 return "?non-numeric component in dotted-decimal address";
306 *srcp = p;
307 if (no > 255)
308 return "byte overflow in dotted-decimal address";
309 *retp = no;
310 return NULL;
311 }
312
313 /*
314 - colon - convert IPv6 "numeric" address
315 */
316 static err_t
317 colon(src, srclen, dst)
318 const char *src;
319 size_t srclen; /* known to be >0 */
320 ip_address *dst;
321 {
322 const char *stop = src + srclen; /* just past end */
323 unsigned piece;
324 int gapat; /* where was empty piece seen */
325 err_t oops;
326 # define NPIECES 8
327 unsigned char buf[NPIECES*2]; /* short may have wrong byte order */
328 int i;
329 int j;
330 # define IT "IPv6 numeric address"
331 int naftergap;
332
333 /* leading or trailing :: becomes single empty field */
334 if (*src == ':') { /* legal only if leading :: */
335 if (srclen == 1 || *(src+1) != ':')
336 return "illegal leading `:' in " IT;
337 if (srclen == 2) {
338 unspecaddr(AF_INET6, dst);
339 return NULL;
340 }
341 src++; /* past first but not second */
342 srclen--;
343 }
344 if (*(stop-1) == ':') { /* legal only if trailing :: */
345 if (srclen == 1 || *(stop-2) != ':')
346 return "illegal trailing `:' in " IT;
347 srclen--; /* leave one */
348 }
349
350 gapat = -1;
351 for (i = 0; i < NPIECES && src < stop; i++) {
352 oops = getpiece(&src, stop, &piece);
353 if (oops != NULL && *oops == ':') { /* empty field */
354 if (gapat >= 0)
355 return "more than one :: in " IT;
356 gapat = i;
357 } else if (oops != NULL)
358 return oops;
359 buf[2*i] = piece >> 8;
360 buf[2*i + 1] = piece & 0xff;
361 if (i < NPIECES-1) { /* there should be more input */
362 if (src == stop && gapat < 0)
363 return IT " ends prematurely";
364 if (src != stop && *src++ != ':')
365 return "syntax error in " IT;
366 }
367 }
368 if (src != stop)
369 return "extra garbage on end of " IT;
370
371 if (gapat < 0 && i < NPIECES) /* should have been caught earlier */
372 return "incomplete " IT " (internal error)";
373 if (gapat >= 0 && i == NPIECES)
374 return "non-abbreviating empty field in " IT;
375 if (gapat >= 0) {
376 naftergap = i - (gapat + 1);
377 for (i--, j = NPIECES-1; naftergap > 0; i--, j--, naftergap--) {
378 buf[2*j] = buf[2*i];
379 buf[2*j + 1] = buf[2*i + 1];
380 }
381 for (; j >= gapat; j--)
382 buf[2*j] = buf[2*j + 1] = 0;
383 }
384
385 return initaddr(buf, sizeof(buf), AF_INET6, dst);
386 }
387
388 /*
389 - getpiece - try to scan one 16-bit piece of an IPv6 address
390 */
391 err_t /* ":" means "empty field seen" */
392 getpiece(srcp, stop, retp)
393 const char **srcp; /* *srcp is updated */
394 const char *stop; /* first untouchable char */
395 unsigned *retp; /* return-value pointer */
396 {
397 const char *p;
398 # define NDIG 4
399 int d;
400 unsigned long ret;
401 err_t oops;
402
403 if (*srcp >= stop || **srcp == ':') { /* empty field */
404 *retp = 0;
405 return ":";
406 }
407
408 p = *srcp;
409 d = 0;
410 while (p < stop && d < NDIG && isxdigit(*p)) {
411 p++;
412 d++;
413 }
414 if (d == 0)
415 return "non-hex field in IPv6 numeric address";
416 if (p < stop && d == NDIG && isxdigit(*p))
417 return "field in IPv6 numeric address longer than 4 hex digits";
418
419 oops = ttoul(*srcp, d, 16, &ret);
420 if (oops != NULL) /* shouldn't happen, really... */
421 return oops;
422
423 *srcp = p;
424 *retp = ret;
425 return NULL;
426 }