]> git.sesse.net Git - vlc/blob - modules/misc/network/ipv4.c
fdfaf8057912aa76e03851ce6c7b6e10fa0d3be5
[vlc] / modules / misc / network / ipv4.c
1 /*****************************************************************************
2  * ipv4.c: IPv4 network abstraction layer
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: ipv4.c,v 1.24 2004/02/01 14:43:08 alexis Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Mathias Kretschmer <mathias@research.att.com>
9  *          Alexis de Lattre <alexis@via.ecp.fr>
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 <string.h>
31 #include <vlc/vlc.h>
32
33 #ifdef HAVE_SYS_TYPES_H
34 #   include <sys/types.h>
35 #endif
36 #ifdef HAVE_SYS_STAT_H
37 #   include <sys/stat.h>
38 #endif
39 #ifdef HAVE_ERRNO_H
40 #   include <errno.h>
41 #endif
42 #ifdef HAVE_FCNTL_H
43 #   include <fcntl.h>
44 #endif
45
46 #ifdef HAVE_UNISTD_H
47 #   include <unistd.h>
48 #endif
49
50 #if defined( UNDER_CE )
51 #   include <winsock.h>
52 #elif defined( WIN32 )
53 #   include <winsock2.h>
54 #   include <ws2tcpip.h>
55 #   define close closesocket
56 #else
57 #   include <netdb.h>                                         /* hostent ... */
58 #   include <sys/socket.h>
59 #   include <netinet/in.h>
60 #   ifdef HAVE_ARPA_INET_H
61 #       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
62 #   endif
63 #endif
64
65 #include "network.h"
66
67 #ifndef INADDR_ANY
68 #   define INADDR_ANY  0x00000000
69 #endif
70 #ifndef INADDR_NONE
71 #   define INADDR_NONE 0xFFFFFFFF
72 #endif
73 #ifndef IN_MULTICAST
74 #   define IN_MULTICAST(a) IN_CLASSD(a)
75 #endif
76
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 static int NetOpen( vlc_object_t * );
82
83 /*****************************************************************************
84  * Module descriptor
85  *****************************************************************************/
86 vlc_module_begin();
87     set_description( _("IPv4 network abstraction layer") );
88     set_capability( "network", 50 );
89     set_callbacks( NetOpen, NULL );
90 vlc_module_end();
91
92 /*****************************************************************************
93  * BuildAddr: utility function to build a struct sockaddr_in
94  *****************************************************************************/
95 static int BuildAddr( struct sockaddr_in * p_socket,
96                       const char * psz_address, int i_port )
97 {
98     /* Reset struct */
99     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
100     p_socket->sin_family = AF_INET;                                /* family */
101     p_socket->sin_port = htons( (uint16_t)i_port );
102     if( !*psz_address )
103     {
104         p_socket->sin_addr.s_addr = INADDR_ANY;
105     }
106     else
107     {
108         struct hostent    * p_hostent;
109
110         /* Try to convert address directly from in_addr - this will work if
111          * psz_address is dotted decimal. */
112 #ifdef HAVE_ARPA_INET_H
113         if( !inet_aton( psz_address, &p_socket->sin_addr ) )
114 #else
115         p_socket->sin_addr.s_addr = inet_addr( psz_address );
116         if( p_socket->sin_addr.s_addr == INADDR_NONE )
117 #endif
118         {
119             /* We have a fqdn, try to find its address */
120             if ( (p_hostent = gethostbyname( psz_address )) == NULL )
121             {
122                 return( -1 );
123             }
124
125             /* Copy the first address of the host in the socket address */
126             memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
127                      p_hostent->h_length );
128         }
129     }
130     return( 0 );
131 }
132
133 /*****************************************************************************
134  * OpenUDP: open a UDP socket
135  *****************************************************************************
136  * psz_bind_addr, i_bind_port : address and port used for the bind()
137  *   system call. If psz_bind_addr == "", the socket is bound to
138  *   INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
139  *   1234 is used. If psz_bind_addr is a multicast (class D) address,
140  *   join the multicast group.
141  * psz_server_addr, i_server_port : address and port used for the connect()
142  *   system call. It can avoid receiving packets from unauthorized IPs.
143  *   Its use leads to great confusion and is currently discouraged.
144  * This function returns -1 in case of error.
145  *****************************************************************************/
146 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
147 {
148     char * psz_bind_addr = p_socket->psz_bind_addr;
149     int i_bind_port = p_socket->i_bind_port;
150     char * psz_server_addr = p_socket->psz_server_addr;
151     int i_server_port = p_socket->i_server_port;
152
153     int i_handle, i_opt;
154     socklen_t i_opt_size;
155     struct sockaddr_in sock;
156
157     /* If IP_ADD_SOURCE_MEMBERSHIP is not defined in the headers
158        (because it's not in glibc for example), we have to define the
159        headers required for IGMPv3 here */
160 #ifndef IP_ADD_SOURCE_MEMBERSHIP
161     #define IP_ADD_SOURCE_MEMBERSHIP  39
162     struct ip_mreq_source {
163         struct in_addr  imr_multiaddr;
164         struct in_addr  imr_interface;
165         struct in_addr  imr_sourceaddr;
166      };
167 #endif
168
169     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
170      * protocol */
171     if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
172     {
173 #ifdef HAVE_ERRNO_H
174         msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
175 #else
176         msg_Warn( p_this, "cannot create socket" );
177 #endif
178         return( -1 );
179     }
180
181     /* We may want to reuse an already used socket */
182     i_opt = 1;
183     if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
184                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
185     {
186 #ifdef HAVE_ERRNO_H
187         msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
188                           strerror(errno));
189 #else
190         msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
191 #endif
192         close( i_handle );
193         return( -1 );
194     }
195
196     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
197      * packet loss caused by scheduling problems */
198     i_opt = 0x80000;
199 #if defined( SYS_BEOS )
200     if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
201 #else
202     if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
203 #endif
204     {
205 #ifdef HAVE_ERRNO_H
206         msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
207                           strerror(errno));
208 #else
209         msg_Warn( p_this, "cannot configure socket (SO_RCVBUF)" );
210 #endif
211     }
212
213     /* Check if we really got what we have asked for, because Linux, etc.
214      * will silently limit the max buffer size to net.core.rmem_max which
215      * is typically only 65535 bytes */
216     i_opt = 0;
217     i_opt_size = sizeof( i_opt );
218 #if defined( SYS_BEOS )
219     if( getsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK, (void*) &i_opt, &i_opt_size ) == -1 )
220 #else
221     if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void*) &i_opt, &i_opt_size ) == -1 )
222 #endif
223     {
224 #ifdef HAVE_ERRNO_H
225         msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
226                           strerror(errno) );
227 #else
228         msg_Warn( p_this, "cannot query socket (SO_RCVBUF)" );
229 #endif
230     }
231     else if( i_opt < 0x80000 )
232     {
233         msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
234                          i_opt, 0x80000 );
235     }
236
237
238     /* Build the local socket */
239
240 #if defined( WIN32 ) && !defined( UNDER_CE )
241     /* Under Win32 and for multicasting, we bind to INADDR_ANY,
242      * so let's call BuildAddr with "" instead of psz_bind_addr */
243     if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
244                    "" : psz_bind_addr, i_bind_port ) == -1 )
245 #else
246     if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
247 #endif
248     {
249         msg_Dbg( p_this, "could not build local address" );
250         close( i_handle );
251         return( -1 );
252     }
253
254     /* Bind it */
255     if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
256     {
257 #ifdef HAVE_ERRNO_H
258         msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
259 #else
260         msg_Warn( p_this, "cannot bind socket" );
261 #endif
262         close( i_handle );
263         return( -1 );
264     }
265
266 #if defined( WIN32 ) && !defined( UNDER_CE )
267     /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
268     if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
269     {
270         if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
271         {
272             msg_Dbg( p_this, "could not build local address" );
273             close( i_handle );
274             return( -1 );
275         }
276     }
277 #endif
278
279     /* Allow broadcast reception if we bound on INADDR_ANY */
280     if( !*psz_bind_addr )
281     {
282         i_opt = 1;
283 #if defined( SYS_BEOS )
284         if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
285 #else
286         if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
287 #endif
288         {
289 #ifdef HAVE_ERRNO_H
290             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
291                        strerror(errno) );
292 #else
293             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST)" );
294 #endif
295         }
296     }
297
298 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
299     /* Join the multicast group if the socket is a multicast address */
300     if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
301     {
302         /* Determine interface to be used for multicast */
303         char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
304
305         /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
306            so that IGMPv3 aware OSes running on IGMPv3 aware networks
307            will do an IGMPv3 query on the network */
308         if( *psz_server_addr )
309         {
310             struct ip_mreq_source imr;
311
312             imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
313             imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
314
315             if( psz_if_addr != NULL && *psz_if_addr
316                 && inet_addr(psz_if_addr) != INADDR_NONE )
317             {
318                 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
319             }
320             else
321             {
322                 imr.imr_interface.s_addr = INADDR_ANY;
323             }
324             if( psz_if_addr != NULL ) free( psz_if_addr );
325
326             msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
327             /* Join Multicast group with source filter */
328             if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
329                          (char*)&imr,
330                          sizeof(struct ip_mreq_source) ) == -1 )
331             {
332 #ifdef HAVE_ERRNO_H
333                 msg_Err( p_this, "failed to join IP multicast group (%s)",
334                                   strerror(errno) );
335                 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
336 #else
337                 msg_Err( p_this, "failed to join IP multicast group" );
338                 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
339 #endif
340                 close( i_handle );
341                 return( -1 );
342             }
343          }
344          /* If there is no source address, we use IP_ADD_MEMBERSHIP */
345          else
346          {
347              struct ip_mreq imr;
348
349              imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
350              if( psz_if_addr != NULL && *psz_if_addr
351                 && inet_addr(psz_if_addr) != INADDR_NONE )
352             {
353                 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
354             }
355             else
356             {
357                 imr.imr_interface.s_addr = INADDR_ANY;
358             }
359             if( psz_if_addr != NULL ) free( psz_if_addr );
360
361             msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
362             /* Join Multicast group without source filter */
363             if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
364                             (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
365             {
366 #ifdef HAVE_ERRNO_H
367                 msg_Err( p_this, "failed to join IP multicast group (%s)",
368                                   strerror(errno) );
369 #else
370                 msg_Err( p_this, "failed to join IP multicast group" );
371 #endif
372                 close( i_handle );
373                 return( -1 );
374             }
375          }
376     }
377 #endif
378
379     if( *psz_server_addr )
380     {
381         /* Build socket for remote connection */
382         if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
383         {
384             msg_Warn( p_this, "cannot build remote address" );
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 #ifdef HAVE_ERRNO_H
394             msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
395 #else
396             msg_Warn( p_this, "cannot connect socket" );
397 #endif
398             close( i_handle );
399             return( -1 );
400         }
401
402 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
403         if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
404         {
405             /* set the time-to-live */
406             int ttl = p_socket->i_ttl;
407             if( ttl < 1 )
408             {
409                 ttl = config_GetInt( p_this, "ttl" );
410             }
411             if( ttl < 1 ) ttl = 1;
412
413             if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
414                             (void *) &ttl, sizeof( ttl ) ) < 0 )
415             {
416 #ifdef HAVE_ERRNO_H
417                 msg_Err( p_this, "failed to set ttl (%s)", strerror(errno) );
418 #else
419                 msg_Err( p_this, "failed to set ttl" );
420 #endif
421                 close( i_handle );
422                 return( -1 );
423             }
424         }
425 #endif
426     }
427
428     p_socket->i_handle = i_handle;
429     p_socket->i_mtu = config_GetInt( p_this, "mtu" );
430     return( 0 );
431 }
432
433 /*****************************************************************************
434  * OpenTCP: open a TCP socket
435  *****************************************************************************
436  * psz_server_addr, i_server_port : address and port used for the connect()
437  *   system call. If i_server_port == 0, 80 is used.
438  * Other parameters are ignored.
439  * This function returns -1 in case of error.
440  *****************************************************************************/
441 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
442 {
443     char * psz_server_addr = p_socket->psz_server_addr;
444     int i_server_port = p_socket->i_server_port;
445
446     int i_handle;
447     struct sockaddr_in sock;
448
449     if( i_server_port == 0 )
450     {
451         i_server_port = 80;
452     }
453
454     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
455      * protocol */
456     if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
457     {
458 #ifdef HAVE_ERRNO_H
459         msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
460 #else
461         msg_Warn( p_this, "cannot create socket" );
462 #endif
463         goto error;
464     }
465
466     /* Build remote address */
467     if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
468     {
469         msg_Dbg( p_this, "could not build local address" );
470         goto error;
471     }
472
473     /* Set to non-blocking */
474 #if defined( WIN32 ) || defined( UNDER_CE )
475     {
476         unsigned long i_dummy = 1;
477         if( ioctlsocket( i_handle, FIONBIO, &i_dummy ) != 0 )
478         {
479             msg_Err( p_this, "cannot set socket to non-blocking mode" );
480         }
481     }
482 #elif defined( HAVE_ERRNO_H )
483     {
484         int i_flags;
485         if( ( i_flags = fcntl( i_handle, F_GETFL, 0 ) ) < 0 ||
486             fcntl( i_handle, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
487         {
488             msg_Err( p_this, "cannot set socket to non-blocking mode" );
489         }
490     }
491 #endif
492
493     /* Connect the socket */
494     if( connect( i_handle, (struct sockaddr *) &sock, sizeof( sock ) ) == -1 )
495     {
496 #if defined( WIN32 ) || defined( UNDER_CE )
497         if( WSAGetLastError() == WSAEWOULDBLOCK )
498 #elif defined( HAVE_ERRNO_H )
499         if( errno == EINPROGRESS )
500 #else
501         if( 0 )
502 #endif
503         {
504             int i_ret;
505             int i_opt;
506             int i_opt_size = sizeof( i_opt );
507             struct timeval  timeout;
508             fd_set          fds;
509
510             msg_Dbg( p_this, "connection in progress" );
511             do
512             {
513                 if( p_this->b_die )
514                 {
515                     msg_Dbg( p_this, "connection aborted" );
516                     goto error;
517                 }
518
519                 /* Initialize file descriptor set */
520                 FD_ZERO( &fds );
521                 FD_SET( i_handle, &fds );
522
523                 /* We'll wait 0.1 second if nothing happens */
524                 timeout.tv_sec = 0;
525                 timeout.tv_usec = 100000;
526
527             } while( ( i_ret = select( i_handle + 1, NULL, &fds, NULL,
528                                        &timeout ) ) == 0 ||
529 #if defined( WIN32 ) || defined( UNDER_CE )
530                      ( i_ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK ) );
531 #elif defined( HAVE_ERRNO_H )
532                      ( i_ret < 0 && errno == EINTR ) );
533 #else
534                      ( i_ret < 0 ) );
535 #endif
536
537             if( i_ret < 0 )
538             {
539                 msg_Warn( p_this, "cannot connect socket (select failed)" );
540                 goto error;
541             }
542
543             if( getsockopt( i_handle, SOL_SOCKET, SO_ERROR, (void*)&i_opt,
544                             &i_opt_size ) == -1 || i_opt != 0 )
545             {
546                 msg_Warn( p_this, "cannot connect socket (SO_ERROR)" );
547                 goto error;
548             }
549         }
550         else
551         {
552 #if defined( HAVE_ERRNO_H )
553             msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
554 #else
555             msg_Warn( p_this, "cannot connect socket" );
556 #endif
557             goto error;
558         }
559     }
560
561     p_socket->i_handle = i_handle;
562     p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
563     return VLC_SUCCESS;
564
565 error:
566     if( i_handle > 0 )
567     {
568         close( i_handle );
569     }
570     return VLC_EGENERIC;
571 }
572
573 /*****************************************************************************
574  * NetOpen: wrapper around OpenUDP and OpenTCP
575  *****************************************************************************/
576 static int NetOpen( vlc_object_t * p_this )
577 {
578     network_socket_t * p_socket = p_this->p_private;
579
580     if( p_socket->i_type == NETWORK_UDP )
581     {
582         return OpenUDP( p_this, p_socket );
583     }
584     else
585     {
586         return OpenTCP( p_this, p_socket );
587     }
588 }