File: getaddrinfo.c

package info (click to toggle)
timidity 2.14.0-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,364 kB
  • sloc: ansic: 168,735; sh: 3,730; makefile: 1,405; tcl: 1,048; perl: 285; ruby: 126
file content (833 lines) | stat: -rw-r--r-- 23,032 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
#if (defined WIN32) && (_WIN32_WINNT < 0x0501)

/*****************************************************************************
 * getaddrinfo.c: getaddrinfo/getnameinfo replacement functions
 *****************************************************************************
 * Copyright (C) 2005 the VideoLAN team
 * Copyright (C) 2002-2007 Rémi Denis-Courmont
 * $Id$
 *
 * Author: Rémi Denis-Courmont <rem # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#if 0
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_charset.h>


#include <stddef.h> /* size_t */
#include <string.h> /* strlen(), memcpy(), memset(), strchr() */
#include <stdlib.h> /* malloc(), free(), strtoul() */
#include <errno.h>
#include <assert.h>

#ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
#endif
#ifdef HAVE_ARPA_INET_H
#   include <arpa/inet.h>
#endif
#ifdef HAVE_NETINET_IN_H
#   include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif

#include <vlc_network.h>

#else

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>

#include "timidity.h"
#include "common.h"
#include "net.h"

/* GAI error codes */
# ifndef EAI_BADFLAGS
#  define EAI_BADFLAGS -1
# endif
# ifndef EAI_NONAME
#  define EAI_NONAME -2
# endif
# ifndef EAI_AGAIN
#  define EAI_AGAIN -3
# endif
# ifndef EAI_FAIL
#  define EAI_FAIL -4
# endif
# ifndef EAI_NODATA
#  define EAI_NODATA -5
# endif
# ifndef EAI_FAMILY
#  define EAI_FAMILY -6
# endif
# ifndef EAI_SOCKTYPE
#  define EAI_SOCKTYPE -7
# endif
# ifndef EAI_SERVICE
#  define EAI_SERVICE -8
# endif
# ifndef EAI_ADDRFAMILY
#  define EAI_ADDRFAMILY -9
# endif
# ifndef EAI_MEMORY
#  define EAI_MEMORY -10
# endif
#ifndef EAI_OVERFLOW
#  define EAI_OVERFLOW -11
#endif
# ifndef EAI_SYSTEM
#  define EAI_SYSTEM -12
# endif

#endif 

#ifndef NO_ADDRESS
#   define NO_ADDRESS  NO_DATA
#endif
#ifndef INADDR_NONE
#   define INADDR_NONE 0xFFFFFFFF
#endif
#ifndef AF_UNSPEC
#   define AF_UNSPEC   0
#endif


#ifndef HAVE_GAI_STRERROR
static const struct
{
    int        code;
    const char msg[41];
} gai_errlist[] =
{
    { 0,              "Error 0" },
    { EAI_BADFLAGS,   "Invalid flag used" },
    { EAI_NONAME,     "Host or service not found" },
    { EAI_AGAIN,      "Temporary name service failure" },
    { EAI_FAIL,       "Non-recoverable name service failure" },
    { EAI_NODATA,     "No data for host name" },
    { EAI_FAMILY,     "Unsupported address family" },
    { EAI_SOCKTYPE,   "Unsupported socket type" },
    { EAI_SERVICE,    "Incompatible service for socket type" },
    { EAI_ADDRFAMILY, "Unavailable address family for host name" },
    { EAI_MEMORY,     "Memory allocation failure" },
    { EAI_OVERFLOW,   "Buffer overflow" },
    { EAI_SYSTEM,     "System error" },
    { 0,              "" },
};
static const char gai_unknownerr[] = "Unrecognized error number";

/****************************************************************************
 * Converts an EAI_* error code into human readable english text.
 ****************************************************************************/
const char *vlc_gai_strerror (int errnum)
{
	unsigned i;
    for ( i = 0; *gai_errlist[i].msg; i++)
        if (errnum == gai_errlist[i].code)
            return gai_errlist[i].msg;

    return gai_unknownerr;
}
#else /* ifndef HAVE_GAI_STRERROR */
const char *vlc_gai_strerror (int errnum)
{
    return gai_strerror (errnum);
}
#endif

#ifndef HAVE_GETNAMEINFO
#define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
                  NI_DGRAM)
/*
 * getnameinfo() non-thread-safe IPv4-only implementation,
 * Address-family-independent address to hostname translation
 * (reverse DNS lookup in case of IPv4).
 *
 * This is meant for use on old IP-enabled systems that are not IPv6-aware,
 * and probably do not have getnameinfo(), but have the old gethostbyaddr()
 * function.
 *
 * GNU C library 2.0.x is known to lack this function, even though it defines
 * getaddrinfo().
 */
#ifdef WIN32
static int WSAAPI
stub_getnameinfo (const struct sockaddr *sa, socklen_t salen,
             char *host, DWORD hostlen, char *serv, DWORD servlen, int flags)
#else
static int
stub_getnameinfo (const struct sockaddr *sa, socklen_t salen,
             char *host, int hostlen, char *serv, int servlen, int flags)
#endif
{
    if (((size_t)salen < sizeof (struct sockaddr_in))
     || (sa->sa_family != AF_INET))
        return EAI_FAMILY;
    else if (flags & (~_NI_MASK))
        return EAI_BADFLAGS;
    else
    {
        const struct sockaddr_in *addr;

        addr = (const struct sockaddr_in *)sa;

        if (host != NULL)
        {
            /* host name resolution */
            if (!(flags & NI_NUMERICHOST))
            {
                if (flags & NI_NAMEREQD)
                    return EAI_NONAME;
            }

            /* inet_ntoa() is not thread-safe, do not use it */
            uint32_t ipv4 = ntohl (addr->sin_addr.s_addr);

            if (snprintf (host, hostlen, "%u.%u.%u.%u", ipv4 >> 24,
                          (ipv4 >> 16) & 0xff, (ipv4 >> 8) & 0xff,
                          ipv4 & 0xff) >= (int)hostlen)
                return EAI_OVERFLOW;
        }

        if (serv != NULL)
        {
            if (snprintf (serv, servlen, "%u",
                          (unsigned int)ntohs (addr->sin_port)) >= (int)servlen)
                return EAI_OVERFLOW;
        }
    }
    return 0;
}
#undef getnameinfo
#define getnameifo stub_getnameinfo
#endif /* if !HAVE_GETNAMEINFO */

#ifndef HAVE_GETADDRINFO
#define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
/*
 * Converts the current herrno error value into an EAI_* error code.
 * That error code is normally returned by getnameinfo() or getaddrinfo().
 */
static int
gai_error_from_herrno (void)
{
    switch (h_errno)
    {
        case HOST_NOT_FOUND:
            return EAI_NONAME;

        case NO_ADDRESS:
# if (NO_ADDRESS != NO_DATA)
        case NO_DATA:
# endif
            return EAI_NODATA;

        case NO_RECOVERY:
            return EAI_FAIL;

        case TRY_AGAIN:
            return EAI_AGAIN;
    }
    return EAI_SYSTEM;
}

/*
 * This functions must be used to free the memory allocated by getaddrinfo().
 */
#ifdef WIN32
static void WSAAPI stub_freeaddrinfo (struct addrinfo *res)
#else
static void stub_freeaddrinfo (struct addrinfo *res)
#endif
{
    if (res == NULL)
        return;
    free (res->ai_canonname);
    free (res->ai_addr);
    free (res->ai_next);
    free (res);
}


/*
 * Internal function that builds an addrinfo struct.
 */
void vlc_freeaddrinfo( struct addrinfo *infos );

static struct addrinfo *
makeaddrinfo (int af, int type, int proto,
              const struct sockaddr *addr, size_t addrlen,
              const char *canonname)
{
    struct addrinfo *res;

    res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
    if (res != NULL)
    {
        res->ai_flags = 0;
        res->ai_family = af;
        res->ai_socktype = type;
        res->ai_protocol = proto;
        res->ai_addrlen = addrlen;
        res->ai_addr = malloc (addrlen);
        res->ai_canonname = NULL;
        res->ai_next = NULL;

        if (res->ai_addr != NULL)
        {
            memcpy (res->ai_addr, addr, addrlen);

            if (canonname != NULL)
            {
                res->ai_canonname = strdup (canonname);
                if (res->ai_canonname != NULL)
                    return res; /* success ! */
            }
            else
                return res;
        }
    }
    /* failsafe */
    vlc_freeaddrinfo (res);
    return NULL;
}


static struct addrinfo *
makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
{
    struct sockaddr_in addr;

    memset (&addr, 0, sizeof (addr));
    addr.sin_family = AF_INET;
# ifdef HAVE_SA_LEN
    addr.sin_len = sizeof (addr);
# endif
    addr.sin_port = port;
    addr.sin_addr.s_addr = ip;

    return makeaddrinfo (AF_INET, type, proto,
                         (struct sockaddr*)&addr, sizeof (addr), name);
}


/*
 * getaddrinfo() non-thread-safe IPv4-only implementation
 * Address-family-independent hostname to address resolution.
 *
 * This is meant for IPv6-unaware systems that do probably not provide
 * getaddrinfo(), but still have old function gethostbyname().
 *
 * Only UDP and TCP over IPv4 are supported here.
 */
#ifdef WIN32
static int WSAAPI
stub_getaddrinfo (const char *node, const char *service,
             const struct addrinfo *hints, struct addrinfo **res)
#else
static int
stub_getaddrinfo (const char *node, const char *service,
             const struct addrinfo *hints, struct addrinfo **res)
#endif
{
    struct addrinfo *info;
    u_long ip;
    u_short port;
    int protocol = 0, flags = 0;
    const char *name = NULL;

#ifdef WIN32
    /*
     * Maybe you knew already that Winsock does not handle TCP/RST packets
     * properly, so that when a TCP connection fails, it will wait until it
     * times out even if the remote host did return a TCP/RST. However, it
     * still sees the TCP/RST as the error code is 10061 instead of 10060.
     * Basically, we have the stupid brainfucked behavior with DNS queries...
     * When the recursive DNS server returns an error, Winsock waits about
     * 2 seconds before it returns to the callers, even though it should know
     * that is pointless. I'd like to know how come this hasn't been fixed
     * for the past decade, or maybe not.
     *
     * Anyway, this is causing a severe delay when the SAP listener tries
     * to resolve more than ten IPv6 numeric addresses. Modern systems will
     * eventually realize that it is an IPv6 address, and won't try to resolve
     * it as a IPv4 address via the Domain Name Service. Old systems
     * (including Windows XP without the IPv6 stack) will not. It is normally
     * not an issue as the DNS server usually returns an error very quickly.
     * But it IS a severe issue on Windows, given the bug explained above.
     * So here comes one more bug-to-bug Windows compatibility fix.
     */
    if ((node != NULL) && (strchr (node, ':') != NULL))
       return EAI_NONAME;
#endif

    if (hints != NULL)
    {
        flags = hints->ai_flags;

        if (flags & ~_AI_MASK)
            return EAI_BADFLAGS;
        /* only accept AF_INET and AF_UNSPEC */
        if (hints->ai_family && (hints->ai_family != AF_INET))
            return EAI_FAMILY;

        /* protocol sanity check */
        switch (hints->ai_socktype)
        {
            case SOCK_STREAM:
                protocol = IPPROTO_TCP;
                break;

            case SOCK_DGRAM:
                protocol = IPPROTO_UDP;
                break;

#ifndef SOCK_RAW
            case SOCK_RAW:
#endif
            case 0:
                break;

            default:
                return EAI_SOCKTYPE;
        }
        if (hints->ai_protocol && protocol
         && (protocol != hints->ai_protocol))
            return EAI_SERVICE;
    }

    *res = NULL;

    /* default values */
    if (node == NULL)
    {
        if (flags & AI_PASSIVE)
            ip = htonl (INADDR_ANY);
        else
            ip = htonl (INADDR_LOOPBACK);
    }
    else
    if ((ip = inet_addr (node)) == INADDR_NONE)
    {
        struct hostent *entry = NULL;

        /* hostname resolution */
        if (!(flags & AI_NUMERICHOST))
            entry = gethostbyname (node);

        if (entry == NULL)
            return gai_error_from_herrno ();

        if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
            return EAI_FAMILY;

        ip = *((u_long *) entry->h_addr);
        if (flags & AI_CANONNAME)
            name = entry->h_name;
    }

    if ((flags & AI_CANONNAME) && (name == NULL))
        name = node;

    /* service resolution */
    if (service == NULL)
        port = 0;
    else
    {
        unsigned long d;
        char *end;

        d = strtoul (service, &end, 0);
        if (end[0] || (d > 65535u))
            return EAI_SERVICE;

        port = htons ((u_short)d);
    }

    /* building results... */
    if ((!protocol) || (protocol == IPPROTO_UDP))
    {
        info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
        if (info == NULL)
        {
            errno = ENOMEM;
            return EAI_SYSTEM;
        }
        if (flags & AI_PASSIVE)
            info->ai_flags |= AI_PASSIVE;
        *res = info;
    }
    if ((!protocol) || (protocol == IPPROTO_TCP))
    {
        info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
        if (info == NULL)
        {
            errno = ENOMEM;
            return EAI_SYSTEM;
        }
        info->ai_next = *res;
        if (flags & AI_PASSIVE)
            info->ai_flags |= AI_PASSIVE;
        *res = info;
    }

    return 0;
}
#undef getaddrinfo
#define getaddrifo stub_getaddrinfo
#undef freeaddrinfo
#define freeaddrifo stub_freeaddrinfo
#endif /* if !HAVE_GETADDRINFO */

#if defined( WIN32 ) && !defined( UNDER_CE )
    /*
     * Here is the kind of kludge you need to keep binary compatibility among
     * varying OS versions...
     */
typedef int (WSAAPI * GETNAMEINFO) ( const struct sockaddr FAR *, socklen_t,
                                           char FAR *, DWORD, char FAR *, DWORD, int );
typedef int (WSAAPI * GETADDRINFO) (const char FAR *, const char FAR *,
                                          const struct addrinfo FAR *,
                                          struct addrinfo FAR * FAR *);

typedef void (WSAAPI * FREEADDRINFO) ( struct addrinfo FAR * );

static int WSAAPI _ws2_getnameinfo_bind ( const struct sockaddr FAR *, socklen_t,
                                           char FAR *, DWORD, char FAR *, DWORD, int );
static int WSAAPI _ws2_getaddrinfo_bind (const char FAR *, const char FAR *,
                                          const struct addrinfo FAR *,
                                          struct addrinfo FAR * FAR *);

static GETNAMEINFO ws2_getnameinfo = _ws2_getnameinfo_bind;
GETADDRINFO ws2_getaddrinfo = _ws2_getaddrinfo_bind;
FREEADDRINFO ws2_freeaddrinfo;

static FARPROC ws2_find_api (LPCTSTR name)
{
    FARPROC f = NULL;

    HMODULE m = GetModuleHandle (TEXT("WS2_32"));
    if (m != NULL)
        f = GetProcAddress (m, name);

    if (f == NULL)
    {
        /* Windows 2K IPv6 preview */
        m = LoadLibrary (TEXT("WSHIP6"));
        if (m != NULL)
            f = GetProcAddress (m, name);
    }

    return f;
}

static int WSAAPI  _ws2_getnameinfo_bind( const struct sockaddr FAR * sa, socklen_t salen,
               char FAR *host, DWORD hostlen, char FAR *serv, DWORD servlen, int flags )
{
    GETNAMEINFO entry = (GETNAMEINFO)ws2_find_api (TEXT("getnameinfo"));
    int result;

    if (entry == NULL)
    {
        /* not found, use replacement API instead */
        entry = stub_getnameinfo;
    }
    /* call API before replacing function pointer to avoid crash */
    result = entry (sa, salen, host, hostlen, serv, servlen, flags);
    ws2_getnameinfo = entry;
    return result;
}
#undef getnameinfo
#define getnameinfo ws2_getnameinfo

static int WSAAPI  _ws2_getaddrinfo_bind(const char FAR *node, const char FAR *service,
               const struct addrinfo FAR *hints, struct addrinfo FAR * FAR *res)

{
    GETADDRINFO entry;
    FREEADDRINFO freentry;
    int result;

    entry = (GETADDRINFO)ws2_find_api (TEXT("getaddrinfo"));
    freentry = (FREEADDRINFO)ws2_find_api (TEXT("freeaddrinfo"));

    if ((entry == NULL) ||  (freentry == NULL))
    {
        /* not found, use replacement API instead */
        entry = stub_getaddrinfo;
        freentry = stub_freeaddrinfo;
    }
    /* call API before replacing function pointer to avoid crash */
    result = entry (node, service, hints, res);
    ws2_freeaddrinfo = freentry;
    ws2_getaddrinfo = entry;
    return result;
}
#undef getaddrinfo
#undef freeaddrinfo
#define getaddrinfo ws2_getaddrinfo
#define freeaddrinfo ws2_freeaddrinfo
#define HAVE_GETADDRINFO
#endif

#if 0
int vlc_getnameinfo( const struct sockaddr *sa, int salen,
                     char *host, int hostlen, int *portnum, int flags )
{
    char psz_servbuf[6], *psz_serv;
    int i_servlen, i_val;

    flags |= NI_NUMERICSERV;
    if( portnum != NULL )
    {
        psz_serv = psz_servbuf;
        i_servlen = sizeof( psz_servbuf );
    }
    else
    {
        psz_serv = NULL;
        i_servlen = 0;
    }

    i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags);

    if( portnum != NULL )
        *portnum = atoi( psz_serv );

    return i_val;
}


int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
                     int i_port, const struct addrinfo *p_hints,
                     struct addrinfo **res )
{
    struct addrinfo hints;
    char psz_buf[NI_MAXHOST], psz_service[6];

    /*
     * In VLC, we always use port number as integer rather than strings
     * for historical reasons (and portability).
     */
    if( ( i_port > 65535 ) || ( i_port < 0 ) )
    {
        msg_Err( p_this, "invalid port number %d specified", i_port );
        return EAI_SERVICE;
    }

    /* cannot overflow */
    snprintf( psz_service, 6, "%d", i_port );

    /* Check if we have to force ipv4 or ipv6 */
    memset (&hints, 0, sizeof (hints));
    if (p_hints != NULL)
    {
        const int safe_flags =
            AI_PASSIVE |
            AI_CANONNAME |
            AI_NUMERICHOST |
            AI_NUMERICSERV |
#ifdef AI_ALL
            AI_ALL |
#endif
#ifdef AI_ADDRCONFIG
            AI_ADDRCONFIG |
#endif
#ifdef AI_V4MAPPED
            AI_V4MAPPED |
#endif
            0;

        hints.ai_family = p_hints->ai_family;
        hints.ai_socktype = p_hints->ai_socktype;
        hints.ai_protocol = p_hints->ai_protocol;
        /* Unfortunately, some flags chang the layout of struct addrinfo, so
         * they cannot be copied blindly from p_hints to &hints. Therefore, we
         * only copy flags that we know for sure are "safe".
         */
        hints.ai_flags = p_hints->ai_flags & safe_flags;
    }

    /* We only ever use port *numbers* */
    hints.ai_flags |= AI_NUMERICSERV;

    if( hints.ai_family == AF_UNSPEC )
    {
#ifdef AF_INET6
        if (var_CreateGetBool (p_this, "ipv6"))
            hints.ai_family = AF_INET6;
        else
#endif
        if (var_CreateGetBool (p_this, "ipv4"))
            hints.ai_family = AF_INET;
    }

    /*
     * VLC extensions :
     * - accept "" as NULL
     * - ignore square brackets
     */
    if (node != NULL)
    {
        if (node[0] == '[')
        {
            size_t len = strlen (node + 1);
            if ((len <= sizeof (psz_buf)) && (node[len] == ']'))
            {
                assert (len > 0);
                memcpy (psz_buf, node + 1, len - 1);
                psz_buf[len - 1] = '\0';
                node = psz_buf;
            }
        }
        if (node[0] == '\0')
            node = NULL;
    }

    int ret;
    node = ToLocale (node);
#ifdef WIN32
    /*
     * Winsock tries to resolve numerical IPv4 addresses as AAAA
     * and IPv6 addresses as A... There comes the bug-to-bug fix.
     */
    if ((hints.ai_flags & AI_NUMERICHOST) == 0)
    {
        hints.ai_flags |= AI_NUMERICHOST;
        ret = getaddrinfo (node, psz_service, &hints, res);
        if (ret == 0)
            goto out;
        hints.ai_flags &= ~AI_NUMERICHOST;
    }
#endif
#ifdef AI_IDN
    /* Run-time I18n Domain Names support */
    hints.ai_flags |= AI_IDN;
    ret = getaddrinfo (node, psz_service, &hints, res);
    if (ret != EAI_BADFLAGS)
        goto out;
    /* IDN not available: disable and retry without it */
    hints.ai_flags &= ~AI_IDN;
#endif
    ret = getaddrinfo (node, psz_service, &hints, res);

out:
    LocaleFree (node);
    return ret;
}

#endif
void vlc_freeaddrinfo( struct addrinfo *infos )
{
    freeaddrinfo (infos);
}
#if 0
/**
 * inet_pton() replacement
 */
int vlc_inet_pton (int af, const char *src, void *dst)
{
#ifndef HAVE_INET_PTON
    /* Windows Vista has inet_pton(), but not XP. */
    /* We have a pretty good example of abstraction inversion here... */
    struct addrinfo hints = {
        .ai_family = af,
        .ai_socktype = SOCK_DGRAM, /* make sure we have... */
        .ai_protocol = IPPROTO_UDP, /* ...only one response */
        .ai_flags = AI_NUMERICHOST,
    }, *res;

    if (getaddrinfo (src, NULL, &hints, &res))
        return 0;

    const void *data;
    size_t len;

    switch (af)
    {
        case AF_INET:
            data = &((const struct sockaddr_in *)res->ai_addr)->sin_addr;
            len = sizeof (struct in_addr);
            break;
#ifdef AF_INET6
        case AF_INET6:
            data = &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
            len = sizeof (struct in6_addr);
            break;
#endif
        default:
            freeaddrinfo (res);
            return -1;
    }
    memcpy (dst, data, len);
    freeaddrinfo (res);
    return 1;
#else /* HAVE_INET_PTON */
    return inet_pton( af, src, dst );
#endif /* HAVE_INET_PTON */
}

/**
 * inet_ntop() replacement
 */
const char *vlc_inet_ntop (int af, const void *src, char *dst, socklen_t cnt)
{
#ifndef HAVE_INET_NTOP
    int ret = EAI_FAMILY;

    switch (af)
    {
#ifdef AF_INET6
        case AF_INET6:
            {
                struct sockaddr_in6 addr;
                memset (&addr, 0, sizeof(addr));
                addr.sin6_family = AF_INET6;
                addr.sin6_addr = *(struct in6_addr *)src;
                ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr),
                                   dst, cnt, NULL, 0, NI_NUMERICHOST);
            }

#endif
        case AF_INET:
            {
                struct sockaddr_in addr;
                memset(&addr, 0, sizeof(addr));
                addr.sin_family = AF_INET;
                addr.sin_addr = *(struct in_addr *)src;
                ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr),
                                   dst, cnt, NULL, 0, NI_NUMERICHOST);
            }
    }
    return (ret == 0) ? dst : NULL;
#else /* HAVE_INET_NTOP */
    return inet_ntop( af, src, dst, cnt );
#endif /* HAVE_INET_NTOP */
}
#endif
#endif /* __MINGW32__ */