1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ipv4.c,v 1.18 2003/04/21 16:22:43 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>
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_Err( p_this, "cannot create socket (%s)", strerror(errno) );
162 msg_Err( 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_Err( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
176 msg_Err( 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,
188 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
190 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
193 msg_Dbg( 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_Dbg( 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 multicasting, we bind to INADDR_ANY,
230 * so let's call BuildAddr with "" instead of psz_bind_addr */
231 if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
232 "" : psz_bind_addr, i_bind_port ) == -1 )
234 if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
237 msg_Dbg( p_this, "could not build local address" );
243 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
246 msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
248 msg_Err( p_this, "cannot bind socket" );
254 #if defined( WIN32 ) && !defined( UNDER_CE )
255 /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
256 if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
258 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
260 msg_Dbg( p_this, "could not build local address" );
267 /* Allow broadcast reception if we bound on INADDR_ANY */
268 if( !*psz_bind_addr )
271 #if defined( SYS_BEOS )
272 if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
274 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
276 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
279 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
282 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST)" );
287 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
288 /* Join the multicast group if the socket is a multicast address */
289 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
293 /* Determine interface to be used for multicast */
294 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
295 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
296 if( psz_if_addr != NULL && *psz_if_addr
297 && inet_addr(psz_if_addr) != INADDR_NONE )
299 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
303 imr.imr_interface.s_addr = INADDR_ANY;
305 if( psz_if_addr != NULL ) free( psz_if_addr );
307 /* Join Multicast group */
308 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
309 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
312 msg_Warn( p_this, "failed to join IP multicast group (%s)",
315 msg_Warn( p_this, "failed to join IP multicast group" );
321 #endif /* UNDER_CE, SYS_BEOS */
323 if( *psz_server_addr )
325 /* Build socket for remote connection */
326 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
328 msg_Err( p_this, "cannot build remote address" );
333 /* Connect the socket */
334 if( connect( i_handle, (struct sockaddr *) &sock,
335 sizeof( sock ) ) == (-1) )
338 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
340 msg_Err( p_this, "cannot connect socket" );
346 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
347 if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
349 /* set the time-to-live */
350 int ttl = config_GetInt( p_this, "ttl" );
351 if( ttl < 1 ) ttl = 1;
353 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
354 (void *) &ttl, sizeof( ttl ) ) < 0 )
357 msg_Warn( p_this, "failed to set ttl (%s)", strerror(errno) );
359 msg_Warn( p_this, "failed to set ttl" );
365 #endif /* UNDER_CE, SYS_BEOS */
368 p_socket->i_handle = i_handle;
369 p_socket->i_mtu = config_GetInt( p_this, "mtu" );
373 /*****************************************************************************
374 * OpenTCP: open a TCP socket
375 *****************************************************************************
376 * psz_server_addr, i_server_port : address and port used for the connect()
377 * system call. If i_server_port == 0, 80 is used.
378 * Other parameters are ignored.
379 * This function returns -1 in case of error.
380 *****************************************************************************/
381 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
383 char * psz_server_addr = p_socket->psz_server_addr;
384 int i_server_port = p_socket->i_server_port;
387 struct sockaddr_in sock;
389 if( i_server_port == 0 )
394 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
396 if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
399 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
401 msg_Err( p_this, "cannot create socket" );
406 /* Build remote address */
407 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
409 msg_Dbg( p_this, "could not build local address" );
414 /* Connect the socket */
415 if( connect( i_handle, (struct sockaddr *) &sock,
416 sizeof( sock ) ) == (-1) )
419 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
421 msg_Err( p_this, "cannot connect socket" );
427 p_socket->i_handle = i_handle;
428 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
433 /*****************************************************************************
434 * NetOpen: wrapper around OpenUDP and OpenTCP
435 *****************************************************************************/
436 static int NetOpen( vlc_object_t * p_this )
438 network_socket_t * p_socket = p_this->p_private;
440 if( p_socket->i_type == NETWORK_UDP )
442 return OpenUDP( p_this, p_socket );
446 return OpenTCP( p_this, p_socket );