1 /*****************************************************************************
2 * io.c: network I/O functions
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@videolan.org>
8 * RĂ©mi Denis-Courmont <rem # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
46 # define INADDR_ANY 0x00000000
49 # define INADDR_NONE 0xFFFFFFFF
52 int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
57 fd = socket( i_family, i_socktype, i_protocol );
60 #if defined(WIN32) || defined(UNDER_CE)
61 if( WSAGetLastError ( ) != WSAEAFNOSUPPORT )
62 msg_Warn( p_this, "cannot create socket (%i)",
65 if( errno != EAFNOSUPPORT )
66 msg_Warn( p_this, "cannot create socket (%s)",
72 if( fd >= FD_SETSIZE )
74 msg_Err( p_this, "cannot create socket (too many already in use)" );
79 /* Set to non-blocking */
80 #if defined( WIN32 ) || defined( UNDER_CE )
82 unsigned long i_dummy = 1;
83 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
84 msg_Err( p_this, "cannot set socket to non-blocking mode" );
87 fcntl( fd, F_SETFD, FD_CLOEXEC );
89 if( ( ( i_val = fcntl( fd, F_GETFL, 0 ) ) < 0 ) ||
90 ( fcntl( fd, F_SETFL, i_val | O_NONBLOCK ) < 0 ) )
91 msg_Err( p_this, "cannot set socket to non-blocking mode (%s)",
96 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, (void *)&i_val,
101 * Accepts only IPv6 connections on IPv6 sockets
102 * (and open an IPv4 socket later as well if needed).
103 * Only Linux and FreeBSD can map IPv4 connections on IPv6 sockets,
104 * so this allows for more uniform handling across platforms. Besides,
105 * it makes sure that IPv4 addresses will be printed as w.x.y.z rather
106 * than ::ffff:w.x.y.z
108 if( i_family == AF_INET6 )
109 setsockopt( fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&i_val,
113 #if defined( WIN32 ) || defined( UNDER_CE )
114 # ifndef IPV6_PROTECTION_LEVEL
115 # define IPV6_PROTECTION_LEVEL 23
117 if( i_family == AF_INET6 )
119 i_val = 30 /*PROTECTION_LEVEL_UNRESTRICTED*/;
120 setsockopt( fd, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL,
121 (const char*)&i_val, sizeof( i_val ) );
128 /*****************************************************************************
130 *****************************************************************************
131 * Close a network handle
132 *****************************************************************************/
133 void net_Close( int fd )
136 CloseHandle( (HANDLE)fd );
137 #elif defined( WIN32 )
144 /*****************************************************************************
146 *****************************************************************************
147 * Read from a network socket
148 * If b_retry is true, then we repeat until we have read the right amount of
150 *****************************************************************************/
151 int __net_Read( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
152 uint8_t *p_data, int i_data, vlc_bool_t b_retry )
154 struct timeval timeout;
159 vlc_bool_t b_die = p_this->b_die;
165 if( p_this->b_die != b_die )
170 /* Initialize file descriptor set */
172 FD_SET( fd, &fds_r );
174 FD_SET( fd, &fds_e );
176 /* We'll wait 0.5 second if nothing happens */
178 timeout.tv_usec = 500000;
180 } while( (i_ret = select(fd + 1, &fds_r, NULL, &fds_e, &timeout)) == 0
181 || ( i_ret < 0 && errno == EINTR ) );
185 #if defined(WIN32) || defined(UNDER_CE)
186 msg_Err( p_this, "network select error (%d)", WSAGetLastError() );
188 msg_Err( p_this, "network select error (%s)", strerror(errno) );
190 return i_total > 0 ? i_total : -1;
193 if( ( i_recv = (p_vs != NULL)
194 ? p_vs->pf_recv( p_vs->p_sys, p_data, i_data )
195 : recv( fd, p_data, i_data, 0 ) ) < 0 )
197 #if defined(WIN32) || defined(UNDER_CE)
198 if( WSAGetLastError() == WSAEWOULDBLOCK )
200 /* only happens with p_vs (SSL) - not really an error */
204 /* On win32 recv() will fail if the datagram doesn't fit inside
205 * the passed buffer, even though the buffer will be filled with
206 * the first part of the datagram. */
207 if( WSAGetLastError() == WSAEMSGSIZE )
209 msg_Err( p_this, "recv() failed. "
210 "Increase the mtu size (--mtu option)" );
213 else if( WSAGetLastError() == WSAEINTR ) continue;
214 else msg_Err( p_this, "recv failed (%i)", WSAGetLastError() );
216 /* EAGAIN only happens with p_vs (TLS) and it's not an error */
217 if( errno != EAGAIN )
218 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
220 return i_total > 0 ? i_total : -1;
222 else if( i_recv == 0 )
224 /* Connection closed */
239 /*****************************************************************************
240 * __net_ReadNonBlock:
241 *****************************************************************************
242 * Read from a network socket, non blocking mode (with timeout)
243 *****************************************************************************/
244 int __net_ReadNonBlock( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
245 uint8_t *p_data, int i_data, mtime_t i_wait)
247 struct timeval timeout;
252 /* Initialize file descriptor set */
254 FD_SET( fd, &fds_r );
256 FD_SET( fd, &fds_e );
259 timeout.tv_usec = i_wait;
261 i_ret = select(fd + 1, &fds_r, NULL, &fds_e, &timeout);
263 if( i_ret < 0 && errno == EINTR )
269 #if defined(WIN32) || defined(UNDER_CE)
270 msg_Err( p_this, "network select error (%d)", WSAGetLastError() );
272 msg_Err( p_this, "network select error (%s)", strerror(errno) );
282 #if !defined(UNDER_CE)
283 if( fd == 0/*STDIN_FILENO*/ ) i_recv = read( fd, p_data, i_data ); else
285 if( ( i_recv = (p_vs != NULL)
286 ? p_vs->pf_recv( p_vs->p_sys, p_data, i_data )
287 : recv( fd, p_data, i_data, 0 ) ) < 0 )
289 #if defined(WIN32) || defined(UNDER_CE)
291 /* On win32 recv() will fail if the datagram doesn't fit inside
292 * the passed buffer, even though the buffer will be filled with
293 * the first part of the datagram. */
294 if( WSAGetLastError() == WSAEMSGSIZE )
296 msg_Err( p_this, "recv() failed. "
297 "Increase the mtu size (--mtu option)" );
299 else msg_Err( p_this, "recv failed (%i)", WSAGetLastError() );
301 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
306 return i_recv ? i_recv : -1; /* !i_recv -> connection closed if tcp */
309 /* We will never be here */
313 /*****************************************************************************
315 *****************************************************************************
316 * Read from several sockets (with timeout). Takes data from the first socket
318 *****************************************************************************/
319 int __net_Select( vlc_object_t *p_this, int *pi_fd, v_socket_t **pp_vs,
320 int i_fd, uint8_t *p_data, int i_data, mtime_t i_wait )
322 struct timeval timeout;
329 /* Initialize file descriptor set */
333 for( i = 0 ; i < i_fd ; i++)
335 if( pi_fd[i] > i_max_fd ) i_max_fd = pi_fd[i];
336 FD_SET( pi_fd[i], &fds_r );
337 FD_SET( pi_fd[i], &fds_e );
341 timeout.tv_usec = i_wait;
343 i_ret = select( i_max_fd + 1, &fds_r, NULL, &fds_e, &timeout );
345 if( i_ret < 0 && errno == EINTR )
351 msg_Err( p_this, "network selection error (%s)", strerror(errno) );
354 else if( i_ret == 0 )
360 for( i = 0 ; i < i_fd ; i++)
362 if( FD_ISSET( pi_fd[i], &fds_r ) )
364 i_recv = ((pp_vs != NULL) && (pp_vs[i] != NULL))
365 ? pp_vs[i]->pf_recv( pp_vs[i]->p_sys, p_data, i_data )
366 : recv( pi_fd[i], p_data, i_data, 0 );
371 /* On win32 recv() will fail if the datagram doesn't
372 * fit inside the passed buffer, even though the buffer
373 * will be filled with the first part of the datagram. */
374 if( WSAGetLastError() == WSAEMSGSIZE )
376 msg_Err( p_this, "recv() failed. "
377 "Increase the mtu size (--mtu option)" );
379 else msg_Err( p_this, "recv failed (%i)",
382 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
392 /* We will never be here */
397 /* Write exact amount requested */
398 int __net_Write( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
399 const uint8_t *p_data, int i_data )
401 struct timeval timeout;
407 vlc_bool_t b_die = p_this->b_die;
413 if( p_this->b_die != b_die )
418 /* Initialize file descriptor set */
420 FD_SET( fd, &fds_w );
422 FD_SET( fd, &fds_e );
424 /* We'll wait 0.5 second if nothing happens */
426 timeout.tv_usec = 500000;
428 } while( (i_ret = select(fd + 1, NULL, &fds_w, &fds_e, &timeout)) == 0
429 || ( i_ret < 0 && errno == EINTR ) );
433 #if defined(WIN32) || defined(UNDER_CE)
434 msg_Err( p_this, "network selection error (%d)", WSAGetLastError() );
436 msg_Err( p_this, "network selection error (%s)", strerror(errno) );
438 return i_total > 0 ? i_total : -1;
441 if( ( i_send = (p_vs != NULL)
442 ? p_vs->pf_send( p_vs->p_sys, p_data, i_data )
443 : send( fd, p_data, i_data, 0 ) ) < 0 )
445 /* XXX With udp for example, it will issue a message if the host
447 /* msg_Err( p_this, "send failed (%s)", strerror(errno) ); */
448 return i_total > 0 ? i_total : -1;
458 char *__net_Gets( vlc_object_t *p_this, int fd, v_socket_t *p_vs )
460 char *psz_line = NULL, *ptr = NULL;
461 size_t i_line = 0, i_max = 0;
466 if( i_line == i_max )
469 psz_line = realloc( psz_line, i_max );
470 ptr = psz_line + i_line;
473 if( net_Read( p_this, fd, p_vs, (uint8_t *)ptr, 1, VLC_TRUE ) != 1 )
492 if( ( ptr >= psz_line ) && ( *ptr == '\r' ) )
498 int net_Printf( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
499 const char *psz_fmt, ... )
503 va_start( args, psz_fmt );
504 i_ret = net_vaPrintf( p_this, fd, p_vs, psz_fmt, args );
510 int __net_vaPrintf( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
511 const char *psz_fmt, va_list args )
516 i_size = vasprintf( &psz, psz_fmt, args );
517 i_ret = __net_Write( p_this, fd, p_vs, (uint8_t *)psz, i_size ) < i_size
525 /*****************************************************************************
526 * inet_pton replacement for obsolete and/or crap operating systems
527 *****************************************************************************/
528 #ifndef HAVE_INET_PTON
529 int inet_pton(int af, const char *src, void *dst)
532 /* As we already know, Microsoft always go its own way, so even if they do
533 * provide IPv6, they don't provide the API. */
534 struct sockaddr_storage addr;
535 int len = sizeof( addr );
537 /* Damn it, they didn't even put LPCSTR for the firs parameter!!! */
539 wchar_t *workaround_for_ill_designed_api =
540 malloc( MAX_PATH * sizeof(wchar_t) );
541 mbstowcs( workaround_for_ill_designed_api, src, MAX_PATH );
542 workaround_for_ill_designed_api[MAX_PATH-1] = 0;
544 char *workaround_for_ill_designed_api = strdup( src );
547 if( !WSAStringToAddress( workaround_for_ill_designed_api, af, NULL,
548 (LPSOCKADDR)&addr, &len ) )
550 free( workaround_for_ill_designed_api );
553 free( workaround_for_ill_designed_api );
558 memcpy( dst, &((struct sockaddr_in6 *)&addr)->sin6_addr, 16 );
562 memcpy( dst, &((struct sockaddr_in *)&addr)->sin_addr, 4 );
566 WSASetLastError( WSAEAFNOSUPPORT );
570 /* Assume IPv6 is not supported. */
571 /* Would be safer and more simpler to use inet_aton() but it is most
572 * likely not provided either. */
577 errno = EAFNOSUPPORT;
581 ipv4 = inet_addr( src );
582 if( ipv4 == INADDR_NONE )
585 memcpy( dst, &ipv4, 4 );
589 #endif /* HAVE_INET_PTON */