]> git.sesse.net Git - vlc/blob - modules/misc/network/ipv4.c
* modules/misc/network/ipv4.c: a bit of cleanup.
[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.18 2003/04/21 16:22:43 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 #   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_Err( p_this, "cannot create socket (%s)", strerror(errno) );
161 #else
162         msg_Err( 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_Err( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
174                           strerror(errno));
175 #else
176         msg_Err( 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,
187 #else
188     if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
189 #endif
190                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
191     {
192 #ifdef HAVE_ERRNO_H
193         msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
194                           strerror(errno));
195 #else
196         msg_Warn( p_this, "cannot configure socket (SO_RCVBUF)" );
197 #endif
198     }
199
200     /* Check if we really got what we have asked for, because Linux, etc.
201      * will silently limit the max buffer size to net.core.rmem_max which
202      * is typically only 65535 bytes */
203     i_opt = 0;
204     i_opt_size = sizeof( i_opt );
205 #if defined( SYS_BEOS )
206     if( getsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
207 #else
208     if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
209 #endif
210                     (void*) &i_opt, &i_opt_size ) == -1 )
211     {
212 #ifdef HAVE_ERRNO_H
213         msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
214                           strerror(errno) );
215 #else
216         msg_Warn( p_this, "cannot query socket (SO_RCVBUF)" );
217 #endif
218     }
219     else if( i_opt < 0x80000 )
220     {
221         msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
222                          i_opt, 0x80000 );
223     }
224
225
226     /* Build the local socket */
227
228 #if defined( WIN32 ) && !defined( UNDER_CE )
229     /* Under Win32 and for multicasting, we bind to INADDR_ANY,
230      * so let's call BuildAddr with "" instead of psz_bind_addr */
231     if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
232                    "" : psz_bind_addr, i_bind_port ) == -1 )
233 #else
234     if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
235 #endif
236     {
237         msg_Dbg( p_this, "could not build local address" );
238         close( i_handle );
239         return( -1 );
240     }
241
242     /* Bind it */
243     if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
244     {
245 #ifdef HAVE_ERRNO_H
246         msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
247 #else
248         msg_Err( p_this, "cannot bind socket" );
249 #endif
250         close( i_handle );
251         return( -1 );
252     }
253
254 #if defined( WIN32 ) && !defined( UNDER_CE )
255     /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
256     if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
257     {
258         if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
259         {
260             msg_Dbg( p_this, "could not build local address" );
261             close( i_handle );
262             return( -1 );
263         }
264     }
265 #endif
266
267     /* Allow broadcast reception if we bound on INADDR_ANY */
268     if( !*psz_bind_addr )
269     {
270         i_opt = 1;
271 #if defined( SYS_BEOS )
272         if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
273 #else
274         if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
275 #endif
276                         (void*) &i_opt, sizeof( i_opt ) ) == -1 )
277         {
278 #ifdef HAVE_ERRNO_H
279             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
280                        strerror(errno) );
281 #else
282             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST)" );
283 #endif
284         }
285     }
286
287 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
288     /* Join the multicast group if the socket is a multicast address */
289     if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
290     {
291         struct ip_mreq imr;
292
293         /* Determine interface to be used for multicast */
294         char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
295         imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
296         if( psz_if_addr != NULL && *psz_if_addr
297             && inet_addr(psz_if_addr) != INADDR_NONE )
298         {
299             imr.imr_interface.s_addr = inet_addr(psz_if_addr);
300         }
301         else
302         {
303             imr.imr_interface.s_addr = INADDR_ANY;
304         }
305         if( psz_if_addr != NULL ) free( psz_if_addr );
306
307         /* Join Multicast group */
308         if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
309                         (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
310         {
311 #ifdef HAVE_ERRNO_H
312             msg_Warn( p_this, "failed to join IP multicast group (%s)",
313                               strerror(errno) );
314 #else
315             msg_Warn( p_this, "failed to join IP multicast group" );
316 #endif
317             close( i_handle );
318             return( -1 );
319         }
320     }
321 #endif /* UNDER_CE, SYS_BEOS */
322
323     if( *psz_server_addr )
324     {
325         /* Build socket for remote connection */
326         if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
327         {
328             msg_Err( p_this, "cannot build remote address" );
329             close( i_handle );
330             return( -1 );
331         }
332
333         /* Connect the socket */
334         if( connect( i_handle, (struct sockaddr *) &sock,
335                      sizeof( sock ) ) == (-1) )
336         {
337 #ifdef HAVE_ERRNO_H
338             msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
339 #else
340             msg_Err( p_this, "cannot connect socket" );
341 #endif
342             close( i_handle );
343             return( -1 );
344         }
345
346 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
347         if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
348         {
349             /* set the time-to-live */
350             int ttl = config_GetInt( p_this, "ttl" );
351             if( ttl < 1 ) ttl = 1;
352
353             if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
354                             (void *) &ttl, sizeof( ttl ) ) < 0 )
355             {
356 #ifdef HAVE_ERRNO_H
357                 msg_Warn( p_this, "failed to set ttl (%s)", strerror(errno) );
358 #else
359                 msg_Warn( p_this, "failed to set ttl" );
360 #endif
361                 close( i_handle );
362                 return( -1 );
363             }
364         }
365 #endif /* UNDER_CE, SYS_BEOS */
366     }
367
368     p_socket->i_handle = i_handle;
369     p_socket->i_mtu = config_GetInt( p_this, "mtu" );
370     return( 0 );
371 }
372
373 /*****************************************************************************
374  * OpenTCP: open a TCP socket
375  *****************************************************************************
376  * psz_server_addr, i_server_port : address and port used for the connect()
377  *   system call. If i_server_port == 0, 80 is used.
378  * Other parameters are ignored.
379  * This function returns -1 in case of error.
380  *****************************************************************************/
381 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
382 {
383     char * psz_server_addr = p_socket->psz_server_addr;
384     int i_server_port = p_socket->i_server_port;
385
386     int i_handle;
387     struct sockaddr_in sock;
388
389     if( i_server_port == 0 )
390     {
391         i_server_port = 80;
392     }
393
394     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
395      * protocol */
396     if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
397     {
398 #ifdef HAVE_ERRNO_H
399         msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
400 #else
401         msg_Err( p_this, "cannot create socket" );
402 #endif
403         return( -1 );
404     }
405
406     /* Build remote address */
407     if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
408     {
409         msg_Dbg( p_this, "could not build local address" );
410         close( i_handle );
411         return( -1 );
412     }
413
414     /* Connect the socket */
415     if( connect( i_handle, (struct sockaddr *) &sock,
416                  sizeof( sock ) ) == (-1) )
417     {
418 #ifdef HAVE_ERRNO_H
419         msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
420 #else
421         msg_Err( p_this, "cannot connect socket" );
422 #endif
423         close( i_handle );
424         return( -1 );
425     }
426
427     p_socket->i_handle = i_handle;
428     p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
429
430     return( 0 );
431 }
432
433 /*****************************************************************************
434  * NetOpen: wrapper around OpenUDP and OpenTCP
435  *****************************************************************************/
436 static int NetOpen( vlc_object_t * p_this )
437 {
438     network_socket_t * p_socket = p_this->p_private;
439
440     if( p_socket->i_type == NETWORK_UDP )
441     {
442         return OpenUDP( p_this, p_socket );
443     }
444     else
445     {
446         return OpenTCP( p_this, p_socket );
447     }
448 }