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