1 /*****************************************************************************
2 * io.c: network I/O functions
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
5 * Copyright © 2005-2006 Rémi Denis-Courmont
8 * Authors: Laurent Aimar <fenrir@videolan.org>
9 * Rémi Denis-Courmont <rem # videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
54 # define INADDR_ANY 0x00000000
57 # define INADDR_NONE 0xFFFFFFFF
60 int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
65 fd = socket( i_family, i_socktype, i_protocol );
68 #if defined(WIN32) || defined(UNDER_CE)
69 if( WSAGetLastError ( ) != WSAEAFNOSUPPORT )
71 if( errno != EAFNOSUPPORT )
73 msg_Warn( p_this, "cannot create socket: %s",
74 net_strerror(net_errno) );
78 #if defined( WIN32 ) || defined( UNDER_CE )
80 unsigned long i_dummy = 1;
81 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
82 msg_Err( p_this, "cannot set socket to non-blocking mode" );
85 fcntl( fd, F_SETFD, FD_CLOEXEC );
86 i_val = fcntl( fd, F_GETFL, 0 );
87 fcntl( fd, F_SETFL, ((i_val != -1) ? i_val : 0) | O_NONBLOCK );
91 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, (void *)&i_val,
96 * Accepts only IPv6 connections on IPv6 sockets
97 * (and open an IPv4 socket later as well if needed).
98 * Only Linux and FreeBSD can map IPv4 connections on IPv6 sockets,
99 * so this allows for more uniform handling across platforms. Besides,
100 * it makes sure that IPv4 addresses will be printed as w.x.y.z rather
101 * than ::ffff:w.x.y.z
103 if( i_family == AF_INET6 )
104 setsockopt( fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&i_val,
108 #if defined( WIN32 ) || defined( UNDER_CE )
109 # ifndef IPV6_PROTECTION_LEVEL
110 # define IPV6_PROTECTION_LEVEL 23
112 if( i_family == AF_INET6 )
114 i_val = 30 /*PROTECTION_LEVEL_UNRESTRICTED*/;
115 setsockopt( fd, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL,
116 (const char*)&i_val, sizeof( i_val ) );
123 /*****************************************************************************
125 *****************************************************************************
126 * Close a network handle
127 *****************************************************************************/
128 void net_Close( int fd )
131 CloseHandle( (HANDLE)fd );
132 #elif defined( WIN32 )
141 net_ReadInner( vlc_object_t *restrict p_this, unsigned fdc, const int *fdv,
142 const v_socket_t *const *restrict vsv,
143 uint8_t *restrict p_buf, size_t i_buflen,
144 int wait_ms, vlc_bool_t waitall )
148 unsigned int i_eof = 0;
149 memset( eof, 0, sizeof( eof ) );
156 struct pollfd ufd[fdc];
163 return i_total; // output buffer full
166 return i_total; // all sockets are at EOF
169 if( (wait_ms != -1) && (wait_ms < 500) )
179 memset(ufd, 0, sizeof (ufd) );
181 for( i = 0; i < fdc; i++ )
184 ufd[i].events = POLLIN;
187 n = poll( ufd, fdc, delay_ms );
191 assert( (unsigned int)n <= fdc );
193 for( i = 0; n > 0; i++ )
195 if( (i_total > 0) && (ufd[i].revents & POLLERR) )
196 return i_total; // error will be dequeued on next run
210 for( i = 0; i < fdc; i++ )
212 #if !defined(WIN32) && !defined(UNDER_CE)
213 if( fdv[i] >= FD_SETSIZE )
215 /* We don't want to overflow select() fd_set */
216 msg_Err( p_this, "select set overflow" );
220 FD_SET( fdv[i], &set );
225 n = select( maxfd + 1, &set, NULL, NULL,
226 (wait_ms == -1) ? NULL
227 : &(struct timeval){ 0, delay_ms * 1000 } );
231 for( i = 0; n > 0; i++ )
232 if( FD_ISSET (fdv[i], &set) )
247 n = (*vsv)->pf_recv( (*vsv)->p_sys, p_buf, i_buflen );
251 #if defined(WIN32) || defined(UNDER_CE)
252 n = recv( *fdv, p_buf, i_buflen, 0 );
254 n = read( *fdv, p_buf, i_buflen );
260 #if defined(WIN32) || defined(UNDER_CE)
261 switch( WSAGetLastError() )
264 /* only happens with vs != NULL (SSL) - not really an error */
269 /* On Win32, recv() fails if the datagram doesn't fit inside
270 * the passed buffer, even though the buffer will be filled
271 * with the first part of the datagram. */
272 msg_Err( p_this, "Receive error: "
273 "Increase the mtu size (--mtu option)" );
278 if( errno == EAGAIN ) /* spurious wake-up (sucks if fdc > 1) */
288 if( n == 0 ) // EOF on socket
291 for( j = 0 ; j < i_eof ; j++ )
292 if( eof[j] == *fdv ) goto end;
308 return i_total; // timeout
311 msg_Err( p_this, "Read error: %s", net_strerror (net_errno) );
312 return i_total ? (int)i_total : -1;
316 /*****************************************************************************
318 *****************************************************************************
319 * Read from a network socket
320 * If b_retry is true, then we repeat until we have read the right amount of
322 *****************************************************************************/
323 int __net_Read( vlc_object_t *restrict p_this, int fd,
324 const v_socket_t *restrict p_vs,
325 uint8_t *restrict p_data, int i_data, vlc_bool_t b_retry )
327 return net_ReadInner( p_this, 1, &(int){ fd },
328 &(const v_socket_t *){ p_vs },
329 p_data, i_data, -1, b_retry );
333 /*****************************************************************************
334 * __net_ReadNonBlock:
335 *****************************************************************************
336 * Read from a network socket, non blocking mode (with timeout)
337 *****************************************************************************/
338 int __net_ReadNonBlock( vlc_object_t *restrict p_this, int fd,
339 const v_socket_t *restrict p_vs,
340 uint8_t *restrict p_data, int i_data, mtime_t i_wait)
342 return net_ReadInner (p_this, 1, &(int){ fd },
343 &(const v_socket_t *){ p_vs },
344 p_data, i_data, i_wait / 1000, VLC_FALSE);
348 /*****************************************************************************
350 *****************************************************************************
351 * Read from several sockets (with timeout). Takes data from the first socket
353 *****************************************************************************/
354 int __net_Select( vlc_object_t *restrict p_this, const int *restrict pi_fd,
355 const v_socket_t *const *restrict pp_vs,
356 int i_fd, uint8_t *restrict p_data, int i_data,
361 const v_socket_t *vsv[i_fd];
362 memset( vsv, 0, sizeof (vsv) );
364 return net_ReadInner( p_this, i_fd, pi_fd, vsv, p_data, i_data,
365 i_wait / 1000, VLC_FALSE );
368 return net_ReadInner( p_this, i_fd, pi_fd, pp_vs, p_data, i_data,
369 i_wait / 1000, VLC_FALSE );
373 /* Write exact amount requested */
374 int __net_Write( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
375 const uint8_t *p_data, int i_data )
385 struct pollfd ufd[1];
386 memset (ufd, 0, sizeof (ufd));
388 ufd[0].events = POLLOUT;
390 int val = poll (ufd, 1, 500);
391 if ((val > 0) && (ufd[0].revents & POLLERR) && (i_total > 0))
392 return i_total; // error will be dequeued separately on next call
397 #if !defined(WIN32) && !defined(UNDER_CE)
398 if (fd >= FD_SETSIZE)
400 /* We don't want to overflow select() fd_set */
401 msg_Err (p_this, "select set overflow");
407 int val = select (fd + 1, NULL, &set, NULL,
408 &(struct timeval){ 0, 500000 });
415 msg_Err (p_this, "Write error: %s",
416 net_strerror (net_errno));
417 return i_total ? (int)i_total : -1;
425 val = p_vs->pf_send (p_vs->p_sys, p_data, i_data);
427 #if defined(WIN32) || defined(UNDER_CE)
428 val = send (fd, p_data, i_data, 0);
430 val = write (fd, p_data, i_data);
434 return i_total ? (int)i_total : -1;
446 char *__net_Gets( vlc_object_t *p_this, int fd, const v_socket_t *p_vs )
448 char *psz_line = NULL, *ptr = NULL;
449 size_t i_line = 0, i_max = 0;
454 if( i_line == i_max )
457 psz_line = realloc( psz_line, i_max );
458 ptr = psz_line + i_line;
461 if( net_Read( p_this, fd, p_vs, (uint8_t *)ptr, 1, VLC_TRUE ) != 1 )
480 if( ( ptr >= psz_line ) && ( *ptr == '\r' ) )
486 int net_Printf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
487 const char *psz_fmt, ... )
491 va_start( args, psz_fmt );
492 i_ret = net_vaPrintf( p_this, fd, p_vs, psz_fmt, args );
498 int __net_vaPrintf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
499 const char *psz_fmt, va_list args )
504 i_size = vasprintf( &psz, psz_fmt, args );
505 i_ret = __net_Write( p_this, fd, p_vs, (uint8_t *)psz, i_size ) < i_size
513 /*****************************************************************************
514 * inet_pton replacement for obsolete and/or crap operating systems
515 *****************************************************************************/
516 #ifndef HAVE_INET_PTON
517 int inet_pton(int af, const char *src, void *dst)
520 /* As we already know, Microsoft always go its own way, so even if they do
521 * provide IPv6, they don't provide the API. */
522 struct sockaddr_storage addr;
523 int len = sizeof( addr );
525 /* Damn it, they didn't even put LPCSTR for the firs parameter!!! */
527 wchar_t *workaround_for_ill_designed_api =
528 malloc( MAX_PATH * sizeof(wchar_t) );
529 mbstowcs( workaround_for_ill_designed_api, src, MAX_PATH );
530 workaround_for_ill_designed_api[MAX_PATH-1] = 0;
532 char *workaround_for_ill_designed_api = strdup( src );
535 if( !WSAStringToAddress( workaround_for_ill_designed_api, af, NULL,
536 (LPSOCKADDR)&addr, &len ) )
538 free( workaround_for_ill_designed_api );
541 free( workaround_for_ill_designed_api );
546 memcpy( dst, &((struct sockaddr_in6 *)&addr)->sin6_addr, 16 );
550 memcpy( dst, &((struct sockaddr_in *)&addr)->sin_addr, 4 );
554 WSASetLastError( WSAEAFNOSUPPORT );
558 /* Assume IPv6 is not supported. */
559 /* Would be safer and more simpler to use inet_aton() but it is most
560 * likely not provided either. */
565 errno = EAFNOSUPPORT;
569 ipv4 = inet_addr( src );
570 if( ipv4 == INADDR_NONE )
573 memcpy( dst, &ipv4, 4 );
577 #endif /* HAVE_INET_PTON */