getaddrinfo.c: Don't fail when SOCK_STREAM or SOCK_DGRAM are
[gnulib.git] / lib / getaddrinfo.c
1 /* Get address information (partial implementation).
2    Copyright (C) 1997, 2001, 2002, 2004, 2005 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 &&
78       hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
79     /* FIXME: Support other socktype. */
80     return EAI_SOCKTYPE; /* FIXME: Better return code? */
81
82   if (!nodename)
83     /* FIXME: Support server bind mode. */
84     return EAI_NONAME;
85
86   if (servname)
87     {
88       const char *proto =
89         (hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
90
91       /* FIXME: Use getservbyname_r if available. */
92       se = getservbyname (servname, proto);
93
94       if (!se)
95         return EAI_SERVICE;
96     }
97
98   /* FIXME: Use gethostbyname_r if available. */
99   he = gethostbyname (nodename);
100   if (!he || he->h_addr_list[0] == NULL)
101     return EAI_NONAME;
102
103   switch (he->h_addrtype)
104     {
105 #if HAVE_IPV6
106     case PF_INET6:
107       sinlen = sizeof (struct sockaddr_in6);
108       break;
109 #endif
110
111 #if HAVE_IPV4
112     case PF_INET:
113       sinlen = sizeof (struct sockaddr_in);
114       break;
115 #endif
116
117     default:
118       return EAI_NODATA;
119     }
120
121   tmp = calloc (1, sizeof (*tmp) + sinlen);
122   if (!tmp)
123     return EAI_MEMORY;
124
125   switch (he->h_addrtype)
126     {
127 #if HAVE_IPV6
128     case PF_INET6:
129       {
130         struct sockaddr_in6 *sinp = (void *) tmp + sizeof (*tmp);
131
132         if (se)
133           sinp->sin6_port = se->s_port;
134
135         if (he->h_length != sizeof (sinp->sin6_addr))
136           return EAI_SYSTEM; /* FIXME: Better return code?  Set errno? */
137
138         memcpy (&sinp->sin6_addr, he->h_addr_list[0], he->h_length);
139
140         tmp->ai_addr = (struct sockaddr *) sinp;
141         tmp->ai_addrlen = sinlen;
142       }
143       break;
144 #endif
145
146 #if HAVE_IPV4
147     case PF_INET:
148       {
149         struct sockaddr_in *sinp = (void *) tmp + sizeof (*tmp);
150
151         if (se)
152           sinp->sin_port = se->s_port;
153
154         if (he->h_length != sizeof (sinp->sin_addr))
155           return EAI_SYSTEM; /* FIXME: Better return code?  Set errno? */
156
157         memcpy (&sinp->sin_addr, he->h_addr_list[0], he->h_length);
158
159         tmp->ai_addr = (struct sockaddr *) sinp;
160         tmp->ai_addrlen = sinlen;
161       }
162       break;
163 #endif
164
165     default:
166       free (tmp);
167       return EAI_NODATA;
168     }
169
170   tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
171   tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
172   tmp->ai_addr->sa_family = he->h_addrtype;
173
174   /* FIXME: If more than one address, create linked list of addrinfo's. */
175
176   *res = tmp;
177
178   return 0;
179 }
180
181 /* Free `addrinfo' structure AI including associated storage.  */
182 void
183 freeaddrinfo (struct addrinfo *ai)
184 {
185   while (ai)
186     {
187       struct addrinfo *cur;
188
189       cur = ai;
190       ai = ai->ai_next;
191       free (cur);
192     }
193 }