1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
40 # if defined(UNDER_CE)
41 # undef IP_MULTICAST_TTL
42 # define IP_MULTICAST_TTL 3
43 # undef IP_ADD_MEMBERSHIP
44 # define IP_ADD_MEMBERSHIP 5
46 # define EAFNOSUPPORT WSAEAFNOSUPPORT
47 # define if_nametoindex( str ) atoi( str )
54 # define SOL_IP IPPROTO_IP
57 # define SOL_IPV6 IPPROTO_IPV6
60 # define IPPROTO_IPV6 41
63 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
67 static int net_SetMcastHopLimit( vlc_object_t *p_this,
68 int fd, int family, int hlim )
73 /* There is some confusion in the world whether IP_MULTICAST_TTL
74 * takes a byte or an int as an argument.
75 * BSD seems to indicate byte so we are going with that and use
76 * int as a fallback to be safe */
81 cmd = IP_MULTICAST_TTL;
84 #ifdef IPV6_MULTICAST_HOPS
87 cmd = IPV6_MULTICAST_HOPS;
92 msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
96 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
98 /* BSD compatibility */
101 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
102 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
110 static int net_SetMcastIface( vlc_object_t *p_this,
111 int fd, int family, const char *str )
120 if( inet_pton( AF_INET, str, &addr) <= 0 )
122 msg_Err( p_this, "Invalid multicast interface %s", str );
126 if( setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &addr,
127 sizeof( addr ) ) < 0 )
129 msg_Err( p_this, "Cannot use %s as multicast interface: %s",
130 str, strerror(errno) );
135 #endif /* SYS_BEOS */
137 #ifdef IPV6_MULTICAST_IF
140 int scope = if_nametoindex( str );
144 msg_Err( p_this, "Invalid multicast interface %s", str );
148 if( setsockopt( fd, SOL_IPV6, IPV6_MULTICAST_IF,
149 &scope, sizeof( scope ) ) < 0 )
151 msg_Err( p_this, "Cannot use %s as multicast interface: %s",
152 str, strerror( errno ) );
160 msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
167 /*****************************************************************************
169 *****************************************************************************
170 * Open a UDP socket to send data to a defined destination, with an optional
172 *****************************************************************************/
173 int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
176 struct addrinfo hints, *res, *ptr;
177 int i_val, i_handle = -1;
178 vlc_bool_t b_unreach = VLC_FALSE;
181 i_port = 1234; /* historical VLC thing */
184 i_hlim = var_CreateGetInteger( p_this, "ttl" );
186 memset( &hints, 0, sizeof( hints ) );
187 hints.ai_socktype = SOCK_DGRAM;
189 msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
191 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
194 msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
195 vlc_gai_strerror( i_val ) );
199 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
204 fd = net_Socket( p_this, ptr->ai_family, ptr->ai_socktype,
208 #if !defined( SYS_BEOS )
213 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
214 * to avoid packet loss caused by scheduling problems */
216 setsockopt( fd, SOL_SOCKET, SO_RCVBUF, (void *)&i_val,
219 setsockopt( fd, SOL_SOCKET, SO_SNDBUF, (void *)&i_val,
222 /* Allow broadcast sending */
224 setsockopt( fd, SOL_SOCKET, SO_BROADCAST, (void*)&i_val,
230 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
231 psz_mif = config_GetPsz( p_this, (ptr->ai_family != AF_INET)
232 ? "miface" : "miface-addr" );
233 if( psz_mif != NULL )
235 net_SetMcastIface( p_this, fd, ptr->ai_family, psz_mif );
239 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
246 #if defined( WIN32 ) || defined( UNDER_CE )
247 if( WSAGetLastError () == WSAENETUNREACH )
249 if( errno == ENETUNREACH )
251 b_unreach = VLC_TRUE;
254 msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
261 vlc_freeaddrinfo( res );
266 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
274 /*****************************************************************************
276 *****************************************************************************
277 * Open a UDP connection and return a handle
278 *****************************************************************************/
279 int __net_OpenUDP( vlc_object_t *p_this, const char *psz_bind, int i_bind,
280 const char *psz_server, int i_server )
284 network_socket_t sock;
285 module_t *p_network = NULL;
287 if( ( psz_server != NULL ) && ( psz_server[0] == '\0' ) )
288 msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
289 "is obsolete - use net_ConnectUDP instead" );
291 msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
292 "port is obsolete - use __net_ConnectUDP instead" );
294 if( psz_server == NULL ) psz_server = "";
295 if( psz_bind == NULL ) psz_bind = "";
297 /* Prepare the network_socket_t structure */
298 sock.psz_bind_addr = psz_bind;
299 sock.i_bind_port = i_bind;
300 sock.psz_server_addr = psz_server;
301 sock.i_server_port = i_server;
306 msg_Dbg( p_this, "net: connecting to '[%s]:%d@[%s]:%d'",
307 psz_server, i_server, psz_bind, i_bind );
309 /* Check if we have force ipv4 or ipv6 */
310 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
311 var_Get( p_this, "ipv4", &v4 );
312 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
313 var_Get( p_this, "ipv6", &v6 );
320 /* try IPv6 first (unless IPv4 forced) */
321 private = p_this->p_private;
322 p_this->p_private = (void*)&sock;
323 p_network = module_Need( p_this, "network", "ipv6", VLC_TRUE );
325 if( p_network != NULL )
326 module_Unneed( p_this, p_network );
328 p_this->p_private = private;
331 * Check if the IP stack can receive IPv4 packets on IPv6 sockets.
332 * If yes, then it is better to use the IPv6 socket.
333 * Otherwise, if we also get an IPv4, we have to choose, so we use
336 if( ( sock.i_handle != -1 ) && ( ( sock.v6only == 0 ) || v6.b_bool ) )
337 return sock.i_handle;
342 int fd6 = sock.i_handle;
344 /* also try IPv4 (unless IPv6 forced) */
345 private = p_this->p_private;
346 p_this->p_private = (void*)&sock;
347 p_network = module_Need( p_this, "network", "ipv4", VLC_TRUE );
349 if( p_network != NULL )
350 module_Unneed( p_this, p_network );
352 p_this->p_private = private;
356 if( sock.i_handle != -1 )
358 msg_Warn( p_this, "net: lame IPv6/IPv4 dual-stack present. "
359 "Using only IPv4." );
367 if( sock.i_handle == -1 )
368 msg_Dbg( p_this, "net: connection to '[%s]:%d@[%s]:%d' failed",
369 psz_server, i_server, psz_bind, i_bind );
371 return sock.i_handle;