1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ipv4.c,v 1.12 2002/05/04 15:49:56 sam Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Mathias Kretschmer <mathias@research.att.com>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <sys/types.h>
35 #include <videolan/vlc.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
44 # include <winsock2.h>
45 # include <ws2tcpip.h>
47 # define IN_MULTICAST(a) IN_CLASSD(a)
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() */
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
64 /*****************************************************************************
66 *****************************************************************************/
67 static void getfunctions( function_list_t * );
68 static int NetworkOpen( struct network_socket_s * );
70 /*****************************************************************************
71 * Build configuration tree.
72 *****************************************************************************/
77 SET_DESCRIPTION( _("IPv4 network abstraction layer") )
78 ADD_CAPABILITY( NETWORK, 50 )
79 ADD_SHORTCUT( "ipv4" )
83 getfunctions( &p_module->p_functions->network );
86 MODULE_DEACTIVATE_START
87 MODULE_DEACTIVATE_STOP
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 )
95 #define f p_function_list->functions.network
96 f.pf_open = NetworkOpen;
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 )
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 );
112 p_socket->sin_addr.s_addr = INADDR_ANY;
116 struct hostent * p_hostent;
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 ) )
123 if( (p_socket->sin_addr.s_addr = inet_addr( psz_address )) == -1 )
126 /* We have a fqdn, try to find its address */
127 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
129 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_address );
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 );
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 == "", 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 )
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;
161 char * psz_bind_win32; /* WIN32 multicast kludge */
164 int i_handle, i_opt, i_opt_size;
165 struct sockaddr_in sock;
167 if( i_bind_port == 0 )
169 i_bind_port = config_GetIntVariable( "server-port" );
172 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
174 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
176 intf_ErrMsg( "ipv4 error: cannot create socket (%s)", strerror(errno) );
180 /* We may want to reuse an already used socket */
182 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
183 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
185 intf_ErrMsg( "ipv4 error: cannot configure socket (SO_REUSEADDR: %s)",
191 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
192 * packet loss caused by scheduling problems */
194 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
195 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
198 "ipv4 warning: cannot configure socket (SO_RCVBUF: %s)",
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 */
206 i_opt_size = sizeof( i_opt );
207 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
208 (void*) &i_opt, &i_opt_size ) == -1 )
210 intf_WarnMsg( 1, "ipv4 warning: cannot query socket (SO_RCVBUF: %s)",
213 else if( i_opt < 0x80000 )
215 intf_WarnMsg( 1, "ipv4 warning: socket buffer size is 0x%x"
216 " instead of 0x%x", i_opt, 0x80000 );
220 /* Build the local socket */
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 ;
227 /* Check if this is a multicast socket */
228 if (IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
232 if ( BuildAddr( &sock, psz_bind_win32, i_bind_port ) == -1 )
234 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
242 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
244 intf_ErrMsg( "ipv4 error: cannot bind socket (%s)", strerror(errno) );
249 /* Allow broadcast reception if we bound on INADDR_ANY */
250 if( !*psz_bind_addr )
253 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
254 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
257 "ipv4 warning: cannot configure socket (SO_BROADCAST: %s)",
262 /* Join the multicast group if the socket is a multicast address */
264 # define IN_MULTICAST(a) IN_CLASSD(a)
268 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
271 imr.imr_interface.s_addr = INADDR_ANY;
272 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
274 if( IN_MULTICAST( ntohl(inet_addr(psz_bind_addr) ) ) )
277 imr.imr_interface.s_addr = INADDR_ANY;
278 imr.imr_multiaddr.s_addr = inet_addr(psz_bind_addr);
280 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
281 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
283 intf_ErrMsg( "ipv4 error: failed to join IP multicast group (%s)",
290 if( *psz_server_addr )
292 /* Build socket for remote connection */
293 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
295 intf_ErrMsg( "ipv4 error: cannot build remote address" );
300 /* Connect the socket */
301 if( connect( i_handle, (struct sockaddr *) &sock,
302 sizeof( sock ) ) == (-1) )
304 intf_ErrMsg( "ipv4 error: cannot connect socket (%s)",
311 p_socket->i_handle = i_handle;
312 p_socket->i_mtu = DEFAULT_MTU;
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 )
326 char * psz_server_addr = p_socket->psz_server_addr;
327 int i_server_port = p_socket->i_server_port;
330 struct sockaddr_in sock;
332 if( i_server_port == 0 )
337 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
339 if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
341 intf_ErrMsg( "ipv4 error: cannot create socket (%s)", strerror(errno) );
345 /* Build remote address */
346 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
352 /* Connect the socket */
353 if( connect( i_handle, (struct sockaddr *) &sock,
354 sizeof( sock ) ) == (-1) )
356 intf_ErrMsg( "ipv4 error: cannot connect socket (%s)",
362 p_socket->i_handle = i_handle;
363 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
368 /*****************************************************************************
369 * NetworkOpen: wrapper around OpenUDP and OpenTCP
370 *****************************************************************************/
371 static int NetworkOpen( network_socket_t * p_socket )
373 if( p_socket->i_type == NETWORK_UDP )
375 return OpenUDP( p_socket );
379 return OpenTCP( p_socket );