]> git.sesse.net Git - vlc/blob - modules/misc/network/ipv4.c
ec0e589316affb28fbd72bc68c0699d25e84dbea
[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.17 2003/03/24 17:15:30 gbazin 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 #   ifndef IN_MULTICAST
55 #       define IN_MULTICAST(a) IN_CLASSD(a)
56 #   endif
57 #else
58 #   include <netdb.h>                                         /* hostent ... */
59 #   include <sys/socket.h>
60 #   include <netinet/in.h>
61 #   ifdef HAVE_ARPA_INET_H
62 #       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
63 #   endif
64 #endif
65
66 #include "network.h"
67
68 #ifndef INADDR_ANY
69 #   define INADDR_ANY  0x00000000
70 #endif
71 #ifndef INADDR_NONE
72 #   define INADDR_NONE 0xFFFFFFFF
73 #endif
74
75 /*****************************************************************************
76  * Local prototypes
77  *****************************************************************************/
78 static int NetOpen( vlc_object_t * );
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83 vlc_module_begin();
84     set_description( _("IPv4 network abstraction layer") );
85     set_capability( "network", 50 );
86     set_callbacks( NetOpen, NULL );
87 vlc_module_end();
88
89 /*****************************************************************************
90  * BuildAddr: utility function to build a struct sockaddr_in
91  *****************************************************************************/
92 static int BuildAddr( struct sockaddr_in * p_socket,
93                       const char * psz_address, int i_port )
94 {
95     /* Reset struct */
96     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
97     p_socket->sin_family = AF_INET;                                /* family */
98     p_socket->sin_port = htons( (uint16_t)i_port );
99     if( !*psz_address )
100     {
101         p_socket->sin_addr.s_addr = INADDR_ANY;
102     }
103     else
104     {
105         struct hostent    * p_hostent;
106
107         /* Try to convert address directly from in_addr - this will work if
108          * psz_address is dotted decimal. */
109 #ifdef HAVE_ARPA_INET_H
110         if( !inet_aton( psz_address, &p_socket->sin_addr ) )
111 #else
112         p_socket->sin_addr.s_addr = inet_addr( psz_address );
113         if( p_socket->sin_addr.s_addr == INADDR_NONE )
114 #endif
115         {
116             /* We have a fqdn, try to find its address */
117             if ( (p_hostent = gethostbyname( psz_address )) == NULL )
118             {
119                 return( -1 );
120             }
121
122             /* Copy the first address of the host in the socket address */
123             memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
124                      p_hostent->h_length );
125         }
126     }
127     return( 0 );
128 }
129
130 /*****************************************************************************
131  * OpenUDP: open a UDP socket
132  *****************************************************************************
133  * psz_bind_addr, i_bind_port : address and port used for the bind()
134  *   system call. If psz_bind_addr == "", the socket is bound to
135  *   INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
136  *   1234 is used. If psz_bind_addr is a multicast (class D) address,
137  *   join the multicast group.
138  * psz_server_addr, i_server_port : address and port used for the connect()
139  *   system call. It can avoid receiving packets from unauthorized IPs.
140  *   Its use leads to great confusion and is currently discouraged.
141  * This function returns -1 in case of error.
142  *****************************************************************************/
143 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
144 {
145     char * psz_bind_addr = p_socket->psz_bind_addr;
146     int i_bind_port = p_socket->i_bind_port;
147     char * psz_server_addr = p_socket->psz_server_addr;
148     int i_server_port = p_socket->i_server_port;
149 #if defined( WIN32 ) && !defined( UNDER_CE )
150     char * psz_bind_win32;        /* WIN32 multicast kludge */
151 #endif
152
153     int i_handle, i_opt;
154     socklen_t i_opt_size;
155     struct sockaddr_in sock;
156
157     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
158      * protocol */
159     if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
160     {
161 #ifdef HAVE_ERRNO_H
162         msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
163 #else
164         msg_Err( p_this, "cannot create socket" );
165 #endif
166         return( -1 );
167     }
168
169     /* We may want to reuse an already used socket */
170     i_opt = 1;
171     if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
172                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
173     {
174 #ifdef HAVE_ERRNO_H
175         msg_Err( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
176                           strerror(errno));
177 #else
178         msg_Err( p_this, "cannot configure socket (SO_REUSEADDR)" );
179 #endif
180 #if defined( WIN32 ) || defined( UNDER_CE )
181         closesocket( i_handle );
182 #else
183         close( i_handle );
184 #endif
185         return( -1 );
186     }
187
188     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
189      * packet loss caused by scheduling problems */
190     i_opt = 0x80000;
191 #if defined( SYS_BEOS )
192     if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
193 #else
194     if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
195 #endif
196                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
197     {
198 #ifdef HAVE_ERRNO_H
199         msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
200                           strerror(errno));
201 #else
202         msg_Warn( p_this, "cannot configure socket (SO_RCVBUF)" );
203 #endif
204     }
205
206     /* Check if we really got what we have asked for, because Linux, etc.
207      * will silently limit the max buffer size to net.core.rmem_max which
208      * is typically only 65535 bytes */
209     i_opt = 0;
210     i_opt_size = sizeof( i_opt );
211 #if defined( SYS_BEOS )
212     if( getsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
213 #else
214     if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
215 #endif
216                     (void*) &i_opt, &i_opt_size ) == -1 )
217     {
218 #ifdef HAVE_ERRNO_H
219         msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
220                           strerror(errno) );
221 #else
222         msg_Warn( p_this, "cannot query socket (SO_RCVBUF)" );
223 #endif
224     }
225     else if( i_opt < 0x80000 )
226     {
227         msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
228                          i_opt, 0x80000 );
229     }
230
231
232     /* Build the local socket */
233
234 #if defined( WIN32 ) && !defined( UNDER_CE )
235     /* Under Win32 and for the multicast, we bind on INADDR_ANY,
236      * so let's call BuildAddr with "" instead of psz_bind_addr */
237     psz_bind_win32 = psz_bind_addr ;
238
239     /* Check if this is a multicast socket */
240     if (IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
241     {
242         psz_bind_win32 = "";
243     }
244     if ( BuildAddr( &sock, psz_bind_win32, 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 #if defined( WIN32 ) || defined( UNDER_CE )
251         closesocket( i_handle );
252 #else
253         close( i_handle );
254 #endif
255         return( -1 );
256     }
257
258     /* Bind it */
259     if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
260     {
261 #ifdef HAVE_ERRNO_H
262         msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
263 #else
264         msg_Err( p_this, "cannot bind socket" );
265 #endif
266 #if defined( WIN32 ) || defined( UNDER_CE )
267         closesocket( i_handle );
268 #else
269         close( i_handle );
270 #endif
271         return( -1 );
272     }
273
274     /* Allow broadcast reception if we bound on INADDR_ANY */
275     if( !*psz_bind_addr )
276     {
277         i_opt = 1;
278 #if defined( SYS_BEOS )
279         if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
280 #else
281         if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
282 #endif
283                         (void*) &i_opt, sizeof( i_opt ) ) == -1 )
284         {
285 #ifdef HAVE_ERRNO_H
286             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
287                        strerror(errno) );
288 #else
289             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST)" );
290 #endif
291         }
292     }
293
294 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
295     /* Join the multicast group if the socket is a multicast address */
296 #ifndef IN_MULTICAST
297 #   define IN_MULTICAST(a)         IN_CLASSD(a)
298 #endif
299
300 #ifndef WIN32
301     if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
302     {
303         struct ip_mreq imr;
304         char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
305         imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
306 #else
307     if( IN_MULTICAST( ntohl(inet_addr(psz_bind_addr) ) ) )
308     {
309         struct ip_mreq imr;
310         char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
311         imr.imr_multiaddr.s_addr = inet_addr(psz_bind_addr);
312 #endif
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         if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
325                         (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
326         {
327 #ifdef HAVE_ERRNO_H
328             msg_Warn( p_this, "failed to join IP multicast group (%s)",
329                               strerror(errno) );
330 #else
331             msg_Warn( p_this, "failed to join IP multicast group" );
332 #endif
333 #if defined( WIN32 ) || defined( UNDER_CE )
334             closesocket( i_handle );
335 #else
336             close( i_handle );
337 #endif
338             return( -1 );
339         }
340     }
341 #endif /* UNDER_CE, SYS_BEOS */
342
343     if( *psz_server_addr )
344     {
345         /* Build socket for remote connection */
346         if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
347         {
348             msg_Err( p_this, "cannot build remote address" );
349 #if defined( WIN32 ) || defined( UNDER_CE )
350             closesocket( i_handle );
351 #else
352             close( i_handle );
353 #endif
354             return( -1 );
355         }
356
357         /* Connect the socket */
358         if( connect( i_handle, (struct sockaddr *) &sock,
359                      sizeof( sock ) ) == (-1) )
360         {
361 #ifdef HAVE_ERRNO_H
362             msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
363 #else
364             msg_Err( p_this, "cannot connect socket" );
365 #endif
366 #if defined( WIN32 ) || defined( UNDER_CE )
367             closesocket( i_handle );
368 #else
369             close( i_handle );
370 #endif
371             return( -1 );
372         }
373
374 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
375 #ifndef WIN32
376         if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
377 #else
378         if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
379 #endif
380         {
381             /* set the time-to-live */
382             int ttl = config_GetInt( p_this, "ttl" );
383             if( ttl < 1 )
384                 ttl = 1;
385
386             if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
387                             (void *) &ttl, sizeof( ttl ) ) < 0 )
388             {
389 #ifdef HAVE_ERRNO_H
390                 msg_Warn( p_this, "failed to set ttl (%s)",
391                           strerror(errno) );
392 #else
393                 msg_Warn( p_this, "failed to set ttl" );
394 #endif
395 #if defined( WIN32 ) || defined( UNDER_CE )
396                 closesocket( i_handle );
397 #else
398                 close( i_handle );
399 #endif
400                 return( -1 );
401             }
402         }
403 #endif /* UNDER_CE, SYS_BEOS */
404     }
405
406     p_socket->i_handle = i_handle;
407     p_socket->i_mtu = config_GetInt( p_this, "mtu" );
408     return( 0 );
409 }
410
411 /*****************************************************************************
412  * OpenTCP: open a TCP socket
413  *****************************************************************************
414  * psz_server_addr, i_server_port : address and port used for the connect()
415  *   system call. If i_server_port == 0, 80 is used.
416  * Other parameters are ignored.
417  * This function returns -1 in case of error.
418  *****************************************************************************/
419 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
420 {
421     char * psz_server_addr = p_socket->psz_server_addr;
422     int i_server_port = p_socket->i_server_port;
423
424     int i_handle;
425     struct sockaddr_in sock;
426
427     if( i_server_port == 0 )
428     {
429         i_server_port = 80;
430     }
431
432     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
433      * protocol */
434     if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
435     {
436 #ifdef HAVE_ERRNO_H
437         msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
438 #else
439         msg_Err( p_this, "cannot create socket" );
440 #endif
441         return( -1 );
442     }
443
444     /* Build remote address */
445     if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
446     {
447         msg_Dbg( p_this, "could not build local address" );
448 #if defined( WIN32 ) || defined( UNDER_CE )
449         closesocket( i_handle );
450 #else
451         close( i_handle );
452 #endif
453         return( -1 );
454     }
455
456     /* Connect the socket */
457     if( connect( i_handle, (struct sockaddr *) &sock,
458                  sizeof( sock ) ) == (-1) )
459     {
460 #ifdef HAVE_ERRNO_H
461         msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
462 #else
463         msg_Err( p_this, "cannot connect socket" );
464 #endif
465 #if defined( WIN32 ) || defined( UNDER_CE )
466         closesocket( i_handle );
467 #else
468         close( i_handle );
469 #endif
470         return( -1 );
471     }
472
473     p_socket->i_handle = i_handle;
474     p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
475
476     return( 0 );
477 }
478
479 /*****************************************************************************
480  * NetOpen: wrapper around OpenUDP and OpenTCP
481  *****************************************************************************/
482 static int NetOpen( vlc_object_t * p_this )
483 {
484     network_socket_t * p_socket = p_this->p_private;
485
486     if( p_socket->i_type == NETWORK_UDP )
487     {
488         return OpenUDP( p_this, p_socket );
489     }
490     else
491     {
492         return OpenTCP( p_this, p_socket );
493     }
494 }