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) )
173 memset(ufd, 0, sizeof (ufd) );
175 for( i = 0; i < fdc; i++ )
178 ufd[i].events = POLLIN;
184 n = poll( ufd, fdc, (wait_ms == -1) ? -1 : delay_ms );
188 assert( (unsigned int)n <= fdc );
190 for( i = 0; n > 0; i++ )
192 if( (i_total > 0) && (ufd[i].revents & POLLERR) )
193 return i_total; // error will be dequeued on next run
207 for( i = 0; i < fdc; i++ )
209 #if !defined(WIN32) && !defined(UNDER_CE)
210 if( fdv[i] >= FD_SETSIZE )
212 /* We don't want to overflow select() fd_set */
213 msg_Err( p_this, "select set overflow" );
217 FD_SET( fdv[i], &set );
222 n = select( maxfd + 1, &set, NULL, NULL,
223 (wait_ms == -1) ? NULL
224 : &(struct timeval){ 0, delay_ms * 1000 } );
228 for( i = 0; n > 0; i++ )
229 if( FD_ISSET (fdv[i], &set) )
244 n = (*vsv)->pf_recv( (*vsv)->p_sys, p_buf, i_buflen );
248 #if defined(WIN32) || defined(UNDER_CE)
249 n = recv( *fdv, p_buf, i_buflen, 0 );
251 n = read( *fdv, p_buf, i_buflen );
257 #if defined(WIN32) || defined(UNDER_CE)
258 switch( WSAGetLastError() )
261 /* only happens with vs != NULL (SSL) - not really an error */
266 /* On Win32, recv() fails if the datagram doesn't fit inside
267 * the passed buffer, even though the buffer will be filled
268 * with the first part of the datagram. */
269 msg_Err( p_this, "Receive error: "
270 "Increase the mtu size (--mtu option)" );
275 if( errno == EAGAIN ) /* spurious wake-up (sucks if fdc > 1) */
285 if( n == 0 ) // EOF on socket
288 for( j = 0 ; j < i_eof ; j++ )
289 if( eof[j] == *fdv ) goto end;
305 return i_total; // timeout
308 msg_Err( p_this, "Read error: %s", net_strerror (net_errno) );
309 return i_total ? (int)i_total : -1;
313 /*****************************************************************************
315 *****************************************************************************
316 * Read from a network socket
317 * If b_retry is true, then we repeat until we have read the right amount of
319 *****************************************************************************/
320 int __net_Read( vlc_object_t *restrict p_this, int fd,
321 const v_socket_t *restrict p_vs,
322 uint8_t *restrict p_data, int i_data, vlc_bool_t b_retry )
324 return net_ReadInner( p_this, 1, &(int){ fd },
325 &(const v_socket_t *){ p_vs },
326 p_data, i_data, -1, b_retry );
330 /*****************************************************************************
331 * __net_ReadNonBlock:
332 *****************************************************************************
333 * Read from a network socket, non blocking mode (with timeout)
334 *****************************************************************************/
335 int __net_ReadNonBlock( vlc_object_t *restrict p_this, int fd,
336 const v_socket_t *restrict p_vs,
337 uint8_t *restrict p_data, int i_data, mtime_t i_wait)
339 return net_ReadInner (p_this, 1, &(int){ fd },
340 &(const v_socket_t *){ p_vs },
341 p_data, i_data, i_wait / 1000, VLC_FALSE);
345 /*****************************************************************************
347 *****************************************************************************
348 * Read from several sockets (with timeout). Takes data from the first socket
350 *****************************************************************************/
351 int __net_Select( vlc_object_t *restrict p_this, const int *restrict pi_fd,
352 const v_socket_t *const *restrict pp_vs,
353 int i_fd, uint8_t *restrict p_data, int i_data,
358 const v_socket_t *vsv[i_fd];
359 memset( vsv, 0, sizeof (vsv) );
361 return net_ReadInner( p_this, i_fd, pi_fd, vsv, p_data, i_data,
362 i_wait / 1000, VLC_FALSE );
365 return net_ReadInner( p_this, i_fd, pi_fd, pp_vs, p_data, i_data,
366 i_wait / 1000, VLC_FALSE );
370 /* Write exact amount requested */
371 int __net_Write( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
372 const uint8_t *p_data, int i_data )
382 struct pollfd ufd[1];
383 memset (ufd, 0, sizeof (ufd));
385 ufd[0].events = POLLOUT;
387 int val = poll (ufd, 1, 500);
388 if ((val > 0) && (ufd[0].revents & POLLERR) && (i_total > 0))
389 return i_total; // error will be dequeued separately on next call
394 #if !defined(WIN32) && !defined(UNDER_CE)
395 if (fd >= FD_SETSIZE)
397 /* We don't want to overflow select() fd_set */
398 msg_Err (p_this, "select set overflow");
404 int val = select (fd + 1, NULL, &set, NULL,
405 &(struct timeval){ 0, 500000 });
412 msg_Err (p_this, "Write error: %s",
413 net_strerror (net_errno));
414 return i_total ? (int)i_total : -1;
422 val = p_vs->pf_send (p_vs->p_sys, p_data, i_data);
424 #if defined(WIN32) || defined(UNDER_CE)
425 val = recv (fd, p_data, i_data, 0);
427 val = write (fd, p_data, i_data);
431 return i_total ? (int)i_total : -1;
443 char *__net_Gets( vlc_object_t *p_this, int fd, const v_socket_t *p_vs )
445 char *psz_line = NULL, *ptr = NULL;
446 size_t i_line = 0, i_max = 0;
451 if( i_line == i_max )
454 psz_line = realloc( psz_line, i_max );
455 ptr = psz_line + i_line;
458 if( net_Read( p_this, fd, p_vs, (uint8_t *)ptr, 1, VLC_TRUE ) != 1 )
477 if( ( ptr >= psz_line ) && ( *ptr == '\r' ) )
483 int net_Printf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
484 const char *psz_fmt, ... )
488 va_start( args, psz_fmt );
489 i_ret = net_vaPrintf( p_this, fd, p_vs, psz_fmt, args );
495 int __net_vaPrintf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
496 const char *psz_fmt, va_list args )
501 i_size = vasprintf( &psz, psz_fmt, args );
502 i_ret = __net_Write( p_this, fd, p_vs, (uint8_t *)psz, i_size ) < i_size
510 /*****************************************************************************
511 * inet_pton replacement for obsolete and/or crap operating systems
512 *****************************************************************************/
513 #ifndef HAVE_INET_PTON
514 int inet_pton(int af, const char *src, void *dst)
517 /* As we already know, Microsoft always go its own way, so even if they do
518 * provide IPv6, they don't provide the API. */
519 struct sockaddr_storage addr;
520 int len = sizeof( addr );
522 /* Damn it, they didn't even put LPCSTR for the firs parameter!!! */
524 wchar_t *workaround_for_ill_designed_api =
525 malloc( MAX_PATH * sizeof(wchar_t) );
526 mbstowcs( workaround_for_ill_designed_api, src, MAX_PATH );
527 workaround_for_ill_designed_api[MAX_PATH-1] = 0;
529 char *workaround_for_ill_designed_api = strdup( src );
532 if( !WSAStringToAddress( workaround_for_ill_designed_api, af, NULL,
533 (LPSOCKADDR)&addr, &len ) )
535 free( workaround_for_ill_designed_api );
538 free( workaround_for_ill_designed_api );
543 memcpy( dst, &((struct sockaddr_in6 *)&addr)->sin6_addr, 16 );
547 memcpy( dst, &((struct sockaddr_in *)&addr)->sin_addr, 4 );
551 WSASetLastError( WSAEAFNOSUPPORT );
555 /* Assume IPv6 is not supported. */
556 /* Would be safer and more simpler to use inet_aton() but it is most
557 * likely not provided either. */
562 errno = EAFNOSUPPORT;
566 ipv4 = inet_addr( src );
567 if( ipv4 == INADDR_NONE )
570 memcpy( dst, &ipv4, 4 );
574 #endif /* HAVE_INET_PTON */