1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ipv4.c,v 1.21 2004/01/15 13:47:01 fenrir 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>
54 # define close closesocket
56 # include <netdb.h> /* hostent ... */
57 # include <sys/socket.h>
58 # include <netinet/in.h>
59 # ifdef HAVE_ARPA_INET_H
60 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
67 # define INADDR_ANY 0x00000000
70 # define INADDR_NONE 0xFFFFFFFF
73 # define IN_MULTICAST(a) IN_CLASSD(a)
76 /*****************************************************************************
78 *****************************************************************************/
79 static int NetOpen( vlc_object_t * );
81 /*****************************************************************************
83 *****************************************************************************/
85 set_description( _("IPv4 network abstraction layer") );
86 set_capability( "network", 50 );
87 set_callbacks( NetOpen, NULL );
90 /*****************************************************************************
91 * BuildAddr: utility function to build a struct sockaddr_in
92 *****************************************************************************/
93 static int BuildAddr( struct sockaddr_in * p_socket,
94 const char * psz_address, int i_port )
97 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
98 p_socket->sin_family = AF_INET; /* family */
99 p_socket->sin_port = htons( (uint16_t)i_port );
102 p_socket->sin_addr.s_addr = INADDR_ANY;
106 struct hostent * p_hostent;
108 /* Try to convert address directly from in_addr - this will work if
109 * psz_address is dotted decimal. */
110 #ifdef HAVE_ARPA_INET_H
111 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
113 p_socket->sin_addr.s_addr = inet_addr( psz_address );
114 if( p_socket->sin_addr.s_addr == INADDR_NONE )
117 /* We have a fqdn, try to find its address */
118 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
123 /* Copy the first address of the host in the socket address */
124 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
125 p_hostent->h_length );
131 /*****************************************************************************
132 * OpenUDP: open a UDP socket
133 *****************************************************************************
134 * psz_bind_addr, i_bind_port : address and port used for the bind()
135 * system call. If psz_bind_addr == "", the socket is bound to
136 * INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
137 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
138 * join the multicast group.
139 * psz_server_addr, i_server_port : address and port used for the connect()
140 * system call. It can avoid receiving packets from unauthorized IPs.
141 * Its use leads to great confusion and is currently discouraged.
142 * This function returns -1 in case of error.
143 *****************************************************************************/
144 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
146 char * psz_bind_addr = p_socket->psz_bind_addr;
147 int i_bind_port = p_socket->i_bind_port;
148 char * psz_server_addr = p_socket->psz_server_addr;
149 int i_server_port = p_socket->i_server_port;
152 socklen_t i_opt_size;
153 struct sockaddr_in sock;
155 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
157 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
160 msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
162 msg_Warn( p_this, "cannot create socket" );
167 /* We may want to reuse an already used socket */
169 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
170 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
173 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
176 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
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, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
188 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
192 msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
195 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF)" );
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 defined( SYS_BEOS )
205 if( getsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK, (void*) &i_opt, &i_opt_size ) == -1 )
207 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void*) &i_opt, &i_opt_size ) == -1 )
211 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
214 msg_Warn( p_this, "cannot query socket (SO_RCVBUF)" );
217 else if( i_opt < 0x80000 )
219 msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
224 /* Build the local socket */
226 #if defined( WIN32 ) && !defined( UNDER_CE )
227 /* Under Win32 and for multicasting, we bind to INADDR_ANY,
228 * so let's call BuildAddr with "" instead of psz_bind_addr */
229 if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
230 "" : psz_bind_addr, i_bind_port ) == -1 )
232 if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
235 msg_Dbg( p_this, "could not build local address" );
241 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
244 msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
246 msg_Warn( p_this, "cannot bind socket" );
252 #if defined( WIN32 ) && !defined( UNDER_CE )
253 /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
254 if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
256 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
258 msg_Dbg( p_this, "could not build local address" );
265 /* Allow broadcast reception if we bound on INADDR_ANY */
266 if( !*psz_bind_addr )
269 #if defined( SYS_BEOS )
270 if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
272 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
276 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
279 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST)" );
284 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
285 /* Join the multicast group if the socket is a multicast address */
286 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
290 /* Determine interface to be used for multicast */
291 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
292 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
293 if( psz_if_addr != NULL && *psz_if_addr
294 && inet_addr(psz_if_addr) != INADDR_NONE )
296 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
300 imr.imr_interface.s_addr = INADDR_ANY;
302 if( psz_if_addr != NULL ) free( psz_if_addr );
304 /* Join Multicast group */
305 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
306 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
309 msg_Err( p_this, "failed to join IP multicast group (%s)",
312 msg_Err( p_this, "failed to join IP multicast group" );
318 #endif /* UNDER_CE, SYS_BEOS */
320 if( *psz_server_addr )
322 /* Build socket for remote connection */
323 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
325 msg_Warn( p_this, "cannot build remote address" );
330 /* Connect the socket */
331 if( connect( i_handle, (struct sockaddr *) &sock,
332 sizeof( sock ) ) == (-1) )
335 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
337 msg_Warn( p_this, "cannot connect socket" );
343 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
344 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
346 /* set the time-to-live */
347 int ttl = p_socket->i_ttl;
350 ttl = config_GetInt( p_this, "ttl" );
352 if( ttl < 1 ) ttl = 1;
354 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
355 (void *) &ttl, sizeof( ttl ) ) < 0 )
358 msg_Err( p_this, "failed to set ttl (%s)", strerror(errno) );
360 msg_Err( p_this, "failed to set ttl" );
366 #endif /* UNDER_CE, SYS_BEOS */
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_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
402 msg_Warn( 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" );
414 /* We need errno to use non blocking connect */
416 /* set to non-blocking */
417 #if defined( WIN32 ) || defined( UNDER_CE )
419 unsigned long i_dummy = 1;
420 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
422 msg_Err( p_httpt, "cannot set socket to non-blocking mode" );
428 if( ( i_flags = fcntl( i_handle, F_GETFL, 0 ) ) < 0 ||
429 fcntl( i_handle, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
431 msg_Err( p_this, "cannot set socket to non-blocking mode" );
437 /* Connect the socket */
438 if( connect( i_handle, (struct sockaddr *) &sock, sizeof( sock ) ) == -1 )
441 if( errno == EINPROGRESS )
445 int i_opt_size = sizeof( i_opt );
446 struct timeval timeout;
449 msg_Dbg( p_this, "connection in progress" );
454 msg_Dbg( p_this, "connection aborted" );
458 /* Initialize file descriptor set */
460 FD_SET( i_handle, &fds );
462 /* We'll wait 0.1 second if nothing happens */
464 timeout.tv_usec = 100000;
465 } while( ( i_ret = select( i_handle + 1, NULL, &fds, NULL, &timeout )) == 0 ||
466 ( i_ret < 0 && errno == EINTR ) );
469 msg_Warn( p_this, "cannot connect socket (select failed)" );
472 if( getsockopt( i_handle, SOL_SOCKET, SO_ERROR, (void*)&i_opt, &i_opt_size ) == -1 ||
475 msg_Warn( p_this, "cannot connect socket (SO_ERROR)" );
481 msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
485 msg_Warn( p_this, "cannot connect socket" );
490 p_socket->i_handle = i_handle;
491 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
502 /*****************************************************************************
503 * NetOpen: wrapper around OpenUDP and OpenTCP
504 *****************************************************************************/
505 static int NetOpen( vlc_object_t * p_this )
507 network_socket_t * p_socket = p_this->p_private;
509 if( p_socket->i_type == NETWORK_UDP )
511 return OpenUDP( p_this, p_socket );
515 return OpenTCP( p_this, p_socket );