1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
5 * Copyright © 2006 Rémi Denis-Courmont
9 * Authors: Laurent Aimar <fenrir@videolan.org>
10 * Rémi Denis-Courmont <rem # videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
42 # if defined(UNDER_CE)
43 # undef IP_MULTICAST_TTL
44 # define IP_MULTICAST_TTL 3
45 # undef IP_ADD_MEMBERSHIP
46 # define IP_ADD_MEMBERSHIP 5
48 # define EAFNOSUPPORT WSAEAFNOSUPPORT
49 # define if_nametoindex( str ) atoi( str )
58 # define SOL_IP IPPROTO_IP
61 # define SOL_IPV6 IPPROTO_IPV6
64 # define IPPROTO_IPV6 41
67 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
71 static int net_SetMcastHopLimit( vlc_object_t *p_this,
72 int fd, int family, int hlim )
77 /* There is some confusion in the world whether IP_MULTICAST_TTL
78 * takes a byte or an int as an argument.
79 * BSD seems to indicate byte so we are going with that and use
80 * int as a fallback to be safe */
85 cmd = IP_MULTICAST_TTL;
88 #ifdef IPV6_MULTICAST_HOPS
91 cmd = IPV6_MULTICAST_HOPS;
96 msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
100 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
102 /* BSD compatibility */
105 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
106 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
114 static int net_SetMcastOutIface (int fd, int family, int scope)
118 #ifdef IPV6_MULTICAST_IF
120 return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
121 &scope, sizeof (scope));
127 struct ip_mreqn req = { .imr_ifindex = scope };
129 return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
135 errno = EAFNOSUPPORT;
140 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
142 #ifdef IP_MULTICAST_IF
143 return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
145 errno = EAFNOSUPPORT;
151 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
152 const char *iface, const char *addr)
156 int scope = if_nametoindex (iface);
159 msg_Err (p_this, "%s: invalid interface for multicast", iface);
163 if (net_SetMcastOutIface (fd, family, scope) == 0)
166 msg_Err (p_this, "%s: %s", iface, net_strerror (net_errno));
171 if (family == AF_INET)
174 if (inet_pton (AF_INET, addr, &ipv4) <= 0)
176 msg_Err (p_this, "%s: invalid IPv4 address for multicast",
181 if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
184 msg_Err (p_this, "%s: %s", addr, net_strerror (net_errno));
192 int net_SetDSCP( int fd, uint8_t dscp )
194 struct sockaddr_storage addr;
195 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
200 switch( addr.ss_family )
221 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
225 /*****************************************************************************
227 *****************************************************************************
228 * Open a UDP socket to send data to a defined destination, with an optional
230 *****************************************************************************/
231 int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
234 struct addrinfo hints, *res, *ptr;
235 int i_val, i_handle = -1;
236 vlc_bool_t b_unreach = VLC_FALSE;
239 i_port = 1234; /* historical VLC thing */
242 i_hlim = var_CreateGetInteger( p_this, "ttl" );
244 memset( &hints, 0, sizeof( hints ) );
245 hints.ai_socktype = SOCK_DGRAM;
247 msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
249 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
252 msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
253 vlc_gai_strerror( i_val ) );
257 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
260 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
265 #if !defined( SYS_BEOS )
266 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
267 * to avoid packet loss caused by scheduling problems */
268 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
269 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
271 /* Allow broadcast sending */
272 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
276 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
278 str = var_CreateGetString (p_this, "miface");
281 net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
285 str = var_CreateGetString (p_this, "miface-addr");
288 net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
292 net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
294 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
301 #if defined( WIN32 ) || defined( UNDER_CE )
302 if( WSAGetLastError () == WSAENETUNREACH )
304 if( errno == ENETUNREACH )
306 b_unreach = VLC_TRUE;
309 msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
316 vlc_freeaddrinfo( res );
321 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
329 /*****************************************************************************
331 *****************************************************************************
332 * Open a UDP connection and return a handle
333 *****************************************************************************/
334 int __net_OpenUDP( vlc_object_t *p_this, const char *psz_bind, int i_bind,
335 const char *psz_server, int i_server )
339 network_socket_t sock;
340 module_t *p_network = NULL;
342 /* if( ( psz_server != NULL ) && ( psz_server[0] == '\0' ) )
343 msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
344 "is obsolete - use net_ConnectUDP instead" );
346 msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
347 "port is obsolete - use __net_ConnectUDP instead" );*/
349 if( psz_server == NULL ) psz_server = "";
350 if( psz_bind == NULL ) psz_bind = "";
352 /* Prepare the network_socket_t structure */
353 sock.psz_bind_addr = psz_bind;
354 sock.i_bind_port = i_bind;
355 sock.psz_server_addr = psz_server;
356 sock.i_server_port = i_server;
361 msg_Dbg( p_this, "net: connecting to '[%s]:%d@[%s]:%d'",
362 psz_server, i_server, psz_bind, i_bind );
364 /* Check if we have force ipv4 or ipv6 */
365 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
366 var_Get( p_this, "ipv4", &v4 );
367 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
368 var_Get( p_this, "ipv6", &v6 );
375 /* try IPv6 first (unless IPv4 forced) */
376 private = p_this->p_private;
377 p_this->p_private = (void*)&sock;
378 p_network = module_Need( p_this, "network", "ipv6", VLC_TRUE );
380 if( p_network != NULL )
381 module_Unneed( p_this, p_network );
383 p_this->p_private = private;
386 * Check if the IP stack can receive IPv4 packets on IPv6 sockets.
387 * If yes, then it is better to use the IPv6 socket.
388 * Otherwise, if we also get an IPv4, we have to choose, so we use
391 if( ( sock.i_handle != -1 ) && ( ( sock.v6only == 0 ) || v6.b_bool ) )
392 return sock.i_handle;
397 int fd6 = sock.i_handle;
399 /* also try IPv4 (unless IPv6 forced) */
400 private = p_this->p_private;
401 p_this->p_private = (void*)&sock;
402 p_network = module_Need( p_this, "network", "ipv4", VLC_TRUE );
404 if( p_network != NULL )
405 module_Unneed( p_this, p_network );
407 p_this->p_private = private;
411 if( sock.i_handle != -1 )
413 msg_Warn( p_this, "net: lame IPv6/IPv4 dual-stack present, "
414 "using only IPv4." );
422 if( sock.i_handle == -1 )
423 msg_Dbg( p_this, "net: connection to '[%s]:%d@[%s]:%d' failed",
424 psz_server, i_server, psz_bind, i_bind );
426 return sock.i_handle;