]> git.sesse.net Git - vlc/blob - src/os2/getaddrinfo.c
macosx: Update progress dialog on the main thread, make check thread safe
[vlc] / src / os2 / getaddrinfo.c
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
7  *
8  * Authors: KO Myung-Hun <komh@chollian.net>
9  *          Rémi Denis-Courmont <rem # videolan.org>
10  *
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.
15  *
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 Lesser General Public License for more details.
20  *
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  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_network.h>
32
33 #include <arpa/inet.h>
34
35 static const struct
36 {
37     int        code;
38     const char msg[41];
39 } gai_errlist[] =
40 {
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,              "" },
55 };
56
57 static const char gai_unknownerr[] = "Unrecognized error number";
58
59 /****************************************************************************
60  * Converts an EAI_* error code into human readable english text.
61  ****************************************************************************/
62 const char *gai_strerror (int errnum)
63 {
64     for (unsigned i = 0; *gai_errlist[i].msg; i++)
65         if (errnum == gai_errlist[i].code)
66             return gai_errlist[i].msg;
67
68     return gai_unknownerr;
69 }
70
71 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
72                   NI_DGRAM)
73 /*
74  * getnameinfo() non-thread-safe IPv4-only implementation,
75  * Address-family-independent address to hostname translation
76  * (reverse DNS lookup in case of IPv4).
77  *
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.
81  */
82 int
83 getnameinfo (const struct sockaddr *sa, socklen_t salen,
84                  char *host, int hostlen, char *serv, int servlen, int flags)
85 {
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
92     {
93         const struct sockaddr_in *addr;
94
95         addr = (const struct sockaddr_in *)sa;
96
97         if (host != NULL)
98         {
99             /* host name resolution */
100             if (!(flags & NI_NUMERICHOST))
101             {
102                 if (flags & NI_NAMEREQD)
103                     return EAI_NONAME;
104             }
105
106             /* inet_ntoa() is not thread-safe, do not use it */
107             uint32_t ipv4 = ntohl (addr->sin_addr.s_addr);
108
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;
113         }
114
115         if (serv != NULL)
116         {
117             if (snprintf (serv, servlen, "%u",
118                           (unsigned int)ntohs (addr->sin_port)) >= (int)servlen)
119                 return EAI_OVERFLOW;
120         }
121     }
122     return 0;
123 }
124
125 #define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
126 /*
127  * Converts the current herrno error value into an EAI_* error code.
128  * That error code is normally returned by getnameinfo() or getaddrinfo().
129  */
130 static int
131 gai_error_from_herrno (void)
132 {
133     switch (h_errno)
134     {
135         case HOST_NOT_FOUND:
136             return EAI_NONAME;
137
138         case NO_ADDRESS:
139 # if (NO_ADDRESS != NO_DATA)
140         case NO_DATA:
141 # endif
142             return EAI_NODATA;
143
144         case NO_RECOVERY:
145             return EAI_FAIL;
146
147         case TRY_AGAIN:
148             return EAI_AGAIN;
149     }
150     return EAI_SYSTEM;
151 }
152
153 /*
154  * Internal function that builds an addrinfo struct.
155  */
156 static struct addrinfo *
157 makeaddrinfo (int af, int type, int proto,
158               const struct sockaddr *addr, size_t addrlen,
159               const char *canonname)
160 {
161     struct addrinfo *res;
162
163     res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
164     if (res != NULL)
165     {
166         res->ai_flags = 0;
167         res->ai_family = af;
168         res->ai_socktype = type;
169         res->ai_protocol = proto;
170         res->ai_addrlen = addrlen;
171         res->ai_addr = malloc (addrlen);
172         res->ai_canonname = NULL;
173         res->ai_next = NULL;
174
175         if (res->ai_addr != NULL)
176         {
177             memcpy (res->ai_addr, addr, addrlen);
178
179             if (canonname != NULL)
180             {
181                 res->ai_canonname = strdup (canonname);
182                 if (res->ai_canonname != NULL)
183                     return res; /* success ! */
184             }
185             else
186                 return res;
187         }
188     }
189     /* failsafe */
190     freeaddrinfo (res);
191     return NULL;
192 }
193
194 static struct addrinfo *
195 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
196 {
197     struct sockaddr_in addr;
198
199     memset (&addr, 0, sizeof (addr));
200     addr.sin_family = AF_INET;
201 # ifdef HAVE_SA_LEN
202     addr.sin_len = sizeof (addr);
203 # endif
204     addr.sin_port = port;
205     addr.sin_addr.s_addr = ip;
206
207     return makeaddrinfo (AF_INET, type, proto,
208                          (struct sockaddr*)&addr, sizeof (addr), name);
209 }
210
211 /*
212  * getaddrinfo() non-thread-safe IPv4-only implementation
213  * Address-family-independent hostname to address resolution.
214  *
215  * This is meant for IPv6-unaware systems that do probably not provide
216  * getaddrinfo(), but still have old function gethostbyname().
217  *
218  * Only UDP and TCP over IPv4 are supported here.
219  */
220 int
221 getaddrinfo (const char *node, const char *service,
222              const struct addrinfo *hints, struct addrinfo **res)
223 {
224     struct addrinfo *info;
225     u_long ip;
226     u_short port;
227     int protocol = 0, flags = 0;
228     const char *name = NULL;
229
230     if (hints != NULL)
231     {
232         flags = hints->ai_flags;
233
234         if (flags & ~_AI_MASK)
235             return EAI_BADFLAGS;
236         /* only accept AF_INET and AF_UNSPEC */
237         if (hints->ai_family && (hints->ai_family != AF_INET))
238             return EAI_FAMILY;
239
240         /* protocol sanity check */
241         switch (hints->ai_socktype)
242         {
243             case SOCK_STREAM:
244                 protocol = IPPROTO_TCP;
245                 break;
246
247             case SOCK_DGRAM:
248                 protocol = IPPROTO_UDP;
249                 break;
250
251 #ifdef SOCK_RAW
252             case SOCK_RAW:
253 #endif
254             case 0:
255                 break;
256
257             default:
258                 return EAI_SOCKTYPE;
259         }
260         if (hints->ai_protocol && protocol
261          && (protocol != hints->ai_protocol))
262             return EAI_SERVICE;
263     }
264
265     *res = NULL;
266
267     /* default values */
268     if (node == NULL)
269     {
270         if (flags & AI_PASSIVE)
271             ip = htonl (INADDR_ANY);
272         else
273             ip = htonl (INADDR_LOOPBACK);
274     }
275     else
276     if ((ip = inet_addr (node)) == INADDR_NONE)
277     {
278         struct hostent *entry = NULL;
279
280         /* hostname resolution */
281         if (!(flags & AI_NUMERICHOST))
282             entry = gethostbyname (node);
283
284         if (entry == NULL)
285             return gai_error_from_herrno ();
286
287         if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
288             return EAI_FAMILY;
289
290         ip = *((u_long *) entry->h_addr);
291         if (flags & AI_CANONNAME)
292             name = entry->h_name;
293     }
294
295     if ((flags & AI_CANONNAME) && (name == NULL))
296         name = node;
297
298     /* service resolution */
299     if (service == NULL)
300         port = 0;
301     else
302     {
303         unsigned long d;
304         char *end;
305
306         d = strtoul (service, &end, 0);
307         if (end[0] || (d > 65535u))
308             return EAI_SERVICE;
309
310         port = htons ((u_short)d);
311     }
312
313     /* building results... */
314     if ((!protocol) || (protocol == IPPROTO_UDP))
315     {
316         info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
317         if (info == NULL)
318         {
319             errno = ENOMEM;
320             return EAI_SYSTEM;
321         }
322         if (flags & AI_PASSIVE)
323             info->ai_flags |= AI_PASSIVE;
324         *res = info;
325     }
326     if ((!protocol) || (protocol == IPPROTO_TCP))
327     {
328         info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
329         if (info == NULL)
330         {
331             errno = ENOMEM;
332             return EAI_SYSTEM;
333         }
334         info->ai_next = *res;
335         if (flags & AI_PASSIVE)
336             info->ai_flags |= AI_PASSIVE;
337         *res = info;
338     }
339
340     return 0;
341 }