1 /*****************************************************************************
2 * ipv6.c: IPv6 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: ipv6.c,v 1.9 2002/06/01 12:32:00 sam Exp $
7 * Authors: Alexis Guillard <alexis.guillard@bt.com>
8 * Christophe Massiot <massiot@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <sys/types.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
44 # include <winsock2.h>
45 # include <ws2tcpip.h>
46 #elif !defined( SYS_BEOS ) && !defined( SYS_NTO )
47 # include <netdb.h> /* hostent ... */
48 # include <sys/socket.h>
49 # include <netinet/in.h>
50 # ifdef HAVE_ARPA_INET_H
51 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
57 /* Default MTU used for UDP socket. FIXME: we should issue some ioctl()
58 * call to get that value from the interface driver. */
59 #define DEFAULT_MTU 1500
62 static const struct in6_addr in6addr_any = {{IN6ADDR_ANY_INIT}};
65 /*****************************************************************************
67 *****************************************************************************/
68 static void getfunctions( function_list_t * );
69 static int NetworkOpen( network_socket_t * );
71 /*****************************************************************************
72 * Build configuration tree.
73 *****************************************************************************/
78 SET_DESCRIPTION( _("IPv6 network abstraction layer") )
79 ADD_CAPABILITY( NETWORK, 40 )
83 getfunctions( &p_module->p_functions->network );
86 MODULE_DEACTIVATE_START
87 MODULE_DEACTIVATE_STOP
89 /*****************************************************************************
90 * Functions exported as capabilities. They are declared as static so that
91 * we don't pollute the namespace too much.
92 *****************************************************************************/
93 static void getfunctions( function_list_t * p_function_list )
95 #define f p_function_list->functions.network
96 f.pf_open = NetworkOpen;
100 /*****************************************************************************
101 * BuildAddr: utility function to build a struct sockaddr_in6
102 *****************************************************************************/
103 static int BuildAddr( struct sockaddr_in6 * p_socket,
104 char * psz_address, int i_port )
107 /* Try to get getaddrinfo() and freeaddrinfo() from wship6.dll */
108 typedef int (CALLBACK * GETADDRINFO) ( const char *nodename,
109 const char *servname,
110 const struct addrinfo *hints,
111 struct addrinfo **res );
112 typedef void (CALLBACK * FREEADDRINFO) ( struct addrinfo FAR *ai );
114 struct addrinfo hints, *res;
115 GETADDRINFO _getaddrinfo = NULL;
116 FREEADDRINFO _freeaddrinfo = NULL;
118 HINSTANCE wship6_dll = LoadLibrary("wship6.dll");
121 _getaddrinfo = (GETADDRINFO) GetProcAddress( wship6_dll,
123 _freeaddrinfo = (FREEADDRINFO) GetProcAddress( wship6_dll,
126 if( !_getaddrinfo || !_freeaddrinfo )
128 intf_ErrMsg( "ipv6 error: no IPv6 stack installed" );
129 if( wship6_dll ) FreeLibrary( wship6_dll );
135 memset( p_socket, 0, sizeof( struct sockaddr_in6 ) );
136 p_socket->sin6_family = AF_INET6; /* family */
137 p_socket->sin6_port = htons( i_port );
140 p_socket->sin6_addr = in6addr_any;
142 else if( psz_address[0] == '['
143 && psz_address[strlen(psz_address) - 1] == ']' )
146 psz_address[strlen(psz_address) - 1] = '\0' ;
148 #if !defined( WIN32 )
149 inet_pton(AF_INET6, psz_address, &p_socket->sin6_addr.s6_addr);
152 memset(&hints, 0, sizeof(hints));
153 hints.ai_family = AF_INET6;
154 hints.ai_flags = AI_NUMERICHOST;
156 if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
158 FreeLibrary( wship6_dll );
161 memcpy( &p_socket->sin6_addr,
162 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
163 sizeof(struct in6_addr) );
164 _freeaddrinfo( res );
170 #ifdef HAVE_GETHOSTBYNAME2
171 struct hostent * p_hostent;
173 /* We have a fqdn, try to find its address */
174 if ( (p_hostent = gethostbyname2( psz_address, AF_INET6 )) == NULL )
176 //X intf_ErrMsg( "ipv6 error: unknown host %s", psz_address );
180 /* Copy the first address of the host in the socket address */
181 memcpy( &p_socket->sin6_addr, p_hostent->h_addr_list[0],
182 p_hostent->h_length );
185 if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
187 FreeLibrary( wship6_dll );
190 memcpy( &p_socket->sin6_addr,
191 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
192 sizeof(struct in6_addr) );
193 _freeaddrinfo( res );
196 //X intf_ErrMsg( "ipv6 error: IPv6 address %s is invalid", psz_address );
202 FreeLibrary( wship6_dll );
208 /*****************************************************************************
209 * OpenUDP: open a UDP socket
210 *****************************************************************************
211 * psz_bind_addr, i_bind_port : address and port used for the bind()
212 * system call. If psz_bind_addr == NULL, the socket is bound to
213 * in6addr_any and broadcast reception is enabled. If i_bind_port == 0,
214 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
215 * join the multicast group.
216 * psz_server_addr, i_server_port : address and port used for the connect()
217 * system call. It can avoid receiving packets from unauthorized IPs.
218 * Its use leads to great confusion and is currently discouraged.
219 * This function returns -1 in case of error.
220 *****************************************************************************/
221 static int OpenUDP( network_socket_t * p_socket )
223 char * psz_bind_addr = p_socket->psz_bind_addr;
224 int i_bind_port = p_socket->i_bind_port;
225 char * psz_server_addr = p_socket->psz_server_addr;
226 int i_server_port = p_socket->i_server_port;
228 int i_handle, i_opt, i_opt_size;
229 struct sockaddr_in6 sock;
231 if( i_bind_port == 0 )
233 //X i_bind_port = config_GetInt( "server-port" );
236 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET6 domain, automatic (0)
238 if( (i_handle = socket( AF_INET6, SOCK_DGRAM, 0 )) == -1 )
240 //X intf_ErrMsg( "ipv6 error: cannot create socket (%s)", strerror(errno) );
244 /* We may want to reuse an already used socket */
246 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
247 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
249 //X intf_ErrMsg( "ipv6 error: cannot configure socket (SO_REUSEADDR: %s)",
255 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
256 * packet loss caused by scheduling problems */
258 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
259 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
262 "ipv6 warning: cannot configure socket (SO_RCVBUF: %s)",
266 /* Check if we really got what we have asked for, because Linux, etc.
267 * will silently limit the max buffer size to net.core.rmem_max which
268 * is typically only 65535 bytes */
270 i_opt_size = sizeof( i_opt );
271 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
272 (void*) &i_opt, &i_opt_size ) == -1 )
274 //X intf_WarnMsg( 1, "ipv6 warning: cannot query socket (SO_RCVBUF: %s)",
275 //X strerror(errno));
277 else if( i_opt < 0x80000 )
279 //X intf_WarnMsg( 1, "ipv6 warning: socket buffer size is 0x%x"
280 " instead of 0x%x", i_opt, 0x80000 );
283 /* Build the local socket */
284 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
291 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
293 //X intf_ErrMsg( "ipv6 error: cannot bind socket (%s)", strerror(errno) );
298 /* Allow broadcast reception if we bound on in6addr_any */
299 if( !*psz_bind_addr )
302 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
303 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
306 //X "ipv6 warning: cannot configure socket (SO_BROADCAST: %s)",
307 //X strerror(errno));
311 /* Join the multicast group if the socket is a multicast address */
312 /* FIXME: To be written */
314 if( *psz_server_addr )
316 /* Build socket for remote connection */
317 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
319 //X intf_ErrMsg( "ipv6 error: cannot build remote address" );
324 /* Connect the socket */
325 if( connect( i_handle, (struct sockaddr *) &sock,
326 sizeof( sock ) ) == (-1) )
328 //X intf_ErrMsg( "ipv6 error: cannot connect socket (%s)",
329 //X strerror(errno) );
335 p_socket->i_handle = i_handle;
336 p_socket->i_mtu = DEFAULT_MTU;
340 /*****************************************************************************
341 * OpenTCP: open a TCP socket
342 *****************************************************************************
343 * psz_server_addr, i_server_port : address and port used for the connect()
344 * system call. If i_server_port == 0, 80 is used.
345 * Other parameters are ignored.
346 * This function returns -1 in case of error.
347 *****************************************************************************/
348 static int OpenTCP( network_socket_t * p_socket )
350 char * psz_server_addr = p_socket->psz_server_addr;
351 int i_server_port = p_socket->i_server_port;
354 struct sockaddr_in6 sock;
356 if( i_server_port == 0 )
361 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET6 domain, automatic (0)
363 if( (i_handle = socket( AF_INET6, SOCK_STREAM, 0 )) == -1 )
365 //X intf_ErrMsg( "ipv6 error: cannot create socket (%s)", strerror(errno) );
369 /* Build remote address */
370 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
376 /* Connect the socket */
377 if( connect( i_handle, (struct sockaddr *) &sock,
378 sizeof( sock ) ) == (-1) )
380 //X intf_ErrMsg( "ipv6 error: cannot connect socket (%s)",
386 p_socket->i_handle = i_handle;
387 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
392 /*****************************************************************************
393 * NetworkOpen: wrapper around OpenUDP and OpenTCP
394 *****************************************************************************/
395 static int NetworkOpen( network_socket_t * p_socket )
397 if( p_socket->i_type == NETWORK_UDP )
399 return OpenUDP( p_socket );
403 return OpenTCP( p_socket );