1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Mathias Kretschmer <mathias@research.att.com>
9 * Alexis de Lattre <alexis@via.ecp.fr>
10 * RĂ©mi Denis-Courmont <rem # videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
39 #ifdef HAVE_SYS_STAT_H
40 # include <sys/stat.h>
50 #if defined(WIN32) || defined(UNDER_CE)
51 # if defined(UNDER_CE) && defined(sockaddr_storage)
52 # undef sockaddr_storage
54 # include <winsock2.h>
55 # include <ws2tcpip.h>
56 # include <iphlpapi.h>
57 # define close closesocket
58 # if defined(UNDER_CE)
59 # undef IP_MULTICAST_TTL
60 # define IP_MULTICAST_TTL 3
61 # undef IP_ADD_MEMBERSHIP
62 # define IP_ADD_MEMBERSHIP 5
65 # include <netdb.h> /* hostent ... */
66 # include <sys/socket.h>
67 # include <netinet/in.h>
68 # ifdef HAVE_ARPA_INET_H
69 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
76 # define INADDR_ANY 0x00000000
79 # define INADDR_NONE 0xFFFFFFFF
82 # define IN_MULTICAST(a) IN_CLASSD(a)
86 /*****************************************************************************
88 *****************************************************************************/
89 static int OpenUDP( vlc_object_t * );
91 /*****************************************************************************
93 *****************************************************************************/
94 #define MIFACE_TEXT N_("Multicast output interface")
95 #define MIFACE_LONGTEXT N_( \
96 "Indicate here the multicast output interface. " \
97 "This overrides the routing table.")
100 set_shortname( "IPv4" );
101 set_description( _("UDP/IPv4 network abstraction layer") );
102 set_capability( "network", 50 );
103 set_category( CAT_INPUT );
104 set_subcategory( SUBCAT_INPUT_GENERAL );
105 set_callbacks( OpenUDP, 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 #if defined(WIN32) || defined(UNDER_CE)
151 # define WINSOCK_STRERROR_SIZE 20
152 static const char *winsock_strerror( char *buf )
154 snprintf( buf, WINSOCK_STRERROR_SIZE, "Winsock error %d",
155 WSAGetLastError( ) );
156 buf[WINSOCK_STRERROR_SIZE - 1] = '\0';
162 /*****************************************************************************
163 * OpenUDP: open a UDP socket
164 *****************************************************************************
165 * psz_bind_addr, i_bind_port : address and port used for the bind()
166 * system call. If psz_bind_addr == "", the socket is bound to
167 * INADDR_ANY and broadcast reception is enabled. If psz_bind_addr is a
168 * multicast (class D) address, join the multicast group.
169 * psz_server_addr, i_server_port : address and port used for the connect()
170 * system call. It can avoid receiving packets from unauthorized IPs.
171 * Its use leads to great confusion and is currently discouraged.
172 * This function returns -1 in case of error.
173 *****************************************************************************/
174 static int OpenUDP( vlc_object_t * p_this )
176 network_socket_t * p_socket = p_this->p_private;
177 const char * psz_bind_addr = p_socket->psz_bind_addr;
178 int i_bind_port = p_socket->i_bind_port;
179 const char * psz_server_addr = p_socket->psz_server_addr;
180 int i_server_port = p_socket->i_server_port;
183 struct sockaddr_in sock;
185 #if defined(WIN32) || defined(UNDER_CE)
186 char strerror_buf[WINSOCK_STRERROR_SIZE];
187 # define strerror( x ) winsock_strerror( strerror_buf )
190 /* If IP_ADD_SOURCE_MEMBERSHIP is not defined in the headers
191 (because it's not in glibc for example), we have to define the
192 headers required for IGMPv3 here */
193 #ifndef IP_ADD_SOURCE_MEMBERSHIP
194 #define IP_ADD_SOURCE_MEMBERSHIP 39
195 struct ip_mreq_source {
196 struct in_addr imr_multiaddr;
197 struct in_addr imr_interface;
198 struct in_addr imr_sourceaddr;
202 p_socket->i_handle = -1;
204 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
206 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
208 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
212 /* We may want to reuse an already used socket */
214 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
215 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
217 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 */
234 #if !defined( SYS_BEOS )
236 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt,
237 sizeof( i_opt ) ) == -1 )
238 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
241 if( setsockopt( i_handle, SOL_SOCKET, SO_SNDBUF, (void *) &i_opt,
242 sizeof( i_opt ) ) == -1 )
243 msg_Dbg( p_this, "cannot configure socket (SO_SNDBUF: %s)",
247 /* Build the local socket */
249 #if defined( WIN32 ) || defined( UNDER_CE )
250 /* Under Win32 and for multicasting, we bind to INADDR_ANY,
251 * so let's call BuildAddr with "" instead of psz_bind_addr */
252 if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
253 "" : psz_bind_addr, i_bind_port ) == -1 )
255 if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
258 msg_Dbg( p_this, "could not build local address" );
264 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
266 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
271 #if defined( WIN32 ) || defined( UNDER_CE )
272 /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
273 if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
275 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
277 msg_Dbg( p_this, "could not build local address" );
284 #if !defined( SYS_BEOS )
285 /* Allow broadcast reception if we bound on INADDR_ANY */
286 if( !*psz_bind_addr )
289 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt,
290 sizeof( i_opt ) ) == -1 )
291 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, "miface-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 msg_Err( p_this, "failed to join IP multicast group (%s)",
332 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
337 /* If there is no source address, we use IP_ADD_MEMBERSHIP */
342 imr.imr_interface.s_addr = INADDR_ANY;
343 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
344 if( psz_if_addr != NULL && *psz_if_addr
345 && inet_addr(psz_if_addr) != INADDR_NONE )
347 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
349 #if defined (WIN32) || defined (UNDER_CE)
352 typedef DWORD (CALLBACK * GETBESTINTERFACE) ( IPAddr, PDWORD );
353 typedef DWORD (CALLBACK * GETIPADDRTABLE) ( PMIB_IPADDRTABLE, PULONG, BOOL );
355 GETBESTINTERFACE OurGetBestInterface;
356 GETIPADDRTABLE OurGetIpAddrTable;
357 HINSTANCE hiphlpapi = LoadLibrary(_T("Iphlpapi.dll"));
362 OurGetBestInterface =
363 (void *)GetProcAddress( hiphlpapi,
364 _T("GetBestInterface") );
366 (void *)GetProcAddress( hiphlpapi,
367 _T("GetIpAddrTable") );
370 if( hiphlpapi && OurGetBestInterface && OurGetIpAddrTable &&
371 OurGetBestInterface( sock.sin_addr.s_addr,
372 &i_index ) == NO_ERROR )
374 PMIB_IPADDRTABLE p_table;
377 msg_Dbg( p_this, "Winsock best interface is %lu",
378 (unsigned long)i_index );
379 OurGetIpAddrTable( NULL, &i, 0 );
381 p_table = (PMIB_IPADDRTABLE)malloc( i );
382 if( p_table != NULL )
384 if( OurGetIpAddrTable( p_table, &i, 0 ) == NO_ERROR )
386 for( i = 0; i < p_table->dwNumEntries; i-- )
388 if( p_table->table[i].dwIndex == i_index )
390 imr.imr_interface.s_addr =
391 p_table->table[i].dwAddr;
392 msg_Dbg( p_this, "using interface 0x%08x",
393 p_table->table[i].dwAddr );
397 else msg_Warn( p_this, "GetIpAddrTable failed" );
401 else msg_Dbg( p_this, "GetBestInterface failed" );
403 if( hiphlpapi ) FreeLibrary( hiphlpapi );
406 if( psz_if_addr != NULL ) free( psz_if_addr );
408 msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
409 /* Join Multicast group without source filter */
410 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
411 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
413 msg_Err( p_this, "failed to join IP multicast group (%s)",
422 if( *psz_server_addr )
424 /* Build socket for remote connection */
425 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
427 msg_Warn( p_this, "cannot build remote address" );
432 /* Connect the socket */
433 if( connect( i_handle, (struct sockaddr *) &sock,
434 sizeof( sock ) ) == (-1) )
436 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
441 #if !defined( SYS_BEOS )
442 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
444 /* set the time-to-live */
445 int i_ttl = p_socket->i_ttl;
448 /* set the multicast interface */
449 char * psz_mif_addr = config_GetPsz( p_this, "miface-addr" );
453 intf.s_addr = inet_addr(psz_mif_addr);
454 free( psz_mif_addr );
456 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_IF,
457 &intf, sizeof( intf ) ) < 0 )
459 msg_Dbg( p_this, "failed to set multicast interface (%s).", strerror(errno) );
467 if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS )
469 var_Create( p_this, "ttl",
470 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
471 var_Get( p_this, "ttl", &val );
475 if( i_ttl < 1 ) i_ttl = 1;
476 ttl = (unsigned char) i_ttl;
478 /* There is some confusion in the world whether IP_MULTICAST_TTL
479 * takes a byte or an int as an argument.
480 * BSD seems to indicate byte so we are going with that and use
481 * int as a fallback to be safe */
482 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
483 &ttl, sizeof( ttl ) ) < 0 )
485 msg_Dbg( p_this, "failed to set ttl (%s). Let's try it "
486 "the integer way.", strerror(errno) );
487 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
488 &i_ttl, sizeof( i_ttl ) ) <0 )
490 msg_Err( p_this, "failed to set ttl (%s)",
500 p_socket->i_handle = i_handle;
502 if( var_Get( p_this, "mtu", &val ) != VLC_SUCCESS )
504 var_Create( p_this, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
505 var_Get( p_this, "mtu", &val );
507 p_socket->i_mtu = val.i_int;