i18n: Add Assamese translation
[vlc.git] / src / os2 / getaddrinfo.c
blobc7e74a15cd70387df6e2f4bbf967dc4916fb2fda
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 Lesser 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 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
36 NI_DGRAM)
38 * getnameinfo() non-thread-safe IPv4-only implementation,
39 * Address-family-independent address to hostname translation
40 * (reverse DNS lookup in case of IPv4).
42 * This is meant for use on old IP-enabled systems that are not IPv6-aware,
43 * and probably do not have getnameinfo(), but have the old gethostbyaddr()
44 * function.
46 int
47 getnameinfo (const struct sockaddr *sa, socklen_t salen,
48 char *host, int hostlen, char *serv, int servlen, int flags)
50 if (((size_t)salen < sizeof (struct sockaddr_in))
51 || (sa->sa_family != AF_INET))
52 return EAI_FAMILY;
53 else if (flags & (~_NI_MASK))
54 return EAI_BADFLAGS;
55 else
57 const struct sockaddr_in *addr;
59 addr = (const struct sockaddr_in *)sa;
61 if (host != NULL)
63 /* host name resolution */
64 if (!(flags & NI_NUMERICHOST))
66 if (flags & NI_NAMEREQD)
67 return EAI_NONAME;
70 /* inet_ntoa() is not thread-safe, do not use it */
71 uint32_t ipv4 = ntohl (addr->sin_addr.s_addr);
73 if (snprintf (host, hostlen, "%u.%u.%u.%u", ipv4 >> 24,
74 (ipv4 >> 16) & 0xff, (ipv4 >> 8) & 0xff,
75 ipv4 & 0xff) >= (int)hostlen)
76 return EAI_OVERFLOW;
79 if (serv != NULL)
81 if (snprintf (serv, servlen, "%u",
82 (unsigned int)ntohs (addr->sin_port)) >= (int)servlen)
83 return EAI_OVERFLOW;
86 return 0;
89 #define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
91 * Converts the current herrno error value into an EAI_* error code.
92 * That error code is normally returned by getnameinfo() or getaddrinfo().
94 static int
95 gai_error_from_herrno (void)
97 switch (h_errno)
99 case HOST_NOT_FOUND:
100 return EAI_NONAME;
102 case NO_ADDRESS:
103 # if (NO_ADDRESS != NO_DATA)
104 case NO_DATA:
105 # endif
106 return EAI_NODATA;
108 case NO_RECOVERY:
109 return EAI_FAIL;
111 case TRY_AGAIN:
112 return EAI_AGAIN;
114 return EAI_SYSTEM;
118 * Internal function that builds an addrinfo struct.
120 static struct addrinfo *
121 makeaddrinfo (int af, int type, int proto,
122 const struct sockaddr *addr, size_t addrlen,
123 const char *canonname)
125 struct addrinfo *res;
127 res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
128 if (res != NULL)
130 res->ai_flags = 0;
131 res->ai_family = af;
132 res->ai_socktype = type;
133 res->ai_protocol = proto;
134 res->ai_addrlen = addrlen;
135 res->ai_addr = malloc (addrlen);
136 res->ai_canonname = NULL;
137 res->ai_next = NULL;
139 if (res->ai_addr != NULL)
141 memcpy (res->ai_addr, addr, addrlen);
143 if (canonname != NULL)
145 res->ai_canonname = strdup (canonname);
146 if (res->ai_canonname != NULL)
147 return res; /* success ! */
149 else
150 return res;
153 /* failsafe */
154 freeaddrinfo (res);
155 return NULL;
158 static struct addrinfo *
159 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
161 struct sockaddr_in addr;
163 memset (&addr, 0, sizeof (addr));
164 addr.sin_family = AF_INET;
165 # ifdef HAVE_SA_LEN
166 addr.sin_len = sizeof (addr);
167 # endif
168 addr.sin_port = port;
169 addr.sin_addr.s_addr = ip;
171 return makeaddrinfo (AF_INET, type, proto,
172 (struct sockaddr*)&addr, sizeof (addr), name);
176 * getaddrinfo() non-thread-safe IPv4-only implementation
177 * Address-family-independent hostname to address resolution.
179 * This is meant for IPv6-unaware systems that do probably not provide
180 * getaddrinfo(), but still have old function gethostbyname().
182 * Only UDP and TCP over IPv4 are supported here.
185 getaddrinfo (const char *node, const char *service,
186 const struct addrinfo *hints, struct addrinfo **res)
188 struct addrinfo *info;
189 u_long ip;
190 u_short port;
191 int protocol = 0, flags = 0;
192 const char *name = NULL;
194 if (hints != NULL)
196 flags = hints->ai_flags;
198 if (flags & ~_AI_MASK)
199 return EAI_BADFLAGS;
200 /* only accept AF_INET and AF_UNSPEC */
201 if (hints->ai_family && (hints->ai_family != AF_INET))
202 return EAI_FAMILY;
204 /* protocol sanity check */
205 switch (hints->ai_socktype)
207 case SOCK_STREAM:
208 protocol = IPPROTO_TCP;
209 break;
211 case SOCK_DGRAM:
212 protocol = IPPROTO_UDP;
213 break;
215 #ifdef SOCK_RAW
216 case SOCK_RAW:
217 #endif
218 case 0:
219 break;
221 default:
222 return EAI_SOCKTYPE;
224 if (hints->ai_protocol && protocol
225 && (protocol != hints->ai_protocol))
226 return EAI_SERVICE;
229 *res = NULL;
231 /* default values */
232 if (node == NULL)
234 if (flags & AI_PASSIVE)
235 ip = htonl (INADDR_ANY);
236 else
237 ip = htonl (INADDR_LOOPBACK);
239 else
240 if ((ip = inet_addr (node)) == INADDR_NONE)
242 struct hostent *entry = NULL;
244 /* hostname resolution */
245 if (!(flags & AI_NUMERICHOST))
246 entry = gethostbyname (node);
248 if (entry == NULL)
249 return gai_error_from_herrno ();
251 if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
252 return EAI_FAMILY;
254 ip = *((u_long *) entry->h_addr);
255 if (flags & AI_CANONNAME)
256 name = entry->h_name;
259 if ((flags & AI_CANONNAME) && (name == NULL))
260 name = node;
262 /* service resolution */
263 if (service == NULL)
264 port = 0;
265 else
267 unsigned long d;
268 char *end;
270 d = strtoul (service, &end, 0);
271 if (end[0] || (d > 65535u))
272 return EAI_SERVICE;
274 port = htons ((u_short)d);
277 /* building results... */
278 if ((!protocol) || (protocol == IPPROTO_UDP))
280 info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
281 if (info == NULL)
283 errno = ENOMEM;
284 return EAI_SYSTEM;
286 if (flags & AI_PASSIVE)
287 info->ai_flags |= AI_PASSIVE;
288 *res = info;
290 if ((!protocol) || (protocol == IPPROTO_TCP))
292 info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
293 if (info == NULL)
295 errno = ENOMEM;
296 return EAI_SYSTEM;
298 info->ai_next = *res;
299 if (flags & AI_PASSIVE)
300 info->ai_flags |= AI_PASSIVE;
301 *res = info;
304 return 0;