]> git.sesse.net Git - vlc/blob - modules/misc/network/ipv6.c
* modules/misc/network/ipv6.c: fixed multicast support on win32. Well, it is working...
[vlc] / modules / misc / network / ipv6.c
1 /*****************************************************************************
2  * ipv6.c: IPv6 network abstraction layer
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: ipv6.c,v 1.12 2003/06/13 12:08:13 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 12
67 #endif
68 #ifndef IPV6_MULTICAST_HOPS
69 #   define IPV6_MULTICAST_HOPS 10
70 #endif
71 #ifndef IPV6_UNICAST_HOPS
72 #   define IPV6_UNICAST_HOPS 4
73 #endif
74 #   define close closesocket
75 #endif
76
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 static int Open( vlc_object_t * );
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 vlc_module_begin();
86     set_description( _("IPv6 network abstraction layer") );
87     set_capability( "network", 40 );
88     set_callbacks( Open, NULL );
89 vlc_module_end();
90
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 )
96 {
97     char * psz_multicast_interface = "";
98     char * psz_backup = strdup(psz_bind_address);
99     char * psz_address = psz_backup;
100
101 #if defined(WIN32)
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 );
108
109     struct addrinfo hints, *res;
110     GETADDRINFO _getaddrinfo = NULL;
111     FREEADDRINFO _freeaddrinfo = NULL;
112
113     HINSTANCE wship6_dll = LoadLibrary("wship6.dll");
114     if( wship6_dll )
115     {
116         _getaddrinfo = (GETADDRINFO) GetProcAddress( wship6_dll,
117                                                      "getaddrinfo" );
118         _freeaddrinfo = (FREEADDRINFO) GetProcAddress( wship6_dll,
119                                                        "freeaddrinfo" );
120     }
121     if( !_getaddrinfo || !_freeaddrinfo )
122     {
123         msg_Err( p_this, "no IPv6 stack installed" );
124         if( wship6_dll ) FreeLibrary( wship6_dll );
125         free( psz_backup );
126         return( -1 );
127     }
128 #endif
129
130     /* Reset struct */
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 );
134     if( !*psz_address )
135     {
136         p_socket->sin6_addr = in6addr_any;
137     }
138     else if( psz_address[0] == '['
139               && psz_address[strlen(psz_address) - 1] == ']' )
140     {
141         psz_address[strlen(psz_address) - 1] = '\0';
142         psz_address++;
143
144         /* see if there is an interface name in there... */
145         if( (psz_multicast_interface = strchr(psz_address, '%')) != NULL )
146         {
147             *psz_multicast_interface = '\0';
148             psz_multicast_interface++;
149             msg_Dbg( p_this, "Interface name specified: \"%s\"",
150                              psz_multicast_interface );
151
152             /* now convert that interface name to an index */
153 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
154             p_socket->sin6_scope_id = if_nametoindex(psz_multicast_interface);
155 #elif defined( WIN32 )
156             /* FIXME ?? */
157             p_socket->sin6_scope_id = atol(psz_multicast_interface);
158 #endif
159             msg_Dbg( p_this, " = #%i", p_socket->sin6_scope_id );
160         }
161
162 #if !defined( WIN32 )
163         inet_pton(AF_INET6, psz_address, &p_socket->sin6_addr.s6_addr); 
164
165 #else
166         memset(&hints, 0, sizeof(hints));
167         hints.ai_family = AF_INET6;
168         hints.ai_flags = AI_NUMERICHOST;
169
170         if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
171         {
172             FreeLibrary( wship6_dll );
173             free( psz_backup );
174             return( -1 );
175         }
176         memcpy( &p_socket->sin6_addr,
177                 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
178                 sizeof(struct in6_addr) );
179         _freeaddrinfo( res );
180
181 #endif
182     }
183     else
184     {
185 #ifdef HAVE_GETHOSTBYNAME2
186         struct hostent    * p_hostent;
187
188         /* We have a fqdn, try to find its address */
189         if ( (p_hostent = gethostbyname2( psz_address, AF_INET6 )) == NULL )
190         {
191             msg_Err( p_this, "ipv6 error: unknown host %s", psz_address );
192             free( psz_backup );
193             return( -1 );
194         }
195
196         /* Copy the first address of the host in the socket address */
197         memcpy( &p_socket->sin6_addr, p_hostent->h_addr_list[0],
198                  p_hostent->h_length );
199
200 #elif defined(WIN32)
201         if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
202         {
203             FreeLibrary( wship6_dll );
204             free( psz_backup );
205             return( -1 );
206         }
207         memcpy( &p_socket->sin6_addr,
208                 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
209                 sizeof(struct in6_addr) );
210         _freeaddrinfo( res );
211
212 #else
213         msg_Err( p_this, "ipv6 error: IPv6 address %s is invalid",
214                  psz_address );
215         free( psz_backup );
216         return( -1 );
217 #endif
218     }
219
220 #if defined(WIN32)
221     FreeLibrary( wship6_dll );
222 #endif
223
224     free( psz_backup );
225     return 0;
226 }
227
228 /*****************************************************************************
229  * OpenUDP: open a UDP socket
230  *****************************************************************************
231  * psz_bind_addr, i_bind_port : address and port used for the bind()
232  *   system call. If psz_bind_addr == NULL, the socket is bound to
233  *   in6addr_any and broadcast reception is enabled. If i_bind_port == 0,
234  *   1234 is used. If psz_bind_addr is a multicast (class D) address,
235  *   join the multicast group.
236  * psz_server_addr, i_server_port : address and port used for the connect()
237  *   system call. It can avoid receiving packets from unauthorized IPs.
238  *   Its use leads to great confusion and is currently discouraged.
239  * This function returns -1 in case of error.
240  *****************************************************************************/
241 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
242 {
243     char * psz_bind_addr = p_socket->psz_bind_addr;
244     int i_bind_port = p_socket->i_bind_port;
245     char * psz_server_addr = p_socket->psz_server_addr;
246     int i_server_port = p_socket->i_server_port;
247
248     int i_handle, i_opt;
249     socklen_t i_opt_size;
250     struct sockaddr_in6 sock;
251
252     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET6 domain, automatic (0)
253      * protocol */
254     if( (i_handle = socket( AF_INET6, SOCK_DGRAM, 0 )) == -1 )
255     {
256         msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
257         return( -1 );
258     }
259
260     /* We may want to reuse an already used socket */
261     i_opt = 1;
262     if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
263                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
264     {
265         msg_Err( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
266                          strerror(errno) );
267         close( i_handle );
268         return( -1 );
269     }
270
271     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
272      * packet loss caused by scheduling problems */
273     i_opt = 0x80000;
274     if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
275                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
276     {
277         msg_Warn( p_this, "cannot configure socket (SO_RCVBUF: %s)",
278                           strerror(errno) );
279     }
280
281     /* Check if we really got what we have asked for, because Linux, etc.
282      * will silently limit the max buffer size to net.core.rmem_max which
283      * is typically only 65535 bytes */
284     i_opt = 0;
285     i_opt_size = sizeof( i_opt );
286     if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
287                     (void*) &i_opt, &i_opt_size ) == -1 )
288     {
289         msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
290                           strerror(errno) );
291     }
292     else if( i_opt < 0x80000 )
293     {
294         msg_Warn( p_this, "socket buffer size is 0x%x instead of 0x%x",
295                           i_opt, 0x80000 );
296     }
297
298     /* Build the local socket */
299     if ( BuildAddr( p_this, &sock, psz_bind_addr, i_bind_port ) == -1 )        
300     {
301         close( i_handle );
302         return( -1 );
303     }
304
305 #if defined(WIN32)
306     /* Under Win32 and for multicasting, we bind to IN6ADDR_ANY */
307     if( IN6_IS_ADDR_MULTICAST(&sock.sin6_addr) )
308     {
309         struct sockaddr_in6 sockany = sock;
310         sockany.sin6_addr = in6addr_any;
311         sockany.sin6_scope_id = 0;
312
313         /* Bind it */
314         if( bind( i_handle, (struct sockaddr *)&sockany, sizeof( sock ) ) < 0 )
315         {
316             msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
317             close( i_handle );
318             return( -1 );
319         }
320     } else
321 #endif
322     /* Bind it */
323     if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
324     {
325         msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
326         close( i_handle );
327         return( -1 );
328     }
329
330     /* Allow broadcast reception if we bound on in6addr_any */
331     if( !*psz_bind_addr )
332     {
333         i_opt = 1;
334         if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
335                         (void*) &i_opt, sizeof( i_opt ) ) == -1 )
336         {
337             msg_Warn( p_this, "ipv6 warning: cannot configure socket "
338                               "(SO_BROADCAST: %s)", strerror(errno) );
339         }
340     }
341
342     /* Join the multicast group if the socket is a multicast address */
343 #if defined(WIN32) || __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
344     if( IN6_IS_ADDR_MULTICAST(&sock.sin6_addr) )
345     {
346         struct ipv6_mreq     imr;
347         int                  res;
348
349         imr.ipv6mr_interface = sock.sin6_scope_id;
350         imr.ipv6mr_multiaddr = sock.sin6_addr;
351         res = setsockopt(i_handle, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void*) &imr,
352 #if defined(WIN32)
353                          sizeof(imr) + 4); /* Doesn't work without this */
354 #else
355                          sizeof(imr));
356 #endif
357
358         if( res == -1 )
359         {
360             msg_Err( p_this, "setsockopt JOIN_GROUP failed" );
361         } 
362     }
363 #else
364     msg_Warn( p_this, "setsockopt JOIN_GROUP not supported with glibc < 2.2" );
365 #endif
366
367
368     if( *psz_server_addr )
369     {
370         int ttl = config_GetInt( p_this, "ttl" );
371         if( ttl < 1 ) ttl = 1;
372
373         /* Build socket for remote connection */
374         if ( BuildAddr( p_this, &sock, psz_server_addr, i_server_port ) == -1 )
375         {
376             msg_Err( p_this, "cannot build remote address" );
377             close( i_handle );
378             return( -1 );
379         }
380
381         /* Connect the socket */
382         if( connect( i_handle, (struct sockaddr *) &sock,
383                      sizeof( sock ) ) == (-1) )
384         {
385             msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
386             close( i_handle );
387             return( -1 );
388         }
389
390         /* Set the time-to-live */
391         if( ttl > 1 )
392         {
393 #if defined(WIN32) || __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
394             if( IN6_IS_ADDR_MULTICAST(&sock.sin6_addr) )
395             {
396                 if( setsockopt( i_handle, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
397                                 (void *)&ttl, sizeof( ttl ) ) < 0 )
398                 {
399 #ifdef HAVE_ERRNO_H
400                     msg_Warn( p_this, "failed to set multicast ttl (%s)",
401                               strerror(errno) );
402 #else
403                     msg_Warn( p_this, "failed to set multicast ttl" );
404 #endif
405                 }
406             }
407             else
408             {
409                 if( setsockopt( i_handle, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
410                                 (void *)&ttl, sizeof( ttl ) ) < 0 )
411                 {
412 #ifdef HAVE_ERRNO_H
413                     msg_Warn( p_this, "failed to set unicast ttl (%s)",
414                               strerror(errno) );
415 #else
416                     msg_Warn( p_this, "failed to set unicast ttl" );
417 #endif
418                 }
419             }
420 #endif
421         }
422     }
423
424     p_socket->i_handle = i_handle;
425     p_socket->i_mtu = config_GetInt( p_this, "mtu" );
426
427     return( 0 );
428 }
429
430 /*****************************************************************************
431  * OpenTCP: open a TCP socket
432  *****************************************************************************
433  * psz_server_addr, i_server_port : address and port used for the connect()
434  *   system call. If i_server_port == 0, 80 is used.
435  * Other parameters are ignored.
436  * This function returns -1 in case of error.
437  *****************************************************************************/
438 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
439 {
440     char * psz_server_addr = p_socket->psz_server_addr;
441     int i_server_port = p_socket->i_server_port;
442
443     int i_handle;
444     struct sockaddr_in6 sock;
445
446     if( i_server_port == 0 )
447     {
448         i_server_port = 80;
449     }
450
451     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET6 domain, automatic (0)
452      * protocol */
453     if( (i_handle = socket( AF_INET6, SOCK_STREAM, 0 )) == -1 )
454     {
455         msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
456         return( -1 );
457     }
458
459     /* Build remote address */
460     if ( BuildAddr( p_this, &sock, psz_server_addr, i_server_port ) == -1 )
461     {
462         close( i_handle );
463         return( -1 );
464     }
465
466     /* Connect the socket */
467     if( connect( i_handle, (struct sockaddr *) &sock,
468                  sizeof( sock ) ) == (-1) )
469     {
470         msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
471         close( i_handle );
472         return( -1 );
473     }
474
475     p_socket->i_handle = i_handle;
476     p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
477
478     return( 0 );
479 }
480
481 /*****************************************************************************
482  * Open: wrapper around OpenUDP and OpenTCP
483  *****************************************************************************/
484 static int Open( vlc_object_t * p_this )
485 {
486     network_socket_t * p_socket = p_this->p_private;
487
488     if( p_socket->i_type == NETWORK_UDP )
489     {
490         return OpenUDP( p_this, p_socket );
491     }
492     else
493     {
494         return OpenTCP( p_this, p_socket );
495     }
496 }