1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001-2005 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 MIFACE_TEXT N_("Multicast output interface")
96 #define MIFACE_LONGTEXT N_( \
97 "Indicate here the multicast output interface. " \
98 "This overrides the routing table.")
101 set_description( _("IPv4 network abstraction layer") );
102 set_capability( "network", 50 );
103 set_category( CAT_INPUT );
104 set_subcategory( SUBCAT_INPUT_ADVANCED );
105 set_callbacks( NetOpen, NULL );
106 add_string( "miface-addr", NULL, NULL, MIFACE_TEXT, MIFACE_LONGTEXT, VLC_TRUE );
109 /*****************************************************************************
110 * BuildAddr: utility function to build a struct sockaddr_in
111 *****************************************************************************/
112 static int BuildAddr( struct sockaddr_in * p_socket,
113 const char * psz_address, int i_port )
116 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
117 p_socket->sin_family = AF_INET; /* family */
118 p_socket->sin_port = htons( (uint16_t)i_port );
121 p_socket->sin_addr.s_addr = INADDR_ANY;
125 struct hostent * p_hostent;
127 /* Try to convert address directly from in_addr - this will work if
128 * psz_address is dotted decimal. */
129 #ifdef HAVE_ARPA_INET_H
130 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
132 p_socket->sin_addr.s_addr = inet_addr( psz_address );
133 if( p_socket->sin_addr.s_addr == INADDR_NONE )
136 /* We have a fqdn, try to find its address */
137 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
142 /* Copy the first address of the host in the socket address */
143 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
144 p_hostent->h_length );
150 /*****************************************************************************
151 * OpenUDP: open a UDP socket
152 *****************************************************************************
153 * psz_bind_addr, i_bind_port : address and port used for the bind()
154 * system call. If psz_bind_addr == "", the socket is bound to
155 * INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
156 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
157 * join the multicast group.
158 * psz_server_addr, i_server_port : address and port used for the connect()
159 * system call. It can avoid receiving packets from unauthorized IPs.
160 * Its use leads to great confusion and is currently discouraged.
161 * This function returns -1 in case of error.
162 *****************************************************************************/
163 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
165 char * psz_bind_addr = p_socket->psz_bind_addr;
166 int i_bind_port = p_socket->i_bind_port;
167 char * psz_server_addr = p_socket->psz_server_addr;
168 int i_server_port = p_socket->i_server_port;
171 socklen_t i_opt_size;
172 struct sockaddr_in sock;
175 /* If IP_ADD_SOURCE_MEMBERSHIP is not defined in the headers
176 (because it's not in glibc for example), we have to define the
177 headers required for IGMPv3 here */
178 #ifndef IP_ADD_SOURCE_MEMBERSHIP
179 #define IP_ADD_SOURCE_MEMBERSHIP 39
180 struct ip_mreq_source {
181 struct in_addr imr_multiaddr;
182 struct in_addr imr_interface;
183 struct in_addr imr_sourceaddr;
187 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
189 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
191 #if defined(WIN32) || defined(UNDER_CE)
192 msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
194 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
199 /* We may want to reuse an already used socket */
201 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
202 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
204 #if defined(WIN32) || defined(UNDER_CE)
205 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %i)",
208 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
217 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEPORT,
218 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
220 msg_Warn( p_this, "cannot configure socket (SO_REUSEPORT)" );
224 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
225 * packet loss caused by scheduling problems */
227 #if !defined( SYS_BEOS )
228 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
230 #if defined(WIN32) || defined(UNDER_CE)
231 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %i)",
234 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
240 #if !defined( SYS_BEOS )
241 /* Check if we really got what we have asked for, because Linux, etc.
242 * will silently limit the max buffer size to net.core.rmem_max which
243 * is typically only 65535 bytes */
245 i_opt_size = sizeof( i_opt );
246 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void*) &i_opt, &i_opt_size ) == -1 )
248 #if defined(WIN32) || defined(UNDER_CE)
249 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %i)",
252 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
256 else if( i_opt < 0x80000 )
258 msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
264 /* Build the local socket */
266 #if defined( WIN32 ) || defined( UNDER_CE )
267 /* Under Win32 and for multicasting, we bind to INADDR_ANY,
268 * so let's call BuildAddr with "" instead of psz_bind_addr */
269 if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
270 "" : psz_bind_addr, i_bind_port ) == -1 )
272 if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
275 msg_Dbg( p_this, "could not build local address" );
281 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
283 #if defined(WIN32) || defined(UNDER_CE)
284 msg_Warn( p_this, "cannot bind socket (%i)", WSAGetLastError() );
286 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
292 #if defined( WIN32 ) || defined( UNDER_CE )
293 /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
294 if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
296 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
298 msg_Dbg( p_this, "could not build local address" );
305 #if !defined( SYS_BEOS )
306 /* Allow broadcast reception if we bound on INADDR_ANY */
307 if( !*psz_bind_addr )
310 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
312 #if defined(WIN32) || defined(UNDER_CE)
313 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %i)",
316 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
323 #if !defined( SYS_BEOS )
324 /* Join the multicast group if the socket is a multicast address */
325 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
327 /* Determine interface to be used for multicast */
328 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
330 /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
331 so that IGMPv3 aware OSes running on IGMPv3 aware networks
332 will do an IGMPv3 query on the network */
333 if( *psz_server_addr )
335 struct ip_mreq_source imr;
337 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
338 imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
340 if( psz_if_addr != NULL && *psz_if_addr
341 && inet_addr(psz_if_addr) != INADDR_NONE )
343 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
347 imr.imr_interface.s_addr = INADDR_ANY;
349 if( psz_if_addr != NULL ) free( psz_if_addr );
351 msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
352 /* Join Multicast group with source filter */
353 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
355 sizeof(struct ip_mreq_source) ) == -1 )
357 #if defined(WIN32) || defined(UNDER_CE)
358 msg_Err( p_this, "failed to join IP multicast group (%i)",
361 msg_Err( p_this, "failed to join IP multicast group (%s)",
364 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
369 /* If there is no source address, we use IP_ADD_MEMBERSHIP */
374 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
375 if( psz_if_addr != NULL && *psz_if_addr
376 && inet_addr(psz_if_addr) != INADDR_NONE )
378 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
382 imr.imr_interface.s_addr = INADDR_ANY;
384 if( psz_if_addr != NULL ) free( psz_if_addr );
386 msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
387 /* Join Multicast group without source filter */
388 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
389 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
391 #if defined(WIN32) || defined(UNDER_CE)
392 msg_Err( p_this, "failed to join IP multicast group (%i)",
395 msg_Err( p_this, "failed to join IP multicast group (%s)",
405 if( *psz_server_addr )
407 /* Build socket for remote connection */
408 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
410 msg_Warn( p_this, "cannot build remote address" );
415 /* Connect the socket */
416 if( connect( i_handle, (struct sockaddr *) &sock,
417 sizeof( sock ) ) == (-1) )
419 #if defined(WIN32) || defined(UNDER_CE)
420 msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
422 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
428 #if !defined( SYS_BEOS )
429 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
431 /* set the time-to-live */
432 int i_ttl = p_socket->i_ttl;
435 /* set the multicast interface */
436 char * psz_mif_addr = config_GetPsz( p_this, "miface-addr" );
440 intf.s_addr = inet_addr(psz_mif_addr);
441 free( psz_mif_addr );
443 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_IF,
444 &intf, sizeof( intf ) ) < 0 )
446 msg_Dbg( p_this, "failed to set multicast interface (%s).", strerror(errno) );
454 if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS )
456 var_Create( p_this, "ttl",
457 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
458 var_Get( p_this, "ttl", &val );
462 if( i_ttl < 1 ) i_ttl = 1;
463 ttl = (unsigned char) i_ttl;
465 /* There is some confusion in the world whether IP_MULTICAST_TTL
466 * takes a byte or an int as an argument.
467 * BSD seems to indicate byte so we are going with that and use
468 * int as a fallback to be safe */
469 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
470 &ttl, sizeof( ttl ) ) < 0 )
472 msg_Dbg( p_this, "failed to set ttl (%s). Let's try it "
473 "the integer way.", strerror(errno) );
474 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
475 &i_ttl, sizeof( i_ttl ) ) <0 )
477 msg_Err( p_this, "failed to set ttl (%s)",
487 p_socket->i_handle = i_handle;
489 if( var_Get( p_this, "mtu", &val ) != VLC_SUCCESS )
491 var_Create( p_this, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
492 var_Get( p_this, "mtu", &val );
494 p_socket->i_mtu = val.i_int;
498 /*****************************************************************************
499 * NetOpen: wrapper around OpenUDP, ListenTCP and OpenTCP
500 *****************************************************************************/
501 static int NetOpen( vlc_object_t * p_this )
503 network_socket_t * p_socket = p_this->p_private;
505 return OpenUDP( p_this, p_socket );