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 *****************************************************************************/
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
49 #if defined(WIN32) || defined(UNDER_CE)
50 # if defined(UNDER_CE) && defined(sockaddr_storage)
51 # undef sockaddr_storage
53 # include <winsock2.h>
54 # include <ws2tcpip.h>
55 # define close closesocket
56 # if defined(UNDER_CE)
57 # undef IP_MULTICAST_TTL
58 # define IP_MULTICAST_TTL 3
59 # undef IP_ADD_MEMBERSHIP
60 # define IP_ADD_MEMBERSHIP 5
63 # include <netdb.h> /* hostent ... */
64 # include <sys/socket.h>
65 # include <netinet/in.h>
66 # ifdef HAVE_ARPA_INET_H
67 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
74 # define INADDR_ANY 0x00000000
77 # define INADDR_NONE 0xFFFFFFFF
80 # define IN_MULTICAST(a) IN_CLASSD(a)
83 # define PF_INET AF_INET /* BeOS */
87 /*****************************************************************************
89 *****************************************************************************/
90 static int NetOpen( vlc_object_t * );
92 /*****************************************************************************
94 *****************************************************************************/
95 #define TIMEOUT_TEXT N_("TCP connection timeout in ms")
96 #define TIMEOUT_LONGTEXT N_( \
97 "Allows you to modify the default TCP connection timeout. This " \
98 "value should be set in millisecond units." )
100 #define MIFACE_TEXT N_("Multicast output interface")
101 #define MIFACE_LONGTEXT N_( \
102 "Indicate here the multicast output interface. " \
103 "This overrides the routing table.")
106 set_description( _("IPv4 network abstraction layer") );
107 set_capability( "network", 50 );
108 set_category( CAT_INPUT );
109 set_subcategory( SUBCAT_INPUT_ADVANCED );
110 set_callbacks( NetOpen, NULL );
112 add_integer( "ipv4-timeout", 5 * 1000, NULL, TIMEOUT_TEXT,
113 TIMEOUT_LONGTEXT, VLC_TRUE );
114 add_string( "miface-addr", NULL, NULL, MIFACE_TEXT, MIFACE_LONGTEXT, VLC_TRUE );
117 /*****************************************************************************
118 * BuildAddr: utility function to build a struct sockaddr_in
119 *****************************************************************************/
120 static int BuildAddr( struct sockaddr_in * p_socket,
121 const char * psz_address, int i_port )
124 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
125 p_socket->sin_family = AF_INET; /* family */
126 p_socket->sin_port = htons( (uint16_t)i_port );
129 p_socket->sin_addr.s_addr = INADDR_ANY;
133 struct hostent * p_hostent;
135 /* Try to convert address directly from in_addr - this will work if
136 * psz_address is dotted decimal. */
137 #ifdef HAVE_ARPA_INET_H
138 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
140 p_socket->sin_addr.s_addr = inet_addr( psz_address );
141 if( p_socket->sin_addr.s_addr == INADDR_NONE )
144 /* We have a fqdn, try to find its address */
145 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
150 /* Copy the first address of the host in the socket address */
151 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
152 p_hostent->h_length );
158 /*****************************************************************************
159 * OpenUDP: open a UDP socket
160 *****************************************************************************
161 * psz_bind_addr, i_bind_port : address and port used for the bind()
162 * system call. If psz_bind_addr == "", the socket is bound to
163 * INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
164 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
165 * join the multicast group.
166 * psz_server_addr, i_server_port : address and port used for the connect()
167 * system call. It can avoid receiving packets from unauthorized IPs.
168 * Its use leads to great confusion and is currently discouraged.
169 * This function returns -1 in case of error.
170 *****************************************************************************/
171 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
173 char * psz_bind_addr = p_socket->psz_bind_addr;
174 int i_bind_port = p_socket->i_bind_port;
175 char * psz_server_addr = p_socket->psz_server_addr;
176 int i_server_port = p_socket->i_server_port;
179 socklen_t i_opt_size;
180 struct sockaddr_in sock;
183 /* If IP_ADD_SOURCE_MEMBERSHIP is not defined in the headers
184 (because it's not in glibc for example), we have to define the
185 headers required for IGMPv3 here */
186 #ifndef IP_ADD_SOURCE_MEMBERSHIP
187 #define IP_ADD_SOURCE_MEMBERSHIP 39
188 struct ip_mreq_source {
189 struct in_addr imr_multiaddr;
190 struct in_addr imr_interface;
191 struct in_addr imr_sourceaddr;
195 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
197 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
199 #if defined(WIN32) || defined(UNDER_CE)
200 msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
202 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
207 /* We may want to reuse an already used socket */
209 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
210 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
212 #if defined(WIN32) || defined(UNDER_CE)
213 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %i)",
216 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
225 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEPORT,
226 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
228 msg_Warn( p_this, "cannot configure socket (SO_REUSEPORT)" );
232 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
233 * packet loss caused by scheduling problems */
235 #if !defined( SYS_BEOS )
236 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
238 #if defined(WIN32) || defined(UNDER_CE)
239 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %i)",
242 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
248 #if !defined( SYS_BEOS )
249 /* Check if we really got what we have asked for, because Linux, etc.
250 * will silently limit the max buffer size to net.core.rmem_max which
251 * is typically only 65535 bytes */
253 i_opt_size = sizeof( i_opt );
254 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void*) &i_opt, &i_opt_size ) == -1 )
256 #if defined(WIN32) || defined(UNDER_CE)
257 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %i)",
260 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
264 else if( i_opt < 0x80000 )
266 msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
272 /* Build the local socket */
274 #if defined( WIN32 ) || defined( UNDER_CE )
275 /* Under Win32 and for multicasting, we bind to INADDR_ANY,
276 * so let's call BuildAddr with "" instead of psz_bind_addr */
277 if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
278 "" : psz_bind_addr, i_bind_port ) == -1 )
280 if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
283 msg_Dbg( p_this, "could not build local address" );
289 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
291 #if defined(WIN32) || defined(UNDER_CE)
292 msg_Warn( p_this, "cannot bind socket (%i)", WSAGetLastError() );
294 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
300 #if defined( WIN32 ) || defined( UNDER_CE )
301 /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
302 if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
304 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
306 msg_Dbg( p_this, "could not build local address" );
313 #if !defined( SYS_BEOS )
314 /* Allow broadcast reception if we bound on INADDR_ANY */
315 if( !*psz_bind_addr )
318 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
320 #if defined(WIN32) || defined(UNDER_CE)
321 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %i)",
324 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
331 #if !defined( SYS_BEOS )
332 /* Join the multicast group if the socket is a multicast address */
333 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
335 /* Determine interface to be used for multicast */
336 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
338 /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
339 so that IGMPv3 aware OSes running on IGMPv3 aware networks
340 will do an IGMPv3 query on the network */
341 if( *psz_server_addr )
343 struct ip_mreq_source imr;
345 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
346 imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
348 if( psz_if_addr != NULL && *psz_if_addr
349 && inet_addr(psz_if_addr) != INADDR_NONE )
351 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
355 imr.imr_interface.s_addr = INADDR_ANY;
357 if( psz_if_addr != NULL ) free( psz_if_addr );
359 msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
360 /* Join Multicast group with source filter */
361 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
363 sizeof(struct ip_mreq_source) ) == -1 )
365 #if defined(WIN32) || defined(UNDER_CE)
366 msg_Err( p_this, "failed to join IP multicast group (%i)",
369 msg_Err( p_this, "failed to join IP multicast group (%s)",
372 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
377 /* If there is no source address, we use IP_ADD_MEMBERSHIP */
382 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
383 if( psz_if_addr != NULL && *psz_if_addr
384 && inet_addr(psz_if_addr) != INADDR_NONE )
386 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
390 imr.imr_interface.s_addr = INADDR_ANY;
392 if( psz_if_addr != NULL ) free( psz_if_addr );
394 msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
395 /* Join Multicast group without source filter */
396 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
397 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
399 #if defined(WIN32) || defined(UNDER_CE)
400 msg_Err( p_this, "failed to join IP multicast group (%i)",
403 msg_Err( p_this, "failed to join IP multicast group (%s)",
413 if( *psz_server_addr )
415 /* Build socket for remote connection */
416 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
418 msg_Warn( p_this, "cannot build remote address" );
423 /* Connect the socket */
424 if( connect( i_handle, (struct sockaddr *) &sock,
425 sizeof( sock ) ) == (-1) )
427 #if defined(WIN32) || defined(UNDER_CE)
428 msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
430 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
436 #if !defined( SYS_BEOS )
437 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
439 /* set the time-to-live */
440 int i_ttl = p_socket->i_ttl;
443 /* set the multicast interface */
444 char * psz_mif_addr = config_GetPsz( p_this, "miface-addr" );
448 intf.s_addr = inet_addr(psz_mif_addr);
449 free( psz_mif_addr );
451 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_IF,
452 &intf, sizeof( intf ) ) < 0 )
454 msg_Dbg( p_this, "failed to set multicast interface (%s).", strerror(errno) );
462 if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS )
464 var_Create( p_this, "ttl",
465 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
466 var_Get( p_this, "ttl", &val );
470 if( i_ttl < 1 ) i_ttl = 1;
471 ttl = (unsigned char) i_ttl;
473 /* There is some confusion in the world whether IP_MULTICAST_TTL
474 * takes a byte or an int as an argument.
475 * BSD seems to indicate byte so we are going with that and use
476 * int as a fallback to be safe */
477 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
478 &ttl, sizeof( ttl ) ) < 0 )
480 msg_Dbg( p_this, "failed to set ttl (%s). Let's try it "
481 "the integer way.", strerror(errno) );
482 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
483 &i_ttl, sizeof( i_ttl ) ) <0 )
485 msg_Err( p_this, "failed to set ttl (%s)",
495 p_socket->i_handle = i_handle;
497 if( var_Get( p_this, "mtu", &val ) != VLC_SUCCESS )
499 var_Create( p_this, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
500 var_Get( p_this, "mtu", &val );
502 p_socket->i_mtu = val.i_int;
506 /*****************************************************************************
507 * SocketTCP: create a TCP socket
508 *****************************************************************************
509 * This function returns -1 in case of error.
510 *****************************************************************************/
511 static int SocketTCP( vlc_object_t * p_this )
515 /* Open a SOCK_STREAM (TCP) socket, in the PF_INET domain, automatic (0)
517 if( (i_handle = socket( PF_INET, SOCK_STREAM, 0 )) == -1 )
519 #if defined(WIN32) || defined(UNDER_CE)
520 msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
522 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
527 /* Set to non-blocking */
528 #if defined( WIN32 ) || defined( UNDER_CE )
530 unsigned long i_dummy = 1;
531 if( ioctlsocket( i_handle, FIONBIO, &i_dummy ) != 0 )
533 msg_Err( p_this, "cannot set socket to non-blocking mode" );
539 if( ( i_flags = fcntl( i_handle, F_GETFL, 0 ) ) < 0 ||
540 fcntl( i_handle, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
542 msg_Err( p_this, "cannot set socket to non-blocking mode" );
550 /*****************************************************************************
551 * OpenTCP: open a TCP socket
552 *****************************************************************************
553 * psz_server_addr, i_server_port : address and port used for the connect()
554 * system call. If i_server_port == 0, 80 is used.
555 * Other parameters are ignored.
556 * This function returns -1 in case of error.
557 *****************************************************************************/
558 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
560 char * psz_server_addr = p_socket->psz_server_addr;
561 int i_server_port = p_socket->i_server_port;
564 struct sockaddr_in sock;
566 if( i_server_port == 0 )
571 if( (i_handle = SocketTCP( p_this )) == -1 )
574 /* Build remote address */
575 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
577 msg_Dbg( p_this, "could not build local address" );
581 /* Connect the socket */
582 if( connect( i_handle, (struct sockaddr *) &sock, sizeof( sock ) ) == -1 )
584 #if defined( WIN32 ) || defined( UNDER_CE )
585 if( WSAGetLastError() == WSAEWOULDBLOCK )
587 if( errno == EINPROGRESS )
590 int i_ret, i_opt, i_opt_size = sizeof( i_opt ), i_max_count;
591 struct timeval timeout;
595 if( var_Get( p_this, "ipv4-timeout", &val ) != VLC_SUCCESS )
597 var_Create( p_this, "ipv4-timeout",
598 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
599 var_Get( p_this, "ipv4-timeout", &val );
601 i_max_count = val.i_int * 1000 / 100000 /* timeout.tv_usec */;
603 msg_Dbg( p_this, "connection in progress" );
606 if( p_this->b_die || i_max_count <= 0 )
608 msg_Dbg( p_this, "connection aborted" );
614 /* Initialize file descriptor set */
616 FD_SET( i_handle, &fds );
618 /* We'll wait 0.1 second if nothing happens */
620 timeout.tv_usec = 100000;
622 } while( ( i_ret = select( i_handle + 1, NULL, &fds, NULL,
624 #if defined( WIN32 ) || defined( UNDER_CE )
625 ( i_ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK ) );
627 ( i_ret < 0 && errno == EINTR ) );
632 msg_Warn( p_this, "cannot connect socket (select failed)" );
636 #if !defined( SYS_BEOS ) && !defined( UNDER_CE )
637 if( getsockopt( i_handle, SOL_SOCKET, SO_ERROR, (void*)&i_opt,
638 &i_opt_size ) == -1 || i_opt != 0 )
640 msg_Warn( p_this, "cannot connect socket (SO_ERROR)" );
647 #if defined(WIN32) || defined(UNDER_CE)
648 msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
650 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
656 p_socket->i_handle = i_handle;
657 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
665 /*****************************************************************************
666 * ListenTCP: open a TCP passive socket (server-side)
667 *****************************************************************************
668 * psz_server_addr, i_server_port : address and port used for the bind()
669 * system call. If i_server_port == 0, 80 is used.
670 * Other parameters are ignored.
671 * This function returns -1 in case of error.
672 *****************************************************************************/
673 static int ListenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
675 char * psz_server_addr = p_socket->psz_server_addr;
676 int i_server_port = p_socket->i_server_port;
678 int i_handle, i_dummy = 1;
679 struct sockaddr_in sock;
681 if( (i_handle = SocketTCP( p_this )) == -1 )
684 if ( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
685 (void *)&i_dummy, sizeof( i_dummy ) ) == -1 )
687 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
690 /* Build remote address */
691 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
693 msg_Dbg( p_this, "could not build local address" );
697 /* Bind the socket */
698 if( bind( i_handle, (struct sockaddr *) &sock, sizeof( sock )) == -1 )
700 #if defined(WIN32) || defined(UNDER_CE)
701 msg_Err( p_this, "cannot bind socket (%i)", WSAGetLastError());
703 msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
709 if( listen( i_handle, 100 ) == -1 )
711 #if defined(WIN32) || defined(UNDER_CE)
712 msg_Err( p_this, "cannot bring socket in listening mode (%i)",
715 msg_Err( p_this, "cannot bring the socket in listening mode (%s)",
721 p_socket->i_handle = i_handle;
722 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
730 /*****************************************************************************
731 * NetOpen: wrapper around OpenUDP, ListenTCP and OpenTCP
732 *****************************************************************************/
733 static int NetOpen( vlc_object_t * p_this )
735 network_socket_t * p_socket = p_this->p_private;
737 if( p_socket->i_type == NETWORK_UDP )
739 return OpenUDP( p_this, p_socket );
741 else if( p_socket->i_type == NETWORK_TCP_PASSIVE )
743 return ListenTCP( p_this, p_socket );
747 return OpenTCP( p_this, p_socket );