io.c 17.3 KB
Newer Older
Laurent Aimar's avatar
   
Laurent Aimar committed
1
/*****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
2
 * io.c: network I/O functions
Laurent Aimar's avatar
   
Laurent Aimar committed
3
 *****************************************************************************
4
 * Copyright (C) 2004-2005, 2007 the VideoLAN team
5
 * Copyright © 2005-2006 Rémi Denis-Courmont
6
 * $Id$
Laurent Aimar's avatar
   
Laurent Aimar committed
7
8
 *
 * Authors: Laurent Aimar <fenrir@videolan.org>
9
 *          Rémi Denis-Courmont <rem # videolan.org>
10
 *          Christophe Mutricy <xtophe at videolan dot org>
Laurent Aimar's avatar
   
Laurent Aimar committed
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * 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
dionoea's avatar
dionoea committed
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Laurent Aimar's avatar
   
Laurent Aimar committed
25
26
27
28
29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
30

Laurent Aimar's avatar
   
Laurent Aimar committed
31
32
#include <vlc/vlc.h>

33
34
#include <stdlib.h>
#include <stdio.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
35
#include <limits.h>
36

37
#include <errno.h>
38
#include <assert.h>
Laurent Aimar's avatar
   
Laurent Aimar committed
39
40
41
42
43
44
45

#ifdef HAVE_FCNTL_H
#   include <fcntl.h>
#endif
#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif
gbazin's avatar
gbazin committed
46
47
48
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif
49
#ifdef HAVE_POLL
50
#   include <poll.h>
51
#endif
gbazin's avatar
gbazin committed
52

zorglub's avatar
zorglub committed
53
#include <vlc_network.h>
Laurent Aimar's avatar
   
Laurent Aimar committed
54

55
56
57
58
59
60
61
#ifndef INADDR_ANY
#   define INADDR_ANY  0x00000000
#endif
#ifndef INADDR_NONE
#   define INADDR_NONE 0xFFFFFFFF
#endif

62
#if defined(WIN32) || defined(UNDER_CE)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
63
64
# undef EAFNOSUPPORT
# define EAFNOSUPPORT WSAEAFNOSUPPORT
65
66
#endif

67
68
69
extern int rootwrap_bind (int family, int socktype, int protocol,
                          const struct sockaddr *addr, size_t alen);

70
71
72
73
74
75
76
77
78
79
80
81
82
83
int net_SetupSocket (int fd)
{
#if defined (WIN32) || defined (UNDER_CE)
    ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
#else
    fcntl (fd, F_SETFD, FD_CLOEXEC);
    fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
#endif

    setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof (int));
    return 0;
}


Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
84
85
86
87
88
int net_Socket (vlc_object_t *p_this, int family, int socktype,
                int protocol)
{
    int fd = socket (family, socktype, protocol);
    if (fd == -1)
89
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
90
        if (net_errno != EAFNOSUPPORT)
91
            msg_Err (p_this, "cannot create socket: %m");
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
92
        return -1;
93
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
94

95
    net_SetupSocket (fd);
96
97
98

#ifdef IPV6_V6ONLY
    /*
99
     * Accepts only IPv6 connections on IPv6 sockets.
100
     * If possible, we should open two sockets, but it is not always possible.
101
     */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
102
    if (family == AF_INET6)
103
        setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){ 1 }, sizeof (int));
104
105
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
106
#if defined (WIN32) || defined (UNDER_CE)
107
# ifndef IPV6_PROTECTION_LEVEL
108
#  warning Please update your C library headers.
109
#  define IPV6_PROTECTION_LEVEL 23
110
#  define PROTECTION_LEVEL_UNRESTRICTED 10
111
# endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
112
113
114
    if (family == AF_INET6)
        setsockopt (fd, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL,
                    &(int){ PROTECTION_LEVEL_UNRESTRICTED }, sizeof (int));
115
#endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
116

117
118
119
    return fd;
}

Laurent Aimar's avatar
Laurent Aimar committed
120

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
121
int *net_Listen (vlc_object_t *p_this, const char *psz_host,
122
                 int i_port, int protocol)
123
124
125
126
{
    struct addrinfo hints, *res;

    memset (&hints, 0, sizeof( hints ));
127
128
129
    /* Since we use port numbers rather than service names, the socket type
     * does not really matter. */
    hints.ai_socktype = SOCK_STREAM;
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    hints.ai_flags = AI_PASSIVE;

    msg_Dbg (p_this, "net: listening to %s port %d", psz_host, i_port);

    int i_val = vlc_getaddrinfo (p_this, psz_host, i_port, &hints, &res);
    if (i_val)
    {
        msg_Err (p_this, "Cannot resolve %s port %d : %s", psz_host, i_port,
                 vlc_gai_strerror (i_val));
        return NULL;
    }

    int *sockv = NULL;
    unsigned sockc = 0;

    for (struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
    {
        int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
148
                             protocol);
149
150
        if (fd == -1)
        {
151
            msg_Dbg (p_this, "socket error: %m");
152
153
154
155
            continue;
        }

        /* Bind the socket */
156
#if defined (WIN32) || defined (UNDER_CE)
157
158
159
160
161
162
163
164
165
        /*
         * Under Win32 and for multicasting, we bind to INADDR_ANY.
         * This is of course a severe bug, since the socket would logically
         * receive unicast traffic, and multicast traffic of groups subscribed
         * to via other sockets.
         */
        if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
         && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
        {
166
167
            // This works for IPv4 too - don't worry!
            struct sockaddr_in6 dumb =
168
            {
169
170
                .sin6_family = ptr->ai_addr->sa_family,
                .sin6_port =  ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
171
172
173
174
175
176
            };

            bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
        }
        else
#endif
177
178
179
180
181
        if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
        {
            net_Close (fd);
#if !defined(WIN32) && !defined(UNDER_CE)
            fd = rootwrap_bind (ptr->ai_family, ptr->ai_socktype,
182
                                protocol ?: ptr->ai_protocol, ptr->ai_addr,
183
184
185
186
187
188
189
190
                                ptr->ai_addrlen);
            if (fd != -1)
            {
                msg_Dbg (p_this, "got socket %d from rootwrap", fd);
            }
            else
#endif
            {
191
                msg_Err (p_this, "socket bind error (%m)");
192
193
194
195
                continue;
            }
        }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
196
197
198
199
200
201
202
203
204
        if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen))
        {
            if (net_Subscribe (p_this, fd, ptr->ai_addr, ptr->ai_addrlen))
            {
                net_Close (fd);
                continue;
            }
        }

205
206
207
208
209
210
        /* Listen */
        switch (ptr->ai_socktype)
        {
            case SOCK_STREAM:
            case SOCK_RDM:
            case SOCK_SEQPACKET:
211
212
213
#ifdef SOCK_DCCP
            case SOCK_DCCP:
#endif
214
215
                if (listen (fd, INT_MAX))
                {
216
                    msg_Err (p_this, "socket listen error (%m)");
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
                    net_Close (fd);
                    continue;
                }
        }

        int *nsockv = (int *)realloc (sockv, (sockc + 2) * sizeof (int));
        if (nsockv != NULL)
        {
            nsockv[sockc++] = fd;
            sockv = nsockv;
        }
        else
            net_Close (fd);
    }

    vlc_freeaddrinfo (res);

    if (sockv != NULL)
        sockv[sockc] = -1;

    return sockv;
}


241
242
243
static ssize_t
net_ReadInner (vlc_object_t *restrict p_this, unsigned fdc, const int *fdv,
               const v_socket_t *const *restrict vsv,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
244
               uint8_t *restrict p_buf, size_t i_buflen, vlc_bool_t waitall)
245
{
246
    size_t i_total = 0;
247

248
    while (i_buflen > 0)
249
    {
250
        if( ( p_this->b_die ) || ( p_this->p_libvlc->b_die ) )
251
        {
252
#if defined(WIN32) || defined(UNDER_CE)
253
            WSASetLastError (WSAEINTR);
254
#else
255
            errno = EINTR;
256
#endif
257
258
259
            goto error;
        }

260
        struct pollfd ufd[fdc];
261

262
        for (unsigned i = 0; i < fdc; i++)
263
        {
264
            ufd[i].fd = fdv[i];
265
            ufd[i].events = POLLIN;
266
            ufd[i].revents = 0;
267
268
        }

269
        switch (poll (ufd, fdc, 500))
270
271
272
        {
            case -1:
                goto error;
273

274
275
276
            case 0: // timeout
                continue;
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
277

278
        for (unsigned i = 0;; i++)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
279
        {
280
            assert (i < fdc); /* no events found = bug ! */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
281

282
            if (ufd[i].revents == 0)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
283
                continue;
284

Christophe Mutricy's avatar
Christophe Mutricy committed
285
#ifndef POLLRDHUP /* This is nice but non-portable */
286
287
# define POLLRDHUP 0
#endif
288
289
290
            if (i_total > 0)
            {
                // Errors (-1) and EOF (0) will be returned on next run
291
                if (ufd[i].revents & (POLLERR|POLLNVAL|POLLRDHUP))
292
293
294
295
                    return i_total;
            }
            else
            {
296
                if (ufd[i].revents & POLLRDHUP)
297
298
299
300
301
302
                    return 0; // EOF, read() would yield 0
            }

            fdc = 1;
            fdv += i;
            vsv += i;
303

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
304
305
            break;
        }
306

307
308
        ssize_t n;
        if (*vsv != NULL)
309
        {
310
            n = (*vsv)->pf_recv ((*vsv)->p_sys, p_buf, i_buflen);
311
        }
312
        else
313
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
314
#ifdef WIN32
315
            n = recv (*fdv, p_buf, i_buflen, 0);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
316
317
318
#else
            n = read (*fdv, p_buf, i_buflen);
#endif
319
        }
320

321
        if (n == -1)
322
323
        {
#if defined(WIN32) || defined(UNDER_CE)
324
            switch (WSAGetLastError ())
325
326
327
328
329
330
331
332
333
334
            {
                case WSAEWOULDBLOCK:
                /* only happens with vs != NULL (SSL) - not really an error */
                    continue;

                case WSAEMSGSIZE:
                /* For UDP only */
                /* On Win32, recv() fails if the datagram doesn't fit inside
                 * the passed buffer, even though the buffer will be filled
                 * with the first part of the datagram. */
335
336
                    msg_Err (p_this, "Receive error: "
                                     "Increase the mtu size (--mtu option)");
337
338
339
340
341
                    n = i_buflen;
                    break;

                default:
                    goto error;
342
343
            }
#else
344
345
            /* spurious wake-up or TLS did not yield any actual data */
            if (errno == EAGAIN)
346
347
                continue;
            goto error;
348
#endif
349
350
        }

351
352
353
        i_total += n;
        p_buf += n;
        i_buflen -= n;
Rémi Denis-Courmont's avatar
Fixes    
Rémi Denis-Courmont committed
354

355
        if ((n == 0) || !waitall)
356
            break;
357
    }
damienf's avatar
damienf committed
358
    return i_total;
359
360

error:
361
    msg_Err (p_this, "Read error: %m");
Rémi Denis-Courmont's avatar
Oups    
Rémi Denis-Courmont committed
362
    return i_total ? (ssize_t)i_total : -1;
363
364
365
}


Laurent Aimar's avatar
   
Laurent Aimar committed
366
367
368
369
/*****************************************************************************
 * __net_Read:
 *****************************************************************************
 * Read from a network socket
370
 * If b_retry is true, then we repeat until we have read the right amount of
371
 * data; in that case, a short count means EOF has been reached.
Laurent Aimar's avatar
   
Laurent Aimar committed
372
 *****************************************************************************/
373
374
375
ssize_t __net_Read( vlc_object_t *restrict p_this, int fd,
                    const v_socket_t *restrict p_vs,
                    uint8_t *restrict buf, size_t len, vlc_bool_t b_retry )
Laurent Aimar's avatar
   
Laurent Aimar committed
376
{
377
    return net_ReadInner( p_this, 1, &(int){ fd },
378
                          &(const v_socket_t *){ p_vs },
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
379
                          buf, len, b_retry );
Laurent Aimar's avatar
   
Laurent Aimar committed
380
381
}

382

zorglub's avatar
zorglub committed
383
384
385
/*****************************************************************************
 * __net_Select:
 *****************************************************************************
386
 * Read from several sockets. Takes data from the first socket that has some.
zorglub's avatar
zorglub committed
387
 *****************************************************************************/
388
389
ssize_t __net_Select( vlc_object_t *restrict p_this,
                      const int *restrict fds, int nfd,
390
                      uint8_t *restrict buf, size_t len )
zorglub's avatar
zorglub committed
391
{
392
    const v_socket_t *vsv[nfd];
393
    memset( vsv, 0, sizeof (vsv) );
zorglub's avatar
zorglub committed
394

395
    return net_ReadInner( p_this, nfd, fds, vsv,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
396
                          buf, len, VLC_FALSE );
zorglub's avatar
zorglub committed
397
398
399
}


Laurent Aimar's avatar
   
Laurent Aimar committed
400
/* Write exact amount requested */
401
402
ssize_t __net_Write( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
                     const uint8_t *p_data, size_t i_data )
Laurent Aimar's avatar
   
Laurent Aimar committed
403
{
404
    size_t i_total = 0;
Laurent Aimar's avatar
   
Laurent Aimar committed
405

406
    while( i_data > 0 )
Laurent Aimar's avatar
   
Laurent Aimar committed
407
    {
408
        if( p_this->b_die )
409
            break;
Laurent Aimar's avatar
   
Laurent Aimar committed
410

411
412
413
414
        struct pollfd ufd[1];
        memset (ufd, 0, sizeof (ufd));
        ufd[0].fd = fd;
        ufd[0].events = POLLOUT;
415

416
417
        int val = poll (ufd, 1, 500);
        switch (val)
Laurent Aimar's avatar
   
Laurent Aimar committed
418
        {
419
            case -1:
420
               msg_Err (p_this, "Write error: %m");
421
               goto out;
422
423
424

            case 0:
                continue;
Laurent Aimar's avatar
   
Laurent Aimar committed
425
426
        }

427
        if ((ufd[0].revents & (POLLERR|POLLNVAL|POLLHUP)) && (i_total > 0))
428
429
            return i_total; // error will be dequeued separately on next call

430
431
432
        if (p_vs != NULL)
            val = p_vs->pf_send (p_vs->p_sys, p_data, i_data);
        else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
433
#ifdef WIN32
zorglub's avatar
zorglub committed
434
            val = send (fd, p_data, i_data, 0);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
435
436
437
#else
            val = write (fd, p_data, i_data);
#endif
438
439

        if (val == -1)
440
        {
441
            msg_Err (p_this, "Write error: %m");
442
443
            break;
        }
444
445
446

        p_data += val;
        i_data -= val;
447
        i_total += val;
Laurent Aimar's avatar
   
Laurent Aimar committed
448
    }
449

450
451
452
453
454
out:
    if ((i_total > 0) || (i_data == 0))
        return i_total;

    return -1;
Laurent Aimar's avatar
   
Laurent Aimar committed
455
456
}

457
char *__net_Gets( vlc_object_t *p_this, int fd, const v_socket_t *p_vs )
Laurent Aimar's avatar
   
Laurent Aimar committed
458
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
459
460
    char *psz_line = NULL, *ptr = NULL;
    size_t  i_line = 0, i_max = 0;
Laurent Aimar's avatar
   
Laurent Aimar committed
461
462
463
464


    for( ;; )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
465
        if( i_line == i_max )
Laurent Aimar's avatar
   
Laurent Aimar committed
466
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
467
468
469
            i_max += 1024;
            psz_line = realloc( psz_line, i_max );
            ptr = psz_line + i_line;
Laurent Aimar's avatar
   
Laurent Aimar committed
470
        }
471

472
        if( net_Read( p_this, fd, p_vs, (uint8_t *)ptr, 1, VLC_TRUE ) != 1 )
Laurent Aimar's avatar
   
Laurent Aimar committed
473
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
474
475
476
477
478
            if( i_line == 0 )
            {
                free( psz_line );
                return NULL;
            }
Laurent Aimar's avatar
   
Laurent Aimar committed
479
480
481
            break;
        }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
482
483
        if ( *ptr == '\n' )
            break;
Laurent Aimar's avatar
   
Laurent Aimar committed
484

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
485
486
        i_line++;
        ptr++;
Laurent Aimar's avatar
   
Laurent Aimar committed
487
488
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
489
490
    *ptr-- = '\0';

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
491
    if( ( ptr >= psz_line ) && ( *ptr == '\r' ) )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
492
493
        *ptr = '\0';

Laurent Aimar's avatar
   
Laurent Aimar committed
494
495
496
    return psz_line;
}

497
498
ssize_t net_Printf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
                    const char *psz_fmt, ... )
Laurent Aimar's avatar
   
Laurent Aimar committed
499
{
500
    int i_ret;
Laurent Aimar's avatar
   
Laurent Aimar committed
501
    va_list args;
502
    va_start( args, psz_fmt );
503
    i_ret = net_vaPrintf( p_this, fd, p_vs, psz_fmt, args );
504
505
506
507
508
    va_end( args );

    return i_ret;
}

509
510
ssize_t __net_vaPrintf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
                        const char *psz_fmt, va_list args )
511
{
Laurent Aimar's avatar
   
Laurent Aimar committed
512
    char    *psz;
513
    int     i_size, i_ret;
Laurent Aimar's avatar
   
Laurent Aimar committed
514

515
    i_size = vasprintf( &psz, psz_fmt, args );
516
517
    i_ret = __net_Write( p_this, fd, p_vs, (uint8_t *)psz, i_size ) < i_size
        ? -1 : i_size;
518
519
520
    free( psz );

    return i_ret;
Laurent Aimar's avatar
   
Laurent Aimar committed
521
}
522
523


Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
524
525
526
527
528
529
530
531
532
533
534
535
536
/*****************************************************************************
 * inet_pton replacement for obsolete and/or crap operating systems
 *****************************************************************************/
#ifndef HAVE_INET_PTON
int inet_pton(int af, const char *src, void *dst)
{
# ifdef WIN32
    /* As we already know, Microsoft always go its own way, so even if they do
     * provide IPv6, they don't provide the API. */
    struct sockaddr_storage addr;
    int len = sizeof( addr );

    /* Damn it, they didn't even put LPCSTR for the firs parameter!!! */
gbazin's avatar
gbazin committed
537
538
539
540
541
542
#ifdef UNICODE
    wchar_t *workaround_for_ill_designed_api =
        malloc( MAX_PATH * sizeof(wchar_t) );
    mbstowcs( workaround_for_ill_designed_api, src, MAX_PATH );
    workaround_for_ill_designed_api[MAX_PATH-1] = 0;
#else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
543
    char *workaround_for_ill_designed_api = strdup( src );
gbazin's avatar
gbazin committed
544
545
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
546
547
548
549
550
551
552
553
554
555
556
557
558
    if( !WSAStringToAddress( workaround_for_ill_designed_api, af, NULL,
                             (LPSOCKADDR)&addr, &len ) )
    {
        free( workaround_for_ill_designed_api );
        return -1;
    }
    free( workaround_for_ill_designed_api );

    switch( af )
    {
        case AF_INET6:
            memcpy( dst, &((struct sockaddr_in6 *)&addr)->sin6_addr, 16 );
            break;
559

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
        case AF_INET:
            memcpy( dst, &((struct sockaddr_in *)&addr)->sin_addr, 4 );
            break;

        default:
            WSASetLastError( WSAEAFNOSUPPORT );
            return -1;
    }
# else
    /* Assume IPv6 is not supported. */
    /* Would be safer and more simpler to use inet_aton() but it is most
     * likely not provided either. */
    uint32_t ipv4;

    if( af != AF_INET )
    {
        errno = EAFNOSUPPORT;
        return -1;
    }

    ipv4 = inet_addr( src );
    if( ipv4 == INADDR_NONE )
        return -1;

Eric Petit's avatar
Eric Petit committed
584
    memcpy( dst, &ipv4, 4 );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
585
586
587
588
# endif /* WIN32 */
    return 0;
}
#endif /* HAVE_INET_PTON */
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603

#ifndef HAVE_INET_NTOP
#ifdef WIN32
const char *inet_ntop(int af, const void * src,
                               char * dst, socklen_t cnt)
{
    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);
604
605
                if( 0 == WSAAddressToStringA((LPSOCKADDR)&addr,
                                             sizeof(struct sockaddr_in6),
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
                                             NULL, dst, &cnt) )
                {
                    dst[cnt] = '\0';
                    return dst;
                }
                errno = WSAGetLastError();
                return NULL;

            }

#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);
                if( 0 == WSAAddressToStringA((LPSOCKADDR)&addr,
                                             sizeof(struct sockaddr_in),
                                             NULL, dst, &cnt) )
                {
                    dst[cnt] = '\0';
                    return dst;
                }
                errno = WSAGetLastError();
                return NULL;

            }
    }
    errno = EAFNOSUPPORT;
    return NULL;
}
#endif
#endif