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