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 /* Set to non-blocking */
73 #if defined( WIN32 ) || defined( UNDER_CE )
75 unsigned long i_dummy = 1;
76 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
77 msg_Err( p_this, "cannot set socket to non-blocking mode" );
80 fcntl( fd, F_SETFD, FD_CLOEXEC );
82 if( ( ( i_val = fcntl( fd, F_GETFL, 0 ) ) < 0 ) ||
83 ( fcntl( fd, F_SETFL, i_val | O_NONBLOCK ) < 0 ) )
84 msg_Err( p_this, "cannot set socket to non-blocking mode (%s)",
89 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, (void *)&i_val,
94 * Accepts only IPv6 connections on IPv6 sockets
95 * (and open an IPv4 socket later as well if needed).
96 * Only Linux and FreeBSD can map IPv4 connections on IPv6 sockets,
97 * so this allows for more uniform handling across platforms. Besides,
98 * it makes sure that IPv4 addresses will be printed as w.x.y.z rather
101 if( i_family == AF_INET6 )
102 setsockopt( fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&i_val,
106 #if defined( WIN32 ) || defined( UNDER_CE )
107 # ifndef IPV6_PROTECTION_LEVEL
108 # define IPV6_PROTECTION_LEVEL 23
110 if( i_family == AF_INET6 )
112 i_val = 30 /*PROTECTION_LEVEL_UNRESTRICTED*/;
113 setsockopt( fd, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL,
114 (const char*)&i_val, sizeof( i_val ) );
121 /*****************************************************************************
123 *****************************************************************************
124 * Close a network handle
125 *****************************************************************************/
126 void net_Close( int fd )
129 CloseHandle( (HANDLE)fd );
130 #elif defined( WIN32 )
137 /*****************************************************************************
139 *****************************************************************************
140 * Read from a network socket
141 * If b_retry is true, then we repeat until we have read the right amount of
143 *****************************************************************************/
144 int __net_Read( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
145 uint8_t *p_data, int i_data, vlc_bool_t b_retry )
147 struct timeval timeout;
152 vlc_bool_t b_die = p_this->b_die;
158 if( p_this->b_die != b_die )
163 /* Initialize file descriptor set */
165 FD_SET( fd, &fds_r );
167 FD_SET( fd, &fds_e );
169 /* We'll wait 0.5 second if nothing happens */
171 timeout.tv_usec = 500000;
173 } while( (i_ret = select(fd + 1, &fds_r, NULL, &fds_e, &timeout)) == 0
174 || ( i_ret < 0 && errno == EINTR ) );
178 #if defined(WIN32) || defined(UNDER_CE)
179 msg_Err( p_this, "network select error (%d)", WSAGetLastError() );
181 msg_Err( p_this, "network select error (%s)", strerror(errno) );
183 return i_total > 0 ? i_total : -1;
186 if( ( i_recv = (p_vs != NULL)
187 ? p_vs->pf_recv( p_vs->p_sys, p_data, i_data )
188 : recv( fd, p_data, i_data, 0 ) ) < 0 )
190 #if defined(WIN32) || defined(UNDER_CE)
191 if( WSAGetLastError() == WSAEWOULDBLOCK )
193 /* only happens with p_vs (SSL) - not really an error */
197 /* On win32 recv() will fail if the datagram doesn't fit inside
198 * the passed buffer, even though the buffer will be filled with
199 * the first part of the datagram. */
200 if( WSAGetLastError() == WSAEMSGSIZE )
202 msg_Err( p_this, "recv() failed. "
203 "Increase the mtu size (--mtu option)" );
206 else if( WSAGetLastError() == WSAEINTR ) continue;
207 else msg_Err( p_this, "recv failed (%i)", WSAGetLastError() );
209 /* EAGAIN only happens with p_vs (TLS) and it's not an error */
210 if( errno != EAGAIN )
211 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
213 return i_total > 0 ? i_total : -1;
215 else if( i_recv == 0 )
217 /* Connection closed */
232 /*****************************************************************************
233 * __net_ReadNonBlock:
234 *****************************************************************************
235 * Read from a network socket, non blocking mode (with timeout)
236 *****************************************************************************/
237 int __net_ReadNonBlock( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
238 uint8_t *p_data, int i_data, mtime_t i_wait)
240 struct timeval timeout;
245 /* Initialize file descriptor set */
247 FD_SET( fd, &fds_r );
249 FD_SET( fd, &fds_e );
252 timeout.tv_usec = i_wait;
254 i_ret = select(fd + 1, &fds_r, NULL, &fds_e, &timeout);
256 if( i_ret < 0 && errno == EINTR )
262 #if defined(WIN32) || defined(UNDER_CE)
263 msg_Err( p_this, "network select error (%d)", WSAGetLastError() );
265 msg_Err( p_this, "network select error (%s)", strerror(errno) );
275 #if !defined(UNDER_CE)
276 if( fd == 0/*STDIN_FILENO*/ ) i_recv = read( fd, p_data, i_data ); else
278 if( ( i_recv = (p_vs != NULL)
279 ? p_vs->pf_recv( p_vs->p_sys, p_data, i_data )
280 : recv( fd, p_data, i_data, 0 ) ) < 0 )
282 #if defined(WIN32) || defined(UNDER_CE)
284 /* On win32 recv() will fail if the datagram doesn't fit inside
285 * the passed buffer, even though the buffer will be filled with
286 * the first part of the datagram. */
287 if( WSAGetLastError() == WSAEMSGSIZE )
289 msg_Err( p_this, "recv() failed. "
290 "Increase the mtu size (--mtu option)" );
292 else msg_Err( p_this, "recv failed (%i)", WSAGetLastError() );
294 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
299 return i_recv ? i_recv : -1; /* !i_recv -> connection closed if tcp */
302 /* We will never be here */
306 /*****************************************************************************
308 *****************************************************************************
309 * Read from several sockets (with timeout). Takes data from the first socket
311 *****************************************************************************/
312 int __net_Select( vlc_object_t *p_this, int *pi_fd, v_socket_t **pp_vs,
313 int i_fd, uint8_t *p_data, int i_data, mtime_t i_wait )
315 struct timeval timeout;
322 /* Initialize file descriptor set */
326 for( i = 0 ; i < i_fd ; i++)
328 if( pi_fd[i] > i_max_fd ) i_max_fd = pi_fd[i];
329 FD_SET( pi_fd[i], &fds_r );
330 FD_SET( pi_fd[i], &fds_e );
334 timeout.tv_usec = i_wait;
336 i_ret = select( i_max_fd + 1, &fds_r, NULL, &fds_e, &timeout );
338 if( i_ret < 0 && errno == EINTR )
344 msg_Err( p_this, "network selection error (%s)", strerror(errno) );
347 else if( i_ret == 0 )
353 for( i = 0 ; i < i_fd ; i++)
355 if( FD_ISSET( pi_fd[i], &fds_r ) )
357 i_recv = ((pp_vs != NULL) && (pp_vs[i] != NULL))
358 ? pp_vs[i]->pf_recv( pp_vs[i]->p_sys, p_data, i_data )
359 : recv( pi_fd[i], p_data, i_data, 0 );
364 /* On win32 recv() will fail if the datagram doesn't
365 * fit inside the passed buffer, even though the buffer
366 * will be filled with the first part of the datagram. */
367 if( WSAGetLastError() == WSAEMSGSIZE )
369 msg_Err( p_this, "recv() failed. "
370 "Increase the mtu size (--mtu option)" );
372 else msg_Err( p_this, "recv failed (%i)",
375 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
385 /* We will never be here */
390 /* Write exact amount requested */
391 int __net_Write( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
392 const uint8_t *p_data, int i_data )
394 struct timeval timeout;
400 vlc_bool_t b_die = p_this->b_die;
406 if( p_this->b_die != b_die )
411 /* Initialize file descriptor set */
413 FD_SET( fd, &fds_w );
415 FD_SET( fd, &fds_e );
417 /* We'll wait 0.5 second if nothing happens */
419 timeout.tv_usec = 500000;
421 } while( (i_ret = select(fd + 1, NULL, &fds_w, &fds_e, &timeout)) == 0
422 || ( i_ret < 0 && errno == EINTR ) );
426 #if defined(WIN32) || defined(UNDER_CE)
427 msg_Err( p_this, "network selection error (%d)", WSAGetLastError() );
429 msg_Err( p_this, "network selection error (%s)", strerror(errno) );
431 return i_total > 0 ? i_total : -1;
434 if( ( i_send = (p_vs != NULL)
435 ? p_vs->pf_send( p_vs->p_sys, p_data, i_data )
436 : send( fd, p_data, i_data, 0 ) ) < 0 )
438 /* XXX With udp for example, it will issue a message if the host
440 /* msg_Err( p_this, "send failed (%s)", strerror(errno) ); */
441 return i_total > 0 ? i_total : -1;
451 char *__net_Gets( vlc_object_t *p_this, int fd, v_socket_t *p_vs )
453 char *psz_line = NULL, *ptr = NULL;
454 size_t i_line = 0, i_max = 0;
459 if( i_line == i_max )
462 psz_line = realloc( psz_line, i_max );
463 ptr = psz_line + i_line;
466 if( net_Read( p_this, fd, p_vs, (uint8_t *)ptr, 1, VLC_TRUE ) != 1 )
485 if( ( ptr >= psz_line ) && ( *ptr == '\r' ) )
491 int net_Printf( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
492 const char *psz_fmt, ... )
496 va_start( args, psz_fmt );
497 i_ret = net_vaPrintf( p_this, fd, p_vs, psz_fmt, args );
503 int __net_vaPrintf( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
504 const char *psz_fmt, va_list args )
509 i_size = vasprintf( &psz, psz_fmt, args );
510 i_ret = __net_Write( p_this, fd, p_vs, (uint8_t *)psz, i_size ) < i_size
518 /*****************************************************************************
519 * inet_pton replacement for obsolete and/or crap operating systems
520 *****************************************************************************/
521 #ifndef HAVE_INET_PTON
522 int inet_pton(int af, const char *src, void *dst)
525 /* As we already know, Microsoft always go its own way, so even if they do
526 * provide IPv6, they don't provide the API. */
527 struct sockaddr_storage addr;
528 int len = sizeof( addr );
530 /* Damn it, they didn't even put LPCSTR for the firs parameter!!! */
532 wchar_t *workaround_for_ill_designed_api =
533 malloc( MAX_PATH * sizeof(wchar_t) );
534 mbstowcs( workaround_for_ill_designed_api, src, MAX_PATH );
535 workaround_for_ill_designed_api[MAX_PATH-1] = 0;
537 char *workaround_for_ill_designed_api = strdup( src );
540 if( !WSAStringToAddress( workaround_for_ill_designed_api, af, NULL,
541 (LPSOCKADDR)&addr, &len ) )
543 free( workaround_for_ill_designed_api );
546 free( workaround_for_ill_designed_api );
551 memcpy( dst, &((struct sockaddr_in6 *)&addr)->sin6_addr, 16 );
555 memcpy( dst, &((struct sockaddr_in *)&addr)->sin_addr, 4 );
559 WSASetLastError( WSAEAFNOSUPPORT );
563 /* Assume IPv6 is not supported. */
564 /* Would be safer and more simpler to use inet_aton() but it is most
565 * likely not provided either. */
570 errno = EAFNOSUPPORT;
574 ipv4 = inet_addr( src );
575 if( ipv4 == INADDR_NONE )
578 memcpy( dst, &ipv4, 4 );
582 #endif /* HAVE_INET_PTON */