1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ipv4.c,v 1.24 2004/02/01 14:43:08 alexis Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Mathias Kretschmer <mathias@research.att.com>
9 * Alexis de Lattre <alexis@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
50 #if defined( UNDER_CE )
52 #elif defined( WIN32 )
53 # include <winsock2.h>
54 # include <ws2tcpip.h>
55 # define close closesocket
57 # include <netdb.h> /* hostent ... */
58 # include <sys/socket.h>
59 # include <netinet/in.h>
60 # ifdef HAVE_ARPA_INET_H
61 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
68 # define INADDR_ANY 0x00000000
71 # define INADDR_NONE 0xFFFFFFFF
74 # define IN_MULTICAST(a) IN_CLASSD(a)
78 /*****************************************************************************
80 *****************************************************************************/
81 static int NetOpen( vlc_object_t * );
83 /*****************************************************************************
85 *****************************************************************************/
87 set_description( _("IPv4 network abstraction layer") );
88 set_capability( "network", 50 );
89 set_callbacks( NetOpen, NULL );
92 /*****************************************************************************
93 * BuildAddr: utility function to build a struct sockaddr_in
94 *****************************************************************************/
95 static int BuildAddr( struct sockaddr_in * p_socket,
96 const char * psz_address, int i_port )
99 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
100 p_socket->sin_family = AF_INET; /* family */
101 p_socket->sin_port = htons( (uint16_t)i_port );
104 p_socket->sin_addr.s_addr = INADDR_ANY;
108 struct hostent * p_hostent;
110 /* Try to convert address directly from in_addr - this will work if
111 * psz_address is dotted decimal. */
112 #ifdef HAVE_ARPA_INET_H
113 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
115 p_socket->sin_addr.s_addr = inet_addr( psz_address );
116 if( p_socket->sin_addr.s_addr == INADDR_NONE )
119 /* We have a fqdn, try to find its address */
120 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
125 /* Copy the first address of the host in the socket address */
126 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
127 p_hostent->h_length );
133 /*****************************************************************************
134 * OpenUDP: open a UDP socket
135 *****************************************************************************
136 * psz_bind_addr, i_bind_port : address and port used for the bind()
137 * system call. If psz_bind_addr == "", the socket is bound to
138 * INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
139 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
140 * join the multicast group.
141 * psz_server_addr, i_server_port : address and port used for the connect()
142 * system call. It can avoid receiving packets from unauthorized IPs.
143 * Its use leads to great confusion and is currently discouraged.
144 * This function returns -1 in case of error.
145 *****************************************************************************/
146 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
148 char * psz_bind_addr = p_socket->psz_bind_addr;
149 int i_bind_port = p_socket->i_bind_port;
150 char * psz_server_addr = p_socket->psz_server_addr;
151 int i_server_port = p_socket->i_server_port;
154 socklen_t i_opt_size;
155 struct sockaddr_in sock;
157 /* If IP_ADD_SOURCE_MEMBERSHIP is not defined in the headers
158 (because it's not in glibc for example), we have to define the
159 headers required for IGMPv3 here */
160 #ifndef IP_ADD_SOURCE_MEMBERSHIP
161 #define IP_ADD_SOURCE_MEMBERSHIP 39
162 struct ip_mreq_source {
163 struct in_addr imr_multiaddr;
164 struct in_addr imr_interface;
165 struct in_addr imr_sourceaddr;
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 )
174 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
176 msg_Warn( p_this, "cannot create socket" );
181 /* We may want to reuse an already used socket */
183 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
184 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
187 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
190 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
196 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
197 * packet loss caused by scheduling problems */
199 #if defined( SYS_BEOS )
200 if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
202 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
206 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
209 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF)" );
213 /* Check if we really got what we have asked for, because Linux, etc.
214 * will silently limit the max buffer size to net.core.rmem_max which
215 * is typically only 65535 bytes */
217 i_opt_size = sizeof( i_opt );
218 #if defined( SYS_BEOS )
219 if( getsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK, (void*) &i_opt, &i_opt_size ) == -1 )
221 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void*) &i_opt, &i_opt_size ) == -1 )
225 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
228 msg_Warn( p_this, "cannot query socket (SO_RCVBUF)" );
231 else if( i_opt < 0x80000 )
233 msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
238 /* Build the local socket */
240 #if defined( WIN32 ) && !defined( UNDER_CE )
241 /* Under Win32 and for multicasting, we bind to INADDR_ANY,
242 * so let's call BuildAddr with "" instead of psz_bind_addr */
243 if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
244 "" : psz_bind_addr, 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" );
255 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
258 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
260 msg_Warn( p_this, "cannot bind socket" );
266 #if defined( WIN32 ) && !defined( UNDER_CE )
267 /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
268 if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
270 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
272 msg_Dbg( p_this, "could not build local address" );
279 /* Allow broadcast reception if we bound on INADDR_ANY */
280 if( !*psz_bind_addr )
283 #if defined( SYS_BEOS )
284 if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
286 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
290 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
293 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST)" );
298 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
299 /* Join the multicast group if the socket is a multicast address */
300 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
302 /* Determine interface to be used for multicast */
303 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
305 /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
306 so that IGMPv3 aware OSes running on IGMPv3 aware networks
307 will do an IGMPv3 query on the network */
308 if( *psz_server_addr )
310 struct ip_mreq_source imr;
312 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
313 imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
315 if( psz_if_addr != NULL && *psz_if_addr
316 && inet_addr(psz_if_addr) != INADDR_NONE )
318 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
322 imr.imr_interface.s_addr = INADDR_ANY;
324 if( psz_if_addr != NULL ) free( psz_if_addr );
326 msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
327 /* Join Multicast group with source filter */
328 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
330 sizeof(struct ip_mreq_source) ) == -1 )
333 msg_Err( p_this, "failed to join IP multicast group (%s)",
335 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
337 msg_Err( p_this, "failed to join IP multicast group" );
338 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
344 /* If there is no source address, we use IP_ADD_MEMBERSHIP */
349 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
350 if( psz_if_addr != NULL && *psz_if_addr
351 && inet_addr(psz_if_addr) != INADDR_NONE )
353 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
357 imr.imr_interface.s_addr = INADDR_ANY;
359 if( psz_if_addr != NULL ) free( psz_if_addr );
361 msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
362 /* Join Multicast group without source filter */
363 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
364 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
367 msg_Err( p_this, "failed to join IP multicast group (%s)",
370 msg_Err( p_this, "failed to join IP multicast group" );
379 if( *psz_server_addr )
381 /* Build socket for remote connection */
382 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
384 msg_Warn( p_this, "cannot build remote address" );
389 /* Connect the socket */
390 if( connect( i_handle, (struct sockaddr *) &sock,
391 sizeof( sock ) ) == (-1) )
394 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
396 msg_Warn( p_this, "cannot connect socket" );
402 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
403 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
405 /* set the time-to-live */
406 int ttl = p_socket->i_ttl;
409 ttl = config_GetInt( p_this, "ttl" );
411 if( ttl < 1 ) ttl = 1;
413 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
414 (void *) &ttl, sizeof( ttl ) ) < 0 )
417 msg_Err( p_this, "failed to set ttl (%s)", strerror(errno) );
419 msg_Err( p_this, "failed to set ttl" );
428 p_socket->i_handle = i_handle;
429 p_socket->i_mtu = config_GetInt( p_this, "mtu" );
433 /*****************************************************************************
434 * OpenTCP: open a TCP socket
435 *****************************************************************************
436 * psz_server_addr, i_server_port : address and port used for the connect()
437 * system call. If i_server_port == 0, 80 is used.
438 * Other parameters are ignored.
439 * This function returns -1 in case of error.
440 *****************************************************************************/
441 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
443 char * psz_server_addr = p_socket->psz_server_addr;
444 int i_server_port = p_socket->i_server_port;
447 struct sockaddr_in sock;
449 if( i_server_port == 0 )
454 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
456 if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
459 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
461 msg_Warn( p_this, "cannot create socket" );
466 /* Build remote address */
467 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
469 msg_Dbg( p_this, "could not build local address" );
473 /* Set to non-blocking */
474 #if defined( WIN32 ) || defined( UNDER_CE )
476 unsigned long i_dummy = 1;
477 if( ioctlsocket( i_handle, FIONBIO, &i_dummy ) != 0 )
479 msg_Err( p_this, "cannot set socket to non-blocking mode" );
482 #elif defined( HAVE_ERRNO_H )
485 if( ( i_flags = fcntl( i_handle, F_GETFL, 0 ) ) < 0 ||
486 fcntl( i_handle, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
488 msg_Err( p_this, "cannot set socket to non-blocking mode" );
493 /* Connect the socket */
494 if( connect( i_handle, (struct sockaddr *) &sock, sizeof( sock ) ) == -1 )
496 #if defined( WIN32 ) || defined( UNDER_CE )
497 if( WSAGetLastError() == WSAEWOULDBLOCK )
498 #elif defined( HAVE_ERRNO_H )
499 if( errno == EINPROGRESS )
506 int i_opt_size = sizeof( i_opt );
507 struct timeval timeout;
510 msg_Dbg( p_this, "connection in progress" );
515 msg_Dbg( p_this, "connection aborted" );
519 /* Initialize file descriptor set */
521 FD_SET( i_handle, &fds );
523 /* We'll wait 0.1 second if nothing happens */
525 timeout.tv_usec = 100000;
527 } while( ( i_ret = select( i_handle + 1, NULL, &fds, NULL,
529 #if defined( WIN32 ) || defined( UNDER_CE )
530 ( i_ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK ) );
531 #elif defined( HAVE_ERRNO_H )
532 ( i_ret < 0 && errno == EINTR ) );
539 msg_Warn( p_this, "cannot connect socket (select failed)" );
543 if( getsockopt( i_handle, SOL_SOCKET, SO_ERROR, (void*)&i_opt,
544 &i_opt_size ) == -1 || i_opt != 0 )
546 msg_Warn( p_this, "cannot connect socket (SO_ERROR)" );
552 #if defined( HAVE_ERRNO_H )
553 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
555 msg_Warn( p_this, "cannot connect socket" );
561 p_socket->i_handle = i_handle;
562 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
573 /*****************************************************************************
574 * NetOpen: wrapper around OpenUDP and OpenTCP
575 *****************************************************************************/
576 static int NetOpen( vlc_object_t * p_this )
578 network_socket_t * p_socket = p_this->p_private;
580 if( p_socket->i_type == NETWORK_UDP )
582 return OpenUDP( p_this, p_socket );
586 return OpenTCP( p_this, p_socket );