]> git.sesse.net Git - vlc/blob - plugins/network/ipv6.c
* ALL: the first libvlc commit.
[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.9 2002/06/01 12:32:00 sam Exp $
6  *
7  * Authors: Alexis Guillard <alexis.guillard@bt.com>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <fcntl.h>
34
35 #include <vlc/vlc.h>
36
37 #ifdef HAVE_UNISTD_H
38 #   include <unistd.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
40 #   include <io.h>
41 #endif
42
43 #ifdef 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() */
52 #   endif
53 #endif
54
55 #include "network.h"
56
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
60
61 #if defined(WIN32)
62 static const struct in6_addr in6addr_any = {{IN6ADDR_ANY_INIT}};
63 #endif
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static void getfunctions( function_list_t * );
69 static int  NetworkOpen( network_socket_t * );
70
71 /*****************************************************************************
72  * Build configuration tree.
73  *****************************************************************************/
74 MODULE_CONFIG_START
75 MODULE_CONFIG_STOP
76  
77 MODULE_INIT_START
78     SET_DESCRIPTION( _("IPv6 network abstraction layer") )
79     ADD_CAPABILITY( NETWORK, 40 )
80 MODULE_INIT_STOP
81  
82 MODULE_ACTIVATE_START
83     getfunctions( &p_module->p_functions->network );
84 MODULE_ACTIVATE_STOP
85  
86 MODULE_DEACTIVATE_START
87 MODULE_DEACTIVATE_STOP
88
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 )
94 {
95 #define f p_function_list->functions.network
96     f.pf_open = NetworkOpen;
97 #undef f
98 }
99
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 )
105 {
106 #if defined(WIN32)
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 );
113
114     struct addrinfo hints, *res;
115     GETADDRINFO _getaddrinfo = NULL;
116     FREEADDRINFO _freeaddrinfo = NULL;
117
118     HINSTANCE wship6_dll = LoadLibrary("wship6.dll");
119     if( wship6_dll )
120     {
121         _getaddrinfo = (GETADDRINFO) GetProcAddress( wship6_dll,
122                                                      "getaddrinfo" );
123         _freeaddrinfo = (FREEADDRINFO) GetProcAddress( wship6_dll,
124                                                        "freeaddrinfo" );
125     }
126     if( !_getaddrinfo || !_freeaddrinfo )
127     {
128         intf_ErrMsg( "ipv6 error: no IPv6 stack installed" );
129         if( wship6_dll ) FreeLibrary( wship6_dll );
130         return( -1 );
131     }
132 #endif
133
134     /* Reset struct */
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 );
138     if( !*psz_address )
139     {
140         p_socket->sin6_addr = in6addr_any;
141     }
142     else if( psz_address[0] == '['
143               && psz_address[strlen(psz_address) - 1] == ']' )
144     {
145         psz_address++;
146         psz_address[strlen(psz_address) - 1] = '\0' ;
147
148 #if !defined( WIN32 )
149         inet_pton(AF_INET6, psz_address, &p_socket->sin6_addr.s6_addr); 
150
151 #else
152         memset(&hints, 0, sizeof(hints));
153         hints.ai_family = AF_INET6;
154         hints.ai_flags = AI_NUMERICHOST;
155
156         if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
157         {
158             FreeLibrary( wship6_dll );
159             return( -1 );
160         }
161         memcpy( &p_socket->sin6_addr,
162                 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
163                 sizeof(struct in6_addr) );
164         _freeaddrinfo( res );
165
166 #endif
167     }
168     else
169     {
170 #ifdef HAVE_GETHOSTBYNAME2
171         struct hostent    * p_hostent;
172
173         /* We have a fqdn, try to find its address */
174         if ( (p_hostent = gethostbyname2( psz_address, AF_INET6 )) == NULL )
175         {
176 //X            intf_ErrMsg( "ipv6 error: unknown host %s", psz_address );
177             return( -1 );
178         }
179
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 );
183
184 #elif defined(WIN32)
185         if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
186         {
187             FreeLibrary( wship6_dll );
188             return( -1 );
189         }
190         memcpy( &p_socket->sin6_addr,
191                 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
192                 sizeof(struct in6_addr) );
193         _freeaddrinfo( res );
194
195 #else
196 //X        intf_ErrMsg( "ipv6 error: IPv6 address %s is invalid", psz_address );
197         return( -1 );
198 #endif
199     }
200
201 #if defined(WIN32)
202     FreeLibrary( wship6_dll );
203 #endif
204
205     return( 0 );
206 }
207
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 )
222 {
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;
227
228     int i_handle, i_opt, i_opt_size;
229     struct sockaddr_in6 sock;
230
231     if( i_bind_port == 0 )
232     {
233 //X        i_bind_port = config_GetInt( "server-port" );
234     }
235
236     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET6 domain, automatic (0)
237      * protocol */
238     if( (i_handle = socket( AF_INET6, SOCK_DGRAM, 0 )) == -1 )
239     {
240 //X        intf_ErrMsg( "ipv6 error: cannot create socket (%s)", strerror(errno) );
241         return( -1 );
242     }
243
244     /* We may want to reuse an already used socket */
245     i_opt = 1;
246     if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
247                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
248     {
249 //X        intf_ErrMsg( "ipv6 error: cannot configure socket (SO_REUSEADDR: %s)",
250                      strerror(errno));
251         close( i_handle );
252         return( -1 );
253     }
254
255     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
256      * packet loss caused by scheduling problems */
257     i_opt = 0x80000;
258     if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
259                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
260     {
261 //X        intf_WarnMsg( 1,
262                       "ipv6 warning: cannot configure socket (SO_RCVBUF: %s)",
263                       strerror(errno));
264     }
265  
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 */
269     i_opt = 0;
270     i_opt_size = sizeof( i_opt );
271     if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
272                     (void*) &i_opt, &i_opt_size ) == -1 )
273     {
274 //X        intf_WarnMsg( 1, "ipv6 warning: cannot query socket (SO_RCVBUF: %s)",
275 //X                         strerror(errno));
276     }
277     else if( i_opt < 0x80000 )
278     {
279 //X        intf_WarnMsg( 1, "ipv6 warning: socket buffer size is 0x%x"
280                          " instead of 0x%x", i_opt, 0x80000 );
281     }
282     
283     /* Build the local socket */
284     if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )        
285     {
286         close( i_handle );
287         return( -1 );
288     }
289  
290     /* Bind it */
291     if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
292     {
293 //X        intf_ErrMsg( "ipv6 error: cannot bind socket (%s)", strerror(errno) );
294         close( i_handle );
295         return( -1 );
296     }
297
298     /* Allow broadcast reception if we bound on in6addr_any */
299     if( !*psz_bind_addr )
300     {
301         i_opt = 1;
302         if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
303                         (void*) &i_opt, sizeof( i_opt ) ) == -1 )
304         {
305 //X            intf_WarnMsg( 1,
306 //X                    "ipv6 warning: cannot configure socket (SO_BROADCAST: %s)",
307 //X                    strerror(errno));
308         }
309     }
310  
311     /* Join the multicast group if the socket is a multicast address */
312     /* FIXME: To be written */
313
314     if( *psz_server_addr )
315     {
316         /* Build socket for remote connection */
317         if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
318         {
319 //X            intf_ErrMsg( "ipv6 error: cannot build remote address" );
320             close( i_handle );
321             return( -1 );
322         }
323  
324         /* Connect the socket */
325         if( connect( i_handle, (struct sockaddr *) &sock,
326                      sizeof( sock ) ) == (-1) )
327         {
328 //X            intf_ErrMsg( "ipv6 error: cannot connect socket (%s)",
329 //X                         strerror(errno) );
330             close( i_handle );
331             return( -1 );
332         }
333     }
334
335     p_socket->i_handle = i_handle;
336     p_socket->i_mtu = DEFAULT_MTU;
337     return( 0 );
338 }
339
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 )
349 {
350     char * psz_server_addr = p_socket->psz_server_addr;
351     int i_server_port = p_socket->i_server_port;
352
353     int i_handle;
354     struct sockaddr_in6 sock;
355
356     if( i_server_port == 0 )
357     {
358         i_server_port = 80;
359     }
360
361     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET6 domain, automatic (0)
362      * protocol */
363     if( (i_handle = socket( AF_INET6, SOCK_STREAM, 0 )) == -1 )
364     {
365 //X        intf_ErrMsg( "ipv6 error: cannot create socket (%s)", strerror(errno) );
366         return( -1 );
367     }
368
369     /* Build remote address */
370     if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
371     {
372         close( i_handle );
373         return( -1 );
374     }
375
376     /* Connect the socket */
377     if( connect( i_handle, (struct sockaddr *) &sock,
378                  sizeof( sock ) ) == (-1) )
379     {
380 //X        intf_ErrMsg( "ipv6 error: cannot connect socket (%s)",
381                      strerror(errno) );
382         close( i_handle );
383         return( -1 );
384     }
385
386     p_socket->i_handle = i_handle;
387     p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
388
389     return( 0 );
390 }
391
392 /*****************************************************************************
393  * NetworkOpen: wrapper around OpenUDP and OpenTCP
394  *****************************************************************************/
395 static int NetworkOpen( network_socket_t * p_socket )
396 {
397     if( p_socket->i_type == NETWORK_UDP )
398     {
399         return OpenUDP( p_socket );
400     }
401     else
402     {
403         return OpenTCP( p_socket );
404     }
405 }