]> git.sesse.net Git - vlc/blob - plugins/network/ipv4.c
4a875438571402458b3b4c70b86cd695eb6e148b
[vlc] / plugins / network / ipv4.c
1 /*****************************************************************************
2  * ipv4.c: IPv4 network abstraction layer
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: ipv4.c,v 1.6 2002/03/15 04:41:54 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 <sys/types.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <fcntl.h>
34
35 #include <videolan/vlc.h>
36
37 #ifdef HAVE_UNISTD_H
38 #   include <unistd.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
40 #   include <io.h>
41 #endif
42
43 #ifdef WIN32
44 #   include <winsock2.h>
45 #   include <ws2tcpip.h>
46 #elif !defined( SYS_BEOS ) && !defined( SYS_NTO )
47 #   include <netdb.h>                                         /* hostent ... */
48 #   include <sys/socket.h>
49 #   include <netinet/in.h>
50 #   ifdef HAVE_ARPA_INET_H
51 #       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
52 #   endif
53 #endif
54
55 #include "network.h"
56
57 /* Default MTU used for UDP socket. FIXME: we should issue some ioctl()
58  * call to get that value from the interface driver. */
59 #define DEFAULT_MTU 1500
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static void getfunctions( function_list_t * );
65 static int  NetworkOpen( struct network_socket_s * );
66
67 /*****************************************************************************
68  * Build configuration tree.
69  *****************************************************************************/
70 MODULE_CONFIG_START
71 MODULE_CONFIG_STOP
72  
73 MODULE_INIT_START
74     SET_DESCRIPTION( "IPv4 network abstraction layer" )
75     ADD_CAPABILITY( NETWORK, 50 )
76     ADD_SHORTCUT( "ipv4" )
77 MODULE_INIT_STOP
78  
79 MODULE_ACTIVATE_START
80     getfunctions( &p_module->p_functions->network );
81 MODULE_ACTIVATE_STOP
82  
83 MODULE_DEACTIVATE_START
84 MODULE_DEACTIVATE_STOP
85
86 /*****************************************************************************
87  * Functions exported as capabilities. They are declared as static so that
88  * we don't pollute the namespace too much.
89  *****************************************************************************/
90 static void getfunctions( function_list_t * p_function_list )
91 {
92 #define f p_function_list->functions.network
93     f.pf_open = NetworkOpen;
94 #undef f
95 }
96
97 /*****************************************************************************
98  * BuildAddr: utility function to build a struct sockaddr_in
99  *****************************************************************************/
100 static int BuildAddr( struct sockaddr_in * p_socket,
101                       const char * psz_address, int i_port )
102 {
103     /* Reset struct */
104     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
105     p_socket->sin_family = AF_INET;                                /* family */
106     p_socket->sin_port = htons( i_port );
107     if( !*psz_address )
108     {
109         p_socket->sin_addr.s_addr = INADDR_ANY;
110     }
111     else
112     {
113         struct hostent    * p_hostent;
114  
115         /* Try to convert address directly from in_addr - this will work if
116          * psz_address is dotted decimal. */
117 #ifdef HAVE_ARPA_INET_H
118         if( !inet_aton( psz_address, &p_socket->sin_addr ) )
119 #else
120         if( (p_socket->sin_addr.s_addr = inet_addr( psz_address )) == -1 )
121 #endif
122         {
123             /* We have a fqdn, try to find its address */
124             if ( (p_hostent = gethostbyname( psz_address )) == NULL )
125             {
126                 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_address );
127                 return( -1 );
128             }
129
130             /* Copy the first address of the host in the socket address */
131             memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
132                      p_hostent->h_length );
133         }
134     }
135     return( 0 );
136 }
137
138 /*****************************************************************************
139  * OpenUDP: open a UDP socket
140  *****************************************************************************
141  * psz_bind_addr, i_bind_port : address and port used for the bind()
142  *   system call. If psz_bind_addr == NULL, the socket is bound to
143  *   INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
144  *   1234 is used. If psz_bind_addr is a multicast (class D) address,
145  *   join the multicast group.
146  * psz_server_addr, i_server_port : address and port used for the connect()
147  *   system call. It can avoid receiving packets from unauthorized IPs.
148  *   Its use leads to great confusion and is currently discouraged.
149  * This function returns -1 in case of error.
150  *****************************************************************************/
151 static int OpenUDP( network_socket_t * p_socket )
152 {
153     char * psz_bind_addr = p_socket->psz_bind_addr;
154     int i_bind_port = p_socket->i_bind_port;
155     char * psz_server_addr = p_socket->psz_server_addr;
156     int i_server_port = p_socket->i_server_port;
157 #ifdef WIN32
158     char * psz_bind_win32;        /* WIN32 multicast kludge */
159 #endif
160
161     int i_handle, i_opt, i_opt_size;
162     struct sockaddr_in sock;
163
164     if( i_bind_port == 0 )
165     {
166         i_bind_port = config_GetIntVariable( "server_port" );
167     }
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         intf_ErrMsg( "ipv4 error: cannot create socket (%s)", strerror(errno) );
174         return( -1 );
175     }
176
177     /* We may want to reuse an already used socket */
178     i_opt = 1;
179     if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
180                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
181     {
182         intf_ErrMsg( "ipv4 error: cannot configure socket (SO_REUSEADDR: %s)",
183                      strerror(errno));
184         close( i_handle );
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( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
192                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
193     {
194         intf_WarnMsg( 1,
195                       "ipv4 warning: cannot configure socket (SO_RCVBUF: %s)",
196                       strerror(errno));
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( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
205                     (void*) &i_opt, &i_opt_size ) == -1 )
206     {
207         intf_WarnMsg( 1, "ipv4 warning: cannot query socket (SO_RCVBUF: %s)",
208                          strerror(errno));
209     }
210     else if( i_opt < 0x80000 )
211     {
212         intf_WarnMsg( 1, "ipv4 warning: socket buffer size is 0x%x"
213                          " instead of 0x%x", i_opt, 0x80000 );
214     }
215     
216 /* Under Win32 and for the multicast, we bind on INADDR_ANY, so let's call BuildAddr with NULL instead of psz_bind_addr */
217     
218     /* Build the local socket */
219
220 #ifdef WIN32
221     
222 #ifndef IN_MULTICAST
223 #   define IN_MULTICAST(a)         IN_CLASSD(a)
224 #endif
225
226     psz_bind_win32 = psz_bind_addr ;
227     
228 /* Check if this is a multicast socket */
229
230     if (IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
231     {
232         psz_bind_win32 = "";
233     }
234     if ( BuildAddr( &sock, psz_bind_win32, i_bind_port ) == -1 )
235 #else
236     if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )        
237 #endif    
238     {
239         close( i_handle );
240         return( -1 );
241     }
242  
243     /* Bind it */
244     if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
245     {
246         intf_ErrMsg( "ipv4 error: cannot bind socket (%s)", strerror(errno) );
247         close( i_handle );
248         return( -1 );
249     }
250
251     /* Allow broadcast reception if we bound on INADDR_ANY */
252     if( !*psz_bind_addr )
253     {
254         i_opt = 1;
255         if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
256                         (void*) &i_opt, sizeof( i_opt ) ) == -1 )
257         {
258             intf_WarnMsg( 1,
259                     "ipv4 warning: cannot configure socket (SO_BROADCAST: %s)",
260                     strerror(errno));
261         }
262     }
263  
264     /* Join the multicast group if the socket is a multicast address */
265 #ifndef IN_MULTICAST
266 #   define IN_MULTICAST(a)         IN_CLASSD(a)
267 #endif
268
269 #ifndef WIN32
270     if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
271     {
272         struct ip_mreq imr;
273         imr.imr_interface.s_addr = INADDR_ANY;
274         imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
275 #else
276     if( IN_MULTICAST( ntohl(inet_addr(psz_bind_addr) ) ) )
277     {
278         struct ip_mreq imr;
279         imr.imr_interface.s_addr = INADDR_ANY;
280         imr.imr_multiaddr.s_addr = inet_addr(psz_bind_addr);
281 #endif                
282         if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
283                         (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
284         {
285             intf_ErrMsg( "ipv4 error: failed to join IP multicast group (%s)",
286                          strerror(errno) );
287             close( i_handle );
288             return( -1 );
289         }
290     }
291
292     if( *psz_server_addr )
293     {
294         /* Build socket for remote connection */
295         if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
296         {
297             intf_ErrMsg( "ipv4 error: cannot build remote address" );
298             close( i_handle );
299             return( -1 );
300         }
301  
302         /* Connect the socket */
303         if( connect( i_handle, (struct sockaddr *) &sock,
304                      sizeof( sock ) ) == (-1) )
305         {
306             intf_ErrMsg( "ipv4 error: cannot connect socket (%s)",
307                          strerror(errno) );
308             close( i_handle );
309             return( -1 );
310         }
311     }
312
313     p_socket->i_handle = i_handle;
314     p_socket->i_mtu = DEFAULT_MTU;
315     return( 0 );
316 }
317
318 /*****************************************************************************
319  * OpenTCP: open a TCP socket
320  *****************************************************************************
321  * psz_server_addr, i_server_port : address and port used for the connect()
322  *   system call. If i_server_port == 0, 80 is used.
323  * Other parameters are ignored.
324  * This function returns -1 in case of error.
325  *****************************************************************************/
326 static int OpenTCP( network_socket_t * p_socket )
327 {
328     char * psz_server_addr = p_socket->psz_server_addr;
329     int i_server_port = p_socket->i_server_port;
330
331     int i_handle;
332     struct sockaddr_in sock;
333
334     if( i_server_port == 0 )
335     {
336         i_server_port = 80;
337     }
338
339     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
340      * protocol */
341     if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
342     {
343         intf_ErrMsg( "ipv4 error: cannot create socket (%s)", strerror(errno) );
344         return( -1 );
345     }
346
347     /* Build remote address */
348     if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
349     {
350         close( i_handle );
351         return( -1 );
352     }
353
354     /* Connect the socket */
355     if( connect( i_handle, (struct sockaddr *) &sock,
356                  sizeof( sock ) ) == (-1) )
357     {
358         intf_ErrMsg( "ipv4 error: cannot connect socket (%s)",
359                      strerror(errno) );
360         close( i_handle );
361         return( -1 );
362     }
363
364     p_socket->i_handle = i_handle;
365     p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
366
367     return( 0 );
368 }
369
370 /*****************************************************************************
371  * NetworkOpen: wrapper around OpenUDP and OpenTCP
372  *****************************************************************************/
373 static int NetworkOpen( network_socket_t * p_socket )
374 {
375     if( p_socket->i_type == NETWORK_UDP )
376     {
377         return OpenUDP( p_socket );
378     }
379     else
380     {
381         return OpenTCP( p_socket );
382     }
383 }