1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001-2005 VideoLAN (Centrale Réseaux) and its contributors
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)
84 /*****************************************************************************
86 *****************************************************************************/
87 static int NetOpen( vlc_object_t * );
89 /*****************************************************************************
91 *****************************************************************************/
92 #define MIFACE_TEXT N_("Multicast output interface")
93 #define MIFACE_LONGTEXT N_( \
94 "Indicate here the multicast output interface. " \
95 "This overrides the routing table.")
98 set_shortname( "IPv4" );
99 set_description( _("IPv4 network abstraction layer") );
100 set_capability( "network", 50 );
101 set_category( CAT_INPUT );
102 set_subcategory( SUBCAT_INPUT_ADVANCED );
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 struct sockaddr_in sock;
172 /* If IP_ADD_SOURCE_MEMBERSHIP is not defined in the headers
173 (because it's not in glibc for example), we have to define the
174 headers required for IGMPv3 here */
175 #ifndef IP_ADD_SOURCE_MEMBERSHIP
176 #define IP_ADD_SOURCE_MEMBERSHIP 39
177 struct ip_mreq_source {
178 struct in_addr imr_multiaddr;
179 struct in_addr imr_interface;
180 struct in_addr imr_sourceaddr;
184 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
186 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
188 #if defined(WIN32) || defined(UNDER_CE)
189 msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
191 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
196 /* We may want to reuse an already used socket */
198 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
199 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
201 #if defined(WIN32) || defined(UNDER_CE)
202 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %i)",
205 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
214 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEPORT,
215 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
217 msg_Warn( p_this, "cannot configure socket (SO_REUSEPORT)" );
221 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
222 * packet loss caused by scheduling problems */
224 #if !defined( SYS_BEOS )
225 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
227 #if defined(WIN32) || defined(UNDER_CE)
228 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %i)",
231 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
237 /* Build the local socket */
239 #if defined( WIN32 ) || defined( UNDER_CE )
240 /* Under Win32 and for multicasting, we bind to INADDR_ANY,
241 * so let's call BuildAddr with "" instead of psz_bind_addr */
242 if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
243 "" : psz_bind_addr, i_bind_port ) == -1 )
245 if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
248 msg_Dbg( p_this, "could not build local address" );
254 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
256 #if defined(WIN32) || defined(UNDER_CE)
257 msg_Warn( p_this, "cannot bind socket (%i)", WSAGetLastError() );
259 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
265 #if defined( WIN32 ) || defined( UNDER_CE )
266 /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
267 if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
269 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
271 msg_Dbg( p_this, "could not build local address" );
278 #if !defined( SYS_BEOS )
279 /* Allow broadcast reception if we bound on INADDR_ANY */
280 if( !*psz_bind_addr )
283 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
285 #if defined(WIN32) || defined(UNDER_CE)
286 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %i)",
289 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
296 #if !defined( SYS_BEOS )
297 /* Join the multicast group if the socket is a multicast address */
298 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
300 /* Determine interface to be used for multicast */
301 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
303 /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
304 so that IGMPv3 aware OSes running on IGMPv3 aware networks
305 will do an IGMPv3 query on the network */
306 if( *psz_server_addr )
308 struct ip_mreq_source imr;
310 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
311 imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
313 if( psz_if_addr != NULL && *psz_if_addr
314 && inet_addr(psz_if_addr) != INADDR_NONE )
316 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
320 imr.imr_interface.s_addr = INADDR_ANY;
322 if( psz_if_addr != NULL ) free( psz_if_addr );
324 msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
325 /* Join Multicast group with source filter */
326 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
328 sizeof(struct ip_mreq_source) ) == -1 )
330 #if defined(WIN32) || defined(UNDER_CE)
331 msg_Err( p_this, "failed to join IP multicast group (%i)",
334 msg_Err( p_this, "failed to join IP multicast group (%s)",
337 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
342 /* If there is no source address, we use IP_ADD_MEMBERSHIP */
347 imr.imr_multiaddr.s_addr = sock.sin_addr.s_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_MEMBERSHIP multicast request" );
360 /* Join Multicast group without source filter */
361 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
362 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
364 #if defined(WIN32) || defined(UNDER_CE)
365 msg_Err( p_this, "failed to join IP multicast group (%i)",
368 msg_Err( p_this, "failed to join IP multicast group (%s)",
378 if( *psz_server_addr )
380 /* Build socket for remote connection */
381 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
383 msg_Warn( p_this, "cannot build remote address" );
388 /* Connect the socket */
389 if( connect( i_handle, (struct sockaddr *) &sock,
390 sizeof( sock ) ) == (-1) )
392 #if defined(WIN32) || defined(UNDER_CE)
393 msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
395 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
401 #if !defined( SYS_BEOS )
402 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
404 /* set the time-to-live */
405 int i_ttl = p_socket->i_ttl;
408 /* set the multicast interface */
409 char * psz_mif_addr = config_GetPsz( p_this, "miface-addr" );
413 intf.s_addr = inet_addr(psz_mif_addr);
414 free( psz_mif_addr );
416 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_IF,
417 &intf, sizeof( intf ) ) < 0 )
419 msg_Dbg( p_this, "failed to set multicast interface (%s).", strerror(errno) );
427 if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS )
429 var_Create( p_this, "ttl",
430 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
431 var_Get( p_this, "ttl", &val );
435 if( i_ttl < 1 ) i_ttl = 1;
436 ttl = (unsigned char) i_ttl;
438 /* There is some confusion in the world whether IP_MULTICAST_TTL
439 * takes a byte or an int as an argument.
440 * BSD seems to indicate byte so we are going with that and use
441 * int as a fallback to be safe */
442 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
443 &ttl, sizeof( ttl ) ) < 0 )
445 msg_Dbg( p_this, "failed to set ttl (%s). Let's try it "
446 "the integer way.", strerror(errno) );
447 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
448 &i_ttl, sizeof( i_ttl ) ) <0 )
450 msg_Err( p_this, "failed to set ttl (%s)",
460 p_socket->i_handle = i_handle;
462 if( var_Get( p_this, "mtu", &val ) != VLC_SUCCESS )
464 var_Create( p_this, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
465 var_Get( p_this, "mtu", &val );
467 p_socket->i_mtu = val.i_int;
471 /*****************************************************************************
472 * NetOpen: wrapper around OpenUDP, ListenTCP and OpenTCP
473 *****************************************************************************/
474 static int NetOpen( vlc_object_t * p_this )
476 network_socket_t * p_socket = p_this->p_private;
478 return OpenUDP( p_this, p_socket );