]> git.sesse.net Git - vlc/blob - src/os2/getaddrinfo.c
skins2: fix nested panels wrongly positioned if not the first child
[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  * This functions must be used to free the memory allocated by getaddrinfo().
155  */
156 void freeaddrinfo (struct addrinfo *res)
157 {
158     if (res == NULL)
159         return;
160     free (res->ai_canonname);
161     free (res->ai_addr);
162     free (res->ai_next);
163     free (res);
164 }
165
166 /*
167  * Internal function that builds an addrinfo struct.
168  */
169 static struct addrinfo *
170 makeaddrinfo (int af, int type, int proto,
171               const struct sockaddr *addr, size_t addrlen,
172               const char *canonname)
173 {
174     struct addrinfo *res;
175
176     res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
177     if (res != NULL)
178     {
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;
187
188         if (res->ai_addr != NULL)
189         {
190             memcpy (res->ai_addr, addr, addrlen);
191
192             if (canonname != NULL)
193             {
194                 res->ai_canonname = strdup (canonname);
195                 if (res->ai_canonname != NULL)
196                     return res; /* success ! */
197             }
198             else
199                 return res;
200         }
201     }
202     /* failsafe */
203     freeaddrinfo (res);
204     return NULL;
205 }
206
207 static struct addrinfo *
208 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
209 {
210     struct sockaddr_in addr;
211
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;
219
220     return makeaddrinfo (AF_INET, type, proto,
221                          (struct sockaddr*)&addr, sizeof (addr), name);
222 }
223
224 /*
225  * getaddrinfo() non-thread-safe IPv4-only implementation
226  * Address-family-independent hostname to address resolution.
227  *
228  * This is meant for IPv6-unaware systems that do probably not provide
229  * getaddrinfo(), but still have old function gethostbyname().
230  *
231  * Only UDP and TCP over IPv4 are supported here.
232  */
233 int
234 getaddrinfo (const char *node, const char *service,
235              const struct addrinfo *hints, struct addrinfo **res)
236 {
237     struct addrinfo *info;
238     u_long ip;
239     u_short port;
240     int protocol = 0, flags = 0;
241     const char *name = NULL;
242
243     if (hints != NULL)
244     {
245         flags = hints->ai_flags;
246
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;
252
253         /* protocol sanity check */
254         switch (hints->ai_socktype)
255         {
256             case SOCK_STREAM:
257                 protocol = IPPROTO_TCP;
258                 break;
259
260             case SOCK_DGRAM:
261                 protocol = IPPROTO_UDP;
262                 break;
263
264 #ifndef SOCK_RAW
265             case SOCK_RAW:
266 #endif
267             case 0:
268                 break;
269
270             default:
271                 return EAI_SOCKTYPE;
272         }
273         if (hints->ai_protocol && protocol
274          && (protocol != hints->ai_protocol))
275             return EAI_SERVICE;
276     }
277
278     *res = NULL;
279
280     /* default values */
281     if (node == NULL)
282     {
283         if (flags & AI_PASSIVE)
284             ip = htonl (INADDR_ANY);
285         else
286             ip = htonl (INADDR_LOOPBACK);
287     }
288     else
289     if ((ip = inet_addr (node)) == INADDR_NONE)
290     {
291         struct hostent *entry = NULL;
292
293         /* hostname resolution */
294         if (!(flags & AI_NUMERICHOST))
295             entry = gethostbyname (node);
296
297         if (entry == NULL)
298             return gai_error_from_herrno ();
299
300         if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
301             return EAI_FAMILY;
302
303         ip = *((u_long *) entry->h_addr);
304         if (flags & AI_CANONNAME)
305             name = entry->h_name;
306     }
307
308     if ((flags & AI_CANONNAME) && (name == NULL))
309         name = node;
310
311     /* service resolution */
312     if (service == NULL)
313         port = 0;
314     else
315     {
316         unsigned long d;
317         char *end;
318
319         d = strtoul (service, &end, 0);
320         if (end[0] || (d > 65535u))
321             return EAI_SERVICE;
322
323         port = htons ((u_short)d);
324     }
325
326     /* building results... */
327     if ((!protocol) || (protocol == IPPROTO_UDP))
328     {
329         info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
330         if (info == NULL)
331         {
332             errno = ENOMEM;
333             return EAI_SYSTEM;
334         }
335         if (flags & AI_PASSIVE)
336             info->ai_flags |= AI_PASSIVE;
337         *res = info;
338     }
339     if ((!protocol) || (protocol == IPPROTO_TCP))
340     {
341         info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
342         if (info == NULL)
343         {
344             errno = ENOMEM;
345             return EAI_SYSTEM;
346         }
347         info->ai_next = *res;
348         if (flags & AI_PASSIVE)
349             info->ai_flags |= AI_PASSIVE;
350         *res = info;
351     }
352
353     return 0;
354 }