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_callbacks( NetOpen, NULL );
110 add_integer( "ipv4-timeout", 5 * 1000, NULL, TIMEOUT_TEXT,
111 TIMEOUT_LONGTEXT, VLC_TRUE );
112 add_string( "miface-addr", NULL, NULL, MIFACE_TEXT, MIFACE_LONGTEXT, VLC_TRUE );
115 /*****************************************************************************
116 * BuildAddr: utility function to build a struct sockaddr_in
117 *****************************************************************************/
118 static int BuildAddr( struct sockaddr_in * p_socket,
119 const char * psz_address, int i_port )
122 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
123 p_socket->sin_family = AF_INET; /* family */
124 p_socket->sin_port = htons( (uint16_t)i_port );
127 p_socket->sin_addr.s_addr = INADDR_ANY;
131 struct hostent * p_hostent;
133 /* Try to convert address directly from in_addr - this will work if
134 * psz_address is dotted decimal. */
135 #ifdef HAVE_ARPA_INET_H
136 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
138 p_socket->sin_addr.s_addr = inet_addr( psz_address );
139 if( p_socket->sin_addr.s_addr == INADDR_NONE )
142 /* We have a fqdn, try to find its address */
143 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
148 /* Copy the first address of the host in the socket address */
149 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
150 p_hostent->h_length );
156 /*****************************************************************************
157 * OpenUDP: open a UDP socket
158 *****************************************************************************
159 * psz_bind_addr, i_bind_port : address and port used for the bind()
160 * system call. If psz_bind_addr == "", the socket is bound to
161 * INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
162 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
163 * join the multicast group.
164 * psz_server_addr, i_server_port : address and port used for the connect()
165 * system call. It can avoid receiving packets from unauthorized IPs.
166 * Its use leads to great confusion and is currently discouraged.
167 * This function returns -1 in case of error.
168 *****************************************************************************/
169 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
171 char * psz_bind_addr = p_socket->psz_bind_addr;
172 int i_bind_port = p_socket->i_bind_port;
173 char * psz_server_addr = p_socket->psz_server_addr;
174 int i_server_port = p_socket->i_server_port;
177 socklen_t i_opt_size;
178 struct sockaddr_in sock;
181 /* If IP_ADD_SOURCE_MEMBERSHIP is not defined in the headers
182 (because it's not in glibc for example), we have to define the
183 headers required for IGMPv3 here */
184 #ifndef IP_ADD_SOURCE_MEMBERSHIP
185 #define IP_ADD_SOURCE_MEMBERSHIP 39
186 struct ip_mreq_source {
187 struct in_addr imr_multiaddr;
188 struct in_addr imr_interface;
189 struct in_addr imr_sourceaddr;
193 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
195 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
197 #if defined(WIN32) || defined(UNDER_CE)
198 msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
200 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
205 /* We may want to reuse an already used socket */
207 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
208 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
210 #if defined(WIN32) || defined(UNDER_CE)
211 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %i)",
214 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
223 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEPORT,
224 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
226 msg_Warn( p_this, "cannot configure socket (SO_REUSEPORT)" );
230 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
231 * packet loss caused by scheduling problems */
233 #if !defined( SYS_BEOS )
234 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
236 #if defined(WIN32) || defined(UNDER_CE)
237 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %i)",
240 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
246 #if !defined( SYS_BEOS )
247 /* Check if we really got what we have asked for, because Linux, etc.
248 * will silently limit the max buffer size to net.core.rmem_max which
249 * is typically only 65535 bytes */
251 i_opt_size = sizeof( i_opt );
252 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void*) &i_opt, &i_opt_size ) == -1 )
254 #if defined(WIN32) || defined(UNDER_CE)
255 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %i)",
258 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
262 else if( i_opt < 0x80000 )
264 msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
270 /* Build the local socket */
272 #if defined( WIN32 ) || defined( UNDER_CE )
273 /* Under Win32 and for multicasting, we bind to INADDR_ANY,
274 * so let's call BuildAddr with "" instead of psz_bind_addr */
275 if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
276 "" : psz_bind_addr, i_bind_port ) == -1 )
278 if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
281 msg_Dbg( p_this, "could not build local address" );
287 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
289 #if defined(WIN32) || defined(UNDER_CE)
290 msg_Warn( p_this, "cannot bind socket (%i)", WSAGetLastError() );
292 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
298 #if defined( WIN32 ) || defined( UNDER_CE )
299 /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
300 if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
302 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
304 msg_Dbg( p_this, "could not build local address" );
311 #if !defined( SYS_BEOS )
312 /* Allow broadcast reception if we bound on INADDR_ANY */
313 if( !*psz_bind_addr )
316 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
318 #if defined(WIN32) || defined(UNDER_CE)
319 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %i)",
322 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
329 #if !defined( SYS_BEOS )
330 /* Join the multicast group if the socket is a multicast address */
331 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
333 /* Determine interface to be used for multicast */
334 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
336 /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
337 so that IGMPv3 aware OSes running on IGMPv3 aware networks
338 will do an IGMPv3 query on the network */
339 if( *psz_server_addr )
341 struct ip_mreq_source imr;
343 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
344 imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
346 if( psz_if_addr != NULL && *psz_if_addr
347 && inet_addr(psz_if_addr) != INADDR_NONE )
349 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
353 imr.imr_interface.s_addr = INADDR_ANY;
355 if( psz_if_addr != NULL ) free( psz_if_addr );
357 msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
358 /* Join Multicast group with source filter */
359 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
361 sizeof(struct ip_mreq_source) ) == -1 )
363 #if defined(WIN32) || defined(UNDER_CE)
364 msg_Err( p_this, "failed to join IP multicast group (%i)",
367 msg_Err( p_this, "failed to join IP multicast group (%s)",
370 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
375 /* If there is no source address, we use IP_ADD_MEMBERSHIP */
380 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
381 if( psz_if_addr != NULL && *psz_if_addr
382 && inet_addr(psz_if_addr) != INADDR_NONE )
384 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
388 imr.imr_interface.s_addr = INADDR_ANY;
390 if( psz_if_addr != NULL ) free( psz_if_addr );
392 msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
393 /* Join Multicast group without source filter */
394 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
395 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
397 #if defined(WIN32) || defined(UNDER_CE)
398 msg_Err( p_this, "failed to join IP multicast group (%i)",
401 msg_Err( p_this, "failed to join IP multicast group (%s)",
411 if( *psz_server_addr )
413 /* Build socket for remote connection */
414 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
416 msg_Warn( p_this, "cannot build remote address" );
421 /* Connect the socket */
422 if( connect( i_handle, (struct sockaddr *) &sock,
423 sizeof( sock ) ) == (-1) )
425 #if defined(WIN32) || defined(UNDER_CE)
426 msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
428 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
434 #if !defined( SYS_BEOS )
435 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
437 /* set the time-to-live */
438 int i_ttl = p_socket->i_ttl;
441 /* set the multicast interface */
442 char * psz_mif_addr = config_GetPsz( p_this, "miface-addr" );
446 intf.s_addr = inet_addr(psz_mif_addr);
447 free( psz_mif_addr );
449 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_IF,
450 &intf, sizeof( intf ) ) < 0 )
452 msg_Dbg( p_this, "failed to set multicast interface (%s).", strerror(errno) );
460 if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS )
462 var_Create( p_this, "ttl",
463 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
464 var_Get( p_this, "ttl", &val );
468 if( i_ttl < 1 ) i_ttl = 1;
469 ttl = (unsigned char) i_ttl;
471 /* There is some confusion in the world whether IP_MULTICAST_TTL
472 * takes a byte or an int as an argument.
473 * BSD seems to indicate byte so we are going with that and use
474 * int as a fallback to be safe */
475 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
476 &ttl, sizeof( ttl ) ) < 0 )
478 msg_Dbg( p_this, "failed to set ttl (%s). Let's try it "
479 "the integer way.", strerror(errno) );
480 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
481 &i_ttl, sizeof( i_ttl ) ) <0 )
483 msg_Err( p_this, "failed to set ttl (%s)",
493 p_socket->i_handle = i_handle;
495 if( var_Get( p_this, "mtu", &val ) != VLC_SUCCESS )
497 var_Create( p_this, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
498 var_Get( p_this, "mtu", &val );
500 p_socket->i_mtu = val.i_int;
504 /*****************************************************************************
505 * SocketTCP: create a TCP socket
506 *****************************************************************************
507 * This function returns -1 in case of error.
508 *****************************************************************************/
509 static int SocketTCP( vlc_object_t * p_this )
513 /* Open a SOCK_STREAM (TCP) socket, in the PF_INET domain, automatic (0)
515 if( (i_handle = socket( PF_INET, SOCK_STREAM, 0 )) == -1 )
517 #if defined(WIN32) || defined(UNDER_CE)
518 msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
520 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
525 /* Set to non-blocking */
526 #if defined( WIN32 ) || defined( UNDER_CE )
528 unsigned long i_dummy = 1;
529 if( ioctlsocket( i_handle, FIONBIO, &i_dummy ) != 0 )
531 msg_Err( p_this, "cannot set socket to non-blocking mode" );
537 if( ( i_flags = fcntl( i_handle, F_GETFL, 0 ) ) < 0 ||
538 fcntl( i_handle, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
540 msg_Err( p_this, "cannot set socket to non-blocking mode" );
548 /*****************************************************************************
549 * OpenTCP: open a TCP socket
550 *****************************************************************************
551 * psz_server_addr, i_server_port : address and port used for the connect()
552 * system call. If i_server_port == 0, 80 is used.
553 * Other parameters are ignored.
554 * This function returns -1 in case of error.
555 *****************************************************************************/
556 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
558 char * psz_server_addr = p_socket->psz_server_addr;
559 int i_server_port = p_socket->i_server_port;
562 struct sockaddr_in sock;
564 if( i_server_port == 0 )
569 if( (i_handle = SocketTCP( p_this )) == -1 )
572 /* Build remote address */
573 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
575 msg_Dbg( p_this, "could not build local address" );
579 /* Connect the socket */
580 if( connect( i_handle, (struct sockaddr *) &sock, sizeof( sock ) ) == -1 )
582 #if defined( WIN32 ) || defined( UNDER_CE )
583 if( WSAGetLastError() == WSAEWOULDBLOCK )
585 if( errno == EINPROGRESS )
588 int i_ret, i_opt, i_opt_size = sizeof( i_opt ), i_max_count;
589 struct timeval timeout;
593 if( var_Get( p_this, "ipv4-timeout", &val ) != VLC_SUCCESS )
595 var_Create( p_this, "ipv4-timeout",
596 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
597 var_Get( p_this, "ipv4-timeout", &val );
599 i_max_count = val.i_int * 1000 / 100000 /* timeout.tv_usec */;
601 msg_Dbg( p_this, "connection in progress" );
604 if( p_this->b_die || i_max_count <= 0 )
606 msg_Dbg( p_this, "connection aborted" );
612 /* Initialize file descriptor set */
614 FD_SET( i_handle, &fds );
616 /* We'll wait 0.1 second if nothing happens */
618 timeout.tv_usec = 100000;
620 } while( ( i_ret = select( i_handle + 1, NULL, &fds, NULL,
622 #if defined( WIN32 ) || defined( UNDER_CE )
623 ( i_ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK ) );
625 ( i_ret < 0 && errno == EINTR ) );
630 msg_Warn( p_this, "cannot connect socket (select failed)" );
634 #if !defined( SYS_BEOS ) && !defined( UNDER_CE )
635 if( getsockopt( i_handle, SOL_SOCKET, SO_ERROR, (void*)&i_opt,
636 &i_opt_size ) == -1 || i_opt != 0 )
638 msg_Warn( p_this, "cannot connect socket (SO_ERROR)" );
645 #if defined(WIN32) || defined(UNDER_CE)
646 msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
648 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
654 p_socket->i_handle = i_handle;
655 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
663 /*****************************************************************************
664 * ListenTCP: open a TCP passive socket (server-side)
665 *****************************************************************************
666 * psz_server_addr, i_server_port : address and port used for the bind()
667 * system call. If i_server_port == 0, 80 is used.
668 * Other parameters are ignored.
669 * This function returns -1 in case of error.
670 *****************************************************************************/
671 static int ListenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
673 char * psz_server_addr = p_socket->psz_server_addr;
674 int i_server_port = p_socket->i_server_port;
676 int i_handle, i_dummy = 1;
677 struct sockaddr_in sock;
679 if( (i_handle = SocketTCP( p_this )) == -1 )
682 if ( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
683 (void *)&i_dummy, sizeof( i_dummy ) ) == -1 )
685 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
688 /* Build remote address */
689 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
691 msg_Dbg( p_this, "could not build local address" );
695 /* Bind the socket */
696 if( bind( i_handle, (struct sockaddr *) &sock, sizeof( sock )) == -1 )
698 #if defined(WIN32) || defined(UNDER_CE)
699 msg_Err( p_this, "cannot bind socket (%i)", WSAGetLastError());
701 msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
707 if( listen( i_handle, 100 ) == -1 )
709 #if defined(WIN32) || defined(UNDER_CE)
710 msg_Err( p_this, "cannot bring socket in listening mode (%i)",
713 msg_Err( p_this, "cannot bring the socket in listening mode (%s)",
719 p_socket->i_handle = i_handle;
720 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
728 /*****************************************************************************
729 * NetOpen: wrapper around OpenUDP, ListenTCP and OpenTCP
730 *****************************************************************************/
731 static int NetOpen( vlc_object_t * p_this )
733 network_socket_t * p_socket = p_this->p_private;
735 if( p_socket->i_type == NETWORK_UDP )
737 return OpenUDP( p_this, p_socket );
739 else if( p_socket->i_type == NETWORK_TCP_PASSIVE )
741 return ListenTCP( p_this, p_socket );
745 return OpenTCP( p_this, p_socket );