(__argp_help): Create a fake struct argp_state and
[gnulib.git] / lib / getaddrinfo.c
1 /* Get address information (partial implementation).
2    Copyright (C) 1997, 2001, 2002, 2004 Free Software Foundation, Inc.
3    Contributed by Simon Josefsson <simon@josefsson.org>.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 /* Get calloc. */
24 #include <stdlib.h>
25
26 /* Get memcpy. */
27 #include <string.h>
28
29 /* Get struct hostent. */
30 #include <netdb.h>
31
32 #include <stdbool.h>
33
34 #include "gettext.h"
35 #define _(String) gettext (String)
36 #define N_(String) String
37
38 #include "getaddrinfo.h"
39
40 static inline bool
41 validate_family (int family)
42 {
43   /* FIXME: Support more families. */
44 #if HAVE_IPV4
45      if (family == PF_INET)
46        return true;
47 #endif
48 #if HAVE_IPV6
49      if (family == PF_INET6)
50        return true;
51 #endif
52      if (family == PF_UNSPEC)
53        return true;
54      return false;
55 }
56
57 /* Translate name of a service location and/or a service name to set of
58    socket addresses. */
59 int
60 getaddrinfo (const char *restrict nodename,
61              const char *restrict servname,
62              const struct addrinfo *restrict hints,
63              struct addrinfo **restrict res)
64 {
65   struct addrinfo *tmp;
66   struct servent *se;
67   struct hostent *he;
68   size_t sinlen;
69
70   if (hints && hints->ai_flags)
71     /* FIXME: Support more flags. */
72     return EAI_BADFLAGS;
73
74   if (hints && !validate_family (hints->ai_family))
75     return EAI_FAMILY;
76
77   if (hints && hints->ai_socktype)
78     /* FIXME: Support more socket types. */
79     return EAI_SOCKTYPE;
80
81   if (hints &&
82       hints->ai_protocol != SOCK_STREAM && hints->ai_protocol != SOCK_DGRAM)
83     /* FIXME: Support other protocols. */
84     return EAI_SERVICE;         /* FIXME: Better return code? */
85
86   if (!nodename)
87     /* FIXME: Support server bind mode. */
88     return EAI_NONAME;
89
90   if (servname)
91     {
92       const char *proto =
93         (hints && hints->ai_protocol == SOCK_DGRAM) ? "udp" : "tcp";
94
95       /* FIXME: Use getservbyname_r if available. */
96       se = getservbyname (servname, proto);
97
98       if (!se)
99         return EAI_SERVICE;
100     }
101
102   /* FIXME: Use gethostbyname_r if available. */
103   he = gethostbyname (nodename);
104   if (!he || he->h_addr_list[0] == NULL)
105     return EAI_NONAME;
106
107   switch (he->h_addrtype)
108     {
109 #if HAVE_IPV6
110     case PF_INET6:
111       sinlen = sizeof (struct sockaddr_in6);
112       break;
113 #endif
114
115 #if HAVE_IPV4
116     case PF_INET:
117       sinlen = sizeof (struct sockaddr_in);
118       break;
119 #endif
120
121     default:
122       return EAI_NODATA;
123     }
124
125   tmp = calloc (1, sizeof (*tmp) + sinlen);
126   if (!tmp)
127     return EAI_MEMORY;
128
129   switch (he->h_addrtype)
130     {
131 #if HAVE_IPV6
132     case PF_INET6:
133       {
134         struct sockaddr_in6 *sinp = (void *) tmp + sizeof (*tmp);
135
136         if (se)
137           sinp->sin6_port = se->s_port;
138
139         if (he->h_length != sizeof (sinp->sin6_addr))
140           return EAI_SYSTEM; /* FIXME: Better return code?  Set errno? */
141
142         memcpy (&sinp->sin6_addr, he->h_addr_list[0], he->h_length);
143
144         tmp->ai_addr = (struct sockaddr *) sinp;
145         tmp->ai_addrlen = sinlen;
146       }
147       break;
148 #endif
149
150 #if HAVE_IPV4
151     case PF_INET:
152       {
153         struct sockaddr_in *sinp = (void *) tmp + sizeof (*tmp);
154
155         if (se)
156           sinp->sin_port = se->s_port;
157
158         if (he->h_length != sizeof (sinp->sin_addr))
159           return EAI_SYSTEM; /* FIXME: Better return code?  Set errno? */
160
161         memcpy (&sinp->sin_addr, he->h_addr_list[0], he->h_length);
162
163         tmp->ai_addr = (struct sockaddr *) sinp;
164         tmp->ai_addrlen = sinlen;
165       }
166       break;
167 #endif
168
169     default:
170       free (tmp);
171       return EAI_NODATA;
172     }
173
174   tmp->ai_addr->sa_family = he->h_addrtype;
175
176   /* FIXME: If more than one address, create linked list of addrinfo's. */
177
178   *res = tmp;
179
180   return 0;
181 }
182
183 /* Free `addrinfo' structure AI including associated storage.  */
184 void
185 freeaddrinfo (struct addrinfo *ai)
186 {
187   while (ai)
188     {
189       struct addrinfo *cur;
190
191       cur = ai;
192       ai = ai->ai_next;
193       free (cur);
194     }
195 }