]> git.sesse.net Git - vlc/blob - modules/misc/network/ipv6.c
* Correct socklen_t detection.
[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.5 2002/11/28 23:24:15 massiot 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 int Open( vlc_object_t * );
74
75 /*****************************************************************************
76  * Module descriptor
77  *****************************************************************************/
78 vlc_module_begin();
79     set_description( _("IPv6 network abstraction layer") );
80     set_capability( "network", 40 );
81     set_callbacks( Open, NULL );
82 vlc_module_end();
83
84 /*****************************************************************************
85  * BuildAddr: utility function to build a struct sockaddr_in6
86  *****************************************************************************/
87 static int BuildAddr( vlc_object_t * p_this, struct sockaddr_in6 * p_socket,
88                       char * psz_address, int i_port )
89 {
90     char * psz_multicast_interface = "";
91
92 #if defined(WIN32)
93     /* Try to get getaddrinfo() and freeaddrinfo() from wship6.dll */
94     typedef int (CALLBACK * GETADDRINFO) ( const char *nodename,
95                                             const char *servname,
96                                             const struct addrinfo *hints,
97                                             struct addrinfo **res );
98     typedef void (CALLBACK * FREEADDRINFO) ( struct addrinfo FAR *ai );
99
100     struct addrinfo hints, *res;
101     GETADDRINFO _getaddrinfo = NULL;
102     FREEADDRINFO _freeaddrinfo = NULL;
103
104     HINSTANCE wship6_dll = LoadLibrary("wship6.dll");
105     if( wship6_dll )
106     {
107         _getaddrinfo = (GETADDRINFO) GetProcAddress( wship6_dll,
108                                                      "getaddrinfo" );
109         _freeaddrinfo = (FREEADDRINFO) GetProcAddress( wship6_dll,
110                                                        "freeaddrinfo" );
111     }
112     if( !_getaddrinfo || !_freeaddrinfo )
113     {
114         msg_Err( p_this, "no IPv6 stack installed" );
115         if( wship6_dll ) FreeLibrary( wship6_dll );
116         return( -1 );
117     }
118 #endif
119
120     /* Reset struct */
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 );
124     if( !*psz_address )
125     {
126         p_socket->sin6_addr = in6addr_any;
127     }
128     else if( psz_address[0] == '['
129               && psz_address[strlen(psz_address) - 1] == ']' )
130     {
131         psz_address++;
132         /* see if there is an interface name in there... */
133         if( (psz_multicast_interface = strchr(psz_address, '%')) != NULL )
134         {
135             *psz_multicast_interface = '\0';
136             psz_multicast_interface++;
137             msg_Dbg( p_this, "Interface name specified: \"%s\"",
138                              psz_multicast_interface );
139
140             /* now convert that interface name to an index */
141 #if !defined( WIN32 )
142             p_socket->sin6_scope_id = if_nametoindex(psz_multicast_interface);
143 #else
144             /* FIXME: for now we always use the default interface */
145             p_socket->sin6_scope_id = 0;
146 #endif
147             msg_Warn( p_this, " = #%i", p_socket->sin6_scope_id );
148         }
149         psz_address[strlen(psz_address) - 1] = '\0' ;
150
151 #if !defined( WIN32 )
152         inet_pton(AF_INET6, psz_address, &p_socket->sin6_addr.s6_addr); 
153
154 #else
155         memset(&hints, 0, sizeof(hints));
156         hints.ai_family = AF_INET6;
157         hints.ai_flags = AI_NUMERICHOST;
158
159         if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
160         {
161             FreeLibrary( wship6_dll );
162             return( -1 );
163         }
164         memcpy( &p_socket->sin6_addr,
165                 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
166                 sizeof(struct in6_addr) );
167         _freeaddrinfo( res );
168
169 #endif
170     }
171     else
172     {
173 #ifdef HAVE_GETHOSTBYNAME2
174         struct hostent    * p_hostent;
175
176         /* We have a fqdn, try to find its address */
177         if ( (p_hostent = gethostbyname2( psz_address, AF_INET6 )) == NULL )
178         {
179             msg_Err( p_this, "ipv6 error: unknown host %s", psz_address );
180             return( -1 );
181         }
182
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 );
186
187 #elif defined(WIN32)
188         if( _getaddrinfo( psz_address, NULL, &hints, &res ) )
189         {
190             FreeLibrary( wship6_dll );
191             return( -1 );
192         }
193         memcpy( &p_socket->sin6_addr,
194                 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
195                 sizeof(struct in6_addr) );
196         _freeaddrinfo( res );
197
198 #else
199         msg_Err( p_this, "ipv6 error: IPv6 address %s is invalid",
200                  psz_address );
201         return( -1 );
202 #endif
203     }
204
205 #if defined(WIN32)
206     FreeLibrary( wship6_dll );
207 #endif
208
209     return( 0 );
210 }
211
212 /*****************************************************************************
213  * OpenUDP: open a UDP socket
214  *****************************************************************************
215  * psz_bind_addr, i_bind_port : address and port used for the bind()
216  *   system call. If psz_bind_addr == NULL, the socket is bound to
217  *   in6addr_any and broadcast reception is enabled. If i_bind_port == 0,
218  *   1234 is used. If psz_bind_addr is a multicast (class D) address,
219  *   join the multicast group.
220  * psz_server_addr, i_server_port : address and port used for the connect()
221  *   system call. It can avoid receiving packets from unauthorized IPs.
222  *   Its use leads to great confusion and is currently discouraged.
223  * This function returns -1 in case of error.
224  *****************************************************************************/
225 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
226 {
227     char * psz_bind_addr = p_socket->psz_bind_addr;
228     int i_bind_port = p_socket->i_bind_port;
229     char * psz_server_addr = p_socket->psz_server_addr;
230     int i_server_port = p_socket->i_server_port;
231
232     int i_handle, i_opt;
233     socklen_t i_opt_size;
234     struct sockaddr_in6 sock;
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         msg_Err( p_this, "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         msg_Err( p_this, "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         msg_Warn( p_this, "cannot configure socket (SO_RCVBUF: %s)",
262                           strerror(errno) );
263     }
264  
265     /* Check if we really got what we have asked for, because Linux, etc.
266      * will silently limit the max buffer size to net.core.rmem_max which
267      * is typically only 65535 bytes */
268     i_opt = 0;
269     i_opt_size = sizeof( i_opt );
270     if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
271                     (void*) &i_opt, &i_opt_size ) == -1 )
272     {
273         msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
274                           strerror(errno) );
275     }
276     else if( i_opt < 0x80000 )
277     {
278         msg_Warn( p_this, "socket buffer size is 0x%x instead of 0x%x",
279                           i_opt, 0x80000 );
280     }
281     
282     /* Build the local socket */
283     if ( BuildAddr( p_this, &sock, psz_bind_addr, i_bind_port ) == -1 )        
284     {
285         close( i_handle );
286         return( -1 );
287     }
288  
289     /* Bind it */
290     if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
291     {
292         msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
293         close( i_handle );
294         return( -1 );
295     }
296
297     /* Allow broadcast reception if we bound on in6addr_any */
298     if( !*psz_bind_addr )
299     {
300         i_opt = 1;
301         if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
302                         (void*) &i_opt, sizeof( i_opt ) ) == -1 )
303         {
304             msg_Warn( p_this, "ipv6 warning: cannot configure socket "
305                               "(SO_BROADCAST: %s)", strerror(errno) );
306         }
307     }
308  
309     /* Join the multicast group if the socket is a multicast address */
310     if( IN6_IS_ADDR_MULTICAST(&sock.sin6_addr) )
311     {
312         struct ipv6_mreq     imr;
313         int                  res;
314
315         imr.ipv6mr_interface = sock.sin6_scope_id;
316         imr.ipv6mr_multiaddr = sock.sin6_addr;
317         res = setsockopt(i_handle, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void*) &imr,
318                          sizeof(imr));
319
320         if( res == -1 )
321         {
322             msg_Err( p_this, "setsockopt JOIN_GROUP failed" );
323         }
324     }
325
326
327     if( *psz_server_addr )
328     {
329         /* Build socket for remote connection */
330         if ( BuildAddr( p_this, &sock, psz_server_addr, i_server_port ) == -1 )
331         {
332             msg_Err( p_this, "cannot build remote address" );
333             close( i_handle );
334             return( -1 );
335         }
336  
337         /* Connect the socket */
338         if( connect( i_handle, (struct sockaddr *) &sock,
339                      sizeof( sock ) ) == (-1) )
340         {
341             msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
342             close( i_handle );
343             return( -1 );
344         }
345     }
346
347     p_socket->i_handle = i_handle;
348     p_socket->i_mtu = config_GetInt( p_this, "mtu" );
349
350     return( 0 );
351 }
352
353 /*****************************************************************************
354  * OpenTCP: open a TCP socket
355  *****************************************************************************
356  * psz_server_addr, i_server_port : address and port used for the connect()
357  *   system call. If i_server_port == 0, 80 is used.
358  * Other parameters are ignored.
359  * This function returns -1 in case of error.
360  *****************************************************************************/
361 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
362 {
363     char * psz_server_addr = p_socket->psz_server_addr;
364     int i_server_port = p_socket->i_server_port;
365
366     int i_handle;
367     struct sockaddr_in6 sock;
368
369     if( i_server_port == 0 )
370     {
371         i_server_port = 80;
372     }
373
374     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET6 domain, automatic (0)
375      * protocol */
376     if( (i_handle = socket( AF_INET6, SOCK_STREAM, 0 )) == -1 )
377     {
378         msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
379         return( -1 );
380     }
381
382     /* Build remote address */
383     if ( BuildAddr( p_this, &sock, psz_server_addr, i_server_port ) == -1 )
384     {
385         close( i_handle );
386         return( -1 );
387     }
388
389     /* Connect the socket */
390     if( connect( i_handle, (struct sockaddr *) &sock,
391                  sizeof( sock ) ) == (-1) )
392     {
393         msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
394         close( i_handle );
395         return( -1 );
396     }
397
398     p_socket->i_handle = i_handle;
399     p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
400
401     return( 0 );
402 }
403
404 /*****************************************************************************
405  * Open: wrapper around OpenUDP and OpenTCP
406  *****************************************************************************/
407 static int Open( vlc_object_t * p_this )
408 {
409     network_socket_t * p_socket = p_this->p_private;
410
411     if( p_socket->i_type == NETWORK_UDP )
412     {
413         return OpenUDP( p_this, p_socket );
414     }
415     else
416     {
417         return OpenTCP( p_this, p_socket );
418     }
419 }