Updated with more tests re: multiple status codes (Sven Nierlein)
[monitoring-plugins.git] / gl / getaddrinfo.c
blob04f0ac2e4d79b0a6c4bcd65e78e03d074415e20d
1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001, 2002, 2004, 2005, 2006, 2007 Free Software
3 Foundation, Inc.
4 Contributed by Simon Josefsson <simon@josefsson.org>.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 #include <config.h>
22 #include "getaddrinfo.h"
24 #if HAVE_NETINET_IN_H
25 # include <netinet/in.h>
26 #endif
28 /* Get calloc. */
29 #include <stdlib.h>
31 /* Get memcpy, strdup. */
32 #include <string.h>
34 /* Get snprintf. */
35 #include <stdio.h>
37 #include <stdbool.h>
39 #include "gettext.h"
40 #define _(String) gettext (String)
41 #define N_(String) String
43 #include "inet_ntop.h"
45 /* BeOS has AF_INET, but not PF_INET. */
46 #ifndef PF_INET
47 # define PF_INET AF_INET
48 #endif
49 /* BeOS also lacks PF_UNSPEC. */
50 #ifndef PF_UNSPEC
51 # define PF_UNSPEC 0
52 #endif
54 #if defined _WIN32 || defined __WIN32__
55 # define WIN32_NATIVE
56 #endif
58 #ifdef WIN32_NATIVE
59 typedef int (WSAAPI *getaddrinfo_func) (const char*, const char*,
60 const struct addrinfo*,
61 struct addrinfo**);
62 typedef void (WSAAPI *freeaddrinfo_func) (struct addrinfo*);
63 typedef int (WSAAPI *getnameinfo_func) (const struct sockaddr*,
64 socklen_t, char*, DWORD,
65 char*, DWORD, int);
67 static getaddrinfo_func getaddrinfo_ptr = NULL;
68 static freeaddrinfo_func freeaddrinfo_ptr = NULL;
69 static getnameinfo_func getnameinfo_ptr = NULL;
71 static int
72 use_win32_p (void)
74 static int done = 0;
75 HMODULE h;
77 if (done)
78 return getaddrinfo_ptr ? 1 : 0;
80 done = 1;
82 h = GetModuleHandle ("ws2_32.dll");
84 if (h)
86 getaddrinfo_ptr = (getaddrinfo_func) GetProcAddress (h, "getaddrinfo");
87 freeaddrinfo_ptr = (freeaddrinfo_func) GetProcAddress (h, "freeaddrinfo");
88 getnameinfo_ptr = (getnameinfo_func) GetProcAddress (h, "getnameinfo");
91 /* If either is missing, something is odd. */
92 if (!getaddrinfo_ptr || !freeaddrinfo_ptr || !getnameinfo_ptr)
94 getaddrinfo_ptr = NULL;
95 freeaddrinfo_ptr = NULL;
96 getnameinfo_ptr = NULL;
97 return 0;
100 return 1;
102 #endif
104 static inline bool
105 validate_family (int family)
107 /* FIXME: Support more families. */
108 #if HAVE_IPV4
109 if (family == PF_INET)
110 return true;
111 #endif
112 #if HAVE_IPV6
113 if (family == PF_INET6)
114 return true;
115 #endif
116 if (family == PF_UNSPEC)
117 return true;
118 return false;
121 /* Translate name of a service location and/or a service name to set of
122 socket addresses. */
124 getaddrinfo (const char *restrict nodename,
125 const char *restrict servname,
126 const struct addrinfo *restrict hints,
127 struct addrinfo **restrict res)
129 struct addrinfo *tmp;
130 int port = 0;
131 struct hostent *he;
132 void *storage;
133 size_t size;
134 #if HAVE_IPV6
135 struct v6_pair {
136 struct addrinfo addrinfo;
137 struct sockaddr_in6 sockaddr_in6;
139 #endif
140 #if HAVE_IPV4
141 struct v4_pair {
142 struct addrinfo addrinfo;
143 struct sockaddr_in sockaddr_in;
145 #endif
147 #ifdef WIN32_NATIVE
148 if (use_win32_p ())
149 return getaddrinfo_ptr (nodename, servname, hints, res);
150 #endif
152 if (hints && (hints->ai_flags & ~(AI_CANONNAME|AI_PASSIVE)))
153 /* FIXME: Support more flags. */
154 return EAI_BADFLAGS;
156 if (hints && !validate_family (hints->ai_family))
157 return EAI_FAMILY;
159 if (hints &&
160 hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
161 /* FIXME: Support other socktype. */
162 return EAI_SOCKTYPE; /* FIXME: Better return code? */
164 if (!nodename)
166 if (!(hints->ai_flags & AI_PASSIVE))
167 return EAI_NONAME;
169 #ifdef HAVE_IPV6
170 nodename = (hints->ai_family == AF_INET6) ? "::" : "0.0.0.0";
171 #else
172 nodename = "0.0.0.0";
173 #endif
176 if (servname)
178 struct servent *se = NULL;
179 const char *proto =
180 (hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
182 if (hints == NULL || !(hints->ai_flags & AI_NUMERICSERV))
183 /* FIXME: Use getservbyname_r if available. */
184 se = getservbyname (servname, proto);
186 if (!se)
188 char *c;
189 if (!(*servname >= '0' && *servname <= '9'))
190 return EAI_NONAME;
191 port = strtoul (servname, &c, 10);
192 if (*c || port > 0xffff)
193 return EAI_NONAME;
194 port = htons (port);
196 else
197 port = se->s_port;
200 /* FIXME: Use gethostbyname_r if available. */
201 he = gethostbyname (nodename);
202 if (!he || he->h_addr_list[0] == NULL)
203 return EAI_NONAME;
205 switch (he->h_addrtype)
207 #if HAVE_IPV6
208 case PF_INET6:
209 size = sizeof (struct v6_pair);
210 break;
211 #endif
213 #if HAVE_IPV4
214 case PF_INET:
215 size = sizeof (struct v4_pair);
216 break;
217 #endif
219 default:
220 return EAI_NODATA;
223 storage = calloc (1, size);
224 if (!storage)
225 return EAI_MEMORY;
227 switch (he->h_addrtype)
229 #if HAVE_IPV6
230 case PF_INET6:
232 struct v6_pair *p = storage;
233 struct sockaddr_in6 *sinp = &p->sockaddr_in6;
234 tmp = &p->addrinfo;
236 if (port)
237 sinp->sin6_port = port;
239 if (he->h_length != sizeof (sinp->sin6_addr))
241 free (storage);
242 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
245 memcpy (&sinp->sin6_addr, he->h_addr_list[0], sizeof sinp->sin6_addr);
247 tmp->ai_addr = (struct sockaddr *) sinp;
248 tmp->ai_addrlen = sizeof *sinp;
250 break;
251 #endif
253 #if HAVE_IPV4
254 case PF_INET:
256 struct v4_pair *p = storage;
257 struct sockaddr_in *sinp = &p->sockaddr_in;
258 tmp = &p->addrinfo;
260 if (port)
261 sinp->sin_port = port;
263 if (he->h_length != sizeof (sinp->sin_addr))
265 free (storage);
266 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
269 memcpy (&sinp->sin_addr, he->h_addr_list[0], sizeof sinp->sin_addr);
271 tmp->ai_addr = (struct sockaddr *) sinp;
272 tmp->ai_addrlen = sizeof *sinp;
274 break;
275 #endif
277 default:
278 free (storage);
279 return EAI_NODATA;
282 if (hints && hints->ai_flags & AI_CANONNAME)
284 const char *cn;
285 if (he->h_name)
286 cn = he->h_name;
287 else
288 cn = nodename;
290 tmp->ai_canonname = strdup (cn);
291 if (!tmp->ai_canonname)
293 free (storage);
294 return EAI_MEMORY;
298 tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
299 tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
300 tmp->ai_addr->sa_family = he->h_addrtype;
301 tmp->ai_family = he->h_addrtype;
303 /* FIXME: If more than one address, create linked list of addrinfo's. */
305 *res = tmp;
307 return 0;
310 /* Free `addrinfo' structure AI including associated storage. */
311 void
312 freeaddrinfo (struct addrinfo *ai)
314 #ifdef WIN32_NATIVE
315 if (use_win32_p ())
317 freeaddrinfo_ptr (ai);
318 return;
320 #endif
322 while (ai)
324 struct addrinfo *cur;
326 cur = ai;
327 ai = ai->ai_next;
329 if (cur->ai_canonname) free (cur->ai_canonname);
330 free (cur);
334 int getnameinfo(const struct sockaddr *restrict sa, socklen_t salen,
335 char *restrict node, socklen_t nodelen,
336 char *restrict service, socklen_t servicelen,
337 int flags)
339 #ifdef WIN32_NATIVE
340 if (use_win32_p ())
341 return getnameinfo_ptr (sa, salen, node, nodelen,
342 service, servicelen, flags);
343 #endif
345 /* FIXME: Support other flags. */
346 if ((node && nodelen > 0 && !(flags & NI_NUMERICHOST)) ||
347 (service && servicelen > 0 && !(flags & NI_NUMERICHOST)) ||
348 (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV)))
349 return EAI_BADFLAGS;
351 if (sa == NULL || salen < sizeof (sa->sa_family))
352 return EAI_FAMILY;
354 switch (sa->sa_family)
356 #if HAVE_IPV4
357 case AF_INET:
358 if (salen < sizeof (struct sockaddr_in))
359 return EAI_FAMILY;
360 break;
361 #endif
362 #if HAVE_IPV6
363 case AF_INET6:
364 if (salen < sizeof (struct sockaddr_in6))
365 return EAI_FAMILY;
366 break;
367 #endif
368 default:
369 return EAI_FAMILY;
372 if (node && nodelen > 0 && flags & NI_NUMERICHOST)
374 switch (sa->sa_family)
376 #if HAVE_IPV4
377 case AF_INET:
378 if (!inet_ntop (AF_INET,
379 &(((const struct sockaddr_in *) sa)->sin_addr),
380 node, nodelen))
381 return EAI_SYSTEM;
382 break;
383 #endif
385 #if HAVE_IPV6
386 case AF_INET6:
387 if (!inet_ntop (AF_INET6,
388 &(((const struct sockaddr_in6 *) sa)->sin6_addr),
389 node, nodelen))
390 return EAI_SYSTEM;
391 break;
392 #endif
394 default:
395 return EAI_FAMILY;
399 if (service && servicelen > 0 && flags & NI_NUMERICSERV)
400 switch (sa->sa_family)
402 #if HAVE_IPV4
403 case AF_INET:
404 #endif
405 #if HAVE_IPV6
406 case AF_INET6:
407 #endif
409 unsigned short int port
410 = ntohs (((const struct sockaddr_in *) sa)->sin_port);
411 if (servicelen <= snprintf (service, servicelen, "%u", port))
412 return EAI_OVERFLOW;
414 break;
417 return 0;