Change tokenizer to reduce number of lexer states and be
[ircu2.10.12-pk.git] / ircd / ircd_getaddrinfo.c
1 /*
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include "ircd_addrinfo.h"
31 #include <stddef.h> /* for offsetof() */
32 #include <stdlib.h> /* for free() */
33 #include <string.h> /* for memset() */
34 #include <unistd.h> /* for close() */
35 #include <arpa/inet.h> /* for inet_aton(), inet_pton() */
36 #include <errno.h> /* for errno */
37
38 #ifndef AI_MASK
39 #define AI_MASK (AI_NUMERICHOST | AI_PASSIVE)
40 #endif
41
42 /*  $Id$ */
43
44 static const char in_addrany[]  = { 0, 0, 0, 0 };
45 static const char in_loopback[] = { 127, 0, 0, 1 };
46 static const char in6_addrany[] = {
47         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
48 };
49 static const char in6_loopback[] = {
50         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
51 };
52
53 static const struct afd {
54         int a_af;
55         int a_addrlen;
56         int a_socklen;
57         int a_off;
58         const char *a_addrany;
59         const char *a_loopback;
60         int a_scoped;
61 } afdl [] = {
62 #define N_INET6 0
63 #ifdef IPV6
64         {PF_INET6, sizeof(struct in6_addr),
65          sizeof(struct sockaddr_in6),
66          offsetof(struct sockaddr_in6, sin6_addr),
67          in6_addrany, in6_loopback, 1},
68 #endif
69 #define N_INET 1
70         {PF_INET, sizeof(struct in_addr),
71          sizeof(struct sockaddr_in),
72          offsetof(struct sockaddr_in, sin_addr),
73          in_addrany, in_loopback, 0},
74         {0, 0, 0, 0, NULL, NULL, 0},
75 };
76
77 struct explore {
78         int e_af;
79         int e_socktype;
80         int e_protocol;
81         const char *e_protostr;
82         int e_wild;
83 #define WILD_AF(ex)             ((ex)->e_wild & 0x01)
84 #define WILD_SOCKTYPE(ex)       ((ex)->e_wild & 0x02)
85 #define WILD_PROTOCOL(ex)       ((ex)->e_wild & 0x04)
86 };
87
88 #define ANY 0
89
90 static const struct explore explore[] = {
91 #ifdef IPV6
92         { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
93         { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
94         { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
95 #endif
96         { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
97         { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
98         { PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
99         { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
100         { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
101         { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
102         { -1, 0, 0, NULL, 0 },
103 };
104
105 #define PTON_MAX        16
106
107 static int str_isnumber(const char *);
108 static int explore_null(const struct addrinfo *,
109         const char *, struct addrinfo **);
110 static int explore_numeric(const struct addrinfo *, const char *,
111         const char *, struct addrinfo **);
112 static struct addrinfo *get_ai(const struct addrinfo *,
113         const struct afd *, const char *);
114 static int get_portmatch(const struct addrinfo *, const char *);
115 static int get_port(struct addrinfo *, const char *, int);
116 static const struct afd *find_afd(int);
117 #if 0
118 /* We will need this should we ever want gai_strerror().
119  * Note though that GNU libc doesn't define EAI_BADHINTS. */
120 static char *ai_errlist[] = {
121         "Success",
122         "Address family for hostname not supported",    /* EAI_ADDRFAMILY */
123         "Temporary failure in name resolution",         /* EAI_AGAIN      */
124         "Invalid value for ai_flags",                   /* EAI_BADFLAGS   */
125         "Non-recoverable failure in name resolution",   /* EAI_FAIL       */
126         "ai_family not supported",                      /* EAI_FAMILY     */
127         "Memory allocation failure",                    /* EAI_MEMORY     */
128         "No address associated with hostname",          /* EAI_NODATA     */
129         "hostname nor servname provided, or not known", /* EAI_NONAME     */
130         "servname not supported for ai_socktype",       /* EAI_SERVICE    */
131         "ai_socktype not supported",                    /* EAI_SOCKTYPE   */
132         "System error returned in errno",               /* EAI_SYSTEM     */
133         "Invalid value for hints",                      /* EAI_BADHINTS   */
134         "Resolved protocol is unknown",                 /* EAI_PROTOCOL   */
135         "Unknown error",                                /* EAI_MAX        */
136 };
137 #endif
138 /* XXX macros that make external reference is BAD. */
139
140 #define GET_AI(ai, afd, addr) \
141 do { \
142         /* external reference: pai, error, and label free */ \
143         (ai) = get_ai(pai, (afd), (addr)); \
144         if ((ai) == NULL) { \
145                 error = EAI_MEMORY; \
146                 goto free; \
147         } \
148 } while (/*CONSTCOND*/0)
149
150 #define GET_PORT(ai, serv) \
151 do { \
152         /* external reference: error and label free */ \
153         error = get_port((ai), (serv), 0); \
154         if (error != 0) \
155                 goto free; \
156 } while (/*CONSTCOND*/0)
157
158 #define ERR(err) \
159 do { \
160         /* external reference: error, and label bad */ \
161         error = (err); \
162         goto bad; \
163         /*NOTREACHED*/ \
164 } while (/*CONSTCOND*/0)
165
166 #define MATCH_FAMILY(x, y, w) \
167         ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
168 #define MATCH(x, y, w) \
169         ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
170
171 #if 0
172 /* We will need this should we ever want gai_strerror() */
173 char *
174 irc_gai_strerror(int ecode)
175 {
176         if (ecode < 0 || ecode > EAI_MAX)
177                 ecode = EAI_MAX;
178         return ai_errlist[ecode];
179 }
180 #endif
181
182 void
183 irc_freeaddrinfo(struct addrinfo *ai)
184 {
185         struct addrinfo *next;
186
187         do {
188                 next = ai->ai_next;
189                 if (ai->ai_canonname)
190                         free(ai->ai_canonname);
191                 /* no need to free(ai->ai_addr) */
192                 free(ai);
193                 ai = next;
194         } while (ai);
195 }
196
197 static int
198 str_isnumber(const char *p)
199 {
200         char *ep;
201
202         if (*p == '\0')
203                 return 0;
204         ep = NULL;
205         errno = 0;
206         (void)strtoul(p, &ep, 10);
207         if (errno == 0 && ep && *ep == '\0')
208                 return 1;
209         else
210                 return 0;
211 }
212
213 int
214 irc_getaddrinfo(const char *hostname, const char *servname,
215                 const struct addrinfo *hints, struct addrinfo **res)
216 {
217   struct addrinfo sentinel;
218   struct addrinfo *cur;
219   int error = 0;
220   struct addrinfo ai;
221   struct addrinfo ai0;
222   struct addrinfo *pai;
223   const struct explore *ex;
224
225   memset(&sentinel, 0, sizeof(sentinel));
226   cur = &sentinel;
227   pai = &ai;
228   pai->ai_flags = 0;
229   pai->ai_family = PF_UNSPEC;
230   pai->ai_socktype = ANY;
231   pai->ai_protocol = ANY;
232   pai->ai_addrlen = 0;
233   pai->ai_canonname = NULL;
234   pai->ai_addr = NULL;
235   pai->ai_next = NULL;
236
237   if (hostname == NULL && servname == NULL)
238     return EAI_NONAME;
239         if (hints) {
240                 /* error check for hints */
241                 if (hints->ai_addrlen || hints->ai_canonname ||
242                     hints->ai_addr || hints->ai_next)
243                 {
244 #ifdef EAI_BADHINTS
245                         ERR(EAI_BADHINTS); /* xxx */
246 #else
247                         errno = EINVAL;
248                         ERR(EAI_SYSTEM);
249 #endif
250                 }
251                 if (hints->ai_flags & ~AI_MASK)
252                         ERR(EAI_BADFLAGS);
253                 switch (hints->ai_family) {
254                 case PF_UNSPEC:
255                 case PF_INET:
256 #ifdef IPV6
257                 case PF_INET6:
258 #endif
259                         break;
260                 default:
261                         ERR(EAI_FAMILY);
262                 }
263                 memcpy(pai, hints, sizeof(*pai));
264
265                 /*
266                  * if both socktype/protocol are specified, check if they
267                  * are meaningful combination.
268                  */
269                 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
270                         for (ex = explore; ex->e_af >= 0; ex++) {
271                                 if (pai->ai_family != ex->e_af)
272                                         continue;
273                                 if (ex->e_socktype == ANY)
274                                         continue;
275                                 if (ex->e_protocol == ANY)
276                                         continue;
277                                 if (pai->ai_socktype == ex->e_socktype &&
278                                     pai->ai_protocol != ex->e_protocol) {
279 #ifdef EAI_BADHINTS
280                                         ERR(EAI_BADHINTS); /* xxx */
281 #else
282                                         errno = EINVAL;
283                                         ERR(EAI_SYSTEM);
284 #endif
285                                 }
286                         }
287                 }
288         }
289
290         /*
291          * check for special cases.  (1) numeric servname is disallowed if
292          * socktype/protocol are left unspecified. (2) servname is disallowed
293          * for raw and other inet{,6} sockets.
294          */
295         if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
296 #ifdef IPV6
297             || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
298 #endif
299             ) {
300                 ai0 = *pai;     /* backup *pai */
301
302                 if (pai->ai_family == PF_UNSPEC) {
303 #ifdef IPV6
304                         pai->ai_family = PF_INET6;
305 #else
306                         pai->ai_family = PF_INET;
307 #endif
308                 }
309                 error = get_portmatch(pai, servname);
310                 if (error)
311                         ERR(error);
312
313                 *pai = ai0;
314         }
315
316         ai0 = *pai;
317
318         /* NULL hostname, or numeric hostname */
319         for (ex = explore; ex->e_af >= 0; ex++) {
320                 *pai = ai0;
321
322                 /* PF_UNSPEC entries are prepared for DNS queries only */
323                 if (ex->e_af == PF_UNSPEC)
324                         continue;
325
326                 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
327                         continue;
328                 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
329                         continue;
330                 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
331                         continue;
332
333                 if (pai->ai_family == PF_UNSPEC)
334                         pai->ai_family = ex->e_af;
335                 if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
336                         pai->ai_socktype = ex->e_socktype;
337                 if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
338                         pai->ai_protocol = ex->e_protocol;
339
340                 if (hostname == NULL)
341                         error = explore_null(pai, servname, &cur->ai_next);
342                 else
343                         error = explore_numeric(pai, hostname, servname, &cur->ai_next);
344
345                 if (error)
346                         goto free;
347
348                 while (cur && cur->ai_next)
349                         cur = cur->ai_next;
350         }
351
352         /*
353          * XXX
354          * If numeric representation of AF1 can be interpreted as FQDN
355          * representation of AF2, we need to think again about the code below.
356          */
357         if (sentinel.ai_next)
358                 goto good;
359
360         if (pai->ai_flags & AI_NUMERICHOST)
361                 ERR(EAI_NONAME);
362         if (hostname == NULL)
363                 ERR(EAI_NODATA);
364
365         /* XXX */
366         if (sentinel.ai_next)
367                 error = 0;
368
369         if (error)
370                 goto free;
371         if (error == 0) {
372                 if (sentinel.ai_next) {
373  good:
374                         *res = sentinel.ai_next;
375                         return 0;
376                 } else
377                         error = EAI_FAIL;
378         }
379  free:
380  bad:
381         if (sentinel.ai_next)
382                 irc_freeaddrinfo(sentinel.ai_next);
383         *res = NULL;
384         return error;
385 }
386
387 /*
388  * hostname == NULL.
389  * passive socket -> anyaddr (0.0.0.0 or ::)
390  * non-passive socket -> localhost (127.0.0.1 or ::1)
391  */
392 static int
393 explore_null(const struct addrinfo *pai, const char *servname, struct addrinfo **res)
394 {
395   int s;
396   const struct afd *afd;
397   struct addrinfo *cur;
398   struct addrinfo sentinel;
399   int error;
400
401         *res = NULL;
402         sentinel.ai_next = NULL;
403         cur = &sentinel;
404
405         /*
406          * filter out AFs that are not supported by the kernel
407          * XXX errno?
408          */
409         s = socket(pai->ai_family, SOCK_DGRAM, 0);
410         if (s < 0) {
411                 if (errno != EMFILE)
412                         return 0;
413         } else
414                 close(s);
415
416         /*
417          * if the servname does not match socktype/protocol, ignore it.
418          */
419         if (get_portmatch(pai, servname) != 0)
420                 return 0;
421
422         afd = find_afd(pai->ai_family);
423         if (afd == NULL)
424                 return 0;
425
426         if (pai->ai_flags & AI_PASSIVE) {
427                 GET_AI(cur->ai_next, afd, afd->a_addrany);
428                 GET_PORT(cur->ai_next, servname);
429         } else {
430                 GET_AI(cur->ai_next, afd, afd->a_loopback);
431                 GET_PORT(cur->ai_next, servname);
432         }
433         cur = cur->ai_next;
434
435         *res = sentinel.ai_next;
436         return 0;
437
438 free:
439         if (sentinel.ai_next)
440                 irc_freeaddrinfo(sentinel.ai_next);
441         return error;
442 }
443
444 /*
445  * numeric hostname
446  */
447 static int
448 explore_numeric(const struct addrinfo *pai, const char *hostname,
449                 const char *servname, struct addrinfo **res)
450 {
451   const struct afd *afd;
452   struct addrinfo *cur;
453   struct addrinfo sentinel;
454   int error;
455   char pton[PTON_MAX];
456
457   *res = NULL;
458   sentinel.ai_next = NULL;
459   cur = &sentinel;
460
461         /*
462          * if the servname does not match socktype/protocol, ignore it.
463          */
464         if (get_portmatch(pai, servname) != 0)
465                 return 0;
466
467         afd = find_afd(pai->ai_family);
468         if (afd == NULL)
469                 return 0;
470
471         switch (afd->a_af) {
472 #if 1 /*X/Open spec*/
473         case AF_INET:
474                 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
475                         if (pai->ai_family == afd->a_af ||
476                             pai->ai_family == PF_UNSPEC /*?*/) {
477                                 GET_AI(cur->ai_next, afd, pton);
478                                 GET_PORT(cur->ai_next, servname);
479                                 while (cur && cur->ai_next)
480                                         cur = cur->ai_next;
481                         } else
482                                 ERR(EAI_FAMILY);        /*xxx*/
483                 }
484                 break;
485 #endif
486         default:
487                 if (inet_pton(afd->a_af, hostname, pton) == 1) {
488                         if (pai->ai_family == afd->a_af ||
489                             pai->ai_family == PF_UNSPEC /*?*/) {
490                                 GET_AI(cur->ai_next, afd, pton);
491                                 GET_PORT(cur->ai_next, servname);
492                                 while (cur && cur->ai_next)
493                                         cur = cur->ai_next;
494                         } else
495                                 ERR(EAI_FAMILY);        /* XXX */
496                 }
497                 break;
498         }
499
500         *res = sentinel.ai_next;
501         return 0;
502
503 free:
504 bad:
505         if (sentinel.ai_next)
506                 irc_freeaddrinfo(sentinel.ai_next);
507         return error;
508 }
509
510 static struct addrinfo *
511 get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
512 {
513   char *p;
514   struct addrinfo *ai;
515         
516     ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
517                 + (afd->a_socklen));
518         if (ai == NULL)
519                 return NULL;
520
521         memcpy(ai, pai, sizeof(struct addrinfo));
522         ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
523         memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
524         ai->ai_addrlen = afd->a_socklen;
525         ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
526         p = (char *)(void *)(ai->ai_addr);
527         memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
528         return ai;
529 }
530
531 static int
532 get_portmatch(const struct addrinfo *ai, const char *servname)
533 {
534   /* get_port does not touch first argument. when matchonly == 1. */
535   /* LINTED const cast */
536   return(get_port((struct addrinfo *)ai, servname, 1));
537 }
538
539 static int
540 get_port(struct addrinfo *ai, const char *servname, int matchonly)
541 {
542   const char *proto;
543   struct servent *sp;
544   int port;
545   int allownumeric;
546
547   if (servname == NULL)
548     return 0;
549         switch (ai->ai_family) {
550         case AF_INET:
551 #ifdef AF_INET6
552         case AF_INET6:
553 #endif
554                 break;
555         default:
556                 return 0;
557         }
558
559         switch (ai->ai_socktype) {
560         case SOCK_RAW:
561                 return EAI_SERVICE;
562         case SOCK_DGRAM:
563         case SOCK_STREAM:
564                 allownumeric = 1;
565                 break;
566         case ANY:
567                 allownumeric = 0;
568                 break;
569         default:
570                 return EAI_SOCKTYPE;
571         }
572
573         if (str_isnumber(servname)) {
574                 if (!allownumeric)
575                         return EAI_SERVICE;
576                 port = atoi(servname);
577                 if (port < 0 || port > 65535)
578                         return EAI_SERVICE;
579                 port = htons(port);
580         } else {
581                 switch (ai->ai_socktype) {
582                 case SOCK_DGRAM:
583                         proto = "udp";
584                         break;
585                 case SOCK_STREAM:
586                         proto = "tcp";
587                         break;
588                 default:
589                         proto = NULL;
590                         break;
591                 }
592
593                 if ((sp = getservbyname(servname, proto)) == NULL)
594                         return EAI_SERVICE;
595                 port = sp->s_port;
596         }
597
598         if (!matchonly) {
599                 switch (ai->ai_family) {
600                 case AF_INET:
601                         ((struct sockaddr_in *)(void *)
602                             ai->ai_addr)->sin_port = port;
603                         break;
604 #ifdef IPV6
605                 case AF_INET6:
606                         ((struct sockaddr_in6 *)(void *)
607                             ai->ai_addr)->sin6_port = port;
608                         break;
609 #endif
610                 }
611         }
612
613         return 0;
614 }
615
616 static const struct afd *
617 find_afd(int af)
618 {
619   const struct afd *afd;
620
621   if (af == PF_UNSPEC)
622     return(NULL);
623
624   for (afd = afdl; afd->a_af; afd++)
625   {
626     if (afd->a_af == af)
627       return(afd);
628   }
629
630   return(NULL);
631 }