1 /*****************************************************************************
2 * ipv6.c: IPv6 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: ipv6.c,v 1.16 2004/02/15 22:20:56 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>
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>
52 # ifdef HAVE_ARPA_INET_H
53 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
60 static const struct in6_addr in6addr_any = {{IN6ADDR_ANY_INIT}};
61 /* the following will have to be removed when w32api defines them */
63 # define IPPROTO_IPV6 41
65 #ifndef IPV6_JOIN_GROUP
66 # define IPV6_JOIN_GROUP 12
68 #ifndef IPV6_MULTICAST_HOPS
69 # define IPV6_MULTICAST_HOPS 10
71 #ifndef IPV6_UNICAST_HOPS
72 # define IPV6_UNICAST_HOPS 4
74 # define close closesocket
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open( vlc_object_t * );
82 /*****************************************************************************
84 *****************************************************************************/
86 set_description( _("IPv6 network abstraction layer") );
87 set_capability( "network", 40 );
88 set_callbacks( Open, NULL );
91 /*****************************************************************************
92 * BuildAddr: utility function to build a struct sockaddr_in6
93 *****************************************************************************/
94 static int BuildAddr( vlc_object_t * p_this, struct sockaddr_in6 * p_socket,
95 const char * psz_bind_address, int i_port )
97 char * psz_multicast_interface = "";
98 char * psz_backup = strdup(psz_bind_address);
99 char * psz_address = psz_backup;
102 /* Try to get getaddrinfo() and freeaddrinfo() from wship6.dll */
103 typedef int (CALLBACK * GETADDRINFO) ( const char *nodename,
104 const char *servname,
105 const struct addrinfo *hints,
106 struct addrinfo **res );
107 typedef void (CALLBACK * FREEADDRINFO) ( struct addrinfo FAR *ai );
109 struct addrinfo hints, *res;
110 GETADDRINFO _getaddrinfo = NULL;
111 FREEADDRINFO _freeaddrinfo = NULL;
113 HINSTANCE wship6_dll = LoadLibrary("wship6.dll");
116 _getaddrinfo = (GETADDRINFO) GetProcAddress( wship6_dll,
118 _freeaddrinfo = (FREEADDRINFO) GetProcAddress( wship6_dll,
121 if( !_getaddrinfo || !_freeaddrinfo )
123 msg_Warn( p_this, "no IPv6 stack installed" );
124 if( wship6_dll ) FreeLibrary( wship6_dll );
131 memset( p_socket, 0, sizeof( struct sockaddr_in6 ) );
132 p_socket->sin6_family = AF_INET6; /* family */
133 p_socket->sin6_port = htons( i_port );
136 p_socket->sin6_addr = in6addr_any;
138 else if( psz_address[0] == '['
139 && psz_address[strlen(psz_address) - 1] == ']' )
141 psz_address[strlen(psz_address) - 1] = '\0';
144 /* see if there is an interface name in there... */
145 if( (psz_multicast_interface = strchr(psz_address, '%')) != NULL )
147 *psz_multicast_interface = '\0';
148 psz_multicast_interface++;
149 msg_Dbg( p_this, "Interface name specified: \"%s\"",
150 psz_multicast_interface );
152 /* now convert that interface name to an index */
155 p_socket->sin6_scope_id = atol(psz_multicast_interface);
156 #elif defined( HAVE_IF_NAMETOINDEX )
157 p_socket->sin6_scope_id = if_nametoindex(psz_multicast_interface);
159 msg_Dbg( p_this, " = #%i", p_socket->sin6_scope_id );
162 #if !defined( WIN32 )
163 inet_pton(AF_INET6, psz_address, &p_socket->sin6_addr.s6_addr);
166 memset(&hints, 0, sizeof(hints));
167 hints.ai_family = AF_INET6;
169 if( _getaddrinfo( psz_address, NULL, &hints, &res ) != 0 )
171 FreeLibrary( wship6_dll );
175 memcpy( &p_socket->sin6_addr,
176 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
177 sizeof(struct in6_addr) );
178 _freeaddrinfo( res );
184 #ifdef HAVE_GETHOSTBYNAME2
185 struct hostent * p_hostent;
187 /* We have a fqdn, try to find its address */
188 if ( (p_hostent = gethostbyname2( psz_address, AF_INET6 )) == NULL )
190 msg_Warn( p_this, "IPv6 error: unknown host %s", psz_address );
195 /* Copy the first address of the host in the socket address */
196 memcpy( &p_socket->sin6_addr, p_hostent->h_addr_list[0],
197 p_hostent->h_length );
200 memset(&hints, 0, sizeof(hints));
201 hints.ai_family = AF_INET6;
203 if( _getaddrinfo( psz_address, NULL, &hints, &res ) != 0 )
205 FreeLibrary( wship6_dll );
209 memcpy( &p_socket->sin6_addr,
210 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
211 sizeof(struct in6_addr) );
212 _freeaddrinfo( res );
215 msg_Warn( p_this, "IPv6 error: IPv6 address %s is invalid",
223 FreeLibrary( wship6_dll );
230 /*****************************************************************************
231 * OpenUDP: open a UDP socket
232 *****************************************************************************
233 * psz_bind_addr, i_bind_port : address and port used for the bind()
234 * system call. If psz_bind_addr == NULL, the socket is bound to
235 * in6addr_any and broadcast reception is enabled. If i_bind_port == 0,
236 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
237 * join the multicast group.
238 * psz_server_addr, i_server_port : address and port used for the connect()
239 * system call. It can avoid receiving packets from unauthorized IPs.
240 * Its use leads to great confusion and is currently discouraged.
241 * This function returns -1 in case of error.
242 *****************************************************************************/
243 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
245 char * psz_bind_addr = p_socket->psz_bind_addr;
246 int i_bind_port = p_socket->i_bind_port;
247 char * psz_server_addr = p_socket->psz_server_addr;
248 int i_server_port = p_socket->i_server_port;
251 socklen_t i_opt_size;
252 struct sockaddr_in6 sock;
254 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET6 domain, automatic (0)
256 if( (i_handle = socket( AF_INET6, SOCK_DGRAM, 0 )) == -1 )
258 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
262 /* We may want to reuse an already used socket */
264 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
265 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
267 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
273 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
274 * packet loss caused by scheduling problems */
276 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
277 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
279 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF: %s)",
283 /* Check if we really got what we have asked for, because Linux, etc.
284 * will silently limit the max buffer size to net.core.rmem_max which
285 * is typically only 65535 bytes */
287 i_opt_size = sizeof( i_opt );
288 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
289 (void*) &i_opt, &i_opt_size ) == -1 )
291 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
294 else if( i_opt < 0x80000 )
296 msg_Warn( p_this, "Socket buffer size is 0x%x instead of 0x%x",
300 /* Build the local socket */
301 if ( BuildAddr( p_this, &sock, psz_bind_addr, i_bind_port ) == -1 )
308 /* Under Win32 and for multicasting, we bind to IN6ADDR_ANY */
309 if( IN6_IS_ADDR_MULTICAST(&sock.sin6_addr) )
311 struct sockaddr_in6 sockany = sock;
312 sockany.sin6_addr = in6addr_any;
313 sockany.sin6_scope_id = 0;
316 if( bind( i_handle, (struct sockaddr *)&sockany, sizeof( sock ) ) < 0 )
318 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
325 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
327 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
332 /* Allow broadcast reception if we bound on in6addr_any */
333 if( !*psz_bind_addr )
336 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
337 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
339 msg_Warn( p_this, "IPv6 warning: cannot configure socket "
340 "(SO_BROADCAST: %s)", strerror(errno) );
344 /* Join the multicast group if the socket is a multicast address */
345 #if defined( WIN32 ) || defined( HAVE_IF_NAMETOINDEX )
346 if( IN6_IS_ADDR_MULTICAST(&sock.sin6_addr) )
348 struct ipv6_mreq imr;
351 imr.ipv6mr_interface = sock.sin6_scope_id;
352 imr.ipv6mr_multiaddr = sock.sin6_addr;
353 res = setsockopt(i_handle, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void*) &imr,
355 sizeof(imr) + 4); /* Doesn't work without this */
362 msg_Err( p_this, "cannot join multicast group" );
366 msg_Warn( p_this, "Multicast IPv6 is not supported on your OS" );
370 if( *psz_server_addr )
372 int ttl = p_socket->i_ttl;
375 ttl = config_GetInt( p_this, "ttl" );
377 if( ttl < 1 ) ttl = 1;
379 /* Build socket for remote connection */
380 if ( BuildAddr( p_this, &sock, psz_server_addr, i_server_port ) == -1 )
382 msg_Warn( p_this, "cannot build remote address" );
387 /* Connect the socket */
388 if( connect( i_handle, (struct sockaddr *) &sock,
389 sizeof( sock ) ) == (-1) )
391 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
396 /* Set the time-to-live */
399 #if defined( WIN32 ) || defined( HAVE_IF_NAMETOINDEX )
400 if( IN6_IS_ADDR_MULTICAST(&sock.sin6_addr) )
402 if( setsockopt( i_handle, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
403 (void *)&ttl, sizeof( ttl ) ) < 0 )
406 msg_Err( p_this, "failed to set multicast ttl (%s)",
409 msg_Err( p_this, "failed to set multicast ttl" );
416 if( setsockopt( i_handle, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
417 (void *)&ttl, sizeof( ttl ) ) < 0 )
420 msg_Err( p_this, "failed to set unicast ttl (%s)",
423 msg_Err( p_this, "failed to set unicast ttl" );
430 p_socket->i_handle = i_handle;
431 p_socket->i_mtu = config_GetInt( p_this, "mtu" );
436 /*****************************************************************************
437 * OpenTCP: open a TCP socket
438 *****************************************************************************
439 * psz_server_addr, i_server_port : address and port used for the connect()
440 * system call. If i_server_port == 0, 80 is used.
441 * Other parameters are ignored.
442 * This function returns -1 in case of error.
443 *****************************************************************************/
444 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
446 char * psz_server_addr = p_socket->psz_server_addr;
447 int i_server_port = p_socket->i_server_port;
450 struct sockaddr_in6 sock;
452 if( i_server_port == 0 )
457 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET6 domain, automatic (0)
459 if( (i_handle = socket( AF_INET6, SOCK_STREAM, 0 )) == -1 )
461 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
465 /* Build remote address */
466 if ( BuildAddr( p_this, &sock, psz_server_addr, i_server_port ) == -1 )
472 /* Connect the socket */
473 if( connect( i_handle, (struct sockaddr *) &sock,
474 sizeof( sock ) ) == (-1) )
476 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
481 p_socket->i_handle = i_handle;
482 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
487 /*****************************************************************************
488 * Open: wrapper around OpenUDP and OpenTCP
489 *****************************************************************************/
490 static int Open( vlc_object_t * p_this )
492 network_socket_t * p_socket = p_this->p_private;
494 if( p_socket->i_type == NETWORK_UDP )
496 return OpenUDP( p_this, p_socket );
500 return OpenTCP( p_this, p_socket );