1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ipv4.c,v 1.25 2004/02/22 23:09:25 titer 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_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
203 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
206 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF)" );
211 #if !defined( SYS_BEOS )
212 /* Check if we really got what we have asked for, because Linux, etc.
213 * will silently limit the max buffer size to net.core.rmem_max which
214 * is typically only 65535 bytes */
216 i_opt_size = sizeof( i_opt );
217 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void*) &i_opt, &i_opt_size ) == -1 )
220 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
223 msg_Warn( p_this, "cannot query socket (SO_RCVBUF)" );
226 else if( i_opt < 0x80000 )
228 msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
234 /* Build the local socket */
236 #if defined( WIN32 ) && !defined( UNDER_CE )
237 /* Under Win32 and for multicasting, we bind to INADDR_ANY,
238 * so let's call BuildAddr with "" instead of psz_bind_addr */
239 if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
240 "" : psz_bind_addr, i_bind_port ) == -1 )
242 if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
245 msg_Dbg( p_this, "could not build local address" );
251 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
254 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
256 msg_Warn( p_this, "cannot bind socket" );
262 #if defined( WIN32 ) && !defined( UNDER_CE )
263 /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
264 if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
266 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
268 msg_Dbg( p_this, "could not build local address" );
275 #if !defined( SYS_BEOS )
276 /* Allow broadcast reception if we bound on INADDR_ANY */
277 if( !*psz_bind_addr )
280 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
283 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
286 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST)" );
292 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
293 /* Join the multicast group if the socket is a multicast address */
294 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
296 /* Determine interface to be used for multicast */
297 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
299 /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
300 so that IGMPv3 aware OSes running on IGMPv3 aware networks
301 will do an IGMPv3 query on the network */
302 if( *psz_server_addr )
304 struct ip_mreq_source imr;
306 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
307 imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
309 if( psz_if_addr != NULL && *psz_if_addr
310 && inet_addr(psz_if_addr) != INADDR_NONE )
312 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
316 imr.imr_interface.s_addr = INADDR_ANY;
318 if( psz_if_addr != NULL ) free( psz_if_addr );
320 msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
321 /* Join Multicast group with source filter */
322 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
324 sizeof(struct ip_mreq_source) ) == -1 )
327 msg_Err( p_this, "failed to join IP multicast group (%s)",
329 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
331 msg_Err( p_this, "failed to join IP multicast group" );
332 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
338 /* If there is no source address, we use IP_ADD_MEMBERSHIP */
343 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
344 if( psz_if_addr != NULL && *psz_if_addr
345 && inet_addr(psz_if_addr) != INADDR_NONE )
347 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
351 imr.imr_interface.s_addr = INADDR_ANY;
353 if( psz_if_addr != NULL ) free( psz_if_addr );
355 msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
356 /* Join Multicast group without source filter */
357 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
358 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
361 msg_Err( p_this, "failed to join IP multicast group (%s)",
364 msg_Err( p_this, "failed to join IP multicast group" );
373 if( *psz_server_addr )
375 /* Build socket for remote connection */
376 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
378 msg_Warn( p_this, "cannot build remote address" );
383 /* Connect the socket */
384 if( connect( i_handle, (struct sockaddr *) &sock,
385 sizeof( sock ) ) == (-1) )
388 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
390 msg_Warn( p_this, "cannot connect socket" );
396 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
397 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
399 /* set the time-to-live */
400 int ttl = p_socket->i_ttl;
403 ttl = config_GetInt( p_this, "ttl" );
405 if( ttl < 1 ) ttl = 1;
407 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
408 (void *) &ttl, sizeof( ttl ) ) < 0 )
411 msg_Err( p_this, "failed to set ttl (%s)", strerror(errno) );
413 msg_Err( p_this, "failed to set ttl" );
422 p_socket->i_handle = i_handle;
423 p_socket->i_mtu = config_GetInt( p_this, "mtu" );
427 /*****************************************************************************
428 * OpenTCP: open a TCP socket
429 *****************************************************************************
430 * psz_server_addr, i_server_port : address and port used for the connect()
431 * system call. If i_server_port == 0, 80 is used.
432 * Other parameters are ignored.
433 * This function returns -1 in case of error.
434 *****************************************************************************/
435 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
437 char * psz_server_addr = p_socket->psz_server_addr;
438 int i_server_port = p_socket->i_server_port;
441 struct sockaddr_in sock;
443 if( i_server_port == 0 )
448 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
450 if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
453 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
455 msg_Warn( p_this, "cannot create socket" );
460 /* Build remote address */
461 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
463 msg_Dbg( p_this, "could not build local address" );
467 /* Set to non-blocking */
468 #if defined( WIN32 ) || defined( UNDER_CE )
470 unsigned long i_dummy = 1;
471 if( ioctlsocket( i_handle, FIONBIO, &i_dummy ) != 0 )
473 msg_Err( p_this, "cannot set socket to non-blocking mode" );
476 #elif defined( HAVE_ERRNO_H )
479 if( ( i_flags = fcntl( i_handle, F_GETFL, 0 ) ) < 0 ||
480 fcntl( i_handle, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
482 msg_Err( p_this, "cannot set socket to non-blocking mode" );
487 /* Connect the socket */
488 if( connect( i_handle, (struct sockaddr *) &sock, sizeof( sock ) ) == -1 )
490 #if defined( WIN32 ) || defined( UNDER_CE )
491 if( WSAGetLastError() == WSAEWOULDBLOCK )
492 #elif defined( HAVE_ERRNO_H )
493 if( errno == EINPROGRESS )
500 int i_opt_size = sizeof( i_opt );
501 struct timeval timeout;
504 msg_Dbg( p_this, "connection in progress" );
509 msg_Dbg( p_this, "connection aborted" );
513 /* Initialize file descriptor set */
515 FD_SET( i_handle, &fds );
517 /* We'll wait 0.1 second if nothing happens */
519 timeout.tv_usec = 100000;
521 } while( ( i_ret = select( i_handle + 1, NULL, &fds, NULL,
523 #if defined( WIN32 ) || defined( UNDER_CE )
524 ( i_ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK ) );
525 #elif defined( HAVE_ERRNO_H )
526 ( i_ret < 0 && errno == EINTR ) );
533 msg_Warn( p_this, "cannot connect socket (select failed)" );
537 #if !defined( SYS_BEOS )
538 if( getsockopt( i_handle, SOL_SOCKET, SO_ERROR, (void*)&i_opt,
539 &i_opt_size ) == -1 || i_opt != 0 )
541 msg_Warn( p_this, "cannot connect socket (SO_ERROR)" );
548 #if defined( HAVE_ERRNO_H )
549 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
551 msg_Warn( p_this, "cannot connect socket" );
557 p_socket->i_handle = i_handle;
558 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
569 /*****************************************************************************
570 * NetOpen: wrapper around OpenUDP and OpenTCP
571 *****************************************************************************/
572 static int NetOpen( vlc_object_t * p_this )
574 network_socket_t * p_socket = p_this->p_private;
576 if( p_socket->i_type == NETWORK_UDP )
578 return OpenUDP( p_this, p_socket );
582 return OpenTCP( p_this, p_socket );