1 /*****************************************************************************
2 * ipv6.c: IPv6 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: ipv6.c,v 1.16 2002/07/31 20:56:52 sam 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 20
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open( vlc_object_t * );
75 /*****************************************************************************
77 *****************************************************************************/
79 set_description( _("IPv6 network abstraction layer") );
80 set_capability( "network", 40 );
81 set_callbacks( Open, NULL );
84 /*****************************************************************************
85 * BuildAddr: utility function to build a struct sockaddr_in6
86 *****************************************************************************/
87 static int BuildAddr( struct sockaddr_in6 * p_socket,
88 char * psz_address, int i_port )
90 char * psz_multicast_interface = "";
93 /* Try to get getaddrinfo() and freeaddrinfo() from wship6.dll */
94 typedef int (CALLBACK * GETADDRINFO) ( const char *nodename,
96 const struct addrinfo *hints,
97 struct addrinfo **res );
98 typedef void (CALLBACK * FREEADDRINFO) ( struct addrinfo FAR *ai );
100 struct addrinfo hints, *res;
101 GETADDRINFO _getaddrinfo = NULL;
102 FREEADDRINFO _freeaddrinfo = NULL;
104 HINSTANCE wship6_dll = LoadLibrary("wship6.dll");
107 _getaddrinfo = (GETADDRINFO) GetProcAddress( wship6_dll,
109 _freeaddrinfo = (FREEADDRINFO) GetProcAddress( wship6_dll,
112 if( !_getaddrinfo || !_freeaddrinfo )
114 //X msg_Err( p_this, "no IPv6 stack installed" );
115 if( wship6_dll ) FreeLibrary( wship6_dll );
121 memset( p_socket, 0, sizeof( struct sockaddr_in6 ) );
122 p_socket->sin6_family = AF_INET6; /* family */
123 p_socket->sin6_port = htons( i_port );
126 p_socket->sin6_addr = in6addr_any;
128 else if( psz_address[0] == '['
129 && psz_address[strlen(psz_address) - 1] == ']' )
132 /* see if there is an interface name in there... */
133 if( (psz_multicast_interface = strchr(psz_address, '%')) != NULL )
135 *psz_multicast_interface = '\0';
136 psz_multicast_interface++;
137 //X msg_Dbg( p_this, "Interface name specified: \"%s\"",
138 // psz_multicast_interface );
139 /* now convert that interface name to an index */
140 #if !defined( WIN32 )
141 p_socket->sin6_scope_id = if_nametoindex(psz_multicast_interface);
143 /* FIXME: for now we always use the default interface */
144 p_socket->sin6_scope_id = 0;
145 //X msg_Warn( 3, "Using default interface. This has to be FIXED!");
147 //X msg_Warn( p_this, " = #%i\n", p_socket->sin6_scope_id );
149 psz_address[strlen(psz_address) - 1] = '\0' ;
151 #if !defined( WIN32 )
152 inet_pton(AF_INET6, psz_address, &p_socket->sin6_addr.s6_addr);
155 memset(&hints, 0, sizeof(hints));
156 hints.ai_family = AF_INET6;
157 hints.ai_flags = AI_NUMERICHOST;
159 if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
161 FreeLibrary( wship6_dll );
164 memcpy( &p_socket->sin6_addr,
165 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
166 sizeof(struct in6_addr) );
167 _freeaddrinfo( res );
173 #ifdef HAVE_GETHOSTBYNAME2
174 struct hostent * p_hostent;
176 /* We have a fqdn, try to find its address */
177 if ( (p_hostent = gethostbyname2( psz_address, AF_INET6 )) == NULL )
179 //X intf_ErrMsg( "ipv6 error: unknown host %s", psz_address );
183 /* Copy the first address of the host in the socket address */
184 memcpy( &p_socket->sin6_addr, p_hostent->h_addr_list[0],
185 p_hostent->h_length );
188 if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
190 FreeLibrary( wship6_dll );
193 memcpy( &p_socket->sin6_addr,
194 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
195 sizeof(struct in6_addr) );
196 _freeaddrinfo( res );
199 //X intf_ErrMsg( "ipv6 error: IPv6 address %s is invalid", psz_address );
205 FreeLibrary( wship6_dll );
211 /*****************************************************************************
212 * OpenUDP: open a UDP socket
213 *****************************************************************************
214 * psz_bind_addr, i_bind_port : address and port used for the bind()
215 * system call. If psz_bind_addr == NULL, the socket is bound to
216 * in6addr_any and broadcast reception is enabled. If i_bind_port == 0,
217 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
218 * join the multicast group.
219 * psz_server_addr, i_server_port : address and port used for the connect()
220 * system call. It can avoid receiving packets from unauthorized IPs.
221 * Its use leads to great confusion and is currently discouraged.
222 * This function returns -1 in case of error.
223 *****************************************************************************/
224 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
226 char * psz_bind_addr = p_socket->psz_bind_addr;
227 int i_bind_port = p_socket->i_bind_port;
228 char * psz_server_addr = p_socket->psz_server_addr;
229 int i_server_port = p_socket->i_server_port;
231 int i_handle, i_opt, i_opt_size;
232 struct sockaddr_in6 sock;
234 if( i_bind_port == 0 )
236 //X i_bind_port = config_GetInt( "server-port" );
239 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET6 domain, automatic (0)
241 if( (i_handle = socket( AF_INET6, SOCK_DGRAM, 0 )) == -1 )
243 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
247 /* We may want to reuse an already used socket */
249 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
250 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
252 msg_Err( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
258 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
259 * packet loss caused by scheduling problems */
261 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
262 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
264 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF: %s)",
268 /* Check if we really got what we have asked for, because Linux, etc.
269 * will silently limit the max buffer size to net.core.rmem_max which
270 * is typically only 65535 bytes */
272 i_opt_size = sizeof( i_opt );
273 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
274 (void*) &i_opt, &i_opt_size ) == -1 )
276 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
279 else if( i_opt < 0x80000 )
281 msg_Warn( p_this, "socket buffer size is 0x%x instead of 0x%x",
285 /* Build the local socket */
286 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
293 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
295 msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
300 /* Allow broadcast reception if we bound on in6addr_any */
301 if( !*psz_bind_addr )
304 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
305 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
307 msg_Warn( p_this, "ipv6 warning: cannot configure socket "
308 "(SO_BROADCAST: %s)", strerror(errno) );
312 /* Join the multicast group if the socket is a multicast address */
313 if( IN6_IS_ADDR_MULTICAST(&sock.sin6_addr) )
315 struct ipv6_mreq imr;
318 imr.ipv6mr_interface = sock.sin6_scope_id;
319 imr.ipv6mr_multiaddr = sock.sin6_addr;
320 res = setsockopt(i_handle, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void*) &imr,
325 //X intf_ErrMsg( "ipv6 error: setsockopt JOIN_GROUP failed" );
330 if( *psz_server_addr )
332 /* Build socket for remote connection */
333 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
335 msg_Err( p_this, "cannot build remote address" );
340 /* Connect the socket */
341 if( connect( i_handle, (struct sockaddr *) &sock,
342 sizeof( sock ) ) == (-1) )
344 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
350 p_socket->i_handle = i_handle;
351 p_socket->i_mtu = config_GetInt( p_this, "mtu" );
356 /*****************************************************************************
357 * OpenTCP: open a TCP socket
358 *****************************************************************************
359 * psz_server_addr, i_server_port : address and port used for the connect()
360 * system call. If i_server_port == 0, 80 is used.
361 * Other parameters are ignored.
362 * This function returns -1 in case of error.
363 *****************************************************************************/
364 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
366 char * psz_server_addr = p_socket->psz_server_addr;
367 int i_server_port = p_socket->i_server_port;
370 struct sockaddr_in6 sock;
372 if( i_server_port == 0 )
377 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET6 domain, automatic (0)
379 if( (i_handle = socket( AF_INET6, SOCK_STREAM, 0 )) == -1 )
381 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
385 /* Build remote address */
386 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
392 /* Connect the socket */
393 if( connect( i_handle, (struct sockaddr *) &sock,
394 sizeof( sock ) ) == (-1) )
396 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
401 p_socket->i_handle = i_handle;
402 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
407 /*****************************************************************************
408 * Open: wrapper around OpenUDP and OpenTCP
409 *****************************************************************************/
410 static int Open( vlc_object_t * p_this )
412 network_socket_t * p_socket = p_this->p_private;
414 if( p_socket->i_type == NETWORK_UDP )
416 return OpenUDP( p_this, p_socket );
420 return OpenTCP( p_this, p_socket );