1 /*****************************************************************************
2 * ipv6.c: IPv6 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: ipv6.c,v 1.8.2.3 2002/07/29 16:15:49 gbazin Exp $
7 * Authors: Alexis Guillard <alexis.guillard@bt.com>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Remco Poortinga <poortinga@telin.nl>
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 *****************************************************************************/
30 #include <sys/types.h>
36 #include <videolan/vlc.h>
40 #elif defined( _MSC_VER ) && defined( _WIN32 )
45 # include <winsock2.h>
46 # include <ws2tcpip.h>
47 #elif !defined( SYS_BEOS ) && !defined( SYS_NTO )
48 # include <netdb.h> /* hostent ... */
49 # include <sys/socket.h>
50 # include <netinet/in.h>
51 # ifdef HAVE_ARPA_INET_H
52 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
59 static const struct in6_addr in6addr_any = {{IN6ADDR_ANY_INIT}};
60 /* the following will have to be removed when w32api defines them */
62 # define IPPROTO_IPV6 41
64 #ifndef IPV6_JOIN_GROUP
65 # define IPV6_JOIN_GROUP 20
69 /*****************************************************************************
71 *****************************************************************************/
72 static void getfunctions( function_list_t * );
73 static int NetworkOpen( struct network_socket_s * );
75 /*****************************************************************************
76 * Build configuration tree.
77 *****************************************************************************/
82 SET_DESCRIPTION( _("IPv6 network abstraction layer") )
83 ADD_CAPABILITY( NETWORK, 40 )
84 ADD_SHORTCUT( "ipv6" )
88 getfunctions( &p_module->p_functions->network );
91 MODULE_DEACTIVATE_START
92 MODULE_DEACTIVATE_STOP
94 /*****************************************************************************
95 * Functions exported as capabilities. They are declared as static so that
96 * we don't pollute the namespace too much.
97 *****************************************************************************/
98 static void getfunctions( function_list_t * p_function_list )
100 #define f p_function_list->functions.network
101 f.pf_open = NetworkOpen;
105 /*****************************************************************************
106 * BuildAddr: utility function to build a struct sockaddr_in6
107 *****************************************************************************/
108 static int BuildAddr( struct sockaddr_in6 * p_socket,
109 char * psz_address, int i_port )
111 char * psz_multicast_interface = "";
114 /* Try to get getaddrinfo() and freeaddrinfo() from wship6.dll */
115 typedef int (CALLBACK * GETADDRINFO) ( const char *nodename,
116 const char *servname,
117 const struct addrinfo *hints,
118 struct addrinfo **res );
119 typedef void (CALLBACK * FREEADDRINFO) ( struct addrinfo FAR *ai );
121 struct addrinfo hints, *res;
122 GETADDRINFO _getaddrinfo = NULL;
123 FREEADDRINFO _freeaddrinfo = NULL;
125 HINSTANCE wship6_dll = LoadLibrary("wship6.dll");
128 _getaddrinfo = (GETADDRINFO) GetProcAddress( wship6_dll,
130 _freeaddrinfo = (FREEADDRINFO) GetProcAddress( wship6_dll,
133 if( !_getaddrinfo || !_freeaddrinfo )
135 intf_ErrMsg( "ipv6 error: no IPv6 stack installed" );
136 if( wship6_dll ) FreeLibrary( wship6_dll );
142 memset( p_socket, 0, sizeof( struct sockaddr_in6 ) );
143 p_socket->sin6_family = AF_INET6; /* family */
144 p_socket->sin6_port = htons( i_port );
147 p_socket->sin6_addr = in6addr_any;
149 else if( psz_address[0] == '['
150 && psz_address[strlen(psz_address) - 1] == ']' )
153 /* see if there is an interface name in there... */
154 if( (psz_multicast_interface = strchr(psz_address, '%')) != NULL )
156 *psz_multicast_interface = '\0';
157 psz_multicast_interface++;
158 intf_WarnMsg( 3, "Interface name specified: \"%s\"",
159 psz_multicast_interface );
160 /* now convert that interface name to an index */
161 #if !defined( WIN32 )
162 p_socket->sin6_scope_id = if_nametoindex(psz_multicast_interface);
164 /* FIXME: for now we always use the default interface */
165 p_socket->sin6_scope_id = 0;
166 intf_WarnMsg( 3, "Using default interface. This has to be FIXED!");
168 intf_WarnMsg( 3, " = #%i\n", p_socket->sin6_scope_id );
170 psz_address[strlen(psz_address) - 1] = '\0' ;
172 #if !defined( WIN32 )
173 inet_pton(AF_INET6, psz_address, &p_socket->sin6_addr.s6_addr);
176 memset(&hints, 0, sizeof(hints));
177 hints.ai_family = AF_INET6;
178 hints.ai_flags = AI_NUMERICHOST;
180 if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
182 FreeLibrary( wship6_dll );
185 memcpy( &p_socket->sin6_addr,
186 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
187 sizeof(struct in6_addr) );
188 _freeaddrinfo( res );
194 #ifdef HAVE_GETHOSTBYNAME2
195 struct hostent * p_hostent;
197 /* We have a fqdn, try to find its address */
198 if ( (p_hostent = gethostbyname2( psz_address, AF_INET6 )) == NULL )
200 intf_ErrMsg( "ipv6 error: unknown host %s", psz_address );
204 /* Copy the first address of the host in the socket address */
205 memcpy( &p_socket->sin6_addr, p_hostent->h_addr_list[0],
206 p_hostent->h_length );
209 if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
211 FreeLibrary( wship6_dll );
214 memcpy( &p_socket->sin6_addr,
215 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
216 sizeof(struct in6_addr) );
217 _freeaddrinfo( res );
220 intf_ErrMsg( "ipv6 error: IPv6 address %s is invalid", psz_address );
226 FreeLibrary( wship6_dll );
232 /*****************************************************************************
233 * OpenUDP: open a UDP socket
234 *****************************************************************************
235 * psz_bind_addr, i_bind_port : address and port used for the bind()
236 * system call. If psz_bind_addr == NULL, the socket is bound to
237 * in6addr_any and broadcast reception is enabled. If i_bind_port == 0,
238 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
239 * join the multicast group.
240 * psz_server_addr, i_server_port : address and port used for the connect()
241 * system call. It can avoid receiving packets from unauthorized IPs.
242 * Its use leads to great confusion and is currently discouraged.
243 * This function returns -1 in case of error.
244 *****************************************************************************/
245 static int OpenUDP( network_socket_t * p_socket )
247 char * psz_bind_addr = p_socket->psz_bind_addr;
248 int i_bind_port = p_socket->i_bind_port;
249 char * psz_server_addr = p_socket->psz_server_addr;
250 int i_server_port = p_socket->i_server_port;
252 int i_handle, i_opt, i_opt_size;
253 struct sockaddr_in6 sock;
255 if( i_bind_port == 0 )
257 i_bind_port = config_GetIntVariable( "server-port" );
260 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET6 domain, automatic (0)
262 if( (i_handle = socket( AF_INET6, SOCK_DGRAM, 0 )) == -1 )
264 intf_ErrMsg( "ipv6 error: cannot create socket (%s)", strerror(errno) );
268 /* We may want to reuse an already used socket */
270 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
271 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
273 intf_ErrMsg( "ipv6 error: cannot configure socket (SO_REUSEADDR: %s)",
279 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
280 * packet loss caused by scheduling problems */
282 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
283 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
286 "ipv6 warning: cannot configure socket (SO_RCVBUF: %s)",
290 /* Check if we really got what we have asked for, because Linux, etc.
291 * will silently limit the max buffer size to net.core.rmem_max which
292 * is typically only 65535 bytes */
294 i_opt_size = sizeof( i_opt );
295 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
296 (void*) &i_opt, &i_opt_size ) == -1 )
298 intf_WarnMsg( 1, "ipv6 warning: cannot query socket (SO_RCVBUF: %s)",
301 else if( i_opt < 0x80000 )
303 intf_WarnMsg( 1, "ipv6 warning: socket buffer size is 0x%x"
304 " instead of 0x%x", i_opt, 0x80000 );
307 /* Build the local socket */
308 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
315 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
317 intf_ErrMsg( "ipv6 error: cannot bind socket (%s)", strerror(errno) );
322 /* Allow broadcast reception if we bound on in6addr_any */
323 if( !*psz_bind_addr )
326 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
327 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
330 "ipv6 warning: cannot configure socket (SO_BROADCAST: %s)",
335 /* Join the multicast group if the socket is a multicast address */
336 if( IN6_IS_ADDR_MULTICAST(&sock.sin6_addr) )
338 struct ipv6_mreq imr;
341 imr.ipv6mr_interface = sock.sin6_scope_id;
342 imr.ipv6mr_multiaddr = sock.sin6_addr;
343 res = setsockopt(i_handle, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void *)&imr,
348 intf_ErrMsg( "ipv6 error: setsockopt JOIN_GROUP failed" );
353 if( *psz_server_addr )
355 /* Build socket for remote connection */
356 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
358 intf_ErrMsg( "ipv6 error: cannot build remote address" );
363 /* Connect the socket */
364 if( connect( i_handle, (struct sockaddr *) &sock,
365 sizeof( sock ) ) == (-1) )
367 intf_ErrMsg( "ipv6 error: cannot connect socket (%s)",
374 p_socket->i_handle = i_handle;
375 p_socket->i_mtu = config_GetIntVariable( "mtu" );
379 /*****************************************************************************
380 * OpenTCP: open a TCP socket
381 *****************************************************************************
382 * psz_server_addr, i_server_port : address and port used for the connect()
383 * system call. If i_server_port == 0, 80 is used.
384 * Other parameters are ignored.
385 * This function returns -1 in case of error.
386 *****************************************************************************/
387 static int OpenTCP( network_socket_t * p_socket )
389 char * psz_server_addr = p_socket->psz_server_addr;
390 int i_server_port = p_socket->i_server_port;
393 struct sockaddr_in6 sock;
395 if( i_server_port == 0 )
400 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET6 domain, automatic (0)
402 if( (i_handle = socket( AF_INET6, SOCK_STREAM, 0 )) == -1 )
404 intf_ErrMsg( "ipv6 error: cannot create socket (%s)", strerror(errno) );
408 /* Build remote address */
409 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
415 /* Connect the socket */
416 if( connect( i_handle, (struct sockaddr *) &sock,
417 sizeof( sock ) ) == (-1) )
419 intf_ErrMsg( "ipv6 error: cannot connect socket (%s)",
425 p_socket->i_handle = i_handle;
426 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
431 /*****************************************************************************
432 * NetworkOpen: wrapper around OpenUDP and OpenTCP
433 *****************************************************************************/
434 static int NetworkOpen( network_socket_t * p_socket )
436 if( p_socket->i_type == NETWORK_UDP )
438 return OpenUDP( p_socket );
442 return OpenTCP( p_socket );