]> git.sesse.net Git - vlc/blob - src/network/getaddrinfo.c
Fix incorrect string fix (revert [15025])
[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-2004 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 #include <vlc/vlc.h>
26
27 #include <stddef.h> /* size_t */
28 #include <string.h> /* strlen(), memcpy(), memset(), strchr() */
29 #include <stdlib.h> /* malloc(), free(), strtoul() */
30 #include <errno.h>
31
32 #ifdef HAVE_SYS_TYPES_H
33 #   include <sys/types.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 #   include <arpa/inet.h>
37 #endif
38 #ifdef HAVE_NETINET_IN_H
39 #   include <netinet/in.h>
40 #endif
41 #ifdef HAVE_UNISTD_H
42 #   include <unistd.h>
43 #endif
44
45 #include <vlc_network.h>
46
47 #ifndef NO_ADDRESS
48 #   define NO_ADDRESS  NO_DATA
49 #endif
50 #ifndef INADDR_NONE
51 #   define INADDR_NONE 0xFFFFFFFF
52 #endif
53 #ifndef AF_UNSPEC
54 #   define AF_UNSPEC   0
55 #endif
56
57 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
58                   NI_DGRAM)
59 #define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
60
61
62 #ifndef HAVE_GAI_STRERROR
63 static struct
64 {
65     int code;
66     const char *msg;
67 } const __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_SYSTEM,     "System error" },
81     { 0,              NULL }
82 };
83
84 static const char *__gai_unknownerr = "Unrecognized error number";
85
86 /****************************************************************************
87  * Converts an EAI_* error code into human readable english text.
88  ****************************************************************************/
89 const char *vlc_gai_strerror( int errnum )
90 {
91     int i;
92
93     for (i = 0; __gai_errlist[i].msg != NULL; i++)
94         if (errnum == __gai_errlist[i].code)
95             return __gai_errlist[i].msg;
96
97     return __gai_unknownerr;
98 }
99 # undef _EAI_POSITIVE_MAX
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 #if !(defined (HAVE_GETNAMEINFO) && defined (HAVE_GETADDRINFO))
108 /*
109  * Converts the current herrno error value into an EAI_* error code.
110  * That error code is normally returned by getnameinfo() or getaddrinfo().
111  */
112 static int
113 gai_error_from_herrno( void )
114 {
115     switch(h_errno)
116     {
117         case HOST_NOT_FOUND:
118             return EAI_NONAME;
119
120         case NO_ADDRESS:
121 # if (NO_ADDRESS != NO_DATA)
122         case NO_DATA:
123 # endif
124             return EAI_NODATA;
125
126         case NO_RECOVERY:
127             return EAI_FAIL;
128
129         case TRY_AGAIN:
130             return EAI_AGAIN;
131     }
132     return EAI_SYSTEM;
133 }
134 #endif /* if !(HAVE_GETNAMEINFO && HAVE_GETADDRINFO) */
135
136 #ifndef HAVE_GETNAMEINFO
137 /*
138  * getnameinfo() non-thread-safe IPv4-only implementation,
139  * Address-family-independant address to hostname translation
140  * (reverse DNS lookup in case of IPv4).
141  *
142  * This is meant for use on old IP-enabled systems that are not IPv6-aware,
143  * and probably do not have getnameinfo(), but have the old gethostbyaddr()
144  * function.
145  *
146  * GNU C library 2.0.x is known to lack this function, even though it defines
147  * getaddrinfo().
148  */
149 static int
150 __getnameinfo( const struct sockaddr *sa, socklen_t salen,
151                char *host, int hostlen, char *serv, int servlen, int flags )
152 {
153     if (((unsigned)salen < sizeof (struct sockaddr_in))
154      || (sa->sa_family != AF_INET))
155         return EAI_FAMILY;
156     else if (flags & (~_NI_MASK))
157         return EAI_BADFLAGS;
158     else
159     {
160         const struct sockaddr_in *addr;
161
162         addr = (const struct sockaddr_in *)sa;
163
164         if (host != NULL)
165         {
166             int solved = 0;
167
168             /* host name resolution */
169             if (!(flags & NI_NUMERICHOST))
170             {
171                 struct hostent *hent;
172
173                 hent = gethostbyaddr ((const void*)&addr->sin_addr,
174                                       4, AF_INET);
175
176                 if (hent != NULL)
177                 {
178                     strlcpy (host, hent->h_name, hostlen);
179
180                     /*
181                      * only keep first part of hostname
182                      * if user don't want fully qualified
183                      * domain name
184                      */
185                     if (flags & NI_NOFQDN)
186                     {
187                         char *ptr;
188
189                         ptr = strchr (host, '.');
190                         if (ptr != NULL)
191                             *ptr = 0;
192                     }
193
194                     solved = 1;
195                 }
196                 else if (flags & NI_NAMEREQD)
197                     return gai_error_from_herrno ();
198             }
199
200             if (!solved)
201                 /* inet_ntoa() can't fail */
202                 strlcpy (host, inet_ntoa (addr->sin_addr), hostlen);
203         }
204
205         if (serv != NULL)
206         {
207             struct servent *sent = NULL;
208
209 #ifndef SYS_BEOS /* No getservbyport() */
210             int solved = 0;
211
212             /* service name resolution */
213             if (!(flags & NI_NUMERICSERV))
214             {
215
216                 sent = getservbyport(addr->sin_port,
217                                      (flags & NI_DGRAM)
218                                      ? "udp" : "tcp");
219                 if (sent != NULL)
220                 {
221                     strlcpy (serv, sent->s_name, servlen);
222                     solved = 1;
223                 }
224             }
225             if (sent == NULL)
226 #endif
227             {
228                 snprintf (serv, servlen, "%u",
229                           (unsigned int)ntohs (addr->sin_port));
230                 serv[servlen - 1] = '\0';
231             }
232         }
233     }
234     return 0;
235 }
236
237 #endif /* if !HAVE_GETNAMEINFO */
238
239 #if defined( WIN32 ) && !defined( UNDER_CE )
240     /*
241      * Here is the kind of kludge you need to keep binary compatibility among
242      * varying OS versions...
243      */
244 typedef int (CALLBACK * GETNAMEINFO) ( const struct sockaddr*, socklen_t,
245                                            char*, DWORD, char*, DWORD, int );
246 typedef int (CALLBACK * GETADDRINFO) (const char *, const char *,
247                                           const struct addrinfo *,
248                                           struct addrinfo **);
249
250 typedef void (CALLBACK * FREEADDRINFO) ( struct addrinfo * );
251
252
253 static WINAPI int _ws2_getnameinfo_bind( const struct sockaddr *sa, socklen_t salen,
254                char *host, DWORD hostlen, char *serv, DWORD servlen, int flags );
255
256 static WINAPI int _ws2_getaddrinfo_bind(const char *node, const char *service,
257                const struct addrinfo *hints, struct addrinfo **res);
258
259 static WINAPI void _ws2_freeaddrinfo_bind( struct addrinfo *infos );
260
261 static GETNAMEINFO ws2_getnameinfo = _ws2_getnameinfo_bind;
262 static GETADDRINFO ws2_getaddrinfo = _ws2_getaddrinfo_bind;
263 static FREEADDRINFO ws2_freeaddrinfo = _ws2_freeaddrinfo_bind;
264
265 static int _ws2_find_ipv6_api(void)
266 {
267     /* For Windows XP and above, IPv6 stack is in WS2_32.DLL */
268     HINSTANCE module = LoadLibrary( "ws2_32.dll" );
269     if( module != NULL )
270     {
271         ws2_getnameinfo = (GETNAMEINFO)GetProcAddress( module, "getnameinfo" );
272         ws2_getaddrinfo = (GETADDRINFO)GetProcAddress( module, "getaddrinfo" );
273         ws2_freeaddrinfo = (FREEADDRINFO)GetProcAddress( module, "freeaddrinfo" );
274         if( ws2_getnameinfo && ws2_getaddrinfo && ws2_freeaddrinfo )
275         {
276             /* got them */
277             return 1;
278         }
279         FreeLibrary( module );
280
281         /* For Windows 2000 and below, try IPv6 stack in in WSHIP6.DLL */
282         module = LoadLibrary( "wship6.dll" );
283         if( module != NULL )
284         {
285             ws2_getnameinfo = (GETNAMEINFO)GetProcAddress( module, "getnameinfo" );
286             ws2_getaddrinfo = (GETADDRINFO)GetProcAddress( module, "getaddrinfo" );
287             ws2_freeaddrinfo = (FREEADDRINFO)GetProcAddress( module, "freeaddrinfo" );
288             if( ws2_getnameinfo && ws2_getaddrinfo && ws2_freeaddrinfo )
289             {
290                 /* got them */
291                 return 1;
292             }
293             FreeLibrary( module );
294         }
295     }
296     /* no API */
297     return 0;
298 }
299
300 static WINAPI int _ws2_getnameinfo_bind( const struct sockaddr *sa, socklen_t salen,
301                char *host, DWORD hostlen, char *serv, DWORD servlen, int flags )
302 {
303     if( _ws2_find_ipv6_api() )
304     {
305         return ws2_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
306     }
307     /* return a possible error if API is not found */
308     WSASetLastError(WSAHOST_NOT_FOUND);
309     return WSAHOST_NOT_FOUND;
310 }
311
312 static WINAPI int _ws2_getaddrinfo_bind(const char *node, const char *service,
313                const struct addrinfo *hints, struct addrinfo **res)
314 {
315     if( _ws2_find_ipv6_api() )
316     {
317         return ws2_getaddrinfo(node, service, hints, res);
318     }
319     /* return a possible error if API is not found */
320     WSASetLastError(WSAHOST_NOT_FOUND);
321     return WSAHOST_NOT_FOUND;
322 }
323
324 static WINAPI void _ws2_freeaddrinfo_bind( struct addrinfo *infos )
325 {
326     if( _ws2_find_ipv6_api() )
327     {
328         ws2_freeaddrinfo(infos);
329     }
330 }
331
332 #endif
333
334 #ifndef HAVE_GETADDRINFO
335 /*
336  * This functions must be used to free the memory allocated by getaddrinfo().
337  */
338 static void
339 __freeaddrinfo (struct addrinfo *res)
340 {
341     if (res != NULL)
342     {
343         if (res->ai_canonname != NULL)
344             free (res->ai_canonname);
345         if (res->ai_addr != NULL)
346             free (res->ai_addr);
347         if (res->ai_next != NULL)
348             free (res->ai_next);
349         free (res);
350     }
351 }
352
353
354 /*
355  * Internal function that builds an addrinfo struct.
356  */
357 static struct addrinfo *
358 makeaddrinfo (int af, int type, int proto,
359               const struct sockaddr *addr, size_t addrlen,
360               const char *canonname)
361 {
362     struct addrinfo *res;
363
364     res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
365     if (res != NULL)
366     {
367         res->ai_flags = 0;
368         res->ai_family = af;
369         res->ai_socktype = type;
370         res->ai_protocol = proto;
371         res->ai_addrlen = addrlen;
372         res->ai_addr = malloc (addrlen);
373         res->ai_canonname = NULL;
374         res->ai_next = NULL;
375
376         if (res->ai_addr != NULL)
377         {
378             memcpy (res->ai_addr, addr, addrlen);
379
380             if (canonname != NULL)
381             {
382                 res->ai_canonname = strdup (canonname);
383                 if (res->ai_canonname != NULL)
384                     return res; /* success ! */
385             }
386             else
387                 return res;
388         }
389     }
390     /* failsafe */
391     vlc_freeaddrinfo (res);
392     return NULL;
393 }
394
395
396 static struct addrinfo *
397 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
398 {
399     struct sockaddr_in addr;
400
401     memset (&addr, 0, sizeof (addr));
402     addr.sin_family = AF_INET;
403 # ifdef HAVE_SA_LEN
404     addr.sin_len = sizeof (addr);
405 # endif
406     addr.sin_port = port;
407     addr.sin_addr.s_addr = ip;
408
409     return makeaddrinfo (AF_INET, type, proto,
410                          (struct sockaddr*)&addr, sizeof (addr), name);
411 }
412
413
414 /*
415  * getaddrinfo() non-thread-safe IPv4-only implementation
416  * Address-family-independant hostname to address resolution.
417  *
418  * This is meant for IPv6-unaware systems that do probably not provide
419  * getaddrinfo(), but still have old function gethostbyname().
420  *
421  * Only UDP and TCP over IPv4 are supported here.
422  */
423 static int
424 __getaddrinfo (const char *node, const char *service,
425                const struct addrinfo *hints, struct addrinfo **res)
426 {
427     struct addrinfo *info;
428     u_long ip;
429     u_short port;
430     int protocol = 0, flags = 0;
431     const char *name = NULL;
432
433 #ifdef WIN32
434     /*
435      * Maybe you knew already that Winsock does not handle TCP/RST packets
436      * properly, so that when a TCP connection fails, it will wait until it
437      * times out even if the remote host did return a TCP/RST. However, it
438      * still sees the TCP/RST as the error code is 10061 instead of 10060.
439      * Basically, we have the stupid brainfucked behavior with DNS queries...
440      * When the recursive DNS server returns an error, Winsock waits about
441      * 2 seconds before it returns to the callers, even though it should know
442      * that is pointless. I'd like to know how come this hasn't been fixed
443      * for the past decade, or maybe not.
444      *
445      * Anyway, this is causing a severe delay when the SAP listener tries
446      * to resolve more than ten IPv6 numeric addresses. Modern systems will
447      * eventually realize that it is an IPv6 address, and won't try to resolve
448      * it as a IPv4 address via the Domain Name Service. Old systems
449      * (including Windows XP without the IPv6 stack) will not. It is normally
450      * not an issue as the DNS server usually returns an error very quickly.
451      * But it IS a severe issue on Windows, given the bug explained above.
452      * So here comes one more bug-to-bug Windows compatibility fix.
453      */
454     if ((node != NULL) && (strchr (node, ':') != NULL))
455        return EAI_NONAME;
456 #endif
457
458     if (hints != NULL)
459     {
460         flags = hints->ai_flags;
461
462         if (flags & ~_AI_MASK)
463             return EAI_BADFLAGS;
464         /* only accept AF_INET and AF_UNSPEC */
465         if (hints->ai_family && (hints->ai_family != AF_INET))
466             return EAI_FAMILY;
467
468         /* protocol sanity check */
469         switch (hints->ai_socktype)
470         {
471             case SOCK_STREAM:
472                 protocol = IPPROTO_TCP;
473                 break;
474
475             case SOCK_DGRAM:
476                 protocol = IPPROTO_UDP;
477                 break;
478
479 #ifndef SYS_BEOS
480             case SOCK_RAW:
481 #endif
482             case 0:
483                 break;
484
485             default:
486                 return EAI_SOCKTYPE;
487         }
488         if (hints->ai_protocol && protocol
489          && (protocol != hints->ai_protocol))
490             return EAI_SERVICE;
491     }
492
493     *res = NULL;
494
495     /* default values */
496     if (node == NULL)
497     {
498         if (flags & AI_PASSIVE)
499             ip = htonl (INADDR_ANY);
500         else
501             ip = htonl (INADDR_LOOPBACK);
502     }
503     else
504     if ((ip = inet_addr (node)) == INADDR_NONE)
505     {
506         struct hostent *entry = NULL;
507
508         /* hostname resolution */
509         if (!(flags & AI_NUMERICHOST))
510             entry = gethostbyname (node);
511
512         if (entry == NULL)
513             return EAI_NONAME;
514
515         if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
516             return EAI_FAMILY;
517
518         ip = *((u_long *) entry->h_addr);
519         if (flags & AI_CANONNAME)
520             name = entry->h_name;
521     }
522
523     if ((flags & AI_CANONNAME) && (name == NULL))
524         name = node;
525
526     /* service resolution */
527     if (service == NULL)
528         port = 0;
529     else
530     {
531         long d;
532         char *end;
533
534         d = strtoul (service, &end, 0);
535         if (end[0] /* service is not a number */
536          || (d > 65535))
537         {
538             struct servent *entry;
539             const char *protoname;
540
541             switch (protocol)
542             {
543                 case IPPROTO_TCP:
544                     protoname = "tcp";
545                     break;
546
547                 case IPPROTO_UDP:
548                     protoname = "udp";
549                     break;
550
551                 default:
552                     protoname = NULL;
553             }
554
555             entry = getservbyname (service, protoname);
556             if (entry == NULL)
557                 return EAI_SERVICE;
558
559             port = entry->s_port;
560         }
561         else
562             port = htons ((u_short)d);
563     }
564
565     /* building results... */
566     if ((!protocol) || (protocol == IPPROTO_UDP))
567     {
568         info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
569         if (info == NULL)
570         {
571             errno = ENOMEM;
572             return EAI_SYSTEM;
573         }
574         if (flags & AI_PASSIVE)
575             info->ai_flags |= AI_PASSIVE;
576         *res = info;
577     }
578     if ((!protocol) || (protocol == IPPROTO_TCP))
579     {
580         info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
581         if (info == NULL)
582         {
583             errno = ENOMEM;
584             return EAI_SYSTEM;
585         }
586         info->ai_next = *res;
587         if (flags & AI_PASSIVE)
588             info->ai_flags |= AI_PASSIVE;
589         *res = info;
590     }
591
592     return 0;
593 }
594 #endif /* if !HAVE_GETADDRINFO */
595
596
597 int vlc_getnameinfo( const struct sockaddr *sa, int salen,
598                      char *host, int hostlen, int *portnum, int flags )
599 {
600     char psz_servbuf[6], *psz_serv;
601     int i_servlen, i_val;
602
603     flags |= NI_NUMERICSERV;
604     if( portnum != NULL )
605     {
606         psz_serv = psz_servbuf;
607         i_servlen = sizeof( psz_servbuf );
608     }
609     else
610     {
611         psz_serv = NULL;
612         i_servlen = 0;
613     }
614 #if defined( WIN32 ) && !defined( UNDER_CE )
615     i_val = ws2_getnameinfo( sa, salen, host, hostlen, psz_serv,
616                                  i_servlen, flags );
617 #endif
618 #if defined( HAVE_GETNAMEINFO ) || defined( UNDER_CE )
619     i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags);
620 #else
621     {
622 # ifdef HAVE_USABLE_MUTEX_THAT_DONT_NEED_LIBVLC_POINTER
623         static vlc_value_t lock;
624
625         /* my getnameinfo implementation is not thread-safe as it uses
626          * gethostbyaddr and the likes */
627         vlc_mutex_lock( lock.p_address );
628 #else
629 //# warning FIXME : This is not thread-safe!
630 #endif
631         i_val = __getnameinfo( sa, salen, host, hostlen, psz_serv, i_servlen,
632                                flags );
633 # ifdef HAVE_USABLE_MUTEX_THAT_DONT_NEED_LIBVLC_POINTER
634         vlc_mutex_unlock( lock.p_address );
635 # endif
636     }
637 #endif
638
639     if( portnum != NULL )
640         *portnum = atoi( psz_serv );
641
642     return i_val;
643 }
644
645
646 /* TODO: support for setting sin6_scope_id */
647 int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
648                      int i_port, const struct addrinfo *p_hints,
649                      struct addrinfo **res )
650 {
651     struct addrinfo hints;
652     char psz_buf[NI_MAXHOST], *psz_node, psz_service[6];
653
654     /*
655      * In VLC, we always use port number as integer rather than strings
656      * for historical reasons (and portability).
657      */
658     if( ( i_port > 65535 ) || ( i_port < 0 ) )
659     {
660         msg_Err( p_this, "invalid port number %d specified", i_port );
661         return EAI_SERVICE;
662     }
663
664     /* cannot overflow */
665     snprintf( psz_service, 6, "%d", i_port );
666
667     /* Check if we have to force ipv4 or ipv6 */
668     if( p_hints == NULL )
669         memset( &hints, 0, sizeof( hints ) );
670     else
671         memcpy( &hints, p_hints, sizeof( hints ) );
672
673     if( hints.ai_family == AF_UNSPEC )
674     {
675         vlc_value_t val;
676
677         var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
678         var_Get( p_this, "ipv4", &val );
679         if( val.b_bool )
680             hints.ai_family = AF_INET;
681
682 #ifdef AF_INET6
683         var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
684         var_Get( p_this, "ipv6", &val );
685         if( val.b_bool )
686             hints.ai_family = AF_INET6;
687 #endif
688     }
689
690     /*
691      * VLC extensions :
692      * - accept "" as NULL
693      * - ignore square brackets
694      */
695     if( ( node == NULL ) || (node[0] == '\0' ) )
696     {
697         psz_node = NULL;
698     }
699     else
700     {
701         strlcpy( psz_buf, node, NI_MAXHOST );
702
703         psz_node = psz_buf;
704
705         if( psz_buf[0] == '[' )
706         {
707             char *ptr;
708
709             ptr = strrchr( psz_buf, ']' );
710             if( ( ptr != NULL ) && (ptr[1] == '\0' ) )
711             {
712                 *ptr = '\0';
713                 psz_node++;
714             }
715         }
716     }
717
718 #if defined( WIN32 ) && !defined( UNDER_CE )
719     /*
720      * Winsock tries to resolve numerical IPv4 addresses as AAAA
721      * and IPv6 addresses as A... There comes the work around.
722      */
723     if ((hints.ai_flags & AI_NUMERICHOST) == 0)
724     {
725         hints.ai_flags |= AI_NUMERICHOST;
726
727         if (ws2_getaddrinfo (psz_node, psz_service, &hints, res) == 0)
728             return 0;
729
730         hints.ai_flags &= ~AI_NUMERICHOST;
731     }
732
733     return ws2_getaddrinfo (psz_node, psz_service, &hints, res);
734 #endif
735 #if defined( HAVE_GETADDRINFO ) || defined( UNDER_CE )
736 # ifdef AI_IDN
737     /* Run-time I18n Domain Names support */
738     {
739         static vlc_bool_t i_idn = VLC_TRUE; /* beware of thread-safety */
740
741         if( i_idn )
742         {
743             int i_ret;
744
745             hints.ai_flags |= AI_IDN;
746             i_ret = getaddrinfo( psz_node, psz_service, &hints, res );
747
748             if( i_ret != EAI_BADFLAGS )
749                 return i_ret;
750
751             /* libidn not available: disable and retry without it */
752
753             /* NOTE: Using i_idn here would not be thread-safe */
754             hints.ai_flags &= ~AI_IDN;
755             i_idn = VLC_FALSE;
756             msg_Dbg( p_this, "International Domain Names not supported - " \
757                 "disabled" );
758         }
759     }
760 # endif
761     return getaddrinfo( psz_node, psz_service, &hints, res );
762 #else
763 {
764     int i_ret;
765
766     vlc_value_t lock;
767
768     var_Create( p_this->p_libvlc, "getaddrinfo_mutex", VLC_VAR_MUTEX );
769     var_Get( p_this->p_libvlc, "getaddrinfo_mutex", &lock );
770     vlc_mutex_lock( lock.p_address );
771
772     i_ret = __getaddrinfo( psz_node, psz_service, &hints, res );
773     vlc_mutex_unlock( lock.p_address );
774     return i_ret;
775 }
776 #endif
777 }
778
779
780 void vlc_freeaddrinfo( struct addrinfo *infos )
781 {
782 #if defined( WIN32 ) && !defined( UNDER_CE )
783     ws2_freeaddrinfo( infos );
784 #endif
785 #if defined( HAVE_GETADDRINFO ) || defined( UNDER_CE )
786     freeaddrinfo( infos );
787 #else
788     __freeaddrinfo( infos );
789 #endif
790 }