1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ipv4.c,v 1.9 2002/12/06 16:34:07 sam 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>
47 #elif defined( _MSC_VER ) && defined( _WIN32 ) && !defined( UNDER_CE )
51 #if defined( UNDER_CE )
53 #elif defined( WIN32 )
54 # include <winsock2.h>
55 # include <ws2tcpip.h>
57 # define IN_MULTICAST(a) IN_CLASSD(a)
60 # include <netdb.h> /* hostent ... */
61 # include <sys/socket.h>
62 # include <netinet/in.h>
63 # ifdef HAVE_ARPA_INET_H
64 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
70 /*****************************************************************************
72 *****************************************************************************/
73 static int NetOpen( vlc_object_t * );
75 /*****************************************************************************
77 *****************************************************************************/
79 set_description( _("IPv4 network abstraction layer") );
80 set_capability( "network", 50 );
81 set_callbacks( NetOpen, NULL );
84 /*****************************************************************************
85 * BuildAddr: utility function to build a struct sockaddr_in
86 *****************************************************************************/
87 static int BuildAddr( struct sockaddr_in * p_socket,
88 const char * psz_address, int i_port )
91 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
92 p_socket->sin_family = AF_INET; /* family */
93 p_socket->sin_port = htons( (uint16_t)i_port );
96 p_socket->sin_addr.s_addr = INADDR_ANY;
100 struct hostent * p_hostent;
102 /* Try to convert address directly from in_addr - this will work if
103 * psz_address is dotted decimal. */
104 #ifdef HAVE_ARPA_INET_H
105 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
107 if( (p_socket->sin_addr.s_addr = inet_addr( psz_address )) == -1 )
110 /* We have a fqdn, try to find its address */
111 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
116 /* Copy the first address of the host in the socket address */
117 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
118 p_hostent->h_length );
124 /*****************************************************************************
125 * OpenUDP: open a UDP socket
126 *****************************************************************************
127 * psz_bind_addr, i_bind_port : address and port used for the bind()
128 * system call. If psz_bind_addr == "", the socket is bound to
129 * INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
130 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
131 * join the multicast group.
132 * psz_server_addr, i_server_port : address and port used for the connect()
133 * system call. It can avoid receiving packets from unauthorized IPs.
134 * Its use leads to great confusion and is currently discouraged.
135 * This function returns -1 in case of error.
136 *****************************************************************************/
137 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
139 char * psz_bind_addr = p_socket->psz_bind_addr;
140 int i_bind_port = p_socket->i_bind_port;
141 char * psz_server_addr = p_socket->psz_server_addr;
142 int i_server_port = p_socket->i_server_port;
143 #if defined( WIN32 ) && !defined( UNDER_CE )
144 char * psz_bind_win32; /* WIN32 multicast kludge */
148 socklen_t i_opt_size;
149 struct sockaddr_in sock;
151 if( i_bind_port == 0 )
153 i_bind_port = config_GetInt( p_this, "server-port" );
156 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
158 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
161 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
163 msg_Err( p_this, "cannot create socket" );
168 /* We may want to reuse an already used socket */
170 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
171 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
174 msg_Err( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
177 msg_Err( p_this, "cannot configure socket (SO_REUSEADDR)" );
179 #if defined( WIN32 ) || defined( UNDER_CE )
180 closesocket( i_handle );
187 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
188 * packet loss caused by scheduling problems */
190 #if defined( SYS_BEOS )
191 if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
193 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
195 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
198 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF: %s)",
201 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF)" );
205 /* Check if we really got what we have asked for, because Linux, etc.
206 * will silently limit the max buffer size to net.core.rmem_max which
207 * is typically only 65535 bytes */
209 i_opt_size = sizeof( i_opt );
210 #if defined( SYS_BEOS )
211 if( getsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
213 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
215 (void*) &i_opt, &i_opt_size ) == -1 )
218 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
221 msg_Warn( p_this, "cannot query socket (SO_RCVBUF)" );
224 else if( i_opt < 0x80000 )
226 msg_Warn( p_this, "socket buffer size is 0x%x instead of 0x%x",
231 /* Build the local socket */
233 #if defined( WIN32 ) && !defined( UNDER_CE )
234 /* Under Win32 and for the multicast, we bind on INADDR_ANY,
235 * so let's call BuildAddr with "" instead of psz_bind_addr */
236 psz_bind_win32 = psz_bind_addr ;
238 /* Check if this is a multicast socket */
239 if (IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
243 if ( BuildAddr( &sock, psz_bind_win32, i_bind_port ) == -1 )
245 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
248 msg_Dbg( p_this, "could not build local address" );
249 #if defined( WIN32 ) || defined( UNDER_CE )
250 closesocket( i_handle );
258 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
261 msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
263 msg_Err( p_this, "cannot bind socket" );
265 #if defined( WIN32 ) || defined( UNDER_CE )
266 closesocket( i_handle );
273 /* Allow broadcast reception if we bound on INADDR_ANY */
274 if( !*psz_bind_addr )
277 #if defined( SYS_BEOS )
278 if( setsockopt( i_handle, SOL_SOCKET, SO_NONBLOCK,
280 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
282 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
285 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
288 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST)" );
293 #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
294 /* Join the multicast group if the socket is a multicast address */
296 # define IN_MULTICAST(a) IN_CLASSD(a)
300 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
303 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
304 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
306 if( IN_MULTICAST( ntohl(inet_addr(psz_bind_addr) ) ) )
309 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
310 imr.imr_multiaddr.s_addr = inet_addr(psz_bind_addr);
312 if ( psz_if_addr != NULL && *psz_if_addr
313 && inet_addr(psz_if_addr) != -1 )
315 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
319 imr.imr_interface.s_addr = INADDR_ANY;
321 if ( psz_if_addr != NULL ) free( psz_if_addr );
323 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
324 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
327 msg_Err( p_this, "failed to join IP multicast group (%s)",
330 msg_Err( p_this, "failed to join IP multicast group" );
332 #if defined( WIN32 ) || defined( UNDER_CE )
333 closesocket( i_handle );
340 #endif /* UNDER_CE */
342 if( *psz_server_addr )
344 /* Build socket for remote connection */
345 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
347 msg_Err( p_this, "cannot build remote address" );
348 #if defined( WIN32 ) || defined( UNDER_CE )
349 closesocket( i_handle );
356 /* Connect the socket */
357 if( connect( i_handle, (struct sockaddr *) &sock,
358 sizeof( sock ) ) == (-1) )
361 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
363 msg_Err( p_this, "cannot connect socket" );
365 #if defined( WIN32 ) || defined( UNDER_CE )
366 closesocket( i_handle );
374 p_socket->i_handle = i_handle;
375 p_socket->i_mtu = config_GetInt( p_this, "mtu" );
379 /*****************************************************************************
380 * OpenTCP: open a TCP socket
381 *****************************************************************************
382 * psz_server_addr, i_server_port : address and port used for the connect()
383 * system call. If i_server_port == 0, 80 is used.
384 * Other parameters are ignored.
385 * This function returns -1 in case of error.
386 *****************************************************************************/
387 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
389 char * psz_server_addr = p_socket->psz_server_addr;
390 int i_server_port = p_socket->i_server_port;
393 struct sockaddr_in sock;
395 if( i_server_port == 0 )
400 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
402 if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
405 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
407 msg_Err( p_this, "cannot create socket" );
412 /* Build remote address */
413 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
415 msg_Dbg( p_this, "could not build local address" );
416 #if defined( WIN32 ) || defined( UNDER_CE )
417 closesocket( i_handle );
424 /* Connect the socket */
425 if( connect( i_handle, (struct sockaddr *) &sock,
426 sizeof( sock ) ) == (-1) )
429 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
431 msg_Err( p_this, "cannot connect socket" );
433 #if defined( WIN32 ) || defined( UNDER_CE )
434 closesocket( i_handle );
441 p_socket->i_handle = i_handle;
442 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
447 /*****************************************************************************
448 * NetOpen: wrapper around OpenUDP and OpenTCP
449 *****************************************************************************/
450 static int NetOpen( vlc_object_t * p_this )
452 network_socket_t * p_socket = p_this->p_private;
454 if( p_socket->i_type == NETWORK_UDP )
456 return OpenUDP( p_this, p_socket );
460 return OpenTCP( p_this, p_socket );