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