Merge pull request #1885 from franzs/feature_proxy_protocol_for_check_smtp
[monitoring-plugins.git] / gl / getaddrinfo.c
blob6586ee58b5b73cf8b4d3a9ac08af633d75fe99dd
1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001-2002, 2004-2023 Free Software Foundation, Inc.
3 Contributed by Simon Josefsson <simon@josefsson.org>.
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.
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.
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/>. */
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)
22 #include <config.h>
24 #include <netdb.h>
26 #if HAVE_NETINET_IN_H
27 # include <netinet/in.h>
28 #endif
30 /* Get inet_ntop. */
31 #include <arpa/inet.h>
33 /* Get calloc. */
34 #include <stdlib.h>
36 /* Get memcpy, strdup. */
37 #include <string.h>
39 /* Get snprintf. */
40 #include <stdio.h>
42 #include "gettext.h"
43 #define _(String) gettext (String)
44 #define N_(String) String
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
55 #if HAVE_GETADDRINFO
57 /* Override with cdecl calling convention. */
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 # undef getaddrinfo
66 return getaddrinfo (nodename, servname, hints, res);
69 void
70 freeaddrinfo (struct addrinfo *ai)
71 # undef freeaddrinfo
73 freeaddrinfo (ai);
76 #else
78 # if defined _WIN32 && !defined __CYGWIN__
79 # define WINDOWS_NATIVE
80 # endif
82 /* gl_sockets_startup */
83 # include "sockets.h"
85 # ifdef WINDOWS_NATIVE
87 /* Don't assume that UNICODE is not defined. */
88 # undef GetModuleHandle
89 # define GetModuleHandle GetModuleHandleA
91 # if !(_WIN32_WINNT >= _WIN32_WINNT_WINXP)
93 /* Avoid warnings from gcc -Wcast-function-type. */
94 # define GetProcAddress \
95 (void *) GetProcAddress
97 typedef int (WSAAPI *getaddrinfo_func) (const char*, const char*,
98 const struct addrinfo*,
99 struct addrinfo**);
100 typedef void (WSAAPI *freeaddrinfo_func) (struct addrinfo*);
101 typedef int (WSAAPI *getnameinfo_func) (const struct sockaddr*,
102 socklen_t, char*, DWORD,
103 char*, DWORD, int);
105 static getaddrinfo_func getaddrinfo_ptr = NULL;
106 static freeaddrinfo_func freeaddrinfo_ptr = NULL;
107 static getnameinfo_func getnameinfo_ptr = NULL;
109 static int
110 use_win32_p (void)
112 static int done = 0;
113 HMODULE h;
115 if (done)
116 return getaddrinfo_ptr ? 1 : 0;
118 done = 1;
120 h = GetModuleHandle ("ws2_32.dll");
122 if (h)
124 getaddrinfo_ptr = (getaddrinfo_func) GetProcAddress (h, "getaddrinfo");
125 freeaddrinfo_ptr = (freeaddrinfo_func) GetProcAddress (h, "freeaddrinfo");
126 getnameinfo_ptr = (getnameinfo_func) GetProcAddress (h, "getnameinfo");
129 /* If either is missing, something is odd. */
130 if (!getaddrinfo_ptr || !freeaddrinfo_ptr || !getnameinfo_ptr)
132 getaddrinfo_ptr = NULL;
133 freeaddrinfo_ptr = NULL;
134 getnameinfo_ptr = NULL;
135 return 0;
138 gl_sockets_startup (SOCKETS_1_1);
140 return 1;
143 # else
145 static int
146 use_win32_p (void)
148 static int done = 0;
150 if (!done)
152 done = 1;
154 gl_sockets_startup (SOCKETS_1_1);
157 return 1;
160 # define getaddrinfo_ptr getaddrinfo
161 # define freeaddrinfo_ptr freeaddrinfo
162 # define getnameinfo_ptr getnameinfo
164 # endif
165 # endif
167 static bool
168 validate_family (int family)
170 /* FIXME: Support more families. */
171 # if HAVE_IPV4
172 if (family == PF_INET)
173 return true;
174 # endif
175 # if HAVE_IPV6
176 if (family == PF_INET6)
177 return true;
178 # endif
179 if (family == PF_UNSPEC)
180 return true;
181 return false;
184 /* Translate name of a service location and/or a service name to set of
185 socket addresses. */
187 getaddrinfo (const char *restrict nodename,
188 const char *restrict servname,
189 const struct addrinfo *restrict hints,
190 struct addrinfo **restrict res)
191 #undef getaddrinfo
193 struct addrinfo *tmp;
194 int port = 0;
195 struct hostent *he;
196 void *storage;
197 size_t size;
198 # if HAVE_IPV6
199 struct v6_pair {
200 struct addrinfo addrinfo;
201 struct sockaddr_in6 sockaddr_in6;
203 # endif
204 # if HAVE_IPV4
205 struct v4_pair {
206 struct addrinfo addrinfo;
207 struct sockaddr_in sockaddr_in;
209 # endif
211 # ifdef WINDOWS_NATIVE
212 if (use_win32_p ())
213 return getaddrinfo_ptr (nodename, servname, hints, res);
214 # endif
216 if (hints && (hints->ai_flags & ~(AI_CANONNAME|AI_PASSIVE)))
217 /* FIXME: Support more flags. */
218 return EAI_BADFLAGS;
220 if (hints && !validate_family (hints->ai_family))
221 return EAI_FAMILY;
223 if (hints &&
224 hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
225 /* FIXME: Support other socktype. */
226 return EAI_SOCKTYPE; /* FIXME: Better return code? */
228 if (!nodename)
230 if (!(hints->ai_flags & AI_PASSIVE))
231 return EAI_NONAME;
233 # ifdef HAVE_IPV6
234 nodename = (hints->ai_family == AF_INET6) ? "::" : "0.0.0.0";
235 # else
236 nodename = "0.0.0.0";
237 # endif
240 if (servname)
242 struct servent *se = NULL;
243 const char *proto =
244 (hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
246 if (hints == NULL || !(hints->ai_flags & AI_NUMERICSERV))
247 /* FIXME: Use getservbyname_r if available. */
248 se = getservbyname (servname, proto);
250 if (!se)
252 char *c;
253 if (!(*servname >= '0' && *servname <= '9'))
254 return EAI_NONAME;
255 port = strtoul (servname, &c, 10);
256 if (*c || port > 0xffff)
257 return EAI_NONAME;
258 port = htons (port);
260 else
261 port = se->s_port;
264 /* FIXME: Use gethostbyname_r if available. */
265 he = gethostbyname (nodename);
266 if (!he || he->h_addr_list[0] == NULL)
267 return EAI_NONAME;
269 switch (he->h_addrtype)
271 # if HAVE_IPV6
272 case PF_INET6:
273 size = sizeof (struct v6_pair);
274 break;
275 # endif
277 # if HAVE_IPV4
278 case PF_INET:
279 size = sizeof (struct v4_pair);
280 break;
281 # endif
283 default:
284 return EAI_NODATA;
287 storage = calloc (1, size);
288 if (!storage)
289 return EAI_MEMORY;
291 switch (he->h_addrtype)
293 # if HAVE_IPV6
294 case PF_INET6:
296 struct v6_pair *p = storage;
297 struct sockaddr_in6 *sinp = &p->sockaddr_in6;
298 tmp = &p->addrinfo;
300 if (port)
301 sinp->sin6_port = port;
303 if (he->h_length != sizeof (sinp->sin6_addr))
305 free (storage);
306 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
309 memcpy (&sinp->sin6_addr, he->h_addr_list[0], sizeof sinp->sin6_addr);
311 tmp->ai_addr = (struct sockaddr *) sinp;
312 tmp->ai_addrlen = sizeof *sinp;
314 break;
315 # endif
317 # if HAVE_IPV4
318 case PF_INET:
320 struct v4_pair *p = storage;
321 struct sockaddr_in *sinp = &p->sockaddr_in;
322 tmp = &p->addrinfo;
324 if (port)
325 sinp->sin_port = port;
327 if (he->h_length != sizeof (sinp->sin_addr))
329 free (storage);
330 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
333 memcpy (&sinp->sin_addr, he->h_addr_list[0], sizeof sinp->sin_addr);
335 tmp->ai_addr = (struct sockaddr *) sinp;
336 tmp->ai_addrlen = sizeof *sinp;
338 break;
339 # endif
341 default:
342 free (storage);
343 return EAI_NODATA;
346 if (hints && hints->ai_flags & AI_CANONNAME)
348 const char *cn;
349 if (he->h_name)
350 cn = he->h_name;
351 else
352 cn = nodename;
354 tmp->ai_canonname = strdup (cn);
355 if (!tmp->ai_canonname)
357 free (storage);
358 return EAI_MEMORY;
362 tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
363 tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
364 tmp->ai_addr->sa_family = he->h_addrtype;
365 tmp->ai_family = he->h_addrtype;
367 # ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
368 switch (he->h_addrtype)
370 # if HAVE_IPV4
371 case AF_INET:
372 tmp->ai_addr->sa_len = sizeof (struct sockaddr_in);
373 break;
374 # endif
375 # if HAVE_IPV6
376 case AF_INET6:
377 tmp->ai_addr->sa_len = sizeof (struct sockaddr_in6);
378 break;
379 # endif
381 # endif
383 /* FIXME: If more than one address, create linked list of addrinfo's. */
385 *res = tmp;
387 return 0;
390 /* Free 'addrinfo' structure AI including associated storage. */
391 void
392 freeaddrinfo (struct addrinfo *ai)
393 #undef freeaddrinfo
395 # ifdef WINDOWS_NATIVE
396 if (use_win32_p ())
398 freeaddrinfo_ptr (ai);
399 return;
401 # endif
403 while (ai)
405 struct addrinfo *cur;
407 cur = ai;
408 ai = ai->ai_next;
410 free (cur->ai_canonname);
411 free (cur);
416 getnameinfo (const struct sockaddr *restrict sa, socklen_t salen,
417 char *restrict node, socklen_t nodelen,
418 char *restrict service, socklen_t servicelen,
419 int flags)
420 #undef getnameinfo
422 # ifdef WINDOWS_NATIVE
423 if (use_win32_p ())
424 return getnameinfo_ptr (sa, salen, node, nodelen,
425 service, servicelen, flags);
426 # endif
428 /* FIXME: Support other flags. */
429 if ((node && nodelen > 0 && !(flags & NI_NUMERICHOST)) ||
430 (service && servicelen > 0 && !(flags & NI_NUMERICHOST)) ||
431 (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV)))
432 return EAI_BADFLAGS;
434 if (sa == NULL || salen < sizeof (sa->sa_family))
435 return EAI_FAMILY;
437 switch (sa->sa_family)
439 # if HAVE_IPV4
440 case AF_INET:
441 if (salen < sizeof (struct sockaddr_in))
442 return EAI_FAMILY;
443 break;
444 # endif
445 # if HAVE_IPV6
446 case AF_INET6:
447 if (salen < sizeof (struct sockaddr_in6))
448 return EAI_FAMILY;
449 break;
450 # endif
451 default:
452 return EAI_FAMILY;
455 if (node && nodelen > 0 && flags & NI_NUMERICHOST)
457 switch (sa->sa_family)
459 # if HAVE_IPV4
460 case AF_INET:
461 if (!inet_ntop (AF_INET,
462 &(((const struct sockaddr_in *) sa)->sin_addr),
463 node, nodelen))
464 return EAI_SYSTEM;
465 break;
466 # endif
468 # if HAVE_IPV6
469 case AF_INET6:
470 if (!inet_ntop (AF_INET6,
471 &(((const struct sockaddr_in6 *) sa)->sin6_addr),
472 node, nodelen))
473 return EAI_SYSTEM;
474 break;
475 # endif
477 default:
478 return EAI_FAMILY;
482 if (service && servicelen > 0 && flags & NI_NUMERICSERV)
483 switch (sa->sa_family)
485 # if HAVE_IPV4
486 case AF_INET:
487 # endif
488 # if HAVE_IPV6
489 case AF_INET6:
490 # endif
492 unsigned short int port
493 = ntohs (((const struct sockaddr_in *) sa)->sin_port);
494 if (servicelen <= snprintf (service, servicelen, "%u", port))
495 return EAI_OVERFLOW;
497 break;
500 return 0;
503 #endif