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 )
63 if( errno != EAFNOSUPPORT )
65 msg_Warn( p_this, "cannot create socket: %s",
66 net_strerror(net_errno) );
70 #if defined( WIN32 ) || defined( UNDER_CE )
72 unsigned long i_dummy = 1;
73 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
74 msg_Err( p_this, "cannot set socket to non-blocking mode" );
77 if( fd >= FD_SETSIZE )
79 /* We don't want to overflow select() fd_set */
80 msg_Err( p_this, "cannot create socket (too many already in use)" );
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 )
139 /*****************************************************************************
141 *****************************************************************************
142 * Read from a network socket
143 * If b_retry is true, then we repeat until we have read the right amount of
145 *****************************************************************************/
146 int __net_Read( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
147 uint8_t *p_data, int i_data, vlc_bool_t b_retry )
149 struct timeval timeout;
154 vlc_bool_t b_die = p_this->b_die;
160 if( p_this->b_die != b_die )
165 /* Initialize file descriptor set */
167 FD_SET( fd, &fds_r );
169 FD_SET( fd, &fds_e );
171 /* We'll wait 0.5 second if nothing happens */
173 timeout.tv_usec = 500000;
175 } while( (i_ret = select(fd + 1, &fds_r, NULL, &fds_e, &timeout)) == 0
176 || ( i_ret < 0 && errno == EINTR ) );
180 msg_Err( p_this, "network select error (%s)",
181 net_strerror(net_errno) );
182 return i_total > 0 ? i_total : -1;
185 if( ( i_recv = (p_vs != NULL)
186 ? p_vs->pf_recv( p_vs->p_sys, p_data, i_data )
187 : recv( fd, p_data, i_data, 0 ) ) < 0 )
189 #if defined(WIN32) || defined(UNDER_CE)
190 if( WSAGetLastError() == WSAEWOULDBLOCK )
192 /* only happens with p_vs (SSL) - not really an error */
196 /* On win32 recv() will fail if the datagram doesn't fit inside
197 * the passed buffer, even though the buffer will be filled with
198 * the first part of the datagram. */
199 if( WSAGetLastError() == WSAEMSGSIZE )
201 msg_Err( p_this, "recv() failed. "
202 "Increase the mtu size (--mtu option)" );
205 else if( WSAGetLastError() == WSAEINTR ) continue;
207 /* EAGAIN only happens with p_vs (TLS) and it's not an error */
208 if( errno != EAGAIN )
210 msg_Err( p_this, "recv failed: %s", net_strerror(net_errno) );
211 return i_total > 0 ? i_total : -1;
213 else if( i_recv == 0 )
215 /* Connection closed */
230 /*****************************************************************************
231 * __net_ReadNonBlock:
232 *****************************************************************************
233 * Read from a network socket, non blocking mode (with timeout)
234 *****************************************************************************/
235 int __net_ReadNonBlock( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
236 uint8_t *p_data, int i_data, mtime_t i_wait)
238 struct timeval timeout;
243 /* Initialize file descriptor set */
245 FD_SET( fd, &fds_r );
247 FD_SET( fd, &fds_e );
250 timeout.tv_usec = i_wait;
252 i_ret = select(fd + 1, &fds_r, NULL, &fds_e, &timeout);
254 if( i_ret < 0 && errno == EINTR )
260 #if defined(WIN32) || defined(UNDER_CE)
261 msg_Err( p_this, "network select error (%d)", WSAGetLastError() );
263 msg_Err( p_this, "network select error (%s)", strerror(errno) );
273 #if !defined(UNDER_CE)
274 if( fd == 0/*STDIN_FILENO*/ ) i_recv = read( fd, p_data, i_data ); else
276 if( ( i_recv = (p_vs != NULL)
277 ? p_vs->pf_recv( p_vs->p_sys, p_data, i_data )
278 : recv( fd, p_data, i_data, 0 ) ) < 0 )
280 #if defined(WIN32) || defined(UNDER_CE)
282 /* On win32 recv() will fail if the datagram doesn't fit inside
283 * the passed buffer, even though the buffer will be filled with
284 * the first part of the datagram. */
285 if( WSAGetLastError() == WSAEMSGSIZE )
287 msg_Err( p_this, "recv() failed. "
288 "Increase the mtu size (--mtu option)" );
290 else msg_Err( p_this, "recv failed (%i)", WSAGetLastError() );
292 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
297 return i_recv ? i_recv : -1; /* !i_recv -> connection closed if tcp */
300 /* We will never be here */
304 /*****************************************************************************
306 *****************************************************************************
307 * Read from several sockets (with timeout). Takes data from the first socket
309 *****************************************************************************/
310 int __net_Select( vlc_object_t *p_this, int *pi_fd, v_socket_t **pp_vs,
311 int i_fd, uint8_t *p_data, int i_data, mtime_t i_wait )
313 struct timeval timeout;
320 /* Initialize file descriptor set */
324 for( i = 0 ; i < i_fd ; i++)
326 if( pi_fd[i] > i_max_fd ) i_max_fd = pi_fd[i];
327 FD_SET( pi_fd[i], &fds_r );
328 FD_SET( pi_fd[i], &fds_e );
332 timeout.tv_usec = i_wait;
334 i_ret = select( i_max_fd + 1, &fds_r, NULL, &fds_e, &timeout );
336 if( i_ret < 0 && errno == EINTR )
342 msg_Err( p_this, "network selection error (%s)", strerror(errno) );
345 else if( i_ret == 0 )
351 for( i = 0 ; i < i_fd ; i++)
353 if( FD_ISSET( pi_fd[i], &fds_r ) )
355 i_recv = ((pp_vs != NULL) && (pp_vs[i] != NULL))
356 ? pp_vs[i]->pf_recv( pp_vs[i]->p_sys, p_data, i_data )
357 : recv( pi_fd[i], p_data, i_data, 0 );
362 /* On win32 recv() will fail if the datagram doesn't
363 * fit inside the passed buffer, even though the buffer
364 * will be filled with the first part of the datagram. */
365 if( WSAGetLastError() == WSAEMSGSIZE )
367 msg_Err( p_this, "recv() failed. "
368 "Increase the mtu size (--mtu option)" );
370 else msg_Err( p_this, "recv failed (%i)",
373 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
383 /* We will never be here */
388 /* Write exact amount requested */
389 int __net_Write( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
390 const uint8_t *p_data, int i_data )
392 struct timeval timeout;
398 vlc_bool_t b_die = p_this->b_die;
404 if( p_this->b_die != b_die )
409 /* Initialize file descriptor set */
411 FD_SET( fd, &fds_w );
413 FD_SET( fd, &fds_e );
415 /* We'll wait 0.5 second if nothing happens */
417 timeout.tv_usec = 500000;
419 } while( (i_ret = select(fd + 1, NULL, &fds_w, &fds_e, &timeout)) == 0
420 || ( i_ret < 0 && errno == EINTR ) );
424 #if defined(WIN32) || defined(UNDER_CE)
425 msg_Err( p_this, "network selection error (%d)", WSAGetLastError() );
427 msg_Err( p_this, "network selection error (%s)", strerror(errno) );
429 return i_total > 0 ? i_total : -1;
432 if( ( i_send = (p_vs != NULL)
433 ? p_vs->pf_send( p_vs->p_sys, p_data, i_data )
434 : send( fd, p_data, i_data, 0 ) ) < 0 )
436 /* XXX With udp for example, it will issue a message if the host
438 /* msg_Err( p_this, "send failed (%s)", strerror(errno) ); */
439 return i_total > 0 ? i_total : -1;
449 char *__net_Gets( vlc_object_t *p_this, int fd, v_socket_t *p_vs )
451 char *psz_line = NULL, *ptr = NULL;
452 size_t i_line = 0, i_max = 0;
457 if( i_line == i_max )
460 psz_line = realloc( psz_line, i_max );
461 ptr = psz_line + i_line;
464 if( net_Read( p_this, fd, p_vs, (uint8_t *)ptr, 1, VLC_TRUE ) != 1 )
483 if( ( ptr >= psz_line ) && ( *ptr == '\r' ) )
489 int net_Printf( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
490 const char *psz_fmt, ... )
494 va_start( args, psz_fmt );
495 i_ret = net_vaPrintf( p_this, fd, p_vs, psz_fmt, args );
501 int __net_vaPrintf( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
502 const char *psz_fmt, va_list args )
507 i_size = vasprintf( &psz, psz_fmt, args );
508 i_ret = __net_Write( p_this, fd, p_vs, (uint8_t *)psz, i_size ) < i_size
516 /*****************************************************************************
517 * inet_pton replacement for obsolete and/or crap operating systems
518 *****************************************************************************/
519 #ifndef HAVE_INET_PTON
520 int inet_pton(int af, const char *src, void *dst)
523 /* As we already know, Microsoft always go its own way, so even if they do
524 * provide IPv6, they don't provide the API. */
525 struct sockaddr_storage addr;
526 int len = sizeof( addr );
528 /* Damn it, they didn't even put LPCSTR for the firs parameter!!! */
530 wchar_t *workaround_for_ill_designed_api =
531 malloc( MAX_PATH * sizeof(wchar_t) );
532 mbstowcs( workaround_for_ill_designed_api, src, MAX_PATH );
533 workaround_for_ill_designed_api[MAX_PATH-1] = 0;
535 char *workaround_for_ill_designed_api = strdup( src );
538 if( !WSAStringToAddress( workaround_for_ill_designed_api, af, NULL,
539 (LPSOCKADDR)&addr, &len ) )
541 free( workaround_for_ill_designed_api );
544 free( workaround_for_ill_designed_api );
549 memcpy( dst, &((struct sockaddr_in6 *)&addr)->sin6_addr, 16 );
553 memcpy( dst, &((struct sockaddr_in *)&addr)->sin_addr, 4 );
557 WSASetLastError( WSAEAFNOSUPPORT );
561 /* Assume IPv6 is not supported. */
562 /* Would be safer and more simpler to use inet_aton() but it is most
563 * likely not provided either. */
568 errno = EAFNOSUPPORT;
572 ipv4 = inet_addr( src );
573 if( ipv4 == INADDR_NONE )
576 memcpy( dst, &ipv4, 4 );
580 #endif /* HAVE_INET_PTON */