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 msg_Err( p_this, "network error: %s", net_strerror(net_errno) );
269 #if !defined(UNDER_CE)
270 if( fd == 0/*STDIN_FILENO*/ ) i_recv = read( fd, p_data, i_data ); else
272 if( ( i_recv = (p_vs != NULL)
273 ? p_vs->pf_recv( p_vs->p_sys, p_data, i_data )
274 : recv( fd, p_data, i_data, 0 ) ) < 0 )
276 #if defined(WIN32) || defined(UNDER_CE)
278 /* On win32 recv() will fail if the datagram doesn't fit inside
279 * the passed buffer, even though the buffer will be filled with
280 * the first part of the datagram. */
281 if( WSAGetLastError() == WSAEMSGSIZE )
283 msg_Err( p_this, "recv() failed. "
284 "Increase the mtu size (--mtu option)" );
288 msg_Err( p_this, "recv failed: %s", net_strerror(net_errno) );
292 return i_recv ? i_recv : -1; /* !i_recv -> connection closed if tcp */
295 /* We will never be here */
299 /*****************************************************************************
301 *****************************************************************************
302 * Read from several sockets (with timeout). Takes data from the first socket
304 *****************************************************************************/
305 int __net_Select( vlc_object_t *p_this, int *pi_fd, v_socket_t **pp_vs,
306 int i_fd, uint8_t *p_data, int i_data, mtime_t i_wait )
308 struct timeval timeout;
315 /* Initialize file descriptor set */
319 for( i = 0 ; i < i_fd ; i++)
321 if( pi_fd[i] > i_max_fd ) i_max_fd = pi_fd[i];
322 FD_SET( pi_fd[i], &fds_r );
323 FD_SET( pi_fd[i], &fds_e );
327 timeout.tv_usec = i_wait;
329 i_ret = select( i_max_fd + 1, &fds_r, NULL, &fds_e, &timeout );
331 if( i_ret < 0 && errno == EINTR )
337 msg_Err( p_this, "network selection error: %s", net_strerror(net_errno) );
340 else if( i_ret == 0 )
346 for( i = 0 ; i < i_fd ; i++)
348 if( FD_ISSET( pi_fd[i], &fds_r ) )
350 i_recv = ((pp_vs != NULL) && (pp_vs[i] != NULL))
351 ? pp_vs[i]->pf_recv( pp_vs[i]->p_sys, p_data, i_data )
352 : recv( pi_fd[i], p_data, i_data, 0 );
357 /* On win32 recv() will fail if the datagram doesn't
358 * fit inside the passed buffer, even though the buffer
359 * will be filled with the first part of the datagram. */
360 if( WSAGetLastError() == WSAEMSGSIZE )
362 msg_Err( p_this, "recv() failed. "
363 "Increase the mtu size (--mtu option)" );
365 else msg_Err( p_this, "recv failed (%i)",
368 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
378 /* We will never be here */
383 /* Write exact amount requested */
384 int __net_Write( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
385 const uint8_t *p_data, int i_data )
387 struct timeval timeout;
393 vlc_bool_t b_die = p_this->b_die;
399 if( p_this->b_die != b_die )
404 /* Initialize file descriptor set */
406 FD_SET( fd, &fds_w );
408 FD_SET( fd, &fds_e );
410 /* We'll wait 0.5 second if nothing happens */
412 timeout.tv_usec = 500000;
414 } while( (i_ret = select(fd + 1, NULL, &fds_w, &fds_e, &timeout)) == 0
415 || ( i_ret < 0 && errno == EINTR ) );
419 #if defined(WIN32) || defined(UNDER_CE)
420 msg_Err( p_this, "network selection error (%d)", WSAGetLastError() );
422 msg_Err( p_this, "network selection error (%s)", strerror(errno) );
424 return i_total > 0 ? i_total : -1;
427 if( ( i_send = (p_vs != NULL)
428 ? p_vs->pf_send( p_vs->p_sys, p_data, i_data )
429 : send( fd, p_data, i_data, 0 ) ) < 0 )
431 /* XXX With udp for example, it will issue a message if the host
433 /* msg_Err( p_this, "send failed (%s)", strerror(errno) ); */
434 return i_total > 0 ? i_total : -1;
444 char *__net_Gets( vlc_object_t *p_this, int fd, v_socket_t *p_vs )
446 char *psz_line = NULL, *ptr = NULL;
447 size_t i_line = 0, i_max = 0;
452 if( i_line == i_max )
455 psz_line = realloc( psz_line, i_max );
456 ptr = psz_line + i_line;
459 if( net_Read( p_this, fd, p_vs, (uint8_t *)ptr, 1, VLC_TRUE ) != 1 )
478 if( ( ptr >= psz_line ) && ( *ptr == '\r' ) )
484 int net_Printf( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
485 const char *psz_fmt, ... )
489 va_start( args, psz_fmt );
490 i_ret = net_vaPrintf( p_this, fd, p_vs, psz_fmt, args );
496 int __net_vaPrintf( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
497 const char *psz_fmt, va_list args )
502 i_size = vasprintf( &psz, psz_fmt, args );
503 i_ret = __net_Write( p_this, fd, p_vs, (uint8_t *)psz, i_size ) < i_size
511 /*****************************************************************************
512 * inet_pton replacement for obsolete and/or crap operating systems
513 *****************************************************************************/
514 #ifndef HAVE_INET_PTON
515 int inet_pton(int af, const char *src, void *dst)
518 /* As we already know, Microsoft always go its own way, so even if they do
519 * provide IPv6, they don't provide the API. */
520 struct sockaddr_storage addr;
521 int len = sizeof( addr );
523 /* Damn it, they didn't even put LPCSTR for the firs parameter!!! */
525 wchar_t *workaround_for_ill_designed_api =
526 malloc( MAX_PATH * sizeof(wchar_t) );
527 mbstowcs( workaround_for_ill_designed_api, src, MAX_PATH );
528 workaround_for_ill_designed_api[MAX_PATH-1] = 0;
530 char *workaround_for_ill_designed_api = strdup( src );
533 if( !WSAStringToAddress( workaround_for_ill_designed_api, af, NULL,
534 (LPSOCKADDR)&addr, &len ) )
536 free( workaround_for_ill_designed_api );
539 free( workaround_for_ill_designed_api );
544 memcpy( dst, &((struct sockaddr_in6 *)&addr)->sin6_addr, 16 );
548 memcpy( dst, &((struct sockaddr_in *)&addr)->sin_addr, 4 );
552 WSASetLastError( WSAEAFNOSUPPORT );
556 /* Assume IPv6 is not supported. */
557 /* Would be safer and more simpler to use inet_aton() but it is most
558 * likely not provided either. */
563 errno = EAFNOSUPPORT;
567 ipv4 = inet_addr( src );
568 if( ipv4 == INADDR_NONE )
571 memcpy( dst, &ipv4, 4 );
575 #endif /* HAVE_INET_PTON */