1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ipv4.c,v 1.11 2003/01/02 23:50:55 massiot 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 *****************************************************************************/
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
47 #elif defined( _MSC_VER ) && defined( _WIN32 ) && !defined( UNDER_CE )
51 #if defined( UNDER_CE )
53 #elif defined( WIN32 )
54 # include <winsock2.h>
55 # include <ws2tcpip.h>
57 # define IN_MULTICAST(a) IN_CLASSD(a)
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() */
70 /*****************************************************************************
72 *****************************************************************************/
73 static int NetOpen( vlc_object_t * );
75 /*****************************************************************************
77 *****************************************************************************/
79 set_description( _("IPv4 network abstraction layer") );
80 set_capability( "network", 50 );
81 set_callbacks( NetOpen, NULL );
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 )
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 );
96 p_socket->sin_addr.s_addr = INADDR_ANY;
100 struct hostent * p_hostent;
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 ) )
107 if( (p_socket->sin_addr.s_addr = inet_addr( psz_address )) == -1 )
110 /* We have a fqdn, try to find its address */
111 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
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 );
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 )
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 */
148 socklen_t i_opt_size;
149 struct sockaddr_in sock;
151 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
153 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
156 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
158 msg_Err( p_this, "cannot create socket" );
163 /* We may want to reuse an already used socket */
165 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
166 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
169 msg_Err( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
172 msg_Err( p_this, "cannot configure socket (SO_REUSEADDR)" );
174 #if defined( WIN32 ) || defined( UNDER_CE )
175 closesocket( i_handle );
182 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
183 * packet loss caused by scheduling problems */
185 #if defined( SYS_BEOS )
186 if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
188 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
190 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
193 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF: %s)",
196 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF)" );
200 /* Check if we really got what we have asked for, because Linux, etc.
201 * will silently limit the max buffer size to net.core.rmem_max which
202 * is typically only 65535 bytes */
204 i_opt_size = sizeof( i_opt );
205 #if defined( SYS_BEOS )
206 if( getsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
208 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
210 (void*) &i_opt, &i_opt_size ) == -1 )
213 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
216 msg_Warn( p_this, "cannot query socket (SO_RCVBUF)" );
219 else if( i_opt < 0x80000 )
221 msg_Warn( p_this, "socket buffer size is 0x%x instead of 0x%x",
226 /* Build the local socket */
228 #if defined( WIN32 ) && !defined( UNDER_CE )
229 /* Under Win32 and for the multicast, we bind on INADDR_ANY,
230 * so let's call BuildAddr with "" instead of psz_bind_addr */
231 psz_bind_win32 = psz_bind_addr ;
233 /* Check if this is a multicast socket */
234 if (IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
238 if ( BuildAddr( &sock, psz_bind_win32, i_bind_port ) == -1 )
240 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
243 msg_Dbg( p_this, "could not build local address" );
244 #if defined( WIN32 ) || defined( UNDER_CE )
245 closesocket( i_handle );
253 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
256 msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
258 msg_Err( p_this, "cannot bind socket" );
260 #if defined( WIN32 ) || defined( UNDER_CE )
261 closesocket( i_handle );
268 /* Allow broadcast reception if we bound on INADDR_ANY */
269 if( !*psz_bind_addr )
272 #if defined( SYS_BEOS )
273 if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
275 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
277 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
280 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
283 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST)" );
288 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
289 /* Join the multicast group if the socket is a multicast address */
291 # define IN_MULTICAST(a) IN_CLASSD(a)
295 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
298 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
299 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
301 if( IN_MULTICAST( ntohl(inet_addr(psz_bind_addr) ) ) )
304 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
305 imr.imr_multiaddr.s_addr = inet_addr(psz_bind_addr);
307 if ( psz_if_addr != NULL && *psz_if_addr
308 && inet_addr(psz_if_addr) != -1 )
310 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
314 imr.imr_interface.s_addr = INADDR_ANY;
316 if ( psz_if_addr != NULL ) free( psz_if_addr );
318 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
319 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
322 msg_Warn( p_this, "failed to join IP multicast group (%s)",
325 msg_Warn( p_this, "failed to join IP multicast group" );
327 #if defined( WIN32 ) || defined( UNDER_CE )
328 closesocket( i_handle );
335 #endif /* UNDER_CE */
337 if( *psz_server_addr )
339 /* Build socket for remote connection */
340 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
342 msg_Err( p_this, "cannot build remote address" );
343 #if defined( WIN32 ) || defined( UNDER_CE )
344 closesocket( i_handle );
351 /* Connect the socket */
352 if( connect( i_handle, (struct sockaddr *) &sock,
353 sizeof( sock ) ) == (-1) )
356 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
358 msg_Err( p_this, "cannot connect socket" );
360 #if defined( WIN32 ) || defined( UNDER_CE )
361 closesocket( i_handle );
369 p_socket->i_handle = i_handle;
370 p_socket->i_mtu = config_GetInt( p_this, "mtu" );
374 /*****************************************************************************
375 * OpenTCP: open a TCP socket
376 *****************************************************************************
377 * psz_server_addr, i_server_port : address and port used for the connect()
378 * system call. If i_server_port == 0, 80 is used.
379 * Other parameters are ignored.
380 * This function returns -1 in case of error.
381 *****************************************************************************/
382 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
384 char * psz_server_addr = p_socket->psz_server_addr;
385 int i_server_port = p_socket->i_server_port;
388 struct sockaddr_in sock;
390 if( i_server_port == 0 )
395 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
397 if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
400 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
402 msg_Err( p_this, "cannot create socket" );
407 /* Build remote address */
408 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
410 msg_Dbg( p_this, "could not build local address" );
411 #if defined( WIN32 ) || defined( UNDER_CE )
412 closesocket( i_handle );
419 /* Connect the socket */
420 if( connect( i_handle, (struct sockaddr *) &sock,
421 sizeof( sock ) ) == (-1) )
424 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
426 msg_Err( p_this, "cannot connect socket" );
428 #if defined( WIN32 ) || defined( UNDER_CE )
429 closesocket( i_handle );
436 p_socket->i_handle = i_handle;
437 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
442 /*****************************************************************************
443 * NetOpen: wrapper around OpenUDP and OpenTCP
444 *****************************************************************************/
445 static int NetOpen( vlc_object_t * p_this )
447 network_socket_t * p_socket = p_this->p_private;
449 if( p_socket->i_type == NETWORK_UDP )
451 return OpenUDP( p_this, p_socket );
455 return OpenTCP( p_this, p_socket );