1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Mathias Kretschmer <mathias@research.att.com>
9 * Alexis de Lattre <alexis@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
48 #if defined( UNDER_CE )
50 # define close(fd) CloseHandle((HANDLE)fd)
51 #elif defined( WIN32 )
52 # include <winsock2.h>
53 # include <ws2tcpip.h>
54 # define close closesocket
56 # include <netdb.h> /* hostent ... */
57 # include <sys/socket.h>
58 # include <netinet/in.h>
59 # ifdef HAVE_ARPA_INET_H
60 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
67 # define INADDR_ANY 0x00000000
70 # define INADDR_NONE 0xFFFFFFFF
73 # define IN_MULTICAST(a) IN_CLASSD(a)
76 # define PF_INET AF_INET /* BeOS */
80 /*****************************************************************************
82 *****************************************************************************/
83 static int NetOpen( vlc_object_t * );
85 /*****************************************************************************
87 *****************************************************************************/
88 #define TIMEOUT_TEXT N_("TCP connection timeout in ms")
89 #define TIMEOUT_LONGTEXT N_( \
90 "Allows you to modify the default TCP connection timeout. This " \
91 "value should be set in millisecond units." )
94 set_description( _("IPv4 network abstraction layer") );
95 set_capability( "network", 50 );
96 set_callbacks( NetOpen, NULL );
98 add_integer( "ipv4-timeout", 5 * 1000, NULL, TIMEOUT_TEXT,
99 TIMEOUT_LONGTEXT, VLC_TRUE );
102 /*****************************************************************************
103 * BuildAddr: utility function to build a struct sockaddr_in
104 *****************************************************************************/
105 static int BuildAddr( struct sockaddr_in * p_socket,
106 const char * psz_address, int i_port )
109 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
110 p_socket->sin_family = AF_INET; /* family */
111 p_socket->sin_port = htons( (uint16_t)i_port );
114 p_socket->sin_addr.s_addr = INADDR_ANY;
118 struct hostent * p_hostent;
120 /* Try to convert address directly from in_addr - this will work if
121 * psz_address is dotted decimal. */
122 #ifdef HAVE_ARPA_INET_H
123 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
125 p_socket->sin_addr.s_addr = inet_addr( psz_address );
126 if( p_socket->sin_addr.s_addr == INADDR_NONE )
129 /* We have a fqdn, try to find its address */
130 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
135 /* Copy the first address of the host in the socket address */
136 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
137 p_hostent->h_length );
143 /*****************************************************************************
144 * OpenUDP: open a UDP socket
145 *****************************************************************************
146 * psz_bind_addr, i_bind_port : address and port used for the bind()
147 * system call. If psz_bind_addr == "", the socket is bound to
148 * INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
149 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
150 * join the multicast group.
151 * psz_server_addr, i_server_port : address and port used for the connect()
152 * system call. It can avoid receiving packets from unauthorized IPs.
153 * Its use leads to great confusion and is currently discouraged.
154 * This function returns -1 in case of error.
155 *****************************************************************************/
156 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
158 char * psz_bind_addr = p_socket->psz_bind_addr;
159 int i_bind_port = p_socket->i_bind_port;
160 char * psz_server_addr = p_socket->psz_server_addr;
161 int i_server_port = p_socket->i_server_port;
164 socklen_t i_opt_size;
165 struct sockaddr_in sock;
168 /* If IP_ADD_SOURCE_MEMBERSHIP is not defined in the headers
169 (because it's not in glibc for example), we have to define the
170 headers required for IGMPv3 here */
171 #ifndef IP_ADD_SOURCE_MEMBERSHIP
172 #define IP_ADD_SOURCE_MEMBERSHIP 39
173 struct ip_mreq_source {
174 struct in_addr imr_multiaddr;
175 struct in_addr imr_interface;
176 struct in_addr imr_sourceaddr;
180 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
182 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
184 #if defined(WIN32) || defined(UNDER_CE)
185 msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
187 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
192 /* We may want to reuse an already used socket */
194 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
195 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
197 #if defined(WIN32) || defined(UNDER_CE)
198 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %i)",
201 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
210 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEPORT,
211 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
213 msg_Warn( p_this, "cannot configure socket (SO_REUSEPORT)" );
217 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
218 * packet loss caused by scheduling problems */
220 #if !defined( SYS_BEOS )
221 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
223 #if defined(WIN32) || defined(UNDER_CE)
224 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %i)",
227 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
233 #if !defined( SYS_BEOS )
234 /* Check if we really got what we have asked for, because Linux, etc.
235 * will silently limit the max buffer size to net.core.rmem_max which
236 * is typically only 65535 bytes */
238 i_opt_size = sizeof( i_opt );
239 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void*) &i_opt, &i_opt_size ) == -1 )
241 #if defined(WIN32) || defined(UNDER_CE)
242 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %i)",
245 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
249 else if( i_opt < 0x80000 )
251 msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
257 /* Build the local socket */
259 #if defined( WIN32 ) && !defined( UNDER_CE )
260 /* Under Win32 and for multicasting, we bind to INADDR_ANY,
261 * so let's call BuildAddr with "" instead of psz_bind_addr */
262 if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
263 "" : psz_bind_addr, i_bind_port ) == -1 )
265 if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
268 msg_Dbg( p_this, "could not build local address" );
274 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
276 #if defined(WIN32) || defined(UNDER_CE)
277 msg_Warn( p_this, "cannot bind socket (%i)", WSAGetLastError() );
279 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
285 #if defined( WIN32 ) && !defined( UNDER_CE )
286 /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
287 if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
289 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
291 msg_Dbg( p_this, "could not build local address" );
298 #if !defined( SYS_BEOS )
299 /* Allow broadcast reception if we bound on INADDR_ANY */
300 if( !*psz_bind_addr )
303 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
305 #if defined(WIN32) || defined(UNDER_CE)
306 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %i)",
309 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
316 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
317 /* Join the multicast group if the socket is a multicast address */
318 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
320 /* Determine interface to be used for multicast */
321 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
323 /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
324 so that IGMPv3 aware OSes running on IGMPv3 aware networks
325 will do an IGMPv3 query on the network */
326 if( *psz_server_addr )
328 struct ip_mreq_source imr;
330 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
331 imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
333 if( psz_if_addr != NULL && *psz_if_addr
334 && inet_addr(psz_if_addr) != INADDR_NONE )
336 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
340 imr.imr_interface.s_addr = INADDR_ANY;
342 if( psz_if_addr != NULL ) free( psz_if_addr );
344 msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
345 /* Join Multicast group with source filter */
346 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
348 sizeof(struct ip_mreq_source) ) == -1 )
350 #if defined(WIN32) || defined(UNDER_CE)
351 msg_Err( p_this, "failed to join IP multicast group (%i)",
354 msg_Err( p_this, "failed to join IP multicast group (%s)",
357 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
362 /* If there is no source address, we use IP_ADD_MEMBERSHIP */
367 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
368 if( psz_if_addr != NULL && *psz_if_addr
369 && inet_addr(psz_if_addr) != INADDR_NONE )
371 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
375 imr.imr_interface.s_addr = INADDR_ANY;
377 if( psz_if_addr != NULL ) free( psz_if_addr );
379 msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
380 /* Join Multicast group without source filter */
381 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
382 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
384 #if defined(WIN32) || defined(UNDER_CE)
385 msg_Err( p_this, "failed to join IP multicast group (%i)",
388 msg_Err( p_this, "failed to join IP multicast group (%s)",
398 if( *psz_server_addr )
400 /* Build socket for remote connection */
401 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
403 msg_Warn( p_this, "cannot build remote address" );
408 /* Connect the socket */
409 if( connect( i_handle, (struct sockaddr *) &sock,
410 sizeof( sock ) ) == (-1) )
412 #if defined(WIN32) || defined(UNDER_CE)
413 msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
415 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
421 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
422 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
424 /* set the time-to-live */
425 int i_ttl = p_socket->i_ttl;
430 if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS )
432 var_Create( p_this, "ttl",
433 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
434 var_Get( p_this, "ttl", &val );
438 if( i_ttl < 1 ) i_ttl = 1;
439 ttl = (unsigned char) i_ttl;
441 /* There is some confusion in the world whether IP_MULTICAST_TTL
442 * takes a byte or an int as an argument.
443 * BSD seems to indicate byte so we are going with that and use
444 * int as a fallback to be safe */
445 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
446 &ttl, sizeof( ttl ) ) < 0 )
448 msg_Dbg( p_this, "failed to set ttl (%s). Let's try it "
449 "the integer way.", strerror(errno) );
450 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
451 &i_ttl, sizeof( i_ttl ) ) <0 )
453 msg_Err( p_this, "failed to set ttl (%s)",
463 p_socket->i_handle = i_handle;
465 if( var_Get( p_this, "mtu", &val ) != VLC_SUCCESS )
467 var_Create( p_this, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
468 var_Get( p_this, "mtu", &val );
470 p_socket->i_mtu = val.i_int;
474 /*****************************************************************************
475 * SocketTCP: create a TCP socket
476 *****************************************************************************
477 * This function returns -1 in case of error.
478 *****************************************************************************/
479 static int SocketTCP( vlc_object_t * p_this )
483 /* Open a SOCK_STREAM (TCP) socket, in the PF_INET domain, automatic (0)
485 if( (i_handle = socket( PF_INET, SOCK_STREAM, 0 )) == -1 )
487 #if defined(WIN32) || defined(UNDER_CE)
488 msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
490 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
495 /* Set to non-blocking */
496 #if defined( WIN32 ) || defined( UNDER_CE )
498 unsigned long i_dummy = 1;
499 if( ioctlsocket( i_handle, FIONBIO, &i_dummy ) != 0 )
501 msg_Err( p_this, "cannot set socket to non-blocking mode" );
507 if( ( i_flags = fcntl( i_handle, F_GETFL, 0 ) ) < 0 ||
508 fcntl( i_handle, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
510 msg_Err( p_this, "cannot set socket to non-blocking mode" );
518 /*****************************************************************************
519 * OpenTCP: open a TCP socket
520 *****************************************************************************
521 * psz_server_addr, i_server_port : address and port used for the connect()
522 * system call. If i_server_port == 0, 80 is used.
523 * Other parameters are ignored.
524 * This function returns -1 in case of error.
525 *****************************************************************************/
526 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
528 char * psz_server_addr = p_socket->psz_server_addr;
529 int i_server_port = p_socket->i_server_port;
532 struct sockaddr_in sock;
534 if( i_server_port == 0 )
539 if( (i_handle = SocketTCP( p_this )) == -1 )
542 /* Build remote address */
543 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
545 msg_Dbg( p_this, "could not build local address" );
549 /* Connect the socket */
550 if( connect( i_handle, (struct sockaddr *) &sock, sizeof( sock ) ) == -1 )
552 #if defined( WIN32 ) || defined( UNDER_CE )
553 if( WSAGetLastError() == WSAEWOULDBLOCK )
555 if( errno == EINPROGRESS )
558 int i_ret, i_opt, i_opt_size = sizeof( i_opt ), i_max_count;
559 struct timeval timeout;
563 if( var_Get( p_this, "ipv4-timeout", &val ) != VLC_SUCCESS )
565 var_Create( p_this, "ipv4-timeout",
566 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
567 var_Get( p_this, "ipv4-timeout", &val );
569 i_max_count = val.i_int * 1000 / 100000 /* timeout.tv_usec */;
571 msg_Dbg( p_this, "connection in progress" );
574 if( p_this->b_die || i_max_count <= 0 )
576 msg_Dbg( p_this, "connection aborted" );
582 /* Initialize file descriptor set */
584 FD_SET( i_handle, &fds );
586 /* We'll wait 0.1 second if nothing happens */
588 timeout.tv_usec = 100000;
590 } while( ( i_ret = select( i_handle + 1, NULL, &fds, NULL,
592 #if defined( WIN32 ) || defined( UNDER_CE )
593 ( i_ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK ) );
595 ( i_ret < 0 && errno == EINTR ) );
600 msg_Warn( p_this, "cannot connect socket (select failed)" );
604 #if !defined( SYS_BEOS ) && !defined( UNDER_CE )
605 if( getsockopt( i_handle, SOL_SOCKET, SO_ERROR, (void*)&i_opt,
606 &i_opt_size ) == -1 || i_opt != 0 )
608 msg_Warn( p_this, "cannot connect socket (SO_ERROR)" );
615 #if defined(WIN32) || defined(UNDER_CE)
616 msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
618 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
624 p_socket->i_handle = i_handle;
625 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
633 /*****************************************************************************
634 * ListenTCP: open a TCP passive socket (server-side)
635 *****************************************************************************
636 * psz_server_addr, i_server_port : address and port used for the bind()
637 * system call. If i_server_port == 0, 80 is used.
638 * Other parameters are ignored.
639 * This function returns -1 in case of error.
640 *****************************************************************************/
641 static int ListenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
643 char * psz_server_addr = p_socket->psz_server_addr;
644 int i_server_port = p_socket->i_server_port;
646 int i_handle, i_dummy = 1;
647 struct sockaddr_in sock;
649 if( (i_handle = SocketTCP( p_this )) == -1 )
652 if ( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
653 (void *)&i_dummy, sizeof( i_dummy ) ) == -1 )
655 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
658 /* Build remote address */
659 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
661 msg_Dbg( p_this, "could not build local address" );
665 /* Bind the socket */
666 if( bind( i_handle, (struct sockaddr *) &sock, sizeof( sock )) == -1 )
668 #if defined(WIN32) || defined(UNDER_CE)
669 msg_Err( p_this, "cannot bind socket (%i)", WSAGetLastError());
671 msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
677 if( listen( i_handle, 100 ) == -1 )
679 #if defined(WIN32) || defined(UNDER_CE)
680 msg_Err( p_this, "cannot bring socket in listening mode (%i)",
683 msg_Err( p_this, "cannot bring the socket in listening mode (%s)",
689 p_socket->i_handle = i_handle;
690 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
698 /*****************************************************************************
699 * NetOpen: wrapper around OpenUDP, ListenTCP and OpenTCP
700 *****************************************************************************/
701 static int NetOpen( vlc_object_t * p_this )
703 network_socket_t * p_socket = p_this->p_private;
705 if( p_socket->i_type == NETWORK_UDP )
707 return OpenUDP( p_this, p_socket );
709 else if( p_socket->i_type == NETWORK_TCP_PASSIVE )
711 return ListenTCP( p_this, p_socket );
715 return OpenTCP( p_this, p_socket );