]> git.sesse.net Git - vlc/blob - src/network/getaddrinfo.c
HTTPd: use the RFC5334 MIME types for their respective extensions
[vlc] / src / network / 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  * $Id$
7  *
8  * Author: Rémi Denis-Courmont <rem # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_charset.h>
31
32 #include <stddef.h> /* size_t */
33 #include <string.h> /* strlen(), memcpy(), memset(), strchr() */
34 #include <stdlib.h> /* malloc(), free(), strtoul() */
35 #include <errno.h>
36 #include <assert.h>
37
38 #ifdef HAVE_SYS_TYPES_H
39 #   include <sys/types.h>
40 #endif
41 #ifdef HAVE_ARPA_INET_H
42 #   include <arpa/inet.h>
43 #endif
44 #ifdef HAVE_NETINET_IN_H
45 #   include <netinet/in.h>
46 #endif
47 #ifdef HAVE_UNISTD_H
48 #   include <unistd.h>
49 #endif
50
51 #include <vlc_network.h>
52
53 #ifndef NO_ADDRESS
54 #   define NO_ADDRESS  NO_DATA
55 #endif
56 #ifndef INADDR_NONE
57 #   define INADDR_NONE 0xFFFFFFFF
58 #endif
59 #ifndef AF_UNSPEC
60 #   define AF_UNSPEC   0
61 #endif
62
63
64 #ifndef HAVE_GAI_STRERROR
65 static const struct
66 {
67     int        code;
68     const char msg[41];
69 } gai_errlist[] =
70 {
71     { 0,              "Error 0" },
72     { EAI_BADFLAGS,   "Invalid flag used" },
73     { EAI_NONAME,     "Host or service not found" },
74     { EAI_AGAIN,      "Temporary name service failure" },
75     { EAI_FAIL,       "Non-recoverable name service failure" },
76     { EAI_NODATA,     "No data for host name" },
77     { EAI_FAMILY,     "Unsupported address family" },
78     { EAI_SOCKTYPE,   "Unsupported socket type" },
79     { EAI_SERVICE,    "Incompatible service for socket type" },
80     { EAI_ADDRFAMILY, "Unavailable address family for host name" },
81     { EAI_MEMORY,     "Memory allocation failure" },
82     { EAI_OVERFLOW,   "Buffer overflow" },
83     { EAI_SYSTEM,     "System error" },
84     { 0,              "" },
85 };
86
87 static const char gai_unknownerr[] = "Unrecognized error number";
88
89 /****************************************************************************
90  * Converts an EAI_* error code into human readable english text.
91  ****************************************************************************/
92 const char *vlc_gai_strerror (int errnum)
93 {
94     for (unsigned i = 0; *gai_errlist[i].msg; i++)
95         if (errnum == gai_errlist[i].code)
96             return gai_errlist[i].msg;
97
98     return gai_unknownerr;
99 }
100 #else /* ifndef HAVE_GAI_STRERROR */
101 const char *vlc_gai_strerror (int errnum)
102 {
103     return gai_strerror (errnum);
104 }
105 #endif
106
107 #ifndef HAVE_GETNAMEINFO
108 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
109                   NI_DGRAM)
110 /*
111  * getnameinfo() non-thread-safe IPv4-only implementation,
112  * Address-family-independent address to hostname translation
113  * (reverse DNS lookup in case of IPv4).
114  *
115  * This is meant for use on old IP-enabled systems that are not IPv6-aware,
116  * and probably do not have getnameinfo(), but have the old gethostbyaddr()
117  * function.
118  *
119  * GNU C library 2.0.x is known to lack this function, even though it defines
120  * getaddrinfo().
121  */
122 #ifdef WIN32
123 static int WSAAPI
124 stub_getnameinfo (const struct sockaddr *sa, socklen_t salen,
125              char *host, DWORD hostlen, char *serv, DWORD servlen, int flags)
126 #else
127 static int
128 stub_getnameinfo (const struct sockaddr *sa, socklen_t salen,
129              char *host, int hostlen, char *serv, int servlen, int flags)
130 #endif
131 {
132     if (((size_t)salen < sizeof (struct sockaddr_in))
133      || (sa->sa_family != AF_INET))
134         return EAI_FAMILY;
135     else if (flags & (~_NI_MASK))
136         return EAI_BADFLAGS;
137     else
138     {
139         const struct sockaddr_in *addr;
140
141         addr = (const struct sockaddr_in *)sa;
142
143         if (host != NULL)
144         {
145             /* host name resolution */
146             if (!(flags & NI_NUMERICHOST))
147             {
148                 if (flags & NI_NAMEREQD)
149                     return EAI_NONAME;
150             }
151
152             /* inet_ntoa() is not thread-safe, do not use it */
153             uint32_t ipv4 = ntohl (addr->sin_addr.s_addr);
154
155             if (snprintf (host, hostlen, "%u.%u.%u.%u", ipv4 >> 24,
156                           (ipv4 >> 16) & 0xff, (ipv4 >> 8) & 0xff,
157                           ipv4 & 0xff) >= (int)hostlen)
158                 return EAI_OVERFLOW;
159         }
160
161         if (serv != NULL)
162         {
163             if (snprintf (serv, servlen, "%u",
164                           (unsigned int)ntohs (addr->sin_port)) >= (int)servlen)
165                 return EAI_OVERFLOW;
166         }
167     }
168     return 0;
169 }
170 #undef getnameinfo
171 #define getnameifo stub_getnameinfo
172 #endif /* if !HAVE_GETNAMEINFO */
173
174 #ifndef HAVE_GETADDRINFO
175 #define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
176 /*
177  * Converts the current herrno error value into an EAI_* error code.
178  * That error code is normally returned by getnameinfo() or getaddrinfo().
179  */
180 static int
181 gai_error_from_herrno (void)
182 {
183     switch (h_errno)
184     {
185         case HOST_NOT_FOUND:
186             return EAI_NONAME;
187
188         case NO_ADDRESS:
189 # if (NO_ADDRESS != NO_DATA)
190         case NO_DATA:
191 # endif
192             return EAI_NODATA;
193
194         case NO_RECOVERY:
195             return EAI_FAIL;
196
197         case TRY_AGAIN:
198             return EAI_AGAIN;
199     }
200     return EAI_SYSTEM;
201 }
202
203 /*
204  * This functions must be used to free the memory allocated by getaddrinfo().
205  */
206 #ifdef WIN32
207 static void WSAAPI stub_freeaddrinfo (struct addrinfo *res)
208 #else
209 static void stub_freeaddrinfo (struct addrinfo *res)
210 #endif
211 {
212     if (res == NULL)
213         return;
214     free (res->ai_canonname);
215     free (res->ai_addr);
216     free (res->ai_next);
217     free (res);
218 }
219
220
221 /*
222  * Internal function that builds an addrinfo struct.
223  */
224 static struct addrinfo *
225 makeaddrinfo (int af, int type, int proto,
226               const struct sockaddr *addr, size_t addrlen,
227               const char *canonname)
228 {
229     struct addrinfo *res;
230
231     res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
232     if (res != NULL)
233     {
234         res->ai_flags = 0;
235         res->ai_family = af;
236         res->ai_socktype = type;
237         res->ai_protocol = proto;
238         res->ai_addrlen = addrlen;
239         res->ai_addr = malloc (addrlen);
240         res->ai_canonname = NULL;
241         res->ai_next = NULL;
242
243         if (res->ai_addr != NULL)
244         {
245             memcpy (res->ai_addr, addr, addrlen);
246
247             if (canonname != NULL)
248             {
249                 res->ai_canonname = strdup (canonname);
250                 if (res->ai_canonname != NULL)
251                     return res; /* success ! */
252             }
253             else
254                 return res;
255         }
256     }
257     /* failsafe */
258     vlc_freeaddrinfo (res);
259     return NULL;
260 }
261
262
263 static struct addrinfo *
264 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
265 {
266     struct sockaddr_in addr;
267
268     memset (&addr, 0, sizeof (addr));
269     addr.sin_family = AF_INET;
270 # ifdef HAVE_SA_LEN
271     addr.sin_len = sizeof (addr);
272 # endif
273     addr.sin_port = port;
274     addr.sin_addr.s_addr = ip;
275
276     return makeaddrinfo (AF_INET, type, proto,
277                          (struct sockaddr*)&addr, sizeof (addr), name);
278 }
279
280
281 /*
282  * getaddrinfo() non-thread-safe IPv4-only implementation
283  * Address-family-independent hostname to address resolution.
284  *
285  * This is meant for IPv6-unaware systems that do probably not provide
286  * getaddrinfo(), but still have old function gethostbyname().
287  *
288  * Only UDP and TCP over IPv4 are supported here.
289  */
290 #ifdef WIN32
291 static int WSAAPI
292 stub_getaddrinfo (const char *node, const char *service,
293              const struct addrinfo *hints, struct addrinfo **res)
294 #else
295 static int
296 stub_getaddrinfo (const char *node, const char *service,
297              const struct addrinfo *hints, struct addrinfo **res)
298 #endif
299 {
300     struct addrinfo *info;
301     u_long ip;
302     u_short port;
303     int protocol = 0, flags = 0;
304     const char *name = NULL;
305
306 #ifdef WIN32
307     /*
308      * Maybe you knew already that Winsock does not handle TCP/RST packets
309      * properly, so that when a TCP connection fails, it will wait until it
310      * times out even if the remote host did return a TCP/RST. However, it
311      * still sees the TCP/RST as the error code is 10061 instead of 10060.
312      * Basically, we have the stupid brainfucked behavior with DNS queries...
313      * When the recursive DNS server returns an error, Winsock waits about
314      * 2 seconds before it returns to the callers, even though it should know
315      * that is pointless. I'd like to know how come this hasn't been fixed
316      * for the past decade, or maybe not.
317      *
318      * Anyway, this is causing a severe delay when the SAP listener tries
319      * to resolve more than ten IPv6 numeric addresses. Modern systems will
320      * eventually realize that it is an IPv6 address, and won't try to resolve
321      * it as a IPv4 address via the Domain Name Service. Old systems
322      * (including Windows XP without the IPv6 stack) will not. It is normally
323      * not an issue as the DNS server usually returns an error very quickly.
324      * But it IS a severe issue on Windows, given the bug explained above.
325      * So here comes one more bug-to-bug Windows compatibility fix.
326      */
327     if ((node != NULL) && (strchr (node, ':') != NULL))
328        return EAI_NONAME;
329 #endif
330
331     if (hints != NULL)
332     {
333         flags = hints->ai_flags;
334
335         if (flags & ~_AI_MASK)
336             return EAI_BADFLAGS;
337         /* only accept AF_INET and AF_UNSPEC */
338         if (hints->ai_family && (hints->ai_family != AF_INET))
339             return EAI_FAMILY;
340
341         /* protocol sanity check */
342         switch (hints->ai_socktype)
343         {
344             case SOCK_STREAM:
345                 protocol = IPPROTO_TCP;
346                 break;
347
348             case SOCK_DGRAM:
349                 protocol = IPPROTO_UDP;
350                 break;
351
352 #ifndef SOCK_RAW
353             case SOCK_RAW:
354 #endif
355             case 0:
356                 break;
357
358             default:
359                 return EAI_SOCKTYPE;
360         }
361         if (hints->ai_protocol && protocol
362          && (protocol != hints->ai_protocol))
363             return EAI_SERVICE;
364     }
365
366     *res = NULL;
367
368     /* default values */
369     if (node == NULL)
370     {
371         if (flags & AI_PASSIVE)
372             ip = htonl (INADDR_ANY);
373         else
374             ip = htonl (INADDR_LOOPBACK);
375     }
376     else
377     if ((ip = inet_addr (node)) == INADDR_NONE)
378     {
379         struct hostent *entry = NULL;
380
381         /* hostname resolution */
382         if (!(flags & AI_NUMERICHOST))
383             entry = gethostbyname (node);
384
385         if (entry == NULL)
386             return gai_error_from_herrno ();
387
388         if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
389             return EAI_FAMILY;
390
391         ip = *((u_long *) entry->h_addr);
392         if (flags & AI_CANONNAME)
393             name = entry->h_name;
394     }
395
396     if ((flags & AI_CANONNAME) && (name == NULL))
397         name = node;
398
399     /* service resolution */
400     if (service == NULL)
401         port = 0;
402     else
403     {
404         unsigned long d;
405         char *end;
406
407         d = strtoul (service, &end, 0);
408         if (end[0] || (d > 65535u))
409             return EAI_SERVICE;
410
411         port = htons ((u_short)d);
412     }
413
414     /* building results... */
415     if ((!protocol) || (protocol == IPPROTO_UDP))
416     {
417         info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
418         if (info == NULL)
419         {
420             errno = ENOMEM;
421             return EAI_SYSTEM;
422         }
423         if (flags & AI_PASSIVE)
424             info->ai_flags |= AI_PASSIVE;
425         *res = info;
426     }
427     if ((!protocol) || (protocol == IPPROTO_TCP))
428     {
429         info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
430         if (info == NULL)
431         {
432             errno = ENOMEM;
433             return EAI_SYSTEM;
434         }
435         info->ai_next = *res;
436         if (flags & AI_PASSIVE)
437             info->ai_flags |= AI_PASSIVE;
438         *res = info;
439     }
440
441     return 0;
442 }
443 #undef getaddrinfo
444 #define getaddrifo stub_getaddrinfo
445 #undef freeaddrinfo
446 #define freeaddrifo stub_freeaddrinfo
447 #endif /* if !HAVE_GETADDRINFO */
448
449 #if defined( WIN32 ) && !defined( UNDER_CE )
450     /*
451      * Here is the kind of kludge you need to keep binary compatibility among
452      * varying OS versions...
453      */
454 typedef int (WSAAPI * GETNAMEINFO) ( const struct sockaddr FAR *, socklen_t,
455                                            char FAR *, DWORD, char FAR *, DWORD, int );
456 typedef int (WSAAPI * GETADDRINFO) (const char FAR *, const char FAR *,
457                                           const struct addrinfo FAR *,
458                                           struct addrinfo FAR * FAR *);
459
460 typedef void (WSAAPI * FREEADDRINFO) ( struct addrinfo FAR * );
461
462 static int WSAAPI _ws2_getnameinfo_bind ( const struct sockaddr FAR *, socklen_t,
463                                            char FAR *, DWORD, char FAR *, DWORD, int );
464 static int WSAAPI _ws2_getaddrinfo_bind (const char FAR *, const char FAR *,
465                                           const struct addrinfo FAR *,
466                                           struct addrinfo FAR * FAR *);
467
468 static GETNAMEINFO ws2_getnameinfo = _ws2_getnameinfo_bind;
469 static GETADDRINFO ws2_getaddrinfo = _ws2_getaddrinfo_bind;
470 static FREEADDRINFO ws2_freeaddrinfo;
471
472 static FARPROC ws2_find_api (LPCTSTR name)
473 {
474     FARPROC f = NULL;
475
476     HMODULE m = GetModuleHandle (TEXT("WS2_32"));
477     if (m != NULL)
478         f = GetProcAddress (m, name);
479
480     if (f == NULL)
481     {
482         /* Windows 2K IPv6 preview */
483         m = LoadLibrary (TEXT("WSHIP6"));
484         if (m != NULL)
485             f = GetProcAddress (m, name);
486     }
487
488     return f;
489 }
490
491 static WSAAPI int _ws2_getnameinfo_bind( const struct sockaddr FAR * sa, socklen_t salen,
492                char FAR *host, DWORD hostlen, char FAR *serv, DWORD servlen, int flags )
493 {
494     GETNAMEINFO entry = (GETNAMEINFO)ws2_find_api (TEXT("getnameinfo"));
495     int result;
496
497     if (entry == NULL)
498     {
499         /* not found, use replacement API instead */
500         entry = stub_getnameinfo;
501     }
502     /* call API before replacing function pointer to avoid crash */
503     result = entry (sa, salen, host, hostlen, serv, servlen, flags);
504     ws2_getnameinfo = entry;
505     return result;
506 }
507 #undef getnameinfo
508 #define getnameinfo ws2_getnameinfo
509
510 static WSAAPI int _ws2_getaddrinfo_bind(const char FAR *node, const char FAR *service,
511                const struct addrinfo FAR *hints, struct addrinfo FAR * FAR *res)
512 {
513     GETADDRINFO entry;
514     FREEADDRINFO freentry;
515     int result;
516
517     entry = (GETADDRINFO)ws2_find_api (TEXT("getaddrinfo"));
518     freentry = (FREEADDRINFO)ws2_find_api (TEXT("freeaddrinfo"));
519
520     if ((entry == NULL) ||  (freentry == NULL))
521     {
522         /* not found, use replacement API instead */
523         entry = stub_getaddrinfo;
524         freentry = stub_freeaddrinfo;
525     }
526     /* call API before replacing function pointer to avoid crash */
527     result = entry (node, service, hints, res);
528     ws2_freeaddrinfo = freentry;
529     ws2_getaddrinfo = entry;
530     return result;
531 }
532 #undef getaddrinfo
533 #undef freeaddrinfo
534 #define getaddrinfo ws2_getaddrinfo
535 #define freeaddrinfo ws2_freeaddrinfo
536 #define HAVE_GETADDRINFO
537 #endif
538
539
540 int vlc_getnameinfo( const struct sockaddr *sa, int salen,
541                      char *host, int hostlen, int *portnum, int flags )
542 {
543     char psz_servbuf[6], *psz_serv;
544     int i_servlen, i_val;
545
546     flags |= NI_NUMERICSERV;
547     if( portnum != NULL )
548     {
549         psz_serv = psz_servbuf;
550         i_servlen = sizeof( psz_servbuf );
551     }
552     else
553     {
554         psz_serv = NULL;
555         i_servlen = 0;
556     }
557
558     i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags);
559
560     if( portnum != NULL )
561         *portnum = atoi( psz_serv );
562
563     return i_val;
564 }
565
566
567 /**
568  * Resolves a host name to a list of socket addresses (like getaddrinfo()).
569  *
570  * @param p_this a VLC object
571  * @param node host name to resolve (encoded as UTF-8), or NULL
572  * @param i_port port number for the socket addresses
573  * @param p_hints parameters (see getaddrinfo() manual page)
574  * @param res pointer set to the resulting chained list.
575  * @return 0 on success, a getaddrinfo() error otherwise.
576  * On failure, *res is undefined. On success, it must be freed with
577  * vlc_freeaddrinfo().
578  */
579 int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
580                      int i_port, const struct addrinfo *p_hints,
581                      struct addrinfo **res )
582 {
583     struct addrinfo hints;
584     char psz_buf[NI_MAXHOST], psz_service[6];
585
586     /*
587      * In VLC, we always use port number as integer rather than strings
588      * for historical reasons (and portability).
589      */
590     if( ( i_port > 65535 ) || ( i_port < 0 ) )
591     {
592         msg_Err( p_this, "invalid port number %d specified", i_port );
593         return EAI_SERVICE;
594     }
595
596     /* cannot overflow */
597     snprintf( psz_service, 6, "%d", i_port );
598
599     /* Check if we have to force ipv4 or ipv6 */
600     memset (&hints, 0, sizeof (hints));
601     if (p_hints != NULL)
602     {
603         const int safe_flags =
604             AI_PASSIVE |
605             AI_CANONNAME |
606             AI_NUMERICHOST |
607             AI_NUMERICSERV |
608 #ifdef AI_ALL
609             AI_ALL |
610 #endif
611 #ifdef AI_ADDRCONFIG
612             AI_ADDRCONFIG |
613 #endif
614 #ifdef AI_V4MAPPED
615             AI_V4MAPPED |
616 #endif
617             0;
618
619         hints.ai_family = p_hints->ai_family;
620         hints.ai_socktype = p_hints->ai_socktype;
621         hints.ai_protocol = p_hints->ai_protocol;
622         /* Unfortunately, some flags chang the layout of struct addrinfo, so
623          * they cannot be copied blindly from p_hints to &hints. Therefore, we
624          * only copy flags that we know for sure are "safe".
625          */
626         hints.ai_flags = p_hints->ai_flags & safe_flags;
627     }
628
629     /* We only ever use port *numbers* */
630     hints.ai_flags |= AI_NUMERICSERV;
631
632     if( hints.ai_family == AF_UNSPEC )
633     {
634 #ifdef AF_INET6
635         if (var_CreateGetBool (p_this, "ipv6"))
636             hints.ai_family = AF_INET6;
637         else
638 #endif
639         if (var_CreateGetBool (p_this, "ipv4"))
640             hints.ai_family = AF_INET;
641     }
642
643     /*
644      * VLC extensions :
645      * - accept "" as NULL
646      * - ignore square brackets
647      */
648     if (node != NULL)
649     {
650         if (node[0] == '[')
651         {
652             size_t len = strlen (node + 1);
653             if ((len <= sizeof (psz_buf)) && (node[len] == ']'))
654             {
655                 assert (len > 0);
656                 memcpy (psz_buf, node + 1, len - 1);
657                 psz_buf[len - 1] = '\0';
658                 node = psz_buf;
659             }
660         }
661         if (node[0] == '\0')
662             node = NULL;
663     }
664
665     int ret;
666     node = ToLocale (node);
667 #ifdef WIN32
668     /*
669      * Winsock tries to resolve numerical IPv4 addresses as AAAA
670      * and IPv6 addresses as A... There comes the bug-to-bug fix.
671      */
672     if ((hints.ai_flags & AI_NUMERICHOST) == 0)
673     {
674         hints.ai_flags |= AI_NUMERICHOST;
675         ret = getaddrinfo (node, psz_service, &hints, res);
676         if (ret == 0)
677             goto out;
678         hints.ai_flags &= ~AI_NUMERICHOST;
679     }
680 #endif
681 #ifdef AI_IDN
682     /* Run-time I18n Domain Names support */
683     hints.ai_flags |= AI_IDN;
684     ret = getaddrinfo (node, psz_service, &hints, res);
685     if (ret != EAI_BADFLAGS)
686         goto out;
687     /* IDN not available: disable and retry without it */
688     hints.ai_flags &= ~AI_IDN;
689 #endif
690     ret = getaddrinfo (node, psz_service, &hints, res);
691
692 out:
693     LocaleFree (node);
694     return ret;
695 }
696
697
698 void vlc_freeaddrinfo( struct addrinfo *infos )
699 {
700     freeaddrinfo (infos);
701 }
702
703 /**
704  * inet_pton() replacement
705  */
706 int vlc_inet_pton (int af, const char *src, void *dst)
707 {
708 #ifndef HAVE_INET_PTON
709     /* Windows Vista has inet_pton(), but not XP. */
710     /* We have a pretty good example of abstraction inversion here... */
711     struct addrinfo hints = {
712         .ai_family = af,
713         .ai_socktype = SOCK_DGRAM, /* make sure we have... */
714         .ai_protocol = IPPROTO_UDP, /* ...only one response */
715         .ai_flags = AI_NUMERICHOST,
716     }, *res;
717
718     if (getaddrinfo (src, NULL, &hints, &res))
719         return 0;
720
721     const void *data;
722     size_t len;
723
724     switch (af)
725     {
726         case AF_INET:
727             data = &((const struct sockaddr_in *)res->ai_addr)->sin_addr;
728             len = sizeof (struct in_addr);
729             break;
730 #ifdef AF_INET6
731         case AF_INET6:
732             data = &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
733             len = sizeof (struct in6_addr);
734             break;
735 #endif
736         default:
737             freeaddrinfo (res);
738             return -1;
739     }
740     memcpy (dst, data, len);
741     freeaddrinfo (res);
742     return 1;
743 #else /* HAVE_INET_PTON */
744     return inet_pton( af, src, dst );
745 #endif /* HAVE_INET_PTON */
746 }
747
748 /**
749  * inet_ntop() replacement
750  */
751 const char *vlc_inet_ntop (int af, const void *src, char *dst, socklen_t cnt)
752 {
753 #ifndef HAVE_INET_NTOP
754     int ret = EAI_FAMILY;
755
756     switch (af)
757     {
758 #ifdef AF_INET6
759         case AF_INET6:
760             {
761                 struct sockaddr_in6 addr;
762                 memset (&addr, 0, sizeof(addr));
763                 addr.sin6_family = AF_INET6;
764                 addr.sin6_addr = *(struct in6_addr *)src;
765                 ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr),
766                                    dst, cnt, NULL, 0, NI_NUMERICHOST);
767             }
768
769 #endif
770         case AF_INET:
771             {
772                 struct sockaddr_in addr;
773                 memset(&addr, 0, sizeof(addr));
774                 addr.sin_family = AF_INET;
775                 addr.sin_addr = *(struct in_addr *)src;
776                 ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr),
777                                    dst, cnt, NULL, 0, NI_NUMERICHOST);
778             }
779     }
780     return (ret == 0) ? dst : NULL;
781 #else /* HAVE_INET_NTOP */
782     return inet_ntop( af, src, dst, cnt );
783 #endif /* HAVE_INET_NTOP */
784 }
785