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_callbacks( NetOpen, NULL );
104 add_string( "miface-addr", NULL, NULL, MIFACE_TEXT, MIFACE_LONGTEXT, VLC_TRUE );
107 /*****************************************************************************
108 * BuildAddr: utility function to build a struct sockaddr_in
109 *****************************************************************************/
110 static int BuildAddr( struct sockaddr_in * p_socket,
111 const char * psz_address, int i_port )
114 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
115 p_socket->sin_family = AF_INET; /* family */
116 p_socket->sin_port = htons( (uint16_t)i_port );
119 p_socket->sin_addr.s_addr = INADDR_ANY;
123 struct hostent * p_hostent;
125 /* Try to convert address directly from in_addr - this will work if
126 * psz_address is dotted decimal. */
127 #ifdef HAVE_ARPA_INET_H
128 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
130 p_socket->sin_addr.s_addr = inet_addr( psz_address );
131 if( p_socket->sin_addr.s_addr == INADDR_NONE )
134 /* We have a fqdn, try to find its address */
135 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
140 /* Copy the first address of the host in the socket address */
141 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
142 p_hostent->h_length );
148 /*****************************************************************************
149 * OpenUDP: open a UDP socket
150 *****************************************************************************
151 * psz_bind_addr, i_bind_port : address and port used for the bind()
152 * system call. If psz_bind_addr == "", the socket is bound to
153 * INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
154 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
155 * join the multicast group.
156 * psz_server_addr, i_server_port : address and port used for the connect()
157 * system call. It can avoid receiving packets from unauthorized IPs.
158 * Its use leads to great confusion and is currently discouraged.
159 * This function returns -1 in case of error.
160 *****************************************************************************/
161 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
163 char * psz_bind_addr = p_socket->psz_bind_addr;
164 int i_bind_port = p_socket->i_bind_port;
165 char * psz_server_addr = p_socket->psz_server_addr;
166 int i_server_port = p_socket->i_server_port;
169 socklen_t i_opt_size;
170 struct sockaddr_in sock;
173 /* If IP_ADD_SOURCE_MEMBERSHIP is not defined in the headers
174 (because it's not in glibc for example), we have to define the
175 headers required for IGMPv3 here */
176 #ifndef IP_ADD_SOURCE_MEMBERSHIP
177 #define IP_ADD_SOURCE_MEMBERSHIP 39
178 struct ip_mreq_source {
179 struct in_addr imr_multiaddr;
180 struct in_addr imr_interface;
181 struct in_addr imr_sourceaddr;
185 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
187 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
189 #if defined(WIN32) || defined(UNDER_CE)
190 msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
192 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
197 /* We may want to reuse an already used socket */
199 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
200 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
202 #if defined(WIN32) || defined(UNDER_CE)
203 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %i)",
206 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
215 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEPORT,
216 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
218 msg_Warn( p_this, "cannot configure socket (SO_REUSEPORT)" );
222 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
223 * packet loss caused by scheduling problems */
225 #if !defined( SYS_BEOS )
226 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
228 #if defined(WIN32) || defined(UNDER_CE)
229 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %i)",
232 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
238 #if !defined( SYS_BEOS )
239 /* Check if we really got what we have asked for, because Linux, etc.
240 * will silently limit the max buffer size to net.core.rmem_max which
241 * is typically only 65535 bytes */
243 i_opt_size = sizeof( i_opt );
244 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void*) &i_opt, &i_opt_size ) == -1 )
246 #if defined(WIN32) || defined(UNDER_CE)
247 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %i)",
250 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
254 else if( i_opt < 0x80000 )
256 msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
262 /* Build the local socket */
264 #if defined( WIN32 ) || defined( UNDER_CE )
265 /* Under Win32 and for multicasting, we bind to INADDR_ANY,
266 * so let's call BuildAddr with "" instead of psz_bind_addr */
267 if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
268 "" : psz_bind_addr, i_bind_port ) == -1 )
270 if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
273 msg_Dbg( p_this, "could not build local address" );
279 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
281 #if defined(WIN32) || defined(UNDER_CE)
282 msg_Warn( p_this, "cannot bind socket (%i)", WSAGetLastError() );
284 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
290 #if defined( WIN32 ) || defined( UNDER_CE )
291 /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
292 if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
294 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
296 msg_Dbg( p_this, "could not build local address" );
303 #if !defined( SYS_BEOS )
304 /* Allow broadcast reception if we bound on INADDR_ANY */
305 if( !*psz_bind_addr )
308 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
310 #if defined(WIN32) || defined(UNDER_CE)
311 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %i)",
314 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
321 #if !defined( SYS_BEOS )
322 /* Join the multicast group if the socket is a multicast address */
323 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
325 /* Determine interface to be used for multicast */
326 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
328 /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
329 so that IGMPv3 aware OSes running on IGMPv3 aware networks
330 will do an IGMPv3 query on the network */
331 if( *psz_server_addr )
333 struct ip_mreq_source imr;
335 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
336 imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
338 if( psz_if_addr != NULL && *psz_if_addr
339 && inet_addr(psz_if_addr) != INADDR_NONE )
341 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
345 imr.imr_interface.s_addr = INADDR_ANY;
347 if( psz_if_addr != NULL ) free( psz_if_addr );
349 msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
350 /* Join Multicast group with source filter */
351 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
353 sizeof(struct ip_mreq_source) ) == -1 )
355 #if defined(WIN32) || defined(UNDER_CE)
356 msg_Err( p_this, "failed to join IP multicast group (%i)",
359 msg_Err( p_this, "failed to join IP multicast group (%s)",
362 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
367 /* If there is no source address, we use IP_ADD_MEMBERSHIP */
372 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
373 if( psz_if_addr != NULL && *psz_if_addr
374 && inet_addr(psz_if_addr) != INADDR_NONE )
376 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
380 imr.imr_interface.s_addr = INADDR_ANY;
382 if( psz_if_addr != NULL ) free( psz_if_addr );
384 msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
385 /* Join Multicast group without source filter */
386 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
387 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
389 #if defined(WIN32) || defined(UNDER_CE)
390 msg_Err( p_this, "failed to join IP multicast group (%i)",
393 msg_Err( p_this, "failed to join IP multicast group (%s)",
403 if( *psz_server_addr )
405 /* Build socket for remote connection */
406 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
408 msg_Warn( p_this, "cannot build remote address" );
413 /* Connect the socket */
414 if( connect( i_handle, (struct sockaddr *) &sock,
415 sizeof( sock ) ) == (-1) )
417 #if defined(WIN32) || defined(UNDER_CE)
418 msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
420 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
426 #if !defined( SYS_BEOS )
427 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
429 /* set the time-to-live */
430 int i_ttl = p_socket->i_ttl;
433 /* set the multicast interface */
434 char * psz_mif_addr = config_GetPsz( p_this, "miface-addr" );
438 intf.s_addr = inet_addr(psz_mif_addr);
439 free( psz_mif_addr );
441 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_IF,
442 &intf, sizeof( intf ) ) < 0 )
444 msg_Dbg( p_this, "failed to set multicast interface (%s).", strerror(errno) );
452 if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS )
454 var_Create( p_this, "ttl",
455 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
456 var_Get( p_this, "ttl", &val );
460 if( i_ttl < 1 ) i_ttl = 1;
461 ttl = (unsigned char) i_ttl;
463 /* There is some confusion in the world whether IP_MULTICAST_TTL
464 * takes a byte or an int as an argument.
465 * BSD seems to indicate byte so we are going with that and use
466 * int as a fallback to be safe */
467 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
468 &ttl, sizeof( ttl ) ) < 0 )
470 msg_Dbg( p_this, "failed to set ttl (%s). Let's try it "
471 "the integer way.", strerror(errno) );
472 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
473 &i_ttl, sizeof( i_ttl ) ) <0 )
475 msg_Err( p_this, "failed to set ttl (%s)",
485 p_socket->i_handle = i_handle;
487 if( var_Get( p_this, "mtu", &val ) != VLC_SUCCESS )
489 var_Create( p_this, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
490 var_Get( p_this, "mtu", &val );
492 p_socket->i_mtu = val.i_int;
496 /*****************************************************************************
497 * NetOpen: wrapper around OpenUDP, ListenTCP and OpenTCP
498 *****************************************************************************/
499 static int NetOpen( vlc_object_t * p_this )
501 network_socket_t * p_socket = p_this->p_private;
503 return OpenUDP( p_this, p_socket );