]> git.sesse.net Git - vlc/blob - modules/misc/network/ipv4.c
ipv4.c : BeOS compile fix
[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.25 2004/02/22 23:09:25 titer 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_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
201     {
202 #ifdef HAVE_ERRNO_H
203         msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
204                           strerror(errno));
205 #else
206         msg_Warn( p_this, "cannot configure socket (SO_RCVBUF)" );
207 #endif
208     }
209 #endif
210
211 #if !defined( SYS_BEOS )
212     /* Check if we really got what we have asked for, because Linux, etc.
213      * will silently limit the max buffer size to net.core.rmem_max which
214      * is typically only 65535 bytes */
215     i_opt = 0;
216     i_opt_size = sizeof( i_opt );
217     if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void*) &i_opt, &i_opt_size ) == -1 )
218     {
219 #ifdef HAVE_ERRNO_H
220         msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
221                           strerror(errno) );
222 #else
223         msg_Warn( p_this, "cannot query socket (SO_RCVBUF)" );
224 #endif
225     }
226     else if( i_opt < 0x80000 )
227     {
228         msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
229                          i_opt, 0x80000 );
230     }
231 #endif
232
233
234     /* Build the local socket */
235
236 #if defined( WIN32 ) && !defined( UNDER_CE )
237     /* Under Win32 and for multicasting, we bind to INADDR_ANY,
238      * so let's call BuildAddr with "" instead of psz_bind_addr */
239     if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
240                    "" : psz_bind_addr, i_bind_port ) == -1 )
241 #else
242     if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
243 #endif
244     {
245         msg_Dbg( p_this, "could not build local address" );
246         close( i_handle );
247         return( -1 );
248     }
249
250     /* Bind it */
251     if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
252     {
253 #ifdef HAVE_ERRNO_H
254         msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
255 #else
256         msg_Warn( p_this, "cannot bind socket" );
257 #endif
258         close( i_handle );
259         return( -1 );
260     }
261
262 #if defined( WIN32 ) && !defined( UNDER_CE )
263     /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
264     if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
265     {
266         if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
267         {
268             msg_Dbg( p_this, "could not build local address" );
269             close( i_handle );
270             return( -1 );
271         }
272     }
273 #endif
274
275 #if !defined( SYS_BEOS )
276     /* Allow broadcast reception if we bound on INADDR_ANY */
277     if( !*psz_bind_addr )
278     {
279         i_opt = 1;
280         if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
281         {
282 #ifdef HAVE_ERRNO_H
283             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
284                        strerror(errno) );
285 #else
286             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST)" );
287 #endif
288         }
289     }
290 #endif
291
292 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
293     /* Join the multicast group if the socket is a multicast address */
294     if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
295     {
296         /* Determine interface to be used for multicast */
297         char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
298
299         /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
300            so that IGMPv3 aware OSes running on IGMPv3 aware networks
301            will do an IGMPv3 query on the network */
302         if( *psz_server_addr )
303         {
304             struct ip_mreq_source imr;
305
306             imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
307             imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
308
309             if( psz_if_addr != NULL && *psz_if_addr
310                 && inet_addr(psz_if_addr) != INADDR_NONE )
311             {
312                 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
313             }
314             else
315             {
316                 imr.imr_interface.s_addr = INADDR_ANY;
317             }
318             if( psz_if_addr != NULL ) free( psz_if_addr );
319
320             msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
321             /* Join Multicast group with source filter */
322             if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
323                          (char*)&imr,
324                          sizeof(struct ip_mreq_source) ) == -1 )
325             {
326 #ifdef HAVE_ERRNO_H
327                 msg_Err( p_this, "failed to join IP multicast group (%s)",
328                                   strerror(errno) );
329                 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
330 #else
331                 msg_Err( p_this, "failed to join IP multicast group" );
332                 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
333 #endif
334                 close( i_handle );
335                 return( -1 );
336             }
337          }
338          /* If there is no source address, we use IP_ADD_MEMBERSHIP */
339          else
340          {
341              struct ip_mreq imr;
342
343              imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
344              if( psz_if_addr != NULL && *psz_if_addr
345                 && inet_addr(psz_if_addr) != INADDR_NONE )
346             {
347                 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
348             }
349             else
350             {
351                 imr.imr_interface.s_addr = INADDR_ANY;
352             }
353             if( psz_if_addr != NULL ) free( psz_if_addr );
354
355             msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
356             /* Join Multicast group without source filter */
357             if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
358                             (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
359             {
360 #ifdef HAVE_ERRNO_H
361                 msg_Err( p_this, "failed to join IP multicast group (%s)",
362                                   strerror(errno) );
363 #else
364                 msg_Err( p_this, "failed to join IP multicast group" );
365 #endif
366                 close( i_handle );
367                 return( -1 );
368             }
369          }
370     }
371 #endif
372
373     if( *psz_server_addr )
374     {
375         /* Build socket for remote connection */
376         if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
377         {
378             msg_Warn( p_this, "cannot build remote address" );
379             close( i_handle );
380             return( -1 );
381         }
382
383         /* Connect the socket */
384         if( connect( i_handle, (struct sockaddr *) &sock,
385                      sizeof( sock ) ) == (-1) )
386         {
387 #ifdef HAVE_ERRNO_H
388             msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
389 #else
390             msg_Warn( p_this, "cannot connect socket" );
391 #endif
392             close( i_handle );
393             return( -1 );
394         }
395
396 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
397         if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
398         {
399             /* set the time-to-live */
400             int ttl = p_socket->i_ttl;
401             if( ttl < 1 )
402             {
403                 ttl = config_GetInt( p_this, "ttl" );
404             }
405             if( ttl < 1 ) ttl = 1;
406
407             if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
408                             (void *) &ttl, sizeof( ttl ) ) < 0 )
409             {
410 #ifdef HAVE_ERRNO_H
411                 msg_Err( p_this, "failed to set ttl (%s)", strerror(errno) );
412 #else
413                 msg_Err( p_this, "failed to set ttl" );
414 #endif
415                 close( i_handle );
416                 return( -1 );
417             }
418         }
419 #endif
420     }
421
422     p_socket->i_handle = i_handle;
423     p_socket->i_mtu = config_GetInt( p_this, "mtu" );
424     return( 0 );
425 }
426
427 /*****************************************************************************
428  * OpenTCP: open a TCP socket
429  *****************************************************************************
430  * psz_server_addr, i_server_port : address and port used for the connect()
431  *   system call. If i_server_port == 0, 80 is used.
432  * Other parameters are ignored.
433  * This function returns -1 in case of error.
434  *****************************************************************************/
435 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
436 {
437     char * psz_server_addr = p_socket->psz_server_addr;
438     int i_server_port = p_socket->i_server_port;
439
440     int i_handle;
441     struct sockaddr_in sock;
442
443     if( i_server_port == 0 )
444     {
445         i_server_port = 80;
446     }
447
448     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
449      * protocol */
450     if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
451     {
452 #ifdef HAVE_ERRNO_H
453         msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
454 #else
455         msg_Warn( p_this, "cannot create socket" );
456 #endif
457         goto error;
458     }
459
460     /* Build remote address */
461     if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
462     {
463         msg_Dbg( p_this, "could not build local address" );
464         goto error;
465     }
466
467     /* Set to non-blocking */
468 #if defined( WIN32 ) || defined( UNDER_CE )
469     {
470         unsigned long i_dummy = 1;
471         if( ioctlsocket( i_handle, FIONBIO, &i_dummy ) != 0 )
472         {
473             msg_Err( p_this, "cannot set socket to non-blocking mode" );
474         }
475     }
476 #elif defined( HAVE_ERRNO_H )
477     {
478         int i_flags;
479         if( ( i_flags = fcntl( i_handle, F_GETFL, 0 ) ) < 0 ||
480             fcntl( i_handle, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
481         {
482             msg_Err( p_this, "cannot set socket to non-blocking mode" );
483         }
484     }
485 #endif
486
487     /* Connect the socket */
488     if( connect( i_handle, (struct sockaddr *) &sock, sizeof( sock ) ) == -1 )
489     {
490 #if defined( WIN32 ) || defined( UNDER_CE )
491         if( WSAGetLastError() == WSAEWOULDBLOCK )
492 #elif defined( HAVE_ERRNO_H )
493         if( errno == EINPROGRESS )
494 #else
495         if( 0 )
496 #endif
497         {
498             int i_ret;
499             int i_opt;
500             int i_opt_size = sizeof( i_opt );
501             struct timeval  timeout;
502             fd_set          fds;
503
504             msg_Dbg( p_this, "connection in progress" );
505             do
506             {
507                 if( p_this->b_die )
508                 {
509                     msg_Dbg( p_this, "connection aborted" );
510                     goto error;
511                 }
512
513                 /* Initialize file descriptor set */
514                 FD_ZERO( &fds );
515                 FD_SET( i_handle, &fds );
516
517                 /* We'll wait 0.1 second if nothing happens */
518                 timeout.tv_sec = 0;
519                 timeout.tv_usec = 100000;
520
521             } while( ( i_ret = select( i_handle + 1, NULL, &fds, NULL,
522                                        &timeout ) ) == 0 ||
523 #if defined( WIN32 ) || defined( UNDER_CE )
524                      ( i_ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK ) );
525 #elif defined( HAVE_ERRNO_H )
526                      ( i_ret < 0 && errno == EINTR ) );
527 #else
528                      ( i_ret < 0 ) );
529 #endif
530
531             if( i_ret < 0 )
532             {
533                 msg_Warn( p_this, "cannot connect socket (select failed)" );
534                 goto error;
535             }
536
537 #if !defined( SYS_BEOS )
538             if( getsockopt( i_handle, SOL_SOCKET, SO_ERROR, (void*)&i_opt,
539                             &i_opt_size ) == -1 || i_opt != 0 )
540             {
541                 msg_Warn( p_this, "cannot connect socket (SO_ERROR)" );
542                 goto error;
543             }
544 #endif
545         }
546         else
547         {
548 #if defined( HAVE_ERRNO_H )
549             msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
550 #else
551             msg_Warn( p_this, "cannot connect socket" );
552 #endif
553             goto error;
554         }
555     }
556
557     p_socket->i_handle = i_handle;
558     p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
559     return VLC_SUCCESS;
560
561 error:
562     if( i_handle > 0 )
563     {
564         close( i_handle );
565     }
566     return VLC_EGENERIC;
567 }
568
569 /*****************************************************************************
570  * NetOpen: wrapper around OpenUDP and OpenTCP
571  *****************************************************************************/
572 static int NetOpen( vlc_object_t * p_this )
573 {
574     network_socket_t * p_socket = p_this->p_private;
575
576     if( p_socket->i_type == NETWORK_UDP )
577     {
578         return OpenUDP( p_this, p_socket );
579     }
580     else
581     {
582         return OpenTCP( p_this, p_socket );
583     }
584 }