Debian Codegraph @

lib/getaddrinfo.c

raw
1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001-2002, 2004-2026 Free Software Foundation, Inc.
3 Contributed by Simon Josefsson <[email protected]>.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
19 optimizes away the sa == NULL test below. */
20 #define _GL_ARG_NONNULL(params)
21
22 #include <config.h>
23
24 #include <netdb.h>
25
26 #if HAVE_NETINET_IN_H
27 # include <netinet/in.h>
28 #endif
29
30 /* Get inet_ntop. */
31 #include <arpa/inet.h>
32
33 /* Get calloc. */
34 #include <stdlib.h>
35
36 /* Get memcpy, strdup. */
37 #include <string.h>
38
39 /* Get snprintf. */
40 #include <stdio.h>
41
42 #include "gettext.h"
43 #define _(msgid) dgettext (GNULIB_TEXT_DOMAIN, msgid)
44 #define N_(msgid) msgid
45
46 /* BeOS has AF_INET, but not PF_INET. */
47 #ifndef PF_INET
48 # define PF_INET AF_INET
49 #endif
50 /* BeOS also lacks PF_UNSPEC. */
51 #ifndef PF_UNSPEC
52 # define PF_UNSPEC 0
53 #endif
54
55 #if defined __sun || !HAVE_GETADDRINFO
56
57 static bool
58 is_numeric_host (const char *host, int family)
59 {
60 # if HAVE_IPV4
61 if (family == PF_INET || family == PF_UNSPEC)
62 {
63 /* glibc supports IPv4 addresses in numbers-and-dots notation, that is,
64 also hexadecimal and octal number formats and formats that don't
65 require all four bytes to be explicitly written, via inet_aton().
66 But POSIX doesn't require support for these legacy formats. Therefore
67 we are free to use inet_pton() instead of inet_aton(). */
68 struct in_addr addr;
69 if (inet_pton (AF_INET, host, &addr))
70 return true;
71 }
72 # endif
73 # if HAVE_IPV6
74 if (family == PF_INET6 || family == PF_UNSPEC)
75 {
76 struct in6_addr addr;
77 if (inet_pton (AF_INET6, host, &addr))
78 return true;
79 }
80 # endif
81 return false;
82 }
83
84 #endif
85
86 #if HAVE_GETADDRINFO
87
88 /* Override with cdecl calling convention and Windows and Solaris 10 fixes. */
89
90 int
91 getaddrinfo (const char *restrict nodename,
92 const char *restrict servname,
93 const struct addrinfo *restrict hints,
94 struct addrinfo **restrict res)
95 # undef getaddrinfo
96 {
97 /* Workaround for native Windows. */
98 if (hints && (hints->ai_flags & AI_NUMERICSERV) != 0
99 && servname && !(*servname >= '0' && *servname <= '9'))
100 return EAI_NONAME;
101
102 # ifdef __sun
103 /* Workaround for Solaris 10. */
104 if (hints && (hints->ai_flags & AI_NUMERICHOST)
105 && nodename && !is_numeric_host (nodename, hints->ai_family))
106 return EAI_NONAME;
107 # endif
108
109 return getaddrinfo (nodename, servname, hints, res);
110 }
111
112 void
113 freeaddrinfo (struct addrinfo *ai)
114 # undef freeaddrinfo
115 {
116 freeaddrinfo (ai);
117 }
118
119 #else
120
121 # if defined _WIN32 && !defined __CYGWIN__
122 # define WINDOWS_NATIVE
123 # endif
124
125 /* gl_sockets_startup */
126 # include "sockets.h"
127
128 # ifdef WINDOWS_NATIVE
129
130 /* Don't assume that UNICODE is not defined. */
131 # undef GetModuleHandle
132 # define GetModuleHandle GetModuleHandleA
133
134 # if !(_WIN32_WINNT >= _WIN32_WINNT_WINXP)
135
136 /* Avoid warnings from gcc -Wcast-function-type. */
137 # define GetProcAddress \
138 (void *) GetProcAddress
139
140 typedef int (WSAAPI *getaddrinfo_func) (const char*, const char*,
141 const struct addrinfo*,
142 struct addrinfo**);
143 typedef void (WSAAPI *freeaddrinfo_func) (struct addrinfo*);
144 typedef int (WSAAPI *getnameinfo_func) (const struct sockaddr*,
145 socklen_t, char*, DWORD,
146 char*, DWORD, int);
147
148 static getaddrinfo_func getaddrinfo_ptr = NULL;
149 static freeaddrinfo_func freeaddrinfo_ptr = NULL;
150 static getnameinfo_func getnameinfo_ptr = NULL;
151
152 static int
153 use_win32_p (void)
154 {
155 static int done = 0;
156
157 if (done)
158 return getaddrinfo_ptr ? 1 : 0;
159
160 done = 1;
161
162 HMODULE h = GetModuleHandle ("ws2_32.dll");
163
164 if (h)
165 {
166 getaddrinfo_ptr = (getaddrinfo_func) GetProcAddress (h, "getaddrinfo");
167 freeaddrinfo_ptr = (freeaddrinfo_func) GetProcAddress (h, "freeaddrinfo");
168 getnameinfo_ptr = (getnameinfo_func) GetProcAddress (h, "getnameinfo");
169 }
170
171 /* If either is missing, something is odd. */
172 if (!getaddrinfo_ptr || !freeaddrinfo_ptr || !getnameinfo_ptr)
173 {
174 getaddrinfo_ptr = NULL;
175 freeaddrinfo_ptr = NULL;
176 getnameinfo_ptr = NULL;
177 return 0;
178 }
179
180 gl_sockets_startup (SOCKETS_1_1);
181
182 return 1;
183 }
184
185 # else
186
187 static int
188 use_win32_p (void)
189 {
190 static int done = 0;
191
192 if (!done)
193 {
194 done = 1;
195
196 gl_sockets_startup (SOCKETS_1_1);
197 }
198
199 return 1;
200 }
201
202 # define getaddrinfo_ptr getaddrinfo
203 # define freeaddrinfo_ptr freeaddrinfo
204 # define getnameinfo_ptr getnameinfo
205
206 # endif
207 # endif
208
209 static bool
210 validate_family (int family)
211 {
212 /* FIXME: Support more families. */
213 # if HAVE_IPV4
214 if (family == PF_INET)
215 return true;
216 # endif
217 # if HAVE_IPV6
218 if (family == PF_INET6)
219 return true;
220 # endif
221 if (family == PF_UNSPEC)
222 return true;
223 return false;
224 }
225
226 /* Translate name of a service location and/or a service name to set of
227 socket addresses. */
228 int
229 getaddrinfo (const char *restrict nodename,
230 const char *restrict servname,
231 const struct addrinfo *restrict hints,
232 struct addrinfo **restrict res)
233 #undef getaddrinfo
234 {
235 # if HAVE_IPV6
236 struct v6_pair {
237 struct addrinfo addrinfo;
238 struct sockaddr_in6 sockaddr_in6;
239 };
240 # endif
241 # if HAVE_IPV4
242 struct v4_pair {
243 struct addrinfo addrinfo;
244 struct sockaddr_in sockaddr_in;
245 };
246 # endif
247
248 # ifdef WINDOWS_NATIVE
249 if (use_win32_p ())
250 {
251 if (hints && (hints->ai_flags & AI_NUMERICSERV) != 0
252 && servname && !(*servname >= '0' && *servname <= '9'))
253 return EAI_NONAME;
254 return getaddrinfo_ptr (nodename, servname, hints, res);
255 }
256 # endif
257
258 if (hints
259 && (hints->ai_flags
260 & ~(AI_CANONNAME | AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV)))
261 /* FIXME: Support more flags. */
262 return EAI_BADFLAGS;
263
264 if (hints && !validate_family (hints->ai_family))
265 return EAI_FAMILY;
266
267 if (hints &&
268 hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
269 /* FIXME: Support other socktype. */
270 return EAI_SOCKTYPE; /* FIXME: Better return code? */
271
272 if (nodename != NULL)
273 {
274 if (hints && (hints->ai_flags & AI_NUMERICHOST) != 0
275 && !is_numeric_host (nodename, hints->ai_family))
276 return EAI_NONAME;
277 }
278 else
279 {
280 if (!(hints->ai_flags & AI_PASSIVE))
281 return EAI_NONAME;
282
283 # if HAVE_IPV6
284 nodename = (hints->ai_family == AF_INET6) ? "::" : "0.0.0.0";
285 # else
286 nodename = "0.0.0.0";
287 # endif
288 }
289
290 int port = 0;
291 if (servname)
292 {
293 struct servent *se = NULL;
294 const char *proto =
295 (hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
296
297 if (hints == NULL || !(hints->ai_flags & AI_NUMERICSERV))
298 /* FIXME: Use getservbyname_r if available. */
299 se = getservbyname (servname, proto);
300
301 if (!se)
302 {
303 char *c;
304 if (!(*servname >= '0' && *servname <= '9'))
305 return EAI_NONAME;
306 port = strtoul (servname, &c, 10);
307 if (*c || port > 0xffff)
308 return EAI_NONAME;
309 port = htons (port);
310 }
311 else
312 port = se->s_port;
313 }
314
315 /* FIXME: Use gethostbyname_r if available. */
316 struct hostent *he = gethostbyname (nodename);
317 if (!he || he->h_addr_list[0] == NULL)
318 return EAI_NONAME;
319
320 size_t size;
321 switch (he->h_addrtype)
322 {
323 # if HAVE_IPV6
324 case PF_INET6:
325 size = sizeof (struct v6_pair);
326 break;
327 # endif
328
329 # if HAVE_IPV4
330 case PF_INET:
331 size = sizeof (struct v4_pair);
332 break;
333 # endif
334
335 default:
336 return EAI_NODATA;
337 }
338
339 void *storage = calloc (1, size);
340 if (!storage)
341 return EAI_MEMORY;
342
343 struct addrinfo *tmp;
344 switch (he->h_addrtype)
345 {
346 # if HAVE_IPV6
347 case PF_INET6:
348 {
349 struct v6_pair *p = storage;
350 struct sockaddr_in6 *sinp = &p->sockaddr_in6;
351 tmp = &p->addrinfo;
352
353 if (port)
354 sinp->sin6_port = port;
355
356 if (he->h_length != sizeof (sinp->sin6_addr))
357 {
358 free (storage);
359 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
360 }
361
362 memcpy (&sinp->sin6_addr, he->h_addr_list[0], sizeof sinp->sin6_addr);
363
364 tmp->ai_addr = (struct sockaddr *) sinp;
365 tmp->ai_addrlen = sizeof *sinp;
366 }
367 break;
368 # endif
369
370 # if HAVE_IPV4
371 case PF_INET:
372 {
373 struct v4_pair *p = storage;
374 struct sockaddr_in *sinp = &p->sockaddr_in;
375 tmp = &p->addrinfo;
376
377 if (port)
378 sinp->sin_port = port;
379
380 if (he->h_length != sizeof (sinp->sin_addr))
381 {
382 free (storage);
383 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
384 }
385
386 memcpy (&sinp->sin_addr, he->h_addr_list[0], sizeof sinp->sin_addr);
387
388 tmp->ai_addr = (struct sockaddr *) sinp;
389 tmp->ai_addrlen = sizeof *sinp;
390 }
391 break;
392 # endif
393
394 default:
395 free (storage);
396 return EAI_NODATA;
397 }
398
399 if (hints && hints->ai_flags & AI_CANONNAME)
400 {
401 const char *cn;
402 if (he->h_name)
403 cn = he->h_name;
404 else
405 cn = nodename;
406
407 tmp->ai_canonname = strdup (cn);
408 if (!tmp->ai_canonname)
409 {
410 free (storage);
411 return EAI_MEMORY;
412 }
413 }
414
415 tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
416 tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
417 tmp->ai_addr->sa_family = he->h_addrtype;
418 tmp->ai_family = he->h_addrtype;
419
420 # ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
421 switch (he->h_addrtype)
422 {
423 # if HAVE_IPV4
424 case AF_INET:
425 tmp->ai_addr->sa_len = sizeof (struct sockaddr_in);
426 break;
427 # endif
428 # if HAVE_IPV6
429 case AF_INET6:
430 tmp->ai_addr->sa_len = sizeof (struct sockaddr_in6);
431 break;
432 # endif
433 }
434 # endif
435
436 /* FIXME: If more than one address, create linked list of addrinfo's. */
437
438 *res = tmp;
439
440 return 0;
441 }
442
443 /* Free 'addrinfo' structure AI including associated storage. */
444 void
445 freeaddrinfo (struct addrinfo *ai)
446 #undef freeaddrinfo
447 {
448 # ifdef WINDOWS_NATIVE
449 if (use_win32_p ())
450 {
451 freeaddrinfo_ptr (ai);
452 return;
453 }
454 # endif
455
456 while (ai)
457 {
458 struct addrinfo *cur = ai;
459 ai = ai->ai_next;
460
461 free (cur->ai_canonname);
462 free (cur);
463 }
464 }
465
466 int
467 getnameinfo (const struct sockaddr *restrict sa, socklen_t salen,
468 char *restrict node, socklen_t nodelen,
469 char *restrict service, socklen_t servicelen,
470 int flags)
471 #undef getnameinfo
472 {
473 # ifdef WINDOWS_NATIVE
474 if (use_win32_p ())
475 return getnameinfo_ptr (sa, salen, node, nodelen,
476 service, servicelen, flags);
477 # endif
478
479 /* FIXME: Support other flags. */
480 if ((node && nodelen > 0 && !(flags & NI_NUMERICHOST)) ||
481 (service && servicelen > 0 && !(flags & NI_NUMERICHOST)) ||
482 (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV)))
483 return EAI_BADFLAGS;
484
485 if (sa == NULL || salen < sizeof (sa->sa_family))
486 return EAI_FAMILY;
487
488 switch (sa->sa_family)
489 {
490 # if HAVE_IPV4
491 case AF_INET:
492 if (salen < sizeof (struct sockaddr_in))
493 return EAI_FAMILY;
494 break;
495 # endif
496 # if HAVE_IPV6
497 case AF_INET6:
498 if (salen < sizeof (struct sockaddr_in6))
499 return EAI_FAMILY;
500 break;
501 # endif
502 default:
503 return EAI_FAMILY;
504 }
505
506 if (node && nodelen > 0 && flags & NI_NUMERICHOST)
507 {
508 switch (sa->sa_family)
509 {
510 # if HAVE_IPV4
511 case AF_INET:
512 if (!inet_ntop (AF_INET,
513 &(((const struct sockaddr_in *) sa)->sin_addr),
514 node, nodelen))
515 return EAI_SYSTEM;
516 break;
517 # endif
518
519 # if HAVE_IPV6
520 case AF_INET6:
521 if (!inet_ntop (AF_INET6,
522 &(((const struct sockaddr_in6 *) sa)->sin6_addr),
523 node, nodelen))
524 return EAI_SYSTEM;
525 break;
526 # endif
527
528 default:
529 return EAI_FAMILY;
530 }
531 }
532
533 if (service && servicelen > 0 && flags & NI_NUMERICSERV)
534 switch (sa->sa_family)
535 {
536 # if HAVE_IPV4
537 case AF_INET:
538 # endif
539 # if HAVE_IPV6
540 case AF_INET6:
541 # endif
542 {
543 unsigned short int port
544 = ntohs (((const struct sockaddr_in *) sa)->sin_port);
545 if (servicelen <= snprintf (service, servicelen, "%u", port))
546 return EAI_OVERFLOW;
547 }
548 break;
549 }
550
551 return 0;
552 }
553
554 #endif