Fixed a crash caused by yadif deinterlacer on Windows XP
[vlc/solaris.git] / src / os2 / getaddrinfo.c
blob6b03f24e8ad02f13c8b2a017f4a45c7e965271f9
1 /*****************************************************************************
2 * getaddrinfo.c: getaddrinfo/getnameinfo replacement functions
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
5 * Copyright (C) 2002-2007 Rémi Denis-Courmont
6 * Copyright (C) 2011 KO Myung-Hun
8 * Authors: KO Myung-Hun <komh@chollian.net>
9 * Rémi Denis-Courmont <rem # videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_network.h>
33 #include <arpa/inet.h>
35 static const struct
37 int code;
38 const char msg[41];
39 } gai_errlist[] =
41 { 0, "Error 0" },
42 { EAI_BADFLAGS, "Invalid flag used" },
43 { EAI_NONAME, "Host or service not found" },
44 { EAI_AGAIN, "Temporary name service failure" },
45 { EAI_FAIL, "Non-recoverable name service failure" },
46 { EAI_NODATA, "No data for host name" },
47 { EAI_FAMILY, "Unsupported address family" },
48 { EAI_SOCKTYPE, "Unsupported socket type" },
49 { EAI_SERVICE, "Incompatible service for socket type" },
50 { EAI_ADDRFAMILY, "Unavailable address family for host name" },
51 { EAI_MEMORY, "Memory allocation failure" },
52 { EAI_OVERFLOW, "Buffer overflow" },
53 { EAI_SYSTEM, "System error" },
54 { 0, "" },
57 static const char gai_unknownerr[] = "Unrecognized error number";
59 /****************************************************************************
60 * Converts an EAI_* error code into human readable english text.
61 ****************************************************************************/
62 const char *gai_strerror (int errnum)
64 for (unsigned i = 0; *gai_errlist[i].msg; i++)
65 if (errnum == gai_errlist[i].code)
66 return gai_errlist[i].msg;
68 return gai_unknownerr;
71 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
72 NI_DGRAM)
74 * getnameinfo() non-thread-safe IPv4-only implementation,
75 * Address-family-independent address to hostname translation
76 * (reverse DNS lookup in case of IPv4).
78 * This is meant for use on old IP-enabled systems that are not IPv6-aware,
79 * and probably do not have getnameinfo(), but have the old gethostbyaddr()
80 * function.
82 int
83 getnameinfo (const struct sockaddr *sa, socklen_t salen,
84 char *host, int hostlen, char *serv, int servlen, int flags)
86 if (((size_t)salen < sizeof (struct sockaddr_in))
87 || (sa->sa_family != AF_INET))
88 return EAI_FAMILY;
89 else if (flags & (~_NI_MASK))
90 return EAI_BADFLAGS;
91 else
93 const struct sockaddr_in *addr;
95 addr = (const struct sockaddr_in *)sa;
97 if (host != NULL)
99 /* host name resolution */
100 if (!(flags & NI_NUMERICHOST))
102 if (flags & NI_NAMEREQD)
103 return EAI_NONAME;
106 /* inet_ntoa() is not thread-safe, do not use it */
107 uint32_t ipv4 = ntohl (addr->sin_addr.s_addr);
109 if (snprintf (host, hostlen, "%u.%u.%u.%u", ipv4 >> 24,
110 (ipv4 >> 16) & 0xff, (ipv4 >> 8) & 0xff,
111 ipv4 & 0xff) >= (int)hostlen)
112 return EAI_OVERFLOW;
115 if (serv != NULL)
117 if (snprintf (serv, servlen, "%u",
118 (unsigned int)ntohs (addr->sin_port)) >= (int)servlen)
119 return EAI_OVERFLOW;
122 return 0;
125 #define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
127 * Converts the current herrno error value into an EAI_* error code.
128 * That error code is normally returned by getnameinfo() or getaddrinfo().
130 static int
131 gai_error_from_herrno (void)
133 switch (h_errno)
135 case HOST_NOT_FOUND:
136 return EAI_NONAME;
138 case NO_ADDRESS:
139 # if (NO_ADDRESS != NO_DATA)
140 case NO_DATA:
141 # endif
142 return EAI_NODATA;
144 case NO_RECOVERY:
145 return EAI_FAIL;
147 case TRY_AGAIN:
148 return EAI_AGAIN;
150 return EAI_SYSTEM;
154 * This functions must be used to free the memory allocated by getaddrinfo().
156 void freeaddrinfo (struct addrinfo *res)
158 if (res == NULL)
159 return;
160 free (res->ai_canonname);
161 free (res->ai_addr);
162 free (res->ai_next);
163 free (res);
167 * Internal function that builds an addrinfo struct.
169 static struct addrinfo *
170 makeaddrinfo (int af, int type, int proto,
171 const struct sockaddr *addr, size_t addrlen,
172 const char *canonname)
174 struct addrinfo *res;
176 res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
177 if (res != NULL)
179 res->ai_flags = 0;
180 res->ai_family = af;
181 res->ai_socktype = type;
182 res->ai_protocol = proto;
183 res->ai_addrlen = addrlen;
184 res->ai_addr = malloc (addrlen);
185 res->ai_canonname = NULL;
186 res->ai_next = NULL;
188 if (res->ai_addr != NULL)
190 memcpy (res->ai_addr, addr, addrlen);
192 if (canonname != NULL)
194 res->ai_canonname = strdup (canonname);
195 if (res->ai_canonname != NULL)
196 return res; /* success ! */
198 else
199 return res;
202 /* failsafe */
203 freeaddrinfo (res);
204 return NULL;
207 static struct addrinfo *
208 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
210 struct sockaddr_in addr;
212 memset (&addr, 0, sizeof (addr));
213 addr.sin_family = AF_INET;
214 # ifdef HAVE_SA_LEN
215 addr.sin_len = sizeof (addr);
216 # endif
217 addr.sin_port = port;
218 addr.sin_addr.s_addr = ip;
220 return makeaddrinfo (AF_INET, type, proto,
221 (struct sockaddr*)&addr, sizeof (addr), name);
225 * getaddrinfo() non-thread-safe IPv4-only implementation
226 * Address-family-independent hostname to address resolution.
228 * This is meant for IPv6-unaware systems that do probably not provide
229 * getaddrinfo(), but still have old function gethostbyname().
231 * Only UDP and TCP over IPv4 are supported here.
234 getaddrinfo (const char *node, const char *service,
235 const struct addrinfo *hints, struct addrinfo **res)
237 struct addrinfo *info;
238 u_long ip;
239 u_short port;
240 int protocol = 0, flags = 0;
241 const char *name = NULL;
243 if (hints != NULL)
245 flags = hints->ai_flags;
247 if (flags & ~_AI_MASK)
248 return EAI_BADFLAGS;
249 /* only accept AF_INET and AF_UNSPEC */
250 if (hints->ai_family && (hints->ai_family != AF_INET))
251 return EAI_FAMILY;
253 /* protocol sanity check */
254 switch (hints->ai_socktype)
256 case SOCK_STREAM:
257 protocol = IPPROTO_TCP;
258 break;
260 case SOCK_DGRAM:
261 protocol = IPPROTO_UDP;
262 break;
264 #ifndef SOCK_RAW
265 case SOCK_RAW:
266 #endif
267 case 0:
268 break;
270 default:
271 return EAI_SOCKTYPE;
273 if (hints->ai_protocol && protocol
274 && (protocol != hints->ai_protocol))
275 return EAI_SERVICE;
278 *res = NULL;
280 /* default values */
281 if (node == NULL)
283 if (flags & AI_PASSIVE)
284 ip = htonl (INADDR_ANY);
285 else
286 ip = htonl (INADDR_LOOPBACK);
288 else
289 if ((ip = inet_addr (node)) == INADDR_NONE)
291 struct hostent *entry = NULL;
293 /* hostname resolution */
294 if (!(flags & AI_NUMERICHOST))
295 entry = gethostbyname (node);
297 if (entry == NULL)
298 return gai_error_from_herrno ();
300 if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
301 return EAI_FAMILY;
303 ip = *((u_long *) entry->h_addr);
304 if (flags & AI_CANONNAME)
305 name = entry->h_name;
308 if ((flags & AI_CANONNAME) && (name == NULL))
309 name = node;
311 /* service resolution */
312 if (service == NULL)
313 port = 0;
314 else
316 unsigned long d;
317 char *end;
319 d = strtoul (service, &end, 0);
320 if (end[0] || (d > 65535u))
321 return EAI_SERVICE;
323 port = htons ((u_short)d);
326 /* building results... */
327 if ((!protocol) || (protocol == IPPROTO_UDP))
329 info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
330 if (info == NULL)
332 errno = ENOMEM;
333 return EAI_SYSTEM;
335 if (flags & AI_PASSIVE)
336 info->ai_flags |= AI_PASSIVE;
337 *res = info;
339 if ((!protocol) || (protocol == IPPROTO_TCP))
341 info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
342 if (info == NULL)
344 errno = ENOMEM;
345 return EAI_SYSTEM;
347 info->ai_next = *res;
348 if (flags & AI_PASSIVE)
349 info->ai_flags |= AI_PASSIVE;
350 *res = info;
353 return 0;