1 /*****************************************************************************
2 * ipv6.c: IPv6 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: ipv6.c,v 1.10 2002/06/01 16:45:34 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( vlc_object_t *, 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 //X msg_Err( p_this, "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( vlc_object_t * p_this, 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 msg_Err( p_this, "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 msg_Err( p_this, "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 )
261 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF: %s)",
265 /* Check if we really got what we have asked for, because Linux, etc.
266 * will silently limit the max buffer size to net.core.rmem_max which
267 * is typically only 65535 bytes */
269 i_opt_size = sizeof( i_opt );
270 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
271 (void*) &i_opt, &i_opt_size ) == -1 )
273 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
276 else if( i_opt < 0x80000 )
278 msg_Warn( p_this, "socket buffer size is 0x%x instead of 0x%x",
282 /* Build the local socket */
283 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
290 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
292 msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
297 /* Allow broadcast reception if we bound on in6addr_any */
298 if( !*psz_bind_addr )
301 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
302 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
304 msg_Warn( p_this, "ipv6 warning: cannot configure socket "
305 "(SO_BROADCAST: %s)", strerror(errno) );
309 /* Join the multicast group if the socket is a multicast address */
310 /* FIXME: To be written */
312 if( *psz_server_addr )
314 /* Build socket for remote connection */
315 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
317 msg_Err( p_this, "cannot build remote address" );
322 /* Connect the socket */
323 if( connect( i_handle, (struct sockaddr *) &sock,
324 sizeof( sock ) ) == (-1) )
326 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
332 p_socket->i_handle = i_handle;
333 p_socket->i_mtu = DEFAULT_MTU;
337 /*****************************************************************************
338 * OpenTCP: open a TCP socket
339 *****************************************************************************
340 * psz_server_addr, i_server_port : address and port used for the connect()
341 * system call. If i_server_port == 0, 80 is used.
342 * Other parameters are ignored.
343 * This function returns -1 in case of error.
344 *****************************************************************************/
345 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
347 char * psz_server_addr = p_socket->psz_server_addr;
348 int i_server_port = p_socket->i_server_port;
351 struct sockaddr_in6 sock;
353 if( i_server_port == 0 )
358 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET6 domain, automatic (0)
360 if( (i_handle = socket( AF_INET6, SOCK_STREAM, 0 )) == -1 )
362 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
366 /* Build remote address */
367 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
373 /* Connect the socket */
374 if( connect( i_handle, (struct sockaddr *) &sock,
375 sizeof( sock ) ) == (-1) )
377 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
382 p_socket->i_handle = i_handle;
383 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
388 /*****************************************************************************
389 * NetworkOpen: wrapper around OpenUDP and OpenTCP
390 *****************************************************************************/
391 static int NetworkOpen( vlc_object_t * p_this, network_socket_t * p_socket )
393 if( p_socket->i_type == NETWORK_UDP )
395 return OpenUDP( p_this, p_socket );
399 return OpenTCP( p_this, p_socket );