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,
73 * XXX: I am too lazy to put all these dual functions in “next generation”
77 static int net_SetMcastHopLimit( vlc_object_t *p_this,
78 int fd, int family, int hlim )
82 /* There is some confusion in the world whether IP_MULTICAST_TTL
83 * takes a byte or an int as an argument.
84 * BSD seems to indicate byte so we are going with that and use
85 * int as a fallback to be safe */
88 #ifdef IP_MULTICAST_TTL
91 cmd = IP_MULTICAST_TTL;
95 #ifdef IPV6_MULTICAST_HOPS
98 cmd = IPV6_MULTICAST_HOPS;
103 msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
107 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
109 /* BSD compatibility */
112 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
113 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
121 static int net_SetMcastOutIface (int fd, int family, int scope)
125 #ifdef IPV6_MULTICAST_IF
127 return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
128 &scope, sizeof (scope));
134 struct ip_mreqn req = { .imr_ifindex = scope };
136 return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
142 errno = EAFNOSUPPORT;
147 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
149 #ifdef IP_MULTICAST_IF
150 return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
152 errno = EAFNOSUPPORT;
158 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
159 const char *iface, const char *addr)
163 int scope = if_nametoindex (iface);
166 msg_Err (p_this, "invalid multicast interface: %s", iface);
170 if (net_SetMcastOutIface (fd, family, scope) == 0)
173 msg_Err (p_this, "%s: %s", iface, net_strerror (net_errno));
178 if (family == AF_INET)
181 if (inet_pton (AF_INET, addr, &ipv4) <= 0)
183 msg_Err (p_this, "invalid IPv4 address for multicast: %s",
188 if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
191 msg_Err (p_this, "%s: %s", addr, net_strerror (net_errno));
200 * Old-style any-source multicast join.
201 * In use on Windows XP/2003 and older.
204 net_IPv4Join (vlc_object_t *obj, int fd,
205 const struct sockaddr_in *src, const struct sockaddr_in *grp)
207 #ifdef IP_ADD_MEMBERSHIP
211 # ifdef IP_ADD_SOURCE_MEMBERSHIP
212 struct ip_mreq_source gsr4;
219 /* Multicast interface IPv4 address */
220 char *iface = var_CreateGetString (obj, "miface-addr");
224 && (inet_pton (AF_INET, iface, &id) <= 0))
226 msg_Err (obj, "invalid multicast interface address %s", iface);
232 memset (&opt, 0, sizeof (opt));
235 # ifdef IP_ADD_SOURCE_MEMBERSHIP
236 cmd = IP_ADD_SOURCE_MEMBERSHIP;
237 opt.gsr4.imr_multiaddr = grp->sin_addr;
238 opt.gsr4.imr_sourceaddr = src->sin_addr;
239 opt.gsr4.imr_interface = id;
240 optlen = sizeof (opt.gsr4);
248 cmd = IP_ADD_MEMBERSHIP;
249 opt.gr4.imr_multiaddr = grp->sin_addr;
250 opt.gr4.imr_interface = id;
251 optlen = sizeof (opt.gr4);
254 msg_Dbg (obj, "IP_ADD_%sMEMBERSHIP multicast request",
255 (src != NULL) ? "SOURCE_" : "");
257 if (setsockopt (fd, SOL_IP, cmd, &opt, optlen) == 0)
263 msg_Err (obj, "cannot join IPv4 multicast group (%s)",
264 net_strerror (net_errno));
270 net_IPv6Join (vlc_object_t *obj, int fd, const struct sockaddr_in6 *src)
272 #ifdef IPV6_JOIN_GROUP
273 struct ipv6_mreq gr6;
274 memset (&gr6, 0, sizeof (gr6));
275 gr6.ipv6mr_interface = src->sin6_scope_id;
276 memcpy (&gr6.ipv6mr_multiaddr, &src->sin6_addr, 16);
278 msg_Dbg (obj, "IPV6_JOIN_GROUP multicast request");
280 if (!setsockopt (fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &gr6, sizeof (gr6)))
286 msg_Err (obj, "cannot join IPv6 any-source multicast group (%s)",
287 net_strerror (net_errno));
292 #if defined (WIN32) && !defined (MCAST_JOIN_SOURCE_GROUP)
294 * I hate manual definitions: Error-prone. Portability hell.
295 * Developers shall use UP-TO-DATE compilers. Full point.
296 * If you remove the warning, you remove the whole ifndef.
298 # warning Your C headers are out-of-date. Please update.
300 /* Most (all?) Mingw32 versions in use are yet to pick up Vista stuff */
301 # define MCAST_JOIN_SOURCE_GROUP 45 /* from <ws2ipdef.h> */
302 struct group_source_req
304 uint32_t gsr_interface;
305 struct sockaddr_storage gsr_group;
306 struct sockaddr_storage gsr_source;
311 * IP-agnostic multicast join,
312 * with fallback to old APIs, and fallback from SSM to ASM.
315 net_SourceSubscribe (vlc_object_t *obj, int fd,
316 const struct sockaddr *src, socklen_t srclen,
317 const struct sockaddr *grp, socklen_t grplen)
319 /* Agnostic ASM/SSM multicast join */
320 #ifdef MCAST_JOIN_SOURCE_GROUP
324 struct group_source_req gsr;
328 vlc_bool_t ssm = VLC_TRUE;
333 memset (&opt, 0, sizeof (opt));
335 char *iface = var_CreateGetString (obj, "miface");
340 iid = if_nametoindex (iface);
343 msg_Err (obj, "invalid multicast interface: %s", iface);
351 switch (grp->sa_family)
356 && memcmp (&((const struct sockaddr_in6 *)src)->sin6_addr,
357 &in6addr_any, sizeof (in6addr_any)) == 0)
360 if (((const struct sockaddr_in6 *)src)->sin6_scope_id)
361 iid = ((const struct sockaddr_in6 *)src)->sin6_scope_id;
367 && ((const struct sockaddr_in *)src)->sin_addr.s_addr
373 errno = EAFNOSUPPORT;
379 if ((grplen > sizeof (opt.gsr.gsr_group))
380 || (srclen > sizeof (opt.gsr.gsr_source)))
383 opt.gsr.gsr_interface = iid;
384 memcpy (&opt.gsr.gsr_source, src, srclen);
385 memcpy (&opt.gsr.gsr_group, grp, grplen);
386 optlen = sizeof (opt.gsr);
390 if (grplen > sizeof (opt.gr.gr_group))
393 opt.gr.gr_interface = iid;
394 memcpy (&opt.gr.gr_group, grp, grplen);
395 optlen = sizeof (opt.gr);
398 msg_Dbg (obj, "Multicast %sgroup join request", ssm ? "source " : "");
400 if (setsockopt (fd, level,
401 ssm ? MCAST_JOIN_SOURCE_GROUP : MCAST_JOIN_GROUP,
402 (void *)&opt, optlen) == 0)
406 /* Fallback to IPv-specific APIs */
407 if (ssm && (src->sa_family != grp->sa_family))
410 switch (grp->sa_family)
413 if ((grplen < sizeof (struct sockaddr_in))
414 || (ssm && (srclen < sizeof (struct sockaddr_in))))
417 if (net_IPv4Join (obj, fd, (const struct sockaddr_in *)src,
418 (const struct sockaddr_in *)grp) == 0)
424 if ((grplen < sizeof (struct sockaddr_in6))
425 || (ssm && (srclen < sizeof (struct sockaddr_in6))))
428 /* We don't provide IPv6-specific SSM at the moment.
429 * It seems all the OSes with IPv6 SSM have the new API anyway. */
431 if (net_IPv6Join (obj, fd, (const struct sockaddr_in6 *)grp) == 0)
437 msg_Err (obj, "Multicast group join error (%s)",
438 net_strerror (net_errno));
442 msg_Warn (obj, "Trying ASM instead of SSM...");
443 return net_Subscribe (obj, fd, grp, grplen);
446 msg_Err (obj, "Multicast not supported");
451 int net_Subscribe (vlc_object_t *obj, int fd,
452 const struct sockaddr *addr, socklen_t addrlen)
454 return net_SourceSubscribe (obj, fd, NULL, 0, addr, addrlen);
458 int net_SetDSCP( int fd, uint8_t dscp )
460 struct sockaddr_storage addr;
461 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
466 switch( addr.ss_family )
487 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
491 /*****************************************************************************
493 *****************************************************************************
494 * Open a UDP socket to send data to a defined destination, with an optional
496 *****************************************************************************/
497 int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
500 struct addrinfo hints, *res, *ptr;
501 int i_val, i_handle = -1;
502 vlc_bool_t b_unreach = VLC_FALSE;
505 i_port = 1234; /* historical VLC thing */
508 i_hlim = var_CreateGetInteger( p_this, "ttl" );
510 memset( &hints, 0, sizeof( hints ) );
511 hints.ai_socktype = SOCK_DGRAM;
513 msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
515 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
518 msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
519 vlc_gai_strerror( i_val ) );
523 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
526 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
531 #if !defined( SYS_BEOS )
532 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
533 * to avoid packet loss caused by scheduling problems */
534 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
535 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
537 /* Allow broadcast sending */
538 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
542 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
544 str = var_CreateGetString (p_this, "miface");
548 net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
552 str = var_CreateGetString (p_this, "miface-addr");
556 net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
560 net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
562 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
569 #if defined( WIN32 ) || defined( UNDER_CE )
570 if( WSAGetLastError () == WSAENETUNREACH )
572 if( errno == ENETUNREACH )
574 b_unreach = VLC_TRUE;
577 msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
584 vlc_freeaddrinfo( res );
589 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
598 /*****************************************************************************
600 *****************************************************************************
601 * Open a UDP connection and return a handle
602 *****************************************************************************/
603 int __net_OpenUDP( vlc_object_t *obj, const char *psz_bind, int i_bind,
604 const char *psz_server, int i_server )
606 struct addrinfo hints, *loc, *rem;
609 msg_Dbg( obj, "net: connecting to '[%s]:%d@[%s]:%d'",
610 psz_server, i_server, psz_bind, i_bind );
612 memset (&hints, 0, sizeof (hints));
613 hints.ai_socktype = SOCK_DGRAM;
614 hints.ai_protocol = IPPROTO_UDP;
615 hints.ai_flags = AI_PASSIVE;
617 val = vlc_getaddrinfo (obj, psz_server, i_server, &hints, &rem);
620 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
621 vlc_gai_strerror (val));
625 val = vlc_getaddrinfo (obj, psz_bind, i_bind, &hints, &loc);
628 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
629 vlc_gai_strerror (val));
630 vlc_freeaddrinfo (rem);
634 for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
636 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
639 continue; // usually, address family not supported
642 setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
646 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
647 * to avoid packet loss caused in case of scheduling hiccups */
648 setsockopt (fd, SOL_SOCKET, SO_RCVBUF,
649 (void *)&(int){ 0x80000 }, sizeof (int));
650 setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
651 (void *)&(int){ 0x80000 }, sizeof (int));
654 #if defined (WIN32) || defined (UNDER_CE)
655 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
656 && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
658 // This works for IPv4 too - don't worry!
659 struct sockaddr_in6 dumb =
661 .sin6_family = ptr->ai_addr->sa_family,
662 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
665 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
669 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
675 struct addrinfo *ptr2;
676 for (ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
678 if ((ptr2->ai_family != ptr->ai_family)
679 || (ptr2->ai_socktype != ptr->ai_socktype)
680 || (ptr2->ai_protocol != ptr->ai_protocol))
683 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen))
685 if (net_SourceSubscribe (obj, fd,
686 ptr2->ai_addr, ptr2->ai_addrlen,
687 ptr->ai_addr, ptr->ai_addrlen) == 0)
692 if (connect (fd, ptr2->ai_addr, ptr2->ai_addrlen) == 0)
699 msg_Err (obj, "cannot connect to %s port %d: %s",
700 psz_server, i_server, net_strerror (net_errno));