io.c 13.8 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
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2004-2005, 2007 VLC authors and VideoLAN
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
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
12
13
14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
Laurent Aimar's avatar
 
Laurent Aimar committed
15
16
17
18
 * (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
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
19
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
Laurent Aimar's avatar
 
Laurent Aimar committed
21
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
22
23
24
 * You should have received a copy of the GNU Lesser 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.
Laurent Aimar's avatar
 
Laurent Aimar committed
25
26
27
28
29
 *****************************************************************************/

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

31
32
33
34
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

35
#include <vlc_common.h>
Laurent Aimar's avatar
 
Laurent Aimar committed
36

37
38
#include <stdlib.h>
#include <stdio.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
39
#include <limits.h>
40

41
#include <errno.h>
42
#include <assert.h>
Laurent Aimar's avatar
 
Laurent Aimar committed
43

44
#include <fcntl.h>
gbazin's avatar
gbazin committed
45
46
47
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif
48
#ifdef HAVE_POLL
49
#   include <poll.h>
50
#endif
gbazin's avatar
gbazin committed
51

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

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

61
#if defined(_WIN32)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
62
63
# undef EAFNOSUPPORT
# define EAFNOSUPPORT WSAEAFNOSUPPORT
64
65
66
67
# undef EWOULDBLOCK
# define EWOULDBLOCK WSAEWOULDBLOCK
# undef EAGAIN
# define EAGAIN WSAEWOULDBLOCK
68
69
#endif

70
71
72
73
74
75
#ifdef HAVE_LINUX_DCCP_H
/* TODO: use glibc instead of linux-kernel headers */
# include <linux/dccp.h>
# define SOL_DCCP 269
#endif

76
77
#include "libvlc.h" /* vlc_object_waitpipe */

78
79
80
extern int rootwrap_bind (int family, int socktype, int protocol,
                          const struct sockaddr *addr, size_t alen);

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

92
    setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof (int));
93
94
95

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

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

114
#ifdef DCCP_SOCKOPT_SERVICE
115
    if (socktype == SOL_DCCP)
116
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
117
        char *dccps = var_InheritString (p_this, "dccp-service");
118
119
120
121
122
123
        if (dccps != NULL)
        {
            setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SERVICE, dccps,
                        (strlen (dccps) + 3) & ~3);
            free (dccps);
        }
124
125
126
    }
#endif

127
128
129
    return fd;
}

Laurent Aimar's avatar
Laurent Aimar committed
130

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
131
int *net_Listen (vlc_object_t *p_this, const char *psz_host,
132
                 int i_port, int type, int protocol)
133
{
134
135
136
    struct addrinfo hints = {
        .ai_socktype = type,
        .ai_protocol = protocol,
137
        .ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_IDN,
138
    }, *res;
139

140
141
    msg_Dbg (p_this, "net: listening to %s port %d",
             (psz_host != NULL) ? psz_host : "*", i_port);
142

143
    int i_val = vlc_getaddrinfo (psz_host, i_port, &hints, &res);
144
145
    if (i_val)
    {
146
147
        msg_Err (p_this, "Cannot resolve %s port %d : %s",
                 (psz_host != NULL) ? psz_host : "", i_port,
148
                 gai_strerror (i_val));
149
150
151
152
153
154
155
156
        return NULL;
    }

    int *sockv = NULL;
    unsigned sockc = 0;

    for (struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
    {
157
158
        int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
                             ptr->ai_protocol);
159
160
        if (fd == -1)
        {
161
            msg_Dbg (p_this, "socket error: %m");
162
163
164
165
            continue;
        }

        /* Bind the socket */
166
#if defined (_WIN32)
167
168
169
170
171
172
173
174
175
        /*
         * 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))
        {
176
177
            // This works for IPv4 too - don't worry!
            struct sockaddr_in6 dumb =
178
            {
179
180
                .sin6_family = ptr->ai_addr->sa_family,
                .sin6_port =  ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
181
182
183
184
185
186
            };

            bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
        }
        else
#endif
187
188
189
        if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
        {
            net_Close (fd);
190
#if !defined(_WIN32)
191
192
            fd = rootwrap_bind (ptr->ai_family, ptr->ai_socktype,
                                ptr->ai_protocol,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
193
                                ptr->ai_addr, ptr->ai_addrlen);
194
195
196
197
198
199
200
            if (fd != -1)
            {
                msg_Dbg (p_this, "got socket %d from rootwrap", fd);
            }
            else
#endif
            {
201
                msg_Err (p_this, "socket bind error (%m)");
202
203
204
205
                continue;
            }
        }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
206
207
208
209
210
211
212
213
214
        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;
            }
        }

215
        /* Listen */
216
        switch (ptr->ai_socktype)
217
218
219
220
        {
            case SOCK_STREAM:
            case SOCK_RDM:
            case SOCK_SEQPACKET:
221
222
223
#ifdef SOCK_DCCP
            case SOCK_DCCP:
#endif
224
225
                if (listen (fd, INT_MAX))
                {
226
                    msg_Err (p_this, "socket listen error (%m)");
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
                    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);
    }

242
    freeaddrinfo (res);
243
244
245
246
247
248
249

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

    return sockv;
}

250
#undef net_Read
251
/*****************************************************************************
252
 * net_Read:
253
 *****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
254
 * Reads from a network socket. Cancellation point.
255
 * If waitall is true, then we repeat until we have read the right amount of
256
257
 * data; in that case, a short count means EOF has been reached or the VLC
 * object has been signaled.
258
259
 *****************************************************************************/
ssize_t
260
261
net_Read (vlc_object_t *restrict p_this, int fd, const v_socket_t *vs,
          void *restrict p_buf, size_t i_buflen, bool waitall)
262
{
263
    struct pollfd ufd[2];
264

265
266
267
268
    ufd[0].fd = fd;
    ufd[0].events = POLLIN;
    ufd[1].fd = vlc_object_waitpipe (p_this);
    ufd[1].events = POLLIN;
269

270
271
    size_t i_total = 0;
    do
272
    {
273
        ssize_t n;
274
        if (vs != NULL)
275
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
276
            int canc = vlc_savecancel ();
277
            n = vs->pf_recv (vs->p_sys, p_buf, i_buflen);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
278
            vlc_restorecancel (canc);
279
        }
280
        else
281
        {
282
#ifdef _WIN32
283
            n = recv (fd, p_buf, i_buflen, 0);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
284
#else
285
            n = read (fd, p_buf, i_buflen);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
286
#endif
287
        }
288

289
        if (n < 0)
290
        {
291
            switch (net_errno)
292
            {
293
                case EAGAIN: /* no data */
294
295
296
#if (EAGAIN != EWOULDBLOCK)
                case EWOULDBLOCK:
#endif
297
                    break;
298
299
300
301
#ifndef _WIN32
                case EINTR:  /* asynchronous signal */
                    continue;
#else
302
303
304
305
306
307
                case WSAEMSGSIZE: /* datagram too big */
                    n = i_buflen;
                    break;
#endif
                default:
                    goto error;
308
            }
309
310
311
312
313
314
315
316
317
318
319
        }
        else
        if (n > 0)
        {
            i_total += n;
            p_buf = (char *)p_buf + n;
            i_buflen -= n;

            if (!waitall)
                break;
        }
320
        else /* n == 0 */
321
322
323
324
325
326
327
            break;/* end of stream or empty packet */

        if (ufd[1].fd == -1)
        {
            errno = EINTR;
            return -1;
        }
328
329
330
331
332
333
334
335

        /* Wait for more data */
        if (poll (ufd, sizeof (ufd) / sizeof (ufd[0]), -1) < 0)
        {
            if (errno == EINTR)
                continue;
            goto error;
        }
336
337
338
339
340
341

        if (ufd[1].revents)
        {
            msg_Dbg (p_this, "socket %d polling interrupted", fd);
            errno = EINTR;
            return -1;
342
343
        }

344
        assert (ufd[0].revents);
345
    }
346
    while (i_buflen > 0);
347

damienf's avatar
damienf committed
348
    return i_total;
349
error:
350
    msg_Err (p_this, "read error: %m");
351
    return -1;
352
353
}

354
#undef net_Write
355
356
357
358
359
360
361
362
363
364
/**
 * Writes data to a file descriptor.
 * This blocks until all data is written or an error occurs.
 *
 * This function is a cancellation point if p_vs is NULL.
 * This function is not cancellation-safe if p_vs is not NULL.
 *
 * @return the total number of bytes written, or -1 if an error occurs
 * before any data is written.
 */
365
366
ssize_t net_Write( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
                   const void *restrict p_data, size_t i_data )
Laurent Aimar's avatar
 
Laurent Aimar committed
367
{
368
    size_t i_total = 0;
369
370
371
372
373
    struct pollfd ufd[2] = {
        { .fd = fd,                           .events = POLLOUT },
        { .fd = vlc_object_waitpipe (p_this), .events = POLLIN  },
    };

374
375
376
    if (unlikely(ufd[1].fd == -1))
    {
        vlc_testcancel ();
377
        return -1;
378
    }
Laurent Aimar's avatar
 
Laurent Aimar committed
379

380
    while( i_data > 0 )
Laurent Aimar's avatar
 
Laurent Aimar committed
381
    {
382
        ssize_t val;
Laurent Aimar's avatar
 
Laurent Aimar committed
383

384
        ufd[0].revents = ufd[1].revents = 0;
385

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
386
        if (poll (ufd, sizeof (ufd) / sizeof (ufd[0]), -1) == -1)
Laurent Aimar's avatar
 
Laurent Aimar committed
387
        {
388
389
390
391
            if (errno == EINTR)
                continue;
            msg_Err (p_this, "Polling error: %m");
            return -1;
Laurent Aimar's avatar
 
Laurent Aimar committed
392
393
        }

394
        if (i_total > 0)
395
396
397
398
        {   /* If POLLHUP resp. POLLERR|POLLNVAL occurs while we have already
             * read some data, it is important that we first return the number
             * of bytes read, and then return 0 resp. -1 on the NEXT call. */
            if (ufd[0].revents & (POLLHUP|POLLERR|POLLNVAL))
399
                break;
400
            if (ufd[1].revents) /* VLC object signaled */
401
402
                break;
        }
403
404
405
406
407
408
409
410
        else
        {
            if (ufd[1].revents)
            {
                errno = EINTR;
                goto error;
            }
        }
411

412
413
414
        if (p_vs != NULL)
            val = p_vs->pf_send (p_vs->p_sys, p_data, i_data);
        else
415
#ifdef _WIN32
zorglub's avatar
zorglub committed
416
            val = send (fd, p_data, i_data, 0);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
417
418
419
#else
            val = write (fd, p_data, i_data);
#endif
420
421

        if (val == -1)
422
        {
423
424
            if (errno == EINTR)
                continue;
425
            msg_Err (p_this, "Write error: %m");
426
427
            break;
        }
428

429
        p_data = (const char *)p_data + val;
430
        i_data -= val;
431
        i_total += val;
Laurent Aimar's avatar
 
Laurent Aimar committed
432
    }
433

434
435
436
    if (unlikely(i_data == 0))
        vlc_testcancel (); /* corner case */

437
438
439
    if ((i_total > 0) || (i_data == 0))
        return i_total;

440
error:
441
    return -1;
Laurent Aimar's avatar
 
Laurent Aimar committed
442
443
}

444
#undef net_Gets
445
446
/**
 * Reads a line from a file descriptor.
Rémi Denis-Courmont's avatar
Typo    
Rémi Denis-Courmont committed
447
448
 * This function is not thread-safe; the same file descriptor I/O cannot be
 * read by another thread at the same time (although it can be written to).
449
 *
450
451
452
 * @note This only works with stream-oriented file descriptors, not with
 * datagram or packet-oriented ones.
 *
453
454
 * @return nul-terminated heap-allocated string, or NULL on I/O error.
 */
455
char *net_Gets(vlc_object_t *obj, int fd, const v_socket_t *vs)
Laurent Aimar's avatar
 
Laurent Aimar committed
456
{
457
458
    char *buf = NULL;
    size_t bufsize = 0, buflen = 0;
Laurent Aimar's avatar
 
Laurent Aimar committed
459

460
    for (;;)
Laurent Aimar's avatar
 
Laurent Aimar committed
461
    {
462
        if (buflen == bufsize)
Laurent Aimar's avatar
 
Laurent Aimar committed
463
        {
464
465
            if (unlikely(bufsize >= (1 << 10)))
                goto error; /* put sane buffer size limit */
466

467
468
469
470
471
            char *newbuf = realloc(buf, bufsize + 1024);
            if (unlikely(newbuf == NULL))
                goto error;
            buf = newbuf;
            bufsize += 1024;
Laurent Aimar's avatar
 
Laurent Aimar committed
472
473
        }

474
475
476
477
478
        ssize_t val = net_Read(obj, fd, vs, buf + buflen, 1, false);
        if (val < 1)
            goto error;

        if (buf[buflen] == '\n')
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
479
            break;
Laurent Aimar's avatar
 
Laurent Aimar committed
480

481
        buflen++;
Laurent Aimar's avatar
 
Laurent Aimar committed
482
483
    }

484
485
486
487
488
489
490
    buf[--buflen] = '\0';
    if (buflen > 0 && buf[buflen - 1] == '\r')
        buf[buflen] = '\0';
    return buf;
error:
    free(buf);
    return NULL;
Laurent Aimar's avatar
 
Laurent Aimar committed
491
492
}

493
#undef net_Printf
494
495
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
496
{
497
    int i_ret;
Laurent Aimar's avatar
 
Laurent Aimar committed
498
    va_list args;
499
    va_start( args, psz_fmt );
500
    i_ret = net_vaPrintf( p_this, fd, p_vs, psz_fmt, args );
501
502
503
504
505
    va_end( args );

    return i_ret;
}

506
507
508
#undef net_vaPrintf
ssize_t net_vaPrintf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
                      const char *psz_fmt, va_list args )
509
{
Laurent Aimar's avatar
 
Laurent Aimar committed
510
    char    *psz;
511
    int      i_ret;
Laurent Aimar's avatar
 
Laurent Aimar committed
512

513
    int i_size = vasprintf( &psz, psz_fmt, args );
514
515
    if( i_size == -1 )
        return -1;
516
    i_ret = net_Write( p_this, fd, p_vs, psz, i_size ) < i_size
517
        ? -1 : i_size;
518
519
520
    free( psz );

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