1 /*****************************************************************************
2 * ipv4.c: IPv4 network abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ipv4.c,v 1.5 2002/11/14 22:38:48 massiot 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 )
106 /* Copy the first address of the host in the socket address */
107 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
108 p_hostent->h_length );
114 /*****************************************************************************
115 * OpenUDP: open a UDP socket
116 *****************************************************************************
117 * psz_bind_addr, i_bind_port : address and port used for the bind()
118 * system call. If psz_bind_addr == "", the socket is bound to
119 * INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
120 * 1234 is used. If psz_bind_addr is a multicast (class D) address,
121 * join the multicast group.
122 * psz_server_addr, i_server_port : address and port used for the connect()
123 * system call. It can avoid receiving packets from unauthorized IPs.
124 * Its use leads to great confusion and is currently discouraged.
125 * This function returns -1 in case of error.
126 *****************************************************************************/
127 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
129 char * psz_bind_addr = p_socket->psz_bind_addr;
130 int i_bind_port = p_socket->i_bind_port;
131 char * psz_server_addr = p_socket->psz_server_addr;
132 int i_server_port = p_socket->i_server_port;
134 char * psz_bind_win32; /* WIN32 multicast kludge */
138 unsigned int 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 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
244 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
246 if( IN_MULTICAST( ntohl(inet_addr(psz_bind_addr) ) ) )
249 char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
250 imr.imr_multiaddr.s_addr = inet_addr(psz_bind_addr);
252 if ( psz_if_addr != NULL && *psz_if_addr
253 && inet_addr(psz_if_addr) != -1 )
255 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
259 imr.imr_interface.s_addr = INADDR_ANY;
261 if ( psz_if_addr != NULL ) free( psz_if_addr );
263 if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
264 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
266 msg_Err( p_this, "failed to join IP multicast group (%s)",
273 if( *psz_server_addr )
275 /* Build socket for remote connection */
276 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
278 msg_Err( p_this, "cannot build remote address" );
283 /* Connect the socket */
284 if( connect( i_handle, (struct sockaddr *) &sock,
285 sizeof( sock ) ) == (-1) )
287 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
293 p_socket->i_handle = i_handle;
294 p_socket->i_mtu = config_GetInt( p_this, "mtu" );
298 /*****************************************************************************
299 * OpenTCP: open a TCP socket
300 *****************************************************************************
301 * psz_server_addr, i_server_port : address and port used for the connect()
302 * system call. If i_server_port == 0, 80 is used.
303 * Other parameters are ignored.
304 * This function returns -1 in case of error.
305 *****************************************************************************/
306 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
308 char * psz_server_addr = p_socket->psz_server_addr;
309 int i_server_port = p_socket->i_server_port;
312 struct sockaddr_in sock;
314 if( i_server_port == 0 )
319 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
321 if( (i_handle = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
323 msg_Err( p_this, "cannot create socket (%s)", strerror(errno) );
327 /* Build remote address */
328 if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
334 /* Connect the socket */
335 if( connect( i_handle, (struct sockaddr *) &sock,
336 sizeof( sock ) ) == (-1) )
338 msg_Err( p_this, "cannot connect socket (%s)", strerror(errno) );
343 p_socket->i_handle = i_handle;
344 p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
349 /*****************************************************************************
350 * NetOpen: wrapper around OpenUDP and OpenTCP
351 *****************************************************************************/
352 static int NetOpen( vlc_object_t * p_this )
354 network_socket_t * p_socket = p_this->p_private;
356 if( p_socket->i_type == NETWORK_UDP )
358 return OpenUDP( p_this, p_socket );
362 return OpenTCP( p_this, p_socket );