1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@videolan.org>
8 * RĂ©mi Denis-Courmont <rem # videolan.org>
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 *****************************************************************************/
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
46 # if defined(UNDER_CE)
47 # undef IP_MULTICAST_TTL
48 # define IP_MULTICAST_TTL 3
49 # undef IP_ADD_MEMBERSHIP
50 # define IP_ADD_MEMBERSHIP 5
52 # define EAFNOSUPPORT WSAEAFNOSUPPORT
56 # define SOL_IP IPPROTO_IP
59 # define SOL_IPV6 IPPROTO_IPV6
62 # define IPPROTO_IPV6 41
65 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
69 static int net_SetMcastHopLimit( vlc_object_t *p_this,
70 int fd, int family, int hlim )
75 /* There is some confusion in the world whether IP_MULTICAST_TTL
76 * takes a byte or an int as an argument.
77 * BSD seems to indicate byte so we are going with that and use
78 * int as a fallback to be safe */
83 cmd = IP_MULTICAST_TTL;
86 #ifdef IPV6_MULTICAST_HOPS
89 cmd = IPV6_MULTICAST_HOPS;
94 msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
98 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
100 /* BSD compatibility */
103 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
104 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
112 static int net_SetMcastSource( vlc_object_t *p_this,
113 int fd, int family, const char *str )
122 if( inet_pton( AF_INET, str, &addr) <= 0 )
124 msg_Err( p_this, "Invalid multicast interface %s",
129 if( setsockopt( fd, IPPROTO_IP, IP_MULTICAST_IF, &addr,
130 sizeof( addr ) ) < 0 )
132 msg_Dbg( p_this, "Cannot set multicast interface (%s)",
139 #ifdef IPV6_MULTICAST_IF
143 msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
151 /*****************************************************************************
153 *****************************************************************************
154 * Open a UDP socket to send data to a defined destination, with an optional
156 *****************************************************************************/
157 int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
160 struct addrinfo hints, *res, *ptr;
161 int i_val, i_handle = -1;
162 vlc_bool_t b_unreach = VLC_FALSE;
165 i_port = 1234; /* historical VLC thing */
171 if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS )
173 var_Create( p_this, "ttl", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
174 var_Get( p_this, "ttl", &val );
178 if( i_hlim < 1 ) i_hlim = 1;
181 memset( &hints, 0, sizeof( hints ) );
182 hints.ai_socktype = SOCK_DGRAM;
184 msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
186 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
189 msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
190 vlc_gai_strerror( i_val ) );
194 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
199 fd = net_Socket( p_this, ptr->ai_family, ptr->ai_socktype,
203 #if !defined( SYS_BEOS )
208 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
209 * to avoid packet loss caused by scheduling problems */
211 setsockopt( fd, SOL_SOCKET, SO_RCVBUF, (void *)&i_val,
214 setsockopt( fd, SOL_SOCKET, SO_SNDBUF, (void *)&i_val,
217 /* Allow broadcast sending */
219 setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*)&i_val,
224 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
225 psz_mif_addr = config_GetPsz( p_this, "miface-addr" );
226 if( psz_mif_addr != NULL )
228 net_SetMcastSource( p_this, fd, ptr->ai_family, psz_mif_addr );
229 free( psz_mif_addr );
232 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
239 #if defined( WIN32 ) || defined( UNDER_CE )
240 if( WSAGetLastError () == WSAENETUNREACH )
242 if( errno == ENETUNREACH )
244 b_unreach = VLC_TRUE;
247 msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
254 vlc_freeaddrinfo( res );
259 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
267 /*****************************************************************************
269 *****************************************************************************
270 * Open a UDP connection and return a handle
271 *****************************************************************************/
272 int __net_OpenUDP( vlc_object_t *p_this, const char *psz_bind, int i_bind,
273 const char *psz_server, int i_server )
277 network_socket_t sock;
278 module_t *p_network = NULL;
280 if( ( psz_server != NULL ) && ( psz_server[0] == '\0' ) )
281 msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
282 "is obsolete - use net_ConnectUDP instead" );
284 msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
285 "port is obsolete - use __net_ConnectUDP instead" );
287 if( psz_server == NULL ) psz_server = "";
288 if( psz_bind == NULL ) psz_bind = "";
290 /* Prepare the network_socket_t structure */
291 sock.psz_bind_addr = psz_bind;
292 sock.i_bind_port = i_bind;
293 sock.psz_server_addr = psz_server;
294 sock.i_server_port = i_server;
299 msg_Dbg( p_this, "net: connecting to '[%s]:%d@[%s]:%d'",
300 psz_server, i_server, psz_bind, i_bind );
302 /* Check if we have force ipv4 or ipv6 */
303 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
304 var_Get( p_this, "ipv4", &v4 );
305 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
306 var_Get( p_this, "ipv6", &v6 );
313 /* try IPv6 first (unless IPv4 forced) */
314 private = p_this->p_private;
315 p_this->p_private = (void*)&sock;
316 p_network = module_Need( p_this, "network", "ipv6", VLC_TRUE );
318 if( p_network != NULL )
319 module_Unneed( p_this, p_network );
321 p_this->p_private = private;
324 * Check if the IP stack can receive IPv4 packets on IPv6 sockets.
325 * If yes, then it is better to use the IPv6 socket.
326 * Otherwise, if we also get an IPv4, we have to choose, so we use
329 if( ( sock.i_handle != -1 ) && ( ( sock.v6only == 0 ) || v6.b_bool ) )
330 return sock.i_handle;
335 int fd6 = sock.i_handle;
337 /* also try IPv4 (unless IPv6 forced) */
338 private = p_this->p_private;
339 p_this->p_private = (void*)&sock;
340 p_network = module_Need( p_this, "network", "ipv4", VLC_TRUE );
342 if( p_network != NULL )
343 module_Unneed( p_this, p_network );
345 p_this->p_private = private;
349 if( sock.i_handle != -1 )
351 msg_Warn( p_this, "net: lame IPv6/IPv4 dual-stack present. "
352 "Using only IPv4." );
360 if( sock.i_handle == -1 )
361 msg_Dbg( p_this, "net: connection to '[%s]:%d@[%s]:%d' failed",
362 psz_server, i_server, psz_bind, i_bind );
364 return sock.i_handle;