]> git.sesse.net Git - vlc/blob - modules/misc/network/ipv4.c
Copyright fixes
[vlc] / modules / misc / network / ipv4.c
1 /*****************************************************************************
2  * ipv4.c: IPv4 network abstraction layer
3  *****************************************************************************
4  * Copyright (C) 2001-2005 VideoLAN (Centrale Réseaux) and its contributors
5  * $Id$
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
32 #include <vlc/vlc.h>
33 #include <errno.h>
34
35 #ifdef HAVE_SYS_TYPES_H
36 #   include <sys/types.h>
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #   include <sys/stat.h>
40 #endif
41 #ifdef HAVE_FCNTL_H
42 #   include <fcntl.h>
43 #endif
44
45 #ifdef HAVE_UNISTD_H
46 #   include <unistd.h>
47 #endif
48
49 #if defined(WIN32) || defined(UNDER_CE)
50 #   if defined(UNDER_CE) && defined(sockaddr_storage)
51 #       undef sockaddr_storage
52 #   endif
53 #   include <winsock2.h>
54 #   include <ws2tcpip.h>
55 #   define close closesocket
56 #   if defined(UNDER_CE)
57 #       undef IP_MULTICAST_TTL
58 #       define IP_MULTICAST_TTL 3
59 #       undef IP_ADD_MEMBERSHIP
60 #       define IP_ADD_MEMBERSHIP 5
61 #   endif
62 #else
63 #   include <netdb.h>                                         /* hostent ... */
64 #   include <sys/socket.h>
65 #   include <netinet/in.h>
66 #   ifdef HAVE_ARPA_INET_H
67 #       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
68 #   endif
69 #endif
70
71 #include "network.h"
72
73 #ifndef INADDR_ANY
74 #   define INADDR_ANY  0x00000000
75 #endif
76 #ifndef INADDR_NONE
77 #   define INADDR_NONE 0xFFFFFFFF
78 #endif
79 #ifndef IN_MULTICAST
80 #   define IN_MULTICAST(a) IN_CLASSD(a)
81 #endif
82
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 static int NetOpen( vlc_object_t * );
88
89 /*****************************************************************************
90  * Module descriptor
91  *****************************************************************************/
92 #define MIFACE_TEXT N_("Multicast output interface")
93 #define MIFACE_LONGTEXT N_( \
94     "Indicate here the multicast output interface. " \
95     "This overrides the routing table.")
96
97 vlc_module_begin();
98     set_shortname( "IPv4" );
99     set_description( _("IPv4 network abstraction layer") );
100     set_capability( "network", 50 );
101     set_category( CAT_INPUT );
102     set_subcategory( SUBCAT_INPUT_ADVANCED );
103     set_callbacks( NetOpen, NULL );
104     add_string( "miface-addr", NULL, NULL, MIFACE_TEXT, MIFACE_LONGTEXT, VLC_TRUE );
105 vlc_module_end();
106
107 /*****************************************************************************
108  * BuildAddr: utility function to build a struct sockaddr_in
109  *****************************************************************************/
110 static int BuildAddr( struct sockaddr_in * p_socket,
111                       const char * psz_address, int i_port )
112 {
113     /* Reset struct */
114     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
115     p_socket->sin_family = AF_INET;                                /* family */
116     p_socket->sin_port = htons( (uint16_t)i_port );
117     if( !*psz_address )
118     {
119         p_socket->sin_addr.s_addr = INADDR_ANY;
120     }
121     else
122     {
123         struct hostent    * p_hostent;
124
125         /* Try to convert address directly from in_addr - this will work if
126          * psz_address is dotted decimal. */
127 #ifdef HAVE_ARPA_INET_H
128         if( !inet_aton( psz_address, &p_socket->sin_addr ) )
129 #else
130         p_socket->sin_addr.s_addr = inet_addr( psz_address );
131         if( p_socket->sin_addr.s_addr == INADDR_NONE )
132 #endif
133         {
134             /* We have a fqdn, try to find its address */
135             if ( (p_hostent = gethostbyname( psz_address )) == NULL )
136             {
137                 return( -1 );
138             }
139
140             /* Copy the first address of the host in the socket address */
141             memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
142                      p_hostent->h_length );
143         }
144     }
145     return( 0 );
146 }
147
148 /*****************************************************************************
149  * OpenUDP: open a UDP socket
150  *****************************************************************************
151  * psz_bind_addr, i_bind_port : address and port used for the bind()
152  *   system call. If psz_bind_addr == "", the socket is bound to
153  *   INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
154  *   1234 is used. If psz_bind_addr is a multicast (class D) address,
155  *   join the multicast group.
156  * psz_server_addr, i_server_port : address and port used for the connect()
157  *   system call. It can avoid receiving packets from unauthorized IPs.
158  *   Its use leads to great confusion and is currently discouraged.
159  * This function returns -1 in case of error.
160  *****************************************************************************/
161 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
162 {
163     char * psz_bind_addr = p_socket->psz_bind_addr;
164     int i_bind_port = p_socket->i_bind_port;
165     char * psz_server_addr = p_socket->psz_server_addr;
166     int i_server_port = p_socket->i_server_port;
167
168     int i_handle, i_opt;
169     struct sockaddr_in sock;
170     vlc_value_t val;
171
172     /* If IP_ADD_SOURCE_MEMBERSHIP is not defined in the headers
173        (because it's not in glibc for example), we have to define the
174        headers required for IGMPv3 here */
175 #ifndef IP_ADD_SOURCE_MEMBERSHIP
176     #define IP_ADD_SOURCE_MEMBERSHIP  39
177     struct ip_mreq_source {
178         struct in_addr  imr_multiaddr;
179         struct in_addr  imr_interface;
180         struct in_addr  imr_sourceaddr;
181      };
182 #endif
183
184     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
185      * protocol */
186     if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
187     {
188 #if defined(WIN32) || defined(UNDER_CE)
189         msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
190 #else
191         msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
192 #endif
193         return( -1 );
194     }
195
196     /* We may want to reuse an already used socket */
197     i_opt = 1;
198     if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
199                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
200     {
201 #if defined(WIN32) || defined(UNDER_CE)
202         msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %i)",
203                   WSAGetLastError() );
204 #else
205         msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
206                           strerror(errno));
207 #endif
208         close( i_handle );
209         return( -1 );
210     }
211
212 #ifdef SO_REUSEPORT
213     i_opt = 1;
214     if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEPORT,
215                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
216     {
217         msg_Warn( p_this, "cannot configure socket (SO_REUSEPORT)" );
218     }
219 #endif
220
221     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
222      * packet loss caused by scheduling problems */
223     i_opt = 0x80000;
224 #if !defined( SYS_BEOS )
225     if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
226     {
227 #if defined(WIN32) || defined(UNDER_CE)
228         msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %i)",
229                  WSAGetLastError() );
230 #else
231         msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
232                           strerror(errno));
233 #endif
234     }
235 #endif
236
237     /* Build the local socket */
238
239 #if defined( WIN32 ) || defined( UNDER_CE )
240     /* Under Win32 and for multicasting, we bind to INADDR_ANY,
241      * so let's call BuildAddr with "" instead of psz_bind_addr */
242     if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
243                    "" : psz_bind_addr, i_bind_port ) == -1 )
244 #else
245     if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
246 #endif
247     {
248         msg_Dbg( p_this, "could not build local address" );
249         close( i_handle );
250         return( -1 );
251     }
252
253     /* Bind it */
254     if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
255     {
256 #if defined(WIN32) || defined(UNDER_CE)
257         msg_Warn( p_this, "cannot bind socket (%i)", WSAGetLastError() );
258 #else
259         msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
260 #endif
261         close( i_handle );
262         return( -1 );
263     }
264
265 #if defined( WIN32 ) || defined( UNDER_CE )
266     /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
267     if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
268     {
269         if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
270         {
271             msg_Dbg( p_this, "could not build local address" );
272             close( i_handle );
273             return( -1 );
274         }
275     }
276 #endif
277
278 #if !defined( SYS_BEOS )
279     /* Allow broadcast reception if we bound on INADDR_ANY */
280     if( !*psz_bind_addr )
281     {
282         i_opt = 1;
283         if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
284         {
285 #if defined(WIN32) || defined(UNDER_CE)
286             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %i)",
287                       WSAGetLastError() );
288 #else
289             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
290                        strerror(errno) );
291 #endif
292         }
293     }
294 #endif
295
296 #if !defined( SYS_BEOS )
297     /* Join the multicast group if the socket is a multicast address */
298     if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
299     {
300         /* Determine interface to be used for multicast */
301         char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
302
303         /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
304            so that IGMPv3 aware OSes running on IGMPv3 aware networks
305            will do an IGMPv3 query on the network */
306         if( *psz_server_addr )
307         {
308             struct ip_mreq_source imr;
309
310             imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
311             imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
312
313             if( psz_if_addr != NULL && *psz_if_addr
314                 && inet_addr(psz_if_addr) != INADDR_NONE )
315             {
316                 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
317             }
318             else
319             {
320                 imr.imr_interface.s_addr = INADDR_ANY;
321             }
322             if( psz_if_addr != NULL ) free( psz_if_addr );
323
324             msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
325             /* Join Multicast group with source filter */
326             if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
327                          (char*)&imr,
328                          sizeof(struct ip_mreq_source) ) == -1 )
329             {
330 #if defined(WIN32) || defined(UNDER_CE)
331                 msg_Err( p_this, "failed to join IP multicast group (%i)",
332                          WSAGetLastError() );
333 #else
334                 msg_Err( p_this, "failed to join IP multicast group (%s)",
335                                   strerror(errno) );
336 #endif
337                 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
338                 close( i_handle );
339                 return( -1 );
340             }
341          }
342          /* If there is no source address, we use IP_ADD_MEMBERSHIP */
343          else
344          {
345              struct ip_mreq imr;
346
347              imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
348              if( psz_if_addr != NULL && *psz_if_addr
349                 && inet_addr(psz_if_addr) != INADDR_NONE )
350             {
351                 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
352             }
353             else
354             {
355                 imr.imr_interface.s_addr = INADDR_ANY;
356             }
357             if( psz_if_addr != NULL ) free( psz_if_addr );
358
359             msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
360             /* Join Multicast group without source filter */
361             if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
362                             (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
363             {
364 #if defined(WIN32) || defined(UNDER_CE)
365                 msg_Err( p_this, "failed to join IP multicast group (%i)",
366                          WSAGetLastError() );
367 #else
368                 msg_Err( p_this, "failed to join IP multicast group (%s)",
369                                   strerror(errno) );
370 #endif
371                 close( i_handle );
372                 return( -1 );
373             }
374          }
375     }
376 #endif
377
378     if( *psz_server_addr )
379     {
380         /* Build socket for remote connection */
381         if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
382         {
383             msg_Warn( p_this, "cannot build remote address" );
384             close( i_handle );
385             return( -1 );
386         }
387
388         /* Connect the socket */
389         if( connect( i_handle, (struct sockaddr *) &sock,
390                      sizeof( sock ) ) == (-1) )
391         {
392 #if defined(WIN32) || defined(UNDER_CE)
393             msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
394 #else
395             msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
396 #endif
397             close( i_handle );
398             return( -1 );
399         }
400
401 #if !defined( SYS_BEOS )
402         if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
403         {
404             /* set the time-to-live */
405             int i_ttl = p_socket->i_ttl;
406             unsigned char ttl;
407             
408             /* set the multicast interface */
409             char * psz_mif_addr = config_GetPsz( p_this, "miface-addr" );
410             if( psz_mif_addr )
411             {
412                 struct in_addr intf;
413                 intf.s_addr = inet_addr(psz_mif_addr);
414                 free( psz_mif_addr  );
415
416                 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_IF,
417                                 &intf, sizeof( intf ) ) < 0 )
418                 {
419                     msg_Dbg( p_this, "failed to set multicast interface (%s).", strerror(errno) );
420                     close( i_handle );
421                     return ( -1 );
422                 }
423             }
424
425             if( i_ttl < 1 )
426             {
427                 if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS )
428                 {
429                     var_Create( p_this, "ttl",
430                                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
431                     var_Get( p_this, "ttl", &val );
432                 }
433                 i_ttl = val.i_int;
434             }
435             if( i_ttl < 1 ) i_ttl = 1;
436             ttl = (unsigned char) i_ttl;
437
438             /* There is some confusion in the world whether IP_MULTICAST_TTL 
439              * takes a byte or an int as an argument.
440              * BSD seems to indicate byte so we are going with that and use
441              * int as a fallback to be safe */
442             if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
443                             &ttl, sizeof( ttl ) ) < 0 )
444             {
445                 msg_Dbg( p_this, "failed to set ttl (%s). Let's try it "
446                          "the integer way.", strerror(errno) );
447                 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
448                                 &i_ttl, sizeof( i_ttl ) ) <0 )
449                 {
450                     msg_Err( p_this, "failed to set ttl (%s)",
451                              strerror(errno) );
452                     close( i_handle );
453                     return( -1 );
454                 }
455             }
456         }
457 #endif
458     }
459
460     p_socket->i_handle = i_handle;
461
462     if( var_Get( p_this, "mtu", &val ) != VLC_SUCCESS )
463     {
464         var_Create( p_this, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
465         var_Get( p_this, "mtu", &val );
466     }
467     p_socket->i_mtu = val.i_int;
468     return( 0 );
469 }
470
471 /*****************************************************************************
472  * NetOpen: wrapper around OpenUDP, ListenTCP and OpenTCP
473  *****************************************************************************/
474 static int NetOpen( vlc_object_t * p_this )
475 {
476     network_socket_t * p_socket = p_this->p_private;
477
478     return OpenUDP( p_this, p_socket );
479 }