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 *****************************************************************************/
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
43 # if defined(UNDER_CE)
44 # undef IP_MULTICAST_TTL
45 # define IP_MULTICAST_TTL 3
46 # undef IP_ADD_MEMBERSHIP
47 # define IP_ADD_MEMBERSHIP 5
49 # define EAFNOSUPPORT WSAEAFNOSUPPORT
50 # define if_nametoindex( str ) atoi( str )
59 # define SOL_IP IPPROTO_IP
62 # define SOL_IPV6 IPPROTO_IPV6
65 # define IPPROTO_IPV6 41
68 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
72 static int net_SetMcastHopLimit( vlc_object_t *p_this,
73 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 */
83 #ifdef IP_MULTICAST_TTL
86 cmd = IP_MULTICAST_TTL;
90 #ifdef IPV6_MULTICAST_HOPS
93 cmd = IPV6_MULTICAST_HOPS;
98 msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
102 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
104 /* BSD compatibility */
107 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
108 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
116 static int net_SetMcastOutIface (int fd, int family, int scope)
120 #ifdef IPV6_MULTICAST_IF
122 return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
123 &scope, sizeof (scope));
129 struct ip_mreqn req = { .imr_ifindex = scope };
131 return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
137 errno = EAFNOSUPPORT;
142 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
144 #ifdef IP_MULTICAST_IF
145 return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
147 errno = EAFNOSUPPORT;
153 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
154 const char *iface, const char *addr)
158 int scope = if_nametoindex (iface);
161 msg_Err (p_this, "%s: invalid interface for multicast", iface);
165 if (net_SetMcastOutIface (fd, family, scope) == 0)
168 msg_Err (p_this, "%s: %s", iface, net_strerror (net_errno));
173 if (family == AF_INET)
176 if (inet_pton (AF_INET, addr, &ipv4) <= 0)
178 msg_Err (p_this, "%s: invalid IPv4 address for multicast",
183 if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
186 msg_Err (p_this, "%s: %s", addr, net_strerror (net_errno));
194 int net_Subscribe (vlc_object_t *obj, int fd, const struct sockaddr *addr,
197 switch (addr->sa_family)
199 #ifdef IP_ADD_MEMBERSHIP
202 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
203 if (addrlen < sizeof (*v4))
207 memset (&imr, 0, sizeof (imr));
208 memcpy (&imr.imr_multiaddr, &v4->sin_addr, 4);
210 /* FIXME: should use a different option for in and out */
211 char *iif = var_CreateGetString (obj, "miface-addr");
214 if ((iif[0] != '\0') &&
215 (inet_pton (AF_INET, iif, &imr.imr_interface) <= 0))
217 msg_Err (obj, "invalid multicast interface address %s",
225 msg_Dbg (obj, "IP_ADD_MEMBERSHIP multicast request");
227 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr,
230 msg_Err (obj, "cannot join IPv4 multicast group (%s)",
231 net_strerror (net_errno));
238 #ifdef IPV6_JOIN_GROUP
241 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
242 if (addrlen < sizeof (*v6))
245 struct ipv6_mreq imr;
246 memset (&imr, 0, sizeof (imr));
247 imr.ipv6mr_interface = v6->sin6_scope_id;
248 memcpy (&imr.ipv6mr_multiaddr, &v6->sin6_addr, 16);
250 msg_Dbg (obj, "IPV6_JOIN_GROUP multicast request");
252 if (setsockopt (fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &imr,
255 msg_Err (obj, "cannot join IPv6 multicast group (%s)",
256 net_strerror (net_errno));
265 msg_Err (obj, "Multicast not supported");
270 int net_SetDSCP( int fd, uint8_t dscp )
272 struct sockaddr_storage addr;
273 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
278 switch( addr.ss_family )
299 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
303 /*****************************************************************************
305 *****************************************************************************
306 * Open a UDP socket to send data to a defined destination, with an optional
308 *****************************************************************************/
309 int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
312 struct addrinfo hints, *res, *ptr;
313 int i_val, i_handle = -1;
314 vlc_bool_t b_unreach = VLC_FALSE;
317 i_port = 1234; /* historical VLC thing */
320 i_hlim = var_CreateGetInteger( p_this, "ttl" );
322 memset( &hints, 0, sizeof( hints ) );
323 hints.ai_socktype = SOCK_DGRAM;
325 msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
327 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
330 msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
331 vlc_gai_strerror( i_val ) );
335 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
338 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
343 #if !defined( SYS_BEOS )
344 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
345 * to avoid packet loss caused by scheduling problems */
346 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
347 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
349 /* Allow broadcast sending */
350 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
354 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
356 str = var_CreateGetString (p_this, "miface");
360 net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
364 str = var_CreateGetString (p_this, "miface-addr");
368 net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
372 net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
374 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
381 #if defined( WIN32 ) || defined( UNDER_CE )
382 if( WSAGetLastError () == WSAENETUNREACH )
384 if( errno == ENETUNREACH )
386 b_unreach = VLC_TRUE;
389 msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
396 vlc_freeaddrinfo( res );
401 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
410 /*****************************************************************************
412 *****************************************************************************
413 * Open a UDP connection and return a handle
414 *****************************************************************************/
415 int __net_OpenUDP( vlc_object_t *p_this, const char *psz_bind, int i_bind,
416 const char *psz_server, int i_server )
419 network_socket_t sock;
420 module_t *p_network = NULL;
422 if (((psz_server == NULL) || (psz_server[0] == '\0')) && (i_server == 0))
425 "Obsolete net_OpenUDP with no remote endpoint; "
426 "Use net_ListenUDP instead");
427 return net_ListenUDP1 (p_this, psz_bind, i_bind);
430 if (((psz_bind == NULL) || (psz_bind[0] == '\0')) && (i_bind == 0))
433 "Obsolete net_OpenUDP with no local endpoint; "
434 "Use net_ConnectUDP instead");
435 return net_ConnectUDP (p_this, psz_server, i_server, -1);
438 if( psz_server == NULL ) psz_server = "";
439 if( psz_bind == NULL ) psz_bind = "";
441 /* Prepare the network_socket_t structure */
442 sock.psz_bind_addr = psz_bind;
443 sock.i_bind_port = i_bind;
444 sock.psz_server_addr = psz_server;
445 sock.i_server_port = i_server;
449 msg_Dbg( p_this, "net: connecting to '[%s]:%d@[%s]:%d'",
450 psz_server, i_server, psz_bind, i_bind );
452 /* Check if we have force ipv4 or ipv6 */
453 vlc_bool_t v4 = var_CreateGetBool (p_this, "ipv4");
454 vlc_bool_t v6 = var_CreateGetBool (p_this, "ipv6");
458 /* try IPv6 first (unless IPv4 forced) */
459 private = p_this->p_private;
460 p_this->p_private = (void*)&sock;
461 p_network = module_Need( p_this, "network", "ipv6", VLC_TRUE );
463 if( p_network != NULL )
464 module_Unneed( p_this, p_network );
466 p_this->p_private = private;
469 if ((sock.i_handle == -1) && !v6)
471 /* also try IPv4 (unless IPv6 forced) */
472 private = p_this->p_private;
473 p_this->p_private = (void*)&sock;
474 p_network = module_Need( p_this, "network", "ipv4", VLC_TRUE );
476 if( p_network != NULL )
477 module_Unneed( p_this, p_network );
479 p_this->p_private = private;
482 if( sock.i_handle == -1 )
483 msg_Dbg( p_this, "net: connection to '[%s]:%d@[%s]:%d' failed",
484 psz_server, i_server, psz_bind, i_bind );
486 return sock.i_handle;