1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ipv4.c,v 1.15 2002/07/31 20:56:52 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 *****************************************************************************/
29 #include <sys/types.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
44 # include <winsock2.h>
45 # include <ws2tcpip.h>
47 # define IN_MULTICAST(a) IN_CLASSD(a)
50 # include <netdb.h> /* hostent ... */
51 # include <sys/socket.h>
52 # include <netinet/in.h>
53 # ifdef HAVE_ARPA_INET_H
54 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
60 /*****************************************************************************
62 *****************************************************************************/
63 static int NetOpen( vlc_object_t * );
65 /*****************************************************************************
67 *****************************************************************************/
69 set_description( _("IPv4 network abstraction layer") );
70 set_capability( "network", 50 );
71 set_callbacks( NetOpen, NULL );
74 /*****************************************************************************
75 * BuildAddr: utility function to build a struct sockaddr_in
76 *****************************************************************************/
77 static int BuildAddr( struct sockaddr_in * p_socket,
78 const char * psz_address, int i_port )
81 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
82 p_socket->sin_family = AF_INET; /* family */
83 p_socket->sin_port = htons( i_port );
86 p_socket->sin_addr.s_addr = INADDR_ANY;
90 struct hostent * p_hostent;
92 /* Try to convert address directly from in_addr - this will work if
93 * psz_address is dotted decimal. */
94 #ifdef HAVE_ARPA_INET_H
95 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
97 if( (p_socket->sin_addr.s_addr = inet_addr( psz_address )) == -1 )
100 /* We have a fqdn, try to find its address */
101 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
103 //X intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_address );
107 /* Copy the first address of the host in the socket address */
108 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
109 p_hostent->h_length );
115 /*****************************************************************************
116 * OpenUDP: open a UDP socket
117 *****************************************************************************
118 * psz_bind_addr, i_bind_port : address and port used for the bind()
119 * system call. If psz_bind_addr == "", the socket is bound to
120 * INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
121 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
122 * join the multicast group.
123 * psz_server_addr, i_server_port : address and port used for the connect()
124 * system call. It can avoid receiving packets from unauthorized IPs.
125 * Its use leads to great confusion and is currently discouraged.
126 * This function returns -1 in case of error.
127 *****************************************************************************/
128 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
130 char * psz_bind_addr = p_socket->psz_bind_addr;
131 int i_bind_port = p_socket->i_bind_port;
132 char * psz_server_addr = p_socket->psz_server_addr;
133 int i_server_port = p_socket->i_server_port;
135 char * psz_bind_win32; /* WIN32 multicast kludge */
138 int i_handle, i_opt, i_opt_size;
139 struct sockaddr_in sock;
141 if( i_bind_port == 0 )
143 i_bind_port = config_GetInt( p_this, "server-port" );
146 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
148 if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
150 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
154 /* We may want to reuse an already used socket */
156 if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
157 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
159 msg_Err( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
165 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
166 * packet loss caused by scheduling problems */
168 if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
169 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
171 msg_Warn( p_this, "cannot configure socket (SO_RCVBUF: %s)",
175 /* Check if we really got what we have asked for, because Linux, etc.
176 * will silently limit the max buffer size to net.core.rmem_max which
177 * is typically only 65535 bytes */
179 i_opt_size = sizeof( i_opt );
180 if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF,
181 (void*) &i_opt, &i_opt_size ) == -1 )
183 msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
186 else if( i_opt < 0x80000 )
188 msg_Warn( p_this, "socket buffer size is 0x%x instead of 0x%x",
193 /* Build the local socket */
196 /* Under Win32 and for the multicast, we bind on INADDR_ANY,
197 * so let's call BuildAddr with "" instead of psz_bind_addr */
198 psz_bind_win32 = psz_bind_addr ;
200 /* Check if this is a multicast socket */
201 if (IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
205 if ( BuildAddr( &sock, psz_bind_win32, i_bind_port ) == -1 )
207 if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
215 if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
217 msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
222 /* Allow broadcast reception if we bound on INADDR_ANY */
223 if( !*psz_bind_addr )
226 if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST,
227 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
229 msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
234 /* Join the multicast group if the socket is a multicast address */
236 # define IN_MULTICAST(a) IN_CLASSD(a)
240 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
243 imr.imr_interface.s_addr = INADDR_ANY;
244 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
246 if( IN_MULTICAST( ntohl(inet_addr(psz_bind_addr) ) ) )
249 imr.imr_interface.s_addr = INADDR_ANY;
250 imr.imr_multiaddr.s_addr = inet_addr(psz_bind_addr);
252 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
253 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
255 msg_Err( p_this, "failed to join IP multicast group (%s)",
262 if( *psz_server_addr )
264 /* Build socket for remote connection */
265 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
267 msg_Err( p_this, "cannot build remote address" );
272 /* Connect the socket */
273 if( connect( i_handle, (struct sockaddr *) &sock,
274 sizeof( sock ) ) == (-1) )
276 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
282 p_socket->i_handle = i_handle;
283 p_socket->i_mtu = config_GetInt( p_this, "mtu" );
287 /*****************************************************************************
288 * OpenTCP: open a TCP socket
289 *****************************************************************************
290 * psz_server_addr, i_server_port : address and port used for the connect()
291 * system call. If i_server_port == 0, 80 is used.
292 * Other parameters are ignored.
293 * This function returns -1 in case of error.
294 *****************************************************************************/
295 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
297 char * psz_server_addr = p_socket->psz_server_addr;
298 int i_server_port = p_socket->i_server_port;
301 struct sockaddr_in sock;
303 if( i_server_port == 0 )
308 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
310 if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
312 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
316 /* Build remote address */
317 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
323 /* Connect the socket */
324 if( connect( i_handle, (struct sockaddr *) &sock,
325 sizeof( sock ) ) == (-1) )
327 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
332 p_socket->i_handle = i_handle;
333 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
338 /*****************************************************************************
339 * NetOpen: wrapper around OpenUDP and OpenTCP
340 *****************************************************************************/
341 static int NetOpen( vlc_object_t * p_this )
343 network_socket_t * p_socket = p_this->p_private;
345 if( p_socket->i_type == NETWORK_UDP )
347 return OpenUDP( p_this, p_socket );
351 return OpenTCP( p_this, p_socket );