gui: macos: use float for rate
[vlc.git] / src / os2 / getaddrinfo.c
bloba59c089761830db45be6bf88ef129035f09714ad
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 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
35 #endif
37 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
38 NI_DGRAM)
40 * getnameinfo() non-thread-safe IPv4-only implementation,
41 * Address-family-independent address to hostname translation
42 * (reverse DNS lookup in case of IPv4).
44 * This is meant for use on old IP-enabled systems that are not IPv6-aware,
45 * and probably do not have getnameinfo(), but have the old gethostbyaddr()
46 * function.
48 int
49 getnameinfo (const struct sockaddr *sa, socklen_t salen,
50 char *host, int hostlen, char *serv, int servlen, int flags)
52 if (((size_t)salen < sizeof (struct sockaddr_in))
53 || (sa->sa_family != AF_INET))
54 return EAI_FAMILY;
55 else if (flags & (~_NI_MASK))
56 return EAI_BADFLAGS;
57 else
59 const struct sockaddr_in *addr;
61 addr = (const struct sockaddr_in *)sa;
63 if (host != NULL)
65 /* host name resolution */
66 if (!(flags & NI_NUMERICHOST))
68 if (flags & NI_NAMEREQD)
69 return EAI_NONAME;
72 /* inet_ntoa() is not thread-safe, do not use it */
73 uint32_t ipv4 = ntohl (addr->sin_addr.s_addr);
75 if (snprintf (host, hostlen, "%u.%u.%u.%u", ipv4 >> 24,
76 (ipv4 >> 16) & 0xff, (ipv4 >> 8) & 0xff,
77 ipv4 & 0xff) >= (int)hostlen)
78 return EAI_OVERFLOW;
81 if (serv != NULL)
83 if (snprintf (serv, servlen, "%u",
84 (unsigned int)ntohs (addr->sin_port)) >= (int)servlen)
85 return EAI_OVERFLOW;
88 return 0;
91 #define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
93 * Converts the current herrno error value into an EAI_* error code.
94 * That error code is normally returned by getnameinfo() or getaddrinfo().
96 static int
97 gai_error_from_herrno (void)
99 switch (h_errno)
101 case HOST_NOT_FOUND:
102 return EAI_NONAME;
104 case NO_ADDRESS:
105 # if (NO_ADDRESS != NO_DATA)
106 case NO_DATA:
107 # endif
108 return EAI_NODATA;
110 case NO_RECOVERY:
111 return EAI_FAIL;
113 case TRY_AGAIN:
114 return EAI_AGAIN;
116 return EAI_SYSTEM;
120 * Internal function that builds an addrinfo struct.
122 static struct addrinfo *
123 makeaddrinfo (int af, int type, int proto,
124 const struct sockaddr *addr, size_t addrlen,
125 const char *canonname)
127 struct addrinfo *res;
129 res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
130 if (res != NULL)
132 res->ai_flags = 0;
133 res->ai_family = af;
134 res->ai_socktype = type;
135 res->ai_protocol = proto;
136 res->ai_addrlen = addrlen;
137 res->ai_addr = malloc (addrlen);
138 res->ai_canonname = NULL;
139 res->ai_next = NULL;
141 if (res->ai_addr != NULL)
143 memcpy (res->ai_addr, addr, addrlen);
145 if (canonname != NULL)
147 res->ai_canonname = strdup (canonname);
148 if (res->ai_canonname != NULL)
149 return res; /* success ! */
151 else
152 return res;
155 /* failsafe */
156 freeaddrinfo (res);
157 return NULL;
160 static struct addrinfo *
161 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
163 struct sockaddr_in addr;
165 memset (&addr, 0, sizeof (addr));
166 addr.sin_family = AF_INET;
167 # ifdef HAVE_SA_LEN
168 addr.sin_len = sizeof (addr);
169 # endif
170 addr.sin_port = port;
171 addr.sin_addr.s_addr = ip;
173 return makeaddrinfo (AF_INET, type, proto,
174 (struct sockaddr*)&addr, sizeof (addr), name);
178 * getaddrinfo() non-thread-safe IPv4-only implementation
179 * Address-family-independent hostname to address resolution.
181 * This is meant for IPv6-unaware systems that do probably not provide
182 * getaddrinfo(), but still have old function gethostbyname().
184 * Only UDP and TCP over IPv4 are supported here.
187 getaddrinfo (const char *node, const char *service,
188 const struct addrinfo *hints, struct addrinfo **res)
190 struct addrinfo *info;
191 u_long ip;
192 u_short port;
193 int protocol = 0, flags = 0;
194 const char *name = NULL;
196 if (hints != NULL)
198 flags = hints->ai_flags;
200 if (flags & ~_AI_MASK)
201 return EAI_BADFLAGS;
202 /* only accept AF_INET and AF_UNSPEC */
203 if (hints->ai_family && (hints->ai_family != AF_INET))
204 return EAI_FAMILY;
206 /* protocol sanity check */
207 switch (hints->ai_socktype)
209 case SOCK_STREAM:
210 protocol = IPPROTO_TCP;
211 break;
213 case SOCK_DGRAM:
214 protocol = IPPROTO_UDP;
215 break;
217 #ifdef SOCK_RAW
218 case SOCK_RAW:
219 #endif
220 case 0:
221 break;
223 default:
224 return EAI_SOCKTYPE;
226 if (hints->ai_protocol && protocol
227 && (protocol != hints->ai_protocol))
228 return EAI_SERVICE;
231 *res = NULL;
233 /* default values */
234 if (node == NULL)
236 if (flags & AI_PASSIVE)
237 ip = htonl (INADDR_ANY);
238 else
239 ip = htonl (INADDR_LOOPBACK);
241 else
242 if ((ip = inet_addr (node)) == INADDR_NONE)
244 struct hostent *entry = NULL;
246 /* hostname resolution */
247 if (!(flags & AI_NUMERICHOST))
248 entry = gethostbyname (node);
250 if (entry == NULL)
251 return gai_error_from_herrno ();
253 if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
254 return EAI_FAMILY;
256 ip = *((u_long *) entry->h_addr);
257 if (flags & AI_CANONNAME)
258 name = entry->h_name;
261 if ((flags & AI_CANONNAME) && (name == NULL))
262 name = node;
264 /* service resolution */
265 if (service == NULL)
266 port = 0;
267 else
269 unsigned long d;
270 char *end;
272 d = strtoul (service, &end, 0);
273 if (end[0] || (d > 65535u))
274 return EAI_SERVICE;
276 port = htons ((u_short)d);
279 /* building results... */
280 if ((!protocol) || (protocol == IPPROTO_UDP))
282 info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
283 if (info == NULL)
285 errno = ENOMEM;
286 return EAI_SYSTEM;
288 if (flags & AI_PASSIVE)
289 info->ai_flags |= AI_PASSIVE;
290 *res = info;
292 if ((!protocol) || (protocol == IPPROTO_TCP))
294 info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
295 if (info == NULL)
297 errno = ENOMEM;
298 return EAI_SYSTEM;
300 info->ai_next = *res;
301 if (flags & AI_PASSIVE)
302 info->ai_flags |= AI_PASSIVE;
303 *res = info;
306 return 0;