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