1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ipv4.c,v 1.17 2003/03/24 17:15:30 gbazin 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>
49 #if defined( UNDER_CE )
51 #elif defined( WIN32 )
52 # include <winsock2.h>
53 # include <ws2tcpip.h>
55 # define IN_MULTICAST(a) IN_CLASSD(a)
58 # include <netdb.h> /* hostent ... */
59 # include <sys/socket.h>
60 # include <netinet/in.h>
61 # ifdef HAVE_ARPA_INET_H
62 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
69 # define INADDR_ANY 0x00000000
72 # define INADDR_NONE 0xFFFFFFFF
75 /*****************************************************************************
77 *****************************************************************************/
78 static int NetOpen( vlc_object_t * );
80 /*****************************************************************************
82 *****************************************************************************/
84 set_description( _("IPv4 network abstraction layer") );
85 set_capability( "network", 50 );
86 set_callbacks( NetOpen, NULL );
89 /*****************************************************************************
90 * BuildAddr: utility function to build a struct sockaddr_in
91 *****************************************************************************/
92 static int BuildAddr( struct sockaddr_in * p_socket,
93 const char * psz_address, int i_port )
96 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
97 p_socket->sin_family = AF_INET; /* family */
98 p_socket->sin_port = htons( (uint16_t)i_port );
101 p_socket->sin_addr.s_addr = INADDR_ANY;
105 struct hostent * p_hostent;
107 /* Try to convert address directly from in_addr - this will work if
108 * psz_address is dotted decimal. */
109 #ifdef HAVE_ARPA_INET_H
110 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
112 p_socket->sin_addr.s_addr = inet_addr( psz_address );
113 if( p_socket->sin_addr.s_addr == INADDR_NONE )
116 /* We have a fqdn, try to find its address */
117 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
122 /* Copy the first address of the host in the socket address */
123 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
124 p_hostent->h_length );
130 /*****************************************************************************
131 * OpenUDP: open a UDP socket
132 *****************************************************************************
133 * psz_bind_addr, i_bind_port : address and port used for the bind()
134 * system call. If psz_bind_addr == "", the socket is bound to
135 * INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
136 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
137 * join the multicast group.
138 * psz_server_addr, i_server_port : address and port used for the connect()
139 * system call. It can avoid receiving packets from unauthorized IPs.
140 * Its use leads to great confusion and is currently discouraged.
141 * This function returns -1 in case of error.
142 *****************************************************************************/
143 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
145 char * psz_bind_addr = p_socket->psz_bind_addr;
146 int i_bind_port = p_socket->i_bind_port;
147 char * psz_server_addr = p_socket->psz_server_addr;
148 int i_server_port = p_socket->i_server_port;
149 #if defined( WIN32 ) && !defined( UNDER_CE )
150 char * psz_bind_win32; /* WIN32 multicast kludge */
154 socklen_t i_opt_size;
155 struct sockaddr_in sock;
157 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
159 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
162 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
164 msg_Err( p_this, "cannot create socket" );
169 /* We may want to reuse an already used socket */
171 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
172 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
175 msg_Err( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
178 msg_Err( p_this, "cannot configure socket (SO_REUSEADDR)" );
180 #if defined( WIN32 ) || defined( UNDER_CE )
181 closesocket( i_handle );
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 defined( SYS_BEOS )
192 if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
194 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
196 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
199 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
202 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF)" );
206 /* Check if we really got what we have asked for, because Linux, etc.
207 * will silently limit the max buffer size to net.core.rmem_max which
208 * is typically only 65535 bytes */
210 i_opt_size = sizeof( i_opt );
211 #if defined( SYS_BEOS )
212 if( getsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
214 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
216 (void*) &i_opt, &i_opt_size ) == -1 )
219 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
222 msg_Warn( p_this, "cannot query socket (SO_RCVBUF)" );
225 else if( i_opt < 0x80000 )
227 msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
232 /* Build the local socket */
234 #if defined( WIN32 ) && !defined( UNDER_CE )
235 /* Under Win32 and for the multicast, we bind on INADDR_ANY,
236 * so let's call BuildAddr with "" instead of psz_bind_addr */
237 psz_bind_win32 = psz_bind_addr ;
239 /* Check if this is a multicast socket */
240 if (IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
244 if ( BuildAddr( &sock, psz_bind_win32, i_bind_port ) == -1 )
246 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
249 msg_Dbg( p_this, "could not build local address" );
250 #if defined( WIN32 ) || defined( UNDER_CE )
251 closesocket( i_handle );
259 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
262 msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
264 msg_Err( p_this, "cannot bind socket" );
266 #if defined( WIN32 ) || defined( UNDER_CE )
267 closesocket( i_handle );
274 /* Allow broadcast reception if we bound on INADDR_ANY */
275 if( !*psz_bind_addr )
278 #if defined( SYS_BEOS )
279 if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
281 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
283 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
286 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
289 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST)" );
294 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
295 /* Join the multicast group if the socket is a multicast address */
297 # define IN_MULTICAST(a) IN_CLASSD(a)
301 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
304 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
305 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
307 if( IN_MULTICAST( ntohl(inet_addr(psz_bind_addr) ) ) )
310 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
311 imr.imr_multiaddr.s_addr = inet_addr(psz_bind_addr);
313 if ( psz_if_addr != NULL && *psz_if_addr
314 && inet_addr(psz_if_addr) != INADDR_NONE )
316 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
320 imr.imr_interface.s_addr = INADDR_ANY;
322 if ( psz_if_addr != NULL ) free( psz_if_addr );
324 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
325 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
328 msg_Warn( p_this, "failed to join IP multicast group (%s)",
331 msg_Warn( p_this, "failed to join IP multicast group" );
333 #if defined( WIN32 ) || defined( UNDER_CE )
334 closesocket( i_handle );
341 #endif /* UNDER_CE, SYS_BEOS */
343 if( *psz_server_addr )
345 /* Build socket for remote connection */
346 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
348 msg_Err( p_this, "cannot build remote address" );
349 #if defined( WIN32 ) || defined( UNDER_CE )
350 closesocket( i_handle );
357 /* Connect the socket */
358 if( connect( i_handle, (struct sockaddr *) &sock,
359 sizeof( sock ) ) == (-1) )
362 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
364 msg_Err( p_this, "cannot connect socket" );
366 #if defined( WIN32 ) || defined( UNDER_CE )
367 closesocket( i_handle );
374 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
376 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
378 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
381 /* set the time-to-live */
382 int ttl = config_GetInt( p_this, "ttl" );
386 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
387 (void *) &ttl, sizeof( ttl ) ) < 0 )
390 msg_Warn( p_this, "failed to set ttl (%s)",
393 msg_Warn( p_this, "failed to set ttl" );
395 #if defined( WIN32 ) || defined( UNDER_CE )
396 closesocket( i_handle );
403 #endif /* UNDER_CE, SYS_BEOS */
406 p_socket->i_handle = i_handle;
407 p_socket->i_mtu = config_GetInt( p_this, "mtu" );
411 /*****************************************************************************
412 * OpenTCP: open a TCP socket
413 *****************************************************************************
414 * psz_server_addr, i_server_port : address and port used for the connect()
415 * system call. If i_server_port == 0, 80 is used.
416 * Other parameters are ignored.
417 * This function returns -1 in case of error.
418 *****************************************************************************/
419 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
421 char * psz_server_addr = p_socket->psz_server_addr;
422 int i_server_port = p_socket->i_server_port;
425 struct sockaddr_in sock;
427 if( i_server_port == 0 )
432 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
434 if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
437 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
439 msg_Err( p_this, "cannot create socket" );
444 /* Build remote address */
445 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
447 msg_Dbg( p_this, "could not build local address" );
448 #if defined( WIN32 ) || defined( UNDER_CE )
449 closesocket( i_handle );
456 /* Connect the socket */
457 if( connect( i_handle, (struct sockaddr *) &sock,
458 sizeof( sock ) ) == (-1) )
461 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
463 msg_Err( p_this, "cannot connect socket" );
465 #if defined( WIN32 ) || defined( UNDER_CE )
466 closesocket( i_handle );
473 p_socket->i_handle = i_handle;
474 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
479 /*****************************************************************************
480 * NetOpen: wrapper around OpenUDP and OpenTCP
481 *****************************************************************************/
482 static int NetOpen( vlc_object_t * p_this )
484 network_socket_t * p_socket = p_this->p_private;
486 if( p_socket->i_type == NETWORK_UDP )
488 return OpenUDP( p_this, p_socket );
492 return OpenTCP( p_this, p_socket );