]> git.sesse.net Git - vlc/blob - plugins/network/ipv6.c
0de88c45573e5836e088305d29b5a603d68757a1
[vlc] / plugins / network / ipv6.c
1 /*****************************************************************************
2  * ipv6.c: IPv6 network abstraction layer
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: ipv6.c,v 1.15 2002/07/29 19:01:27 gbazin Exp $
6  *
7  * Authors: Alexis Guillard <alexis.guillard@bt.com>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Remco Poortinga <poortinga@telin.nl>
10  *
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.
15  * 
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <fcntl.h>
35
36 #include <vlc/vlc.h>
37
38 #ifdef HAVE_UNISTD_H
39 #   include <unistd.h>
40 #elif defined( _MSC_VER ) && defined( _WIN32 )
41 #   include <io.h>
42 #endif
43
44 #ifdef 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 #   include <net/if.h>
52 #   ifdef HAVE_ARPA_INET_H
53 #       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
54 #   endif
55 #endif
56
57 #include "network.h"
58
59 #if defined(WIN32)
60 static const struct in6_addr in6addr_any = {{IN6ADDR_ANY_INIT}};
61 /* the following will have to be removed when w32api defines them */
62 #ifndef IPPROTO_IPV6
63 #   define IPPROTO_IPV6 41 
64 #endif
65 #ifndef IPV6_JOIN_GROUP
66 #   define IPV6_JOIN_GROUP 20
67 #endif
68 #endif
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static void getfunctions( function_list_t * );
74 static int  NetworkOpen( vlc_object_t *, network_socket_t * );
75
76 /*****************************************************************************
77  * Build configuration tree.
78  *****************************************************************************/
79 MODULE_CONFIG_START
80 MODULE_CONFIG_STOP
81  
82 MODULE_INIT_START
83     SET_DESCRIPTION( _("IPv6 network abstraction layer") )
84     ADD_CAPABILITY( NETWORK, 40 )
85 MODULE_INIT_STOP
86  
87 MODULE_ACTIVATE_START
88     getfunctions( &p_module->p_functions->network );
89 MODULE_ACTIVATE_STOP
90  
91 MODULE_DEACTIVATE_START
92 MODULE_DEACTIVATE_STOP
93
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 )
99 {
100 #define f p_function_list->functions.network
101     f.pf_open = NetworkOpen;
102 #undef f
103 }
104
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 )
110 {
111     char * psz_multicast_interface = "";
112
113 #if defined(WIN32)
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 );
120
121     struct addrinfo hints, *res;
122     GETADDRINFO _getaddrinfo = NULL;
123     FREEADDRINFO _freeaddrinfo = NULL;
124
125     HINSTANCE wship6_dll = LoadLibrary("wship6.dll");
126     if( wship6_dll )
127     {
128         _getaddrinfo = (GETADDRINFO) GetProcAddress( wship6_dll,
129                                                      "getaddrinfo" );
130         _freeaddrinfo = (FREEADDRINFO) GetProcAddress( wship6_dll,
131                                                        "freeaddrinfo" );
132     }
133     if( !_getaddrinfo || !_freeaddrinfo )
134     {
135 //X        msg_Err( p_this, "no IPv6 stack installed" );
136         if( wship6_dll ) FreeLibrary( wship6_dll );
137         return( -1 );
138     }
139 #endif
140
141     /* Reset struct */
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 );
145     if( !*psz_address )
146     {
147         p_socket->sin6_addr = in6addr_any;
148     }
149     else if( psz_address[0] == '['
150               && psz_address[strlen(psz_address) - 1] == ']' )
151     {
152         psz_address++;
153         /* see if there is an interface name in there... */
154         if( (psz_multicast_interface = strchr(psz_address, '%')) != NULL )
155         {
156             *psz_multicast_interface = '\0';
157             psz_multicast_interface++;
158 //X            msg_Dbg( p_this, "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);
163 #else
164             /* FIXME: for now we always use the default interface */
165             p_socket->sin6_scope_id = 0;
166 //X            msg_Warn( 3, "Using default interface. This has to be FIXED!");
167 #endif
168 //X            msg_Warn( p_this, " = #%i\n", p_socket->sin6_scope_id );
169         }
170         psz_address[strlen(psz_address) - 1] = '\0' ;
171
172 #if !defined( WIN32 )
173         inet_pton(AF_INET6, psz_address, &p_socket->sin6_addr.s6_addr); 
174
175 #else
176         memset(&hints, 0, sizeof(hints));
177         hints.ai_family = AF_INET6;
178         hints.ai_flags = AI_NUMERICHOST;
179
180         if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
181         {
182             FreeLibrary( wship6_dll );
183             return( -1 );
184         }
185         memcpy( &p_socket->sin6_addr,
186                 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
187                 sizeof(struct in6_addr) );
188         _freeaddrinfo( res );
189
190 #endif
191     }
192     else
193     {
194 #ifdef HAVE_GETHOSTBYNAME2
195         struct hostent    * p_hostent;
196
197         /* We have a fqdn, try to find its address */
198         if ( (p_hostent = gethostbyname2( psz_address, AF_INET6 )) == NULL )
199         {
200 //X            intf_ErrMsg( "ipv6 error: unknown host %s", psz_address );
201             return( -1 );
202         }
203
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 );
207
208 #elif defined(WIN32)
209         if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
210         {
211             FreeLibrary( wship6_dll );
212             return( -1 );
213         }
214         memcpy( &p_socket->sin6_addr,
215                 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
216                 sizeof(struct in6_addr) );
217         _freeaddrinfo( res );
218
219 #else
220 //X        intf_ErrMsg( "ipv6 error: IPv6 address %s is invalid", psz_address );
221         return( -1 );
222 #endif
223     }
224
225 #if defined(WIN32)
226     FreeLibrary( wship6_dll );
227 #endif
228
229     return( 0 );
230 }
231
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( vlc_object_t * p_this, network_socket_t * p_socket )
246 {
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;
251
252     int i_handle, i_opt, i_opt_size;
253     struct sockaddr_in6 sock;
254
255     if( i_bind_port == 0 )
256     {
257 //X        i_bind_port = config_GetInt( "server-port" );
258     }
259
260     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET6 domain, automatic (0)
261      * protocol */
262     if( (i_handle = socket( AF_INET6, SOCK_DGRAM, 0 )) == -1 )
263     {
264         msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
265         return( -1 );
266     }
267
268     /* We may want to reuse an already used socket */
269     i_opt = 1;
270     if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
271                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
272     {
273         msg_Err( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
274                          strerror(errno) );
275         close( i_handle );
276         return( -1 );
277     }
278
279     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
280      * packet loss caused by scheduling problems */
281     i_opt = 0x80000;
282     if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
283                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
284     {
285         msg_Warn( p_this, "cannot configure socket (SO_RCVBUF: %s)",
286                           strerror(errno) );
287     }
288  
289     /* Check if we really got what we have asked for, because Linux, etc.
290      * will silently limit the max buffer size to net.core.rmem_max which
291      * is typically only 65535 bytes */
292     i_opt = 0;
293     i_opt_size = sizeof( i_opt );
294     if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
295                     (void*) &i_opt, &i_opt_size ) == -1 )
296     {
297         msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
298                           strerror(errno) );
299     }
300     else if( i_opt < 0x80000 )
301     {
302         msg_Warn( p_this, "socket buffer size is 0x%x instead of 0x%x",
303                           i_opt, 0x80000 );
304     }
305     
306     /* Build the local socket */
307     if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )        
308     {
309         close( i_handle );
310         return( -1 );
311     }
312  
313     /* Bind it */
314     if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
315     {
316         msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
317         close( i_handle );
318         return( -1 );
319     }
320
321     /* Allow broadcast reception if we bound on in6addr_any */
322     if( !*psz_bind_addr )
323     {
324         i_opt = 1;
325         if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
326                         (void*) &i_opt, sizeof( i_opt ) ) == -1 )
327         {
328             msg_Warn( p_this, "ipv6 warning: cannot configure socket "
329                               "(SO_BROADCAST: %s)", strerror(errno) );
330         }
331     }
332  
333     /* Join the multicast group if the socket is a multicast address */
334     if( IN6_IS_ADDR_MULTICAST(&sock.sin6_addr) )
335     {
336         struct ipv6_mreq     imr;
337         int                  res;
338
339         imr.ipv6mr_interface = sock.sin6_scope_id;
340         imr.ipv6mr_multiaddr = sock.sin6_addr;
341         res = setsockopt(i_handle, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void*) &imr,
342                          sizeof(imr));
343
344         if( res == -1 )
345         {
346 //X            intf_ErrMsg( "ipv6 error: setsockopt JOIN_GROUP failed" );
347         }
348     }
349
350
351     if( *psz_server_addr )
352     {
353         /* Build socket for remote connection */
354         if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
355         {
356             msg_Err( p_this, "cannot build remote address" );
357             close( i_handle );
358             return( -1 );
359         }
360  
361         /* Connect the socket */
362         if( connect( i_handle, (struct sockaddr *) &sock,
363                      sizeof( sock ) ) == (-1) )
364         {
365             msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
366             close( i_handle );
367             return( -1 );
368         }
369     }
370
371     p_socket->i_handle = i_handle;
372     p_socket->i_mtu = config_GetInt( p_this, "mtu" );
373
374     return( 0 );
375 }
376
377 /*****************************************************************************
378  * OpenTCP: open a TCP socket
379  *****************************************************************************
380  * psz_server_addr, i_server_port : address and port used for the connect()
381  *   system call. If i_server_port == 0, 80 is used.
382  * Other parameters are ignored.
383  * This function returns -1 in case of error.
384  *****************************************************************************/
385 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
386 {
387     char * psz_server_addr = p_socket->psz_server_addr;
388     int i_server_port = p_socket->i_server_port;
389
390     int i_handle;
391     struct sockaddr_in6 sock;
392
393     if( i_server_port == 0 )
394     {
395         i_server_port = 80;
396     }
397
398     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET6 domain, automatic (0)
399      * protocol */
400     if( (i_handle = socket( AF_INET6, SOCK_STREAM, 0 )) == -1 )
401     {
402         msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
403         return( -1 );
404     }
405
406     /* Build remote address */
407     if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
408     {
409         close( i_handle );
410         return( -1 );
411     }
412
413     /* Connect the socket */
414     if( connect( i_handle, (struct sockaddr *) &sock,
415                  sizeof( sock ) ) == (-1) )
416     {
417         msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
418         close( i_handle );
419         return( -1 );
420     }
421
422     p_socket->i_handle = i_handle;
423     p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
424
425     return( 0 );
426 }
427
428 /*****************************************************************************
429  * NetworkOpen: wrapper around OpenUDP and OpenTCP
430  *****************************************************************************/
431 static int NetworkOpen( vlc_object_t * p_this, network_socket_t * p_socket )
432 {
433     if( p_socket->i_type == NETWORK_UDP )
434     {
435         return OpenUDP( p_this, p_socket );
436     }
437     else
438     {
439         return OpenTCP( p_this, p_socket );
440     }
441 }