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 $
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>
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() */
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
61 /*****************************************************************************
63 *****************************************************************************/
64 static void getfunctions( function_list_t * );
65 static int NetworkOpen( struct network_socket_s * );
67 /*****************************************************************************
68 * Build configuration tree.
69 *****************************************************************************/
74 SET_DESCRIPTION( "IPv4 network abstraction layer" )
75 ADD_CAPABILITY( NETWORK, 50 )
76 ADD_SHORTCUT( "ipv4" )
80 getfunctions( &p_module->p_functions->network );
83 MODULE_DEACTIVATE_START
84 MODULE_DEACTIVATE_STOP
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 )
92 #define f p_function_list->functions.network
93 f.pf_open = NetworkOpen;
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 )
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 );
109 p_socket->sin_addr.s_addr = INADDR_ANY;
113 struct hostent * p_hostent;
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 ) )
120 if( (p_socket->sin_addr.s_addr = inet_addr( psz_address )) == -1 )
123 /* We have a fqdn, try to find its address */
124 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
126 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_address );
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 );
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 )
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;
158 char * psz_bind_win32; /* WIN32 multicast kludge */
161 int i_handle, i_opt, i_opt_size;
162 struct sockaddr_in sock;
164 if( i_bind_port == 0 )
166 i_bind_port = config_GetIntVariable( "server_port" );
169 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
171 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
173 intf_ErrMsg( "ipv4 error: cannot create socket (%s)", strerror(errno) );
177 /* We may want to reuse an already used socket */
179 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
180 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
182 intf_ErrMsg( "ipv4 error: cannot configure socket (SO_REUSEADDR: %s)",
188 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
189 * packet loss caused by scheduling problems */
191 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
192 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
195 "ipv4 warning: cannot configure socket (SO_RCVBUF: %s)",
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 */
203 i_opt_size = sizeof( i_opt );
204 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
205 (void*) &i_opt, &i_opt_size ) == -1 )
207 intf_WarnMsg( 1, "ipv4 warning: cannot query socket (SO_RCVBUF: %s)",
210 else if( i_opt < 0x80000 )
212 intf_WarnMsg( 1, "ipv4 warning: socket buffer size is 0x%x"
213 " instead of 0x%x", i_opt, 0x80000 );
216 /* Under Win32 and for the multicast, we bind on INADDR_ANY, so let's call BuildAddr with NULL instead of psz_bind_addr */
218 /* Build the local socket */
223 # define IN_MULTICAST(a) IN_CLASSD(a)
226 psz_bind_win32 = psz_bind_addr ;
228 /* Check if this is a multicast socket */
230 if (IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
234 if ( BuildAddr( &sock, psz_bind_win32, i_bind_port ) == -1 )
236 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
244 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
246 intf_ErrMsg( "ipv4 error: cannot bind socket (%s)", strerror(errno) );
251 /* Allow broadcast reception if we bound on INADDR_ANY */
252 if( !*psz_bind_addr )
255 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
256 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
259 "ipv4 warning: cannot configure socket (SO_BROADCAST: %s)",
264 /* Join the multicast group if the socket is a multicast address */
266 # define IN_MULTICAST(a) IN_CLASSD(a)
270 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
273 imr.imr_interface.s_addr = INADDR_ANY;
274 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
276 if( IN_MULTICAST( ntohl(inet_addr(psz_bind_addr) ) ) )
279 imr.imr_interface.s_addr = INADDR_ANY;
280 imr.imr_multiaddr.s_addr = inet_addr(psz_bind_addr);
282 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
283 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
285 intf_ErrMsg( "ipv4 error: failed to join IP multicast group (%s)",
292 if( *psz_server_addr )
294 /* Build socket for remote connection */
295 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
297 intf_ErrMsg( "ipv4 error: cannot build remote address" );
302 /* Connect the socket */
303 if( connect( i_handle, (struct sockaddr *) &sock,
304 sizeof( sock ) ) == (-1) )
306 intf_ErrMsg( "ipv4 error: cannot connect socket (%s)",
313 p_socket->i_handle = i_handle;
314 p_socket->i_mtu = DEFAULT_MTU;
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 )
328 char * psz_server_addr = p_socket->psz_server_addr;
329 int i_server_port = p_socket->i_server_port;
332 struct sockaddr_in sock;
334 if( i_server_port == 0 )
339 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
341 if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
343 intf_ErrMsg( "ipv4 error: cannot create socket (%s)", strerror(errno) );
347 /* Build remote address */
348 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
354 /* Connect the socket */
355 if( connect( i_handle, (struct sockaddr *) &sock,
356 sizeof( sock ) ) == (-1) )
358 intf_ErrMsg( "ipv4 error: cannot connect socket (%s)",
364 p_socket->i_handle = i_handle;
365 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
370 /*****************************************************************************
371 * NetworkOpen: wrapper around OpenUDP and OpenTCP
372 *****************************************************************************/
373 static int NetworkOpen( network_socket_t * p_socket )
375 if( p_socket->i_type == NETWORK_UDP )
377 return OpenUDP( p_socket );
381 return OpenTCP( p_socket );