X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fnetwork%2Fio.c;h=0491c3992bdcbc34561684257a3083b019928793;hb=d727c4590f83751a2f28fcdfd8af3886ebeb45d8;hp=33119db4c377cf50a805c499214716a51d49641a;hpb=fdb990bd55f690131f00a443f187577fc82c8aa6;p=vlc diff --git a/src/network/io.c b/src/network/io.c index 33119db4c3..0491c3992b 100644 --- a/src/network/io.c +++ b/src/network/io.c @@ -28,7 +28,11 @@ * Preamble *****************************************************************************/ -#include +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include #include #include @@ -64,6 +68,12 @@ # define EAFNOSUPPORT WSAEAFNOSUPPORT #endif +#ifdef HAVE_LINUX_DCCP_H +/* TODO: use glibc instead of linux-kernel headers */ +# include +# define SOL_DCCP 269 +#endif + extern int rootwrap_bind (int family, int socktype, int protocol, const struct sockaddr *addr, size_t alen); @@ -114,6 +124,19 @@ int net_Socket (vlc_object_t *p_this, int family, int socktype, &(int){ PROTECTION_LEVEL_UNRESTRICTED }, sizeof (int)); #endif +#ifdef DCCP_SOCKOPT_SERVICE + if (socktype == SOL_DCCP) + { + char *dccps = var_CreateGetNonEmptyString (p_this, "dccp-service"); + if (dccps != NULL) + { + setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SERVICE, dccps, + (strlen (dccps) + 3) & ~3); + free (dccps); + } + } +#endif + return fd; } @@ -263,46 +286,28 @@ int *net_Listen (vlc_object_t *p_this, const char *psz_host, *****************************************************************************/ ssize_t __net_Read (vlc_object_t *restrict p_this, int fd, const v_socket_t *vs, - uint8_t *restrict p_buf, size_t i_buflen, vlc_bool_t waitall) + uint8_t *restrict p_buf, size_t i_buflen, bool waitall) { size_t i_total = 0; struct pollfd ufd[2] = { - { .fd = fd, .events = POLLIN }, - { .fd = -1, .events = POLLIN }, + { .fd = fd, .events = POLLIN }, + { .fd = vlc_object_waitpipe (p_this), .events = POLLIN }, }; + if (ufd[1].fd == -1) + return -1; /* vlc_object_waitpipe() sets errno */ + while (i_buflen > 0) { - int val; - - ufd[1].fd = vlc_object_waitpipe (p_this); - if (ufd[1].fd == -1) - { - } ufd[0].revents = ufd[1].revents = 0; - val = poll (ufd, sizeof (ufd) / sizeof (ufd[0]), -1); - - if (val < 0) - goto error; - - if (ufd[1].revents) + if (poll (ufd, sizeof (ufd) / sizeof (ufd[0]), -1) < 0) { - msg_Dbg (p_this, "socket %d polling interrupted", fd); - if (i_total == 0) - { -#if defined(WIN32) || defined(UNDER_CE) - WSASetLastError (WSAEINTR); -#else - errno = EINTR; -#endif + if (errno != EINTR) goto error; - } - break; + continue; } - assert (ufd[0].revents); - #ifndef POLLRDHUP /* This is nice but non-portable */ # define POLLRDHUP 0 #endif @@ -313,8 +318,26 @@ __net_Read (vlc_object_t *restrict p_this, int fd, const v_socket_t *vs, * bad idea™. */ if (ufd[0].revents & (POLLERR|POLLNVAL|POLLRDHUP)) break; + if (ufd[1].revents) + break; + } + else + { + if (ufd[1].revents) + { + assert (p_this->b_die); + msg_Dbg (p_this, "socket %d polling interrupted", fd); +#if defined(WIN32) || defined(UNDER_CE) + WSASetLastError (WSAEINTR); +#else + errno = EINTR; +#endif + goto silent; + } } + assert (ufd[0].revents); + ssize_t n; if (vs != NULL) { @@ -347,16 +370,16 @@ __net_Read (vlc_object_t *restrict p_this, int fd, const v_socket_t *vs, "Increase the mtu size (--mtu option)"); n = i_buflen; break; - - default: - goto error; } #else - /* spurious wake-up or TLS did not yield any actual data */ - if (errno == EAGAIN) - continue; - goto error; + switch (errno) + { + case EAGAIN: /* spurious wakeup or no TLS data */ + case EINTR: /* asynchronous signal */ + continue; + } #endif + goto error; } if (n == 0) @@ -380,6 +403,7 @@ __net_Read (vlc_object_t *restrict p_this, int fd, const v_socket_t *vs, error: msg_Err (p_this, "Read error: %m"); +silent: return -1; } @@ -389,30 +413,46 @@ 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 ) { size_t i_total = 0; + struct pollfd ufd[2] = { + { .fd = fd, .events = POLLOUT }, + { .fd = vlc_object_waitpipe (p_this), .events = POLLIN }, + }; + + if (ufd[1].fd == -1) + return -1; while( i_data > 0 ) { - if( p_this->b_die ) - break; + ssize_t val; - struct pollfd ufd[1]; - memset (ufd, 0, sizeof (ufd)); - ufd[0].fd = fd; - ufd[0].events = POLLOUT; + ufd[0].revents = ufd[1].revents = 0; - int val = poll (ufd, 1, 500); - switch (val) + if (poll (ufd, 1, -1) == -1) { - case -1: - msg_Err (p_this, "Write error: %m"); - goto out; - - case 0: + if (errno == EINTR) continue; + msg_Err (p_this, "Polling error: %m"); + return -1; } - if ((ufd[0].revents & (POLLERR|POLLNVAL|POLLHUP)) && (i_total > 0)) - return i_total; // error will be dequeued separately on next call + if (i_total > 0) + { /* 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)) + break; + if (ufd[1].revents) /* VLC object signaled */ + break; + } + else + { + if (ufd[1].revents) + { + assert (p_this->b_die); + errno = EINTR; + goto error; + } + } if (p_vs != NULL) val = p_vs->pf_send (p_vs->p_sys, p_data, i_data); @@ -425,6 +465,8 @@ ssize_t __net_Write( vlc_object_t *p_this, int fd, const v_socket_t *p_vs, if (val == -1) { + if (errno == EINTR) + continue; msg_Err (p_this, "Write error: %m"); break; } @@ -434,10 +476,10 @@ ssize_t __net_Write( vlc_object_t *p_this, int fd, const v_socket_t *p_vs, i_total += val; } -out: if ((i_total > 0) || (i_data == 0)) return i_total; +error: return -1; } @@ -456,7 +498,7 @@ char *__net_Gets( vlc_object_t *p_this, int fd, const v_socket_t *p_vs ) ptr = psz_line + i_line; } - if( net_Read( p_this, fd, p_vs, (uint8_t *)ptr, 1, VLC_TRUE ) != 1 ) + if( net_Read( p_this, fd, p_vs, (uint8_t *)ptr, 1, true ) != 1 ) { if( i_line == 0 ) { @@ -511,9 +553,9 @@ ssize_t __net_vaPrintf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs, /***************************************************************************** * inet_pton replacement for obsolete and/or crap operating systems *****************************************************************************/ -#ifndef HAVE_INET_PTON -int inet_pton(int af, const char *src, void *dst) +int vlc_inet_pton(int af, const char *src, void *dst) { +#ifndef HAVE_INET_PTON # 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. */ @@ -530,7 +572,7 @@ int inet_pton(int af, const char *src, void *dst) char *workaround_for_ill_designed_api = strdup( src ); #endif - if( !WSAStringToAddress( workaround_for_ill_designed_api, af, NULL, + if( WSAStringToAddress( workaround_for_ill_designed_api, af, NULL, (LPSOCKADDR)&addr, &len ) ) { free( workaround_for_ill_designed_api ); @@ -571,14 +613,16 @@ int inet_pton(int af, const char *src, void *dst) memcpy( dst, &ipv4, 4 ); # endif /* WIN32 */ return 0; -} +#else /* HAVE_INET_PTON */ + return inet_pton( af, src, dst ); #endif /* HAVE_INET_PTON */ +} -#ifndef HAVE_INET_NTOP -#ifdef WIN32 -const char *inet_ntop(int af, const void * src, +const char *vlc_inet_ntop(int af, const void * src, char * dst, socklen_t cnt) { +#ifndef HAVE_INET_NTOP +#ifdef WIN32 switch( af ) { #ifdef AF_INET6 @@ -621,6 +665,25 @@ const char *inet_ntop(int af, const void * src, } errno = EAFNOSUPPORT; return NULL; +#else /* WIN32 */ + return NULL; +#endif /* WIN32 */ +#else /* HAVE_INET_NTOP */ + return inet_ntop( af, src, dst, cnt ); +#endif /* HAVE_INET_NTOP */ } -#endif -#endif + +#ifdef WIN32 + /* vlc_sendmsg, vlc_recvmsg Defined in winsock.c */ +#else /* !WIN32 */ +ssize_t vlc_sendmsg (int s, struct msghdr *hdr, int flags) +{ + return sendmsg (s, hdr, flags); +} + +ssize_t vlc_recvmsg (int s, struct msghdr *hdr, int flags) +{ + return recvmsg (s, hdr, flags); +} +#endif /* WIN32 */ +