1 /*****************************************************************************
2 * io.c: network I/O functions
3 *****************************************************************************
4 * Copyright (C) 2004-2005, 2007 VLC authors and VideoLAN
5 * Copyright © 2005-2006 Rémi Denis-Courmont
8 * Authors: Laurent Aimar <fenrir@videolan.org>
9 * Rémi Denis-Courmont <rem # videolan.org>
10 * Christophe Mutricy <xtophe at videolan dot org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_common.h>
52 #include <vlc_network.h>
55 # define INADDR_ANY 0x00000000
58 # define INADDR_NONE 0xFFFFFFFF
63 # define EAFNOSUPPORT WSAEAFNOSUPPORT
65 # define EWOULDBLOCK WSAEWOULDBLOCK
67 # define EAGAIN WSAEWOULDBLOCK
70 #ifdef HAVE_LINUX_DCCP_H
71 /* TODO: use glibc instead of linux-kernel headers */
72 # include <linux/dccp.h>
76 #include "libvlc.h" /* vlc_object_waitpipe */
78 extern int rootwrap_bind (int family, int socktype, int protocol,
79 const struct sockaddr *addr, size_t alen);
81 int net_Socket (vlc_object_t *p_this, int family, int socktype,
84 int fd = vlc_socket (family, socktype, protocol, true);
87 if (net_errno != EAFNOSUPPORT)
88 msg_Err (p_this, "cannot create socket: %m");
92 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof (int));
96 * Accepts only IPv6 connections on IPv6 sockets.
97 * If possible, we should open two sockets, but it is not always possible.
99 if (family == AF_INET6)
100 setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){ 1 }, sizeof (int));
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
109 if (family == AF_INET6)
110 setsockopt (fd, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL,
111 &(int){ PROTECTION_LEVEL_UNRESTRICTED }, sizeof (int));
114 #ifdef DCCP_SOCKOPT_SERVICE
115 if (socktype == SOL_DCCP)
117 char *dccps = var_InheritString (p_this, "dccp-service");
120 setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SERVICE, dccps,
121 (strlen (dccps) + 3) & ~3);
131 int *net_Listen (vlc_object_t *p_this, const char *psz_host,
132 int i_port, int type, int protocol)
134 struct addrinfo hints = {
136 .ai_protocol = protocol,
137 .ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_IDN,
140 msg_Dbg (p_this, "net: listening to %s port %d",
141 (psz_host != NULL) ? psz_host : "*", i_port);
143 int i_val = vlc_getaddrinfo (psz_host, i_port, &hints, &res);
146 msg_Err (p_this, "Cannot resolve %s port %d : %s",
147 (psz_host != NULL) ? psz_host : "", i_port,
148 gai_strerror (i_val));
155 for (struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
157 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
161 msg_Dbg (p_this, "socket error: %m");
165 /* Bind the socket */
168 * Under Win32 and for multicasting, we bind to INADDR_ANY.
169 * This is of course a severe bug, since the socket would logically
170 * receive unicast traffic, and multicast traffic of groups subscribed
171 * to via other sockets.
173 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
174 && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
176 // This works for IPv4 too - don't worry!
177 struct sockaddr_in6 dumb =
179 .sin6_family = ptr->ai_addr->sa_family,
180 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
183 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
187 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
191 fd = rootwrap_bind (ptr->ai_family, ptr->ai_socktype,
193 ptr->ai_addr, ptr->ai_addrlen);
196 msg_Dbg (p_this, "got socket %d from rootwrap", fd);
201 msg_Err (p_this, "socket bind error (%m)");
206 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen))
208 if (net_Subscribe (p_this, fd, ptr->ai_addr, ptr->ai_addrlen))
216 switch (ptr->ai_socktype)
224 if (listen (fd, INT_MAX))
226 msg_Err (p_this, "socket listen error (%m)");
232 int *nsockv = (int *)realloc (sockv, (sockc + 2) * sizeof (int));
235 nsockv[sockc++] = fd;
251 /*****************************************************************************
253 *****************************************************************************
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 * data; in that case, a short count means EOF has been reached or the VLC
257 * object has been signaled.
258 *****************************************************************************/
260 net_Read (vlc_object_t *restrict p_this, int fd, const v_socket_t *vs,
261 void *restrict p_buf, size_t i_buflen, bool waitall)
263 struct pollfd ufd[2];
266 ufd[0].events = POLLIN;
267 ufd[1].fd = vlc_object_waitpipe (p_this);
268 ufd[1].events = POLLIN;
276 int canc = vlc_savecancel ();
277 n = vs->pf_recv (vs->p_sys, p_buf, i_buflen);
278 vlc_restorecancel (canc);
283 n = recv (fd, p_buf, i_buflen, 0);
285 n = read (fd, p_buf, i_buflen);
293 case EAGAIN: /* no data */
294 #if (EAGAIN != EWOULDBLOCK)
299 case EINTR: /* asynchronous signal */
302 case WSAEMSGSIZE: /* datagram too big */
314 p_buf = (char *)p_buf + n;
317 if (!waitall || i_buflen == 0)
321 break;/* end of stream or empty packet */
329 /* Wait for more data */
330 if (poll (ufd, sizeof (ufd) / sizeof (ufd[0]), -1) < 0)
339 msg_Dbg (p_this, "socket %d polling interrupted", fd);
344 assert (ufd[0].revents);
346 while (i_buflen > 0);
350 msg_Err (p_this, "read error: %m");
356 * Writes data to a file descriptor.
357 * This blocks until all data is written or an error occurs.
359 * This function is a cancellation point if p_vs is NULL.
360 * This function is not cancellation-safe if p_vs is not NULL.
362 * @return the total number of bytes written, or -1 if an error occurs
363 * before any data is written.
365 ssize_t net_Write( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
366 const void *restrict p_data, size_t i_data )
369 struct pollfd ufd[2] = {
370 { .fd = fd, .events = POLLOUT },
371 { .fd = vlc_object_waitpipe (p_this), .events = POLLIN },
374 if (unlikely(ufd[1].fd == -1))
384 ufd[0].revents = ufd[1].revents = 0;
386 if (poll (ufd, sizeof (ufd) / sizeof (ufd[0]), -1) == -1)
390 msg_Err (p_this, "Polling error: %m");
395 { /* If POLLHUP resp. POLLERR|POLLNVAL occurs while we have already
396 * read some data, it is important that we first return the number
397 * of bytes read, and then return 0 resp. -1 on the NEXT call. */
398 if (ufd[0].revents & (POLLHUP|POLLERR|POLLNVAL))
400 if (ufd[1].revents) /* VLC object signaled */
413 val = p_vs->pf_send (p_vs->p_sys, p_data, i_data);
416 val = send (fd, p_data, i_data, 0);
418 val = write (fd, p_data, i_data);
425 msg_Err (p_this, "Write error: %m");
429 p_data = (const char *)p_data + val;
434 if (unlikely(i_data == 0))
435 vlc_testcancel (); /* corner case */
437 if ((i_total > 0) || (i_data == 0))
446 * Reads a line from a file descriptor.
447 * This function is not thread-safe; the same file descriptor I/O cannot be
448 * read by another thread at the same time (although it can be written to).
450 * @note This only works with stream-oriented file descriptors, not with
451 * datagram or packet-oriented ones.
453 * @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)
458 size_t bufsize = 0, buflen = 0;
462 if (buflen == bufsize)
464 if (unlikely(bufsize >= (1 << 16)))
465 goto error; /* put sane buffer size limit */
467 char *newbuf = realloc(buf, bufsize + 1024);
468 if (unlikely(newbuf == NULL))
474 ssize_t val = net_Read(obj, fd, vs, buf + buflen, 1, false);
478 if (buf[buflen] == '\n')
485 if (buflen > 0 && buf[buflen - 1] == '\r')
486 buf[buflen - 1] = '\0';
494 ssize_t net_Printf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
495 const char *psz_fmt, ... )
499 va_start( args, psz_fmt );
500 i_ret = net_vaPrintf( p_this, fd, p_vs, psz_fmt, args );
507 ssize_t net_vaPrintf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
508 const char *psz_fmt, va_list args )
513 int i_size = vasprintf( &psz, psz_fmt, args );
516 i_ret = net_Write( p_this, fd, p_vs, psz, i_size ) < i_size