1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
5 * Copyright © 2006-2007 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 *****************************************************************************/
34 #include <vlc_common.h>
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
42 #include <vlc_network.h>
45 # if defined(UNDER_CE)
46 # undef IP_MULTICAST_TTL
47 # define IP_MULTICAST_TTL 3
48 # undef IP_ADD_MEMBERSHIP
49 # define IP_ADD_MEMBERSHIP 5
51 # define EAFNOSUPPORT WSAEAFNOSUPPORT
52 # define if_nametoindex( str ) atoi( str )
60 #ifdef HAVE_LINUX_DCCP_H
61 # include <linux/dccp.h>
62 # ifndef SOCK_DCCP /* provisional API */
68 # define SOL_IP IPPROTO_IP
71 # define SOL_IPV6 IPPROTO_IPV6
74 # define IPPROTO_IPV6 41 /* IANA */
77 # define SOL_DCCP IPPROTO_DCCP
80 # define IPPROTO_DCCP 33 /* IANA */
83 # define SOL_UDPLITE IPPROTO_UDPLITE
85 #ifndef IPPROTO_UDPLITE
86 # define IPPROTO_UDPLITE 136 /* IANA */
89 #if defined (HAVE_NETINET_UDPLITE_H)
90 # include <netinet/udplite.h>
91 #elif defined (__linux__)
92 /* still missing from glibc 2.6 */
93 # define UDPLITE_SEND_CSCOV 10
94 # define UDPLITE_RECV_CSCOV 11
97 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
101 static int net_SetupDgramSocket( vlc_object_t *p_obj, int fd, const struct addrinfo *ptr )
104 setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
108 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
109 * to avoid packet loss caused in case of scheduling hiccups */
110 setsockopt (fd, SOL_SOCKET, SO_RCVBUF,
111 (void *)&(int){ 0x80000 }, sizeof (int));
112 setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
113 (void *)&(int){ 0x80000 }, sizeof (int));
116 #if defined (WIN32) || defined (UNDER_CE)
117 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
118 && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
120 // This works for IPv4 too - don't worry!
121 struct sockaddr_in6 dumb =
123 .sin6_family = ptr->ai_addr->sa_family,
124 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
127 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
131 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
133 msg_Err( p_obj, "socket bind error (%m)" );
141 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
142 int family, int protocol)
144 struct addrinfo hints, *res;
146 memset (&hints, 0, sizeof( hints ));
147 hints.ai_family = family;
148 hints.ai_socktype = SOCK_DGRAM;
149 hints.ai_flags = AI_PASSIVE;
154 msg_Dbg (obj, "net: opening %s datagram port %d", host ?: "any", port);
156 int val = vlc_getaddrinfo (obj, host, port, &hints, &res);
159 msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
160 vlc_gai_strerror (val));
166 for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
168 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
169 protocol ?: ptr->ai_protocol);
172 msg_Dbg (obj, "socket error: %m");
176 if (ptr->ai_next != NULL)
179 if ((ptr->ai_family != AF_INET6)
180 || setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &(int){ 0 },
184 msg_Err (obj, "Multiple network protocols present");
185 msg_Err (obj, "Please select network protocol manually");
189 fd = net_SetupDgramSocket( obj, fd, ptr );
193 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
194 && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
204 vlc_freeaddrinfo (res);
209 static int net_SetMcastHopLimit( vlc_object_t *p_this,
210 int fd, int family, int hlim )
214 /* There is some confusion in the world whether IP_MULTICAST_TTL
215 * takes a byte or an int as an argument.
216 * BSD seems to indicate byte so we are going with that and use
217 * int as a fallback to be safe */
220 #ifdef IP_MULTICAST_TTL
223 cmd = IP_MULTICAST_TTL;
227 #ifdef IPV6_MULTICAST_HOPS
230 cmd = IPV6_MULTICAST_HOPS;
235 errno = EAFNOSUPPORT;
236 msg_Warn( p_this, "%m" );
240 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
242 /* BSD compatibility */
245 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
246 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
254 static int net_SetMcastOutIface (int fd, int family, int scope)
258 #ifdef IPV6_MULTICAST_IF
260 return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
261 &scope, sizeof (scope));
267 struct ip_mreqn req = { .imr_ifindex = scope };
269 return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
275 errno = EAFNOSUPPORT;
280 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
282 #ifdef IP_MULTICAST_IF
283 return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
285 errno = EAFNOSUPPORT;
291 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
292 const char *iface, const char *addr)
296 int scope = if_nametoindex (iface);
299 msg_Err (p_this, "invalid multicast interface: %s", iface);
303 if (net_SetMcastOutIface (fd, family, scope) == 0)
306 msg_Err (p_this, "%s: %m", iface);
311 if (family == AF_INET)
314 if (inet_pton (AF_INET, addr, &ipv4) <= 0)
316 msg_Err (p_this, "invalid IPv4 address for multicast: %s",
321 if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
324 msg_Err (p_this, "%s: %m", addr);
333 * Old-style any-source multicast join.
334 * In use on Windows XP/2003 and older.
337 net_IPv4Join (vlc_object_t *obj, int fd,
338 const struct sockaddr_in *src, const struct sockaddr_in *grp)
340 #ifdef IP_ADD_MEMBERSHIP
344 # ifdef IP_ADD_SOURCE_MEMBERSHIP
345 struct ip_mreq_source gsr4;
349 struct in_addr id = { .s_addr = INADDR_ANY };
352 /* Multicast interface IPv4 address */
353 char *iface = var_CreateGetNonEmptyString (obj, "miface-addr");
355 && (inet_pton (AF_INET, iface, &id) <= 0))
357 msg_Err (obj, "invalid multicast interface address %s", iface);
363 memset (&opt, 0, sizeof (opt));
366 # ifdef IP_ADD_SOURCE_MEMBERSHIP
367 cmd = IP_ADD_SOURCE_MEMBERSHIP;
368 opt.gsr4.imr_multiaddr = grp->sin_addr;
369 opt.gsr4.imr_sourceaddr = src->sin_addr;
370 opt.gsr4.imr_interface = id;
371 optlen = sizeof (opt.gsr4);
379 cmd = IP_ADD_MEMBERSHIP;
380 opt.gr4.imr_multiaddr = grp->sin_addr;
381 opt.gr4.imr_interface = id;
382 optlen = sizeof (opt.gr4);
385 msg_Dbg (obj, "IP_ADD_%sMEMBERSHIP multicast request",
386 (src != NULL) ? "SOURCE_" : "");
388 if (setsockopt (fd, SOL_IP, cmd, &opt, optlen) == 0)
394 msg_Err (obj, "cannot join IPv4 multicast group (%m)");
400 net_IPv6Join (vlc_object_t *obj, int fd, const struct sockaddr_in6 *src)
402 #ifdef IPV6_JOIN_GROUP
403 struct ipv6_mreq gr6;
404 memset (&gr6, 0, sizeof (gr6));
405 gr6.ipv6mr_interface = src->sin6_scope_id;
406 memcpy (&gr6.ipv6mr_multiaddr, &src->sin6_addr, 16);
408 msg_Dbg (obj, "IPV6_JOIN_GROUP multicast request");
410 if (!setsockopt (fd, SOL_IPV6, IPV6_JOIN_GROUP, &gr6, sizeof (gr6)))
416 msg_Err (obj, "cannot join IPv6 any-source multicast group (%m)");
421 #if defined (WIN32) && !defined (MCAST_JOIN_SOURCE_GROUP)
423 * I hate manual definitions: Error-prone. Portability hell.
424 * Developers shall use UP-TO-DATE compilers. Full point.
425 * If you remove the warning, you remove the whole ifndef.
427 # warning Your C headers are out-of-date. Please update.
429 # define MCAST_JOIN_GROUP 41
433 struct sockaddr_storage gr_group;
436 # define MCAST_JOIN_SOURCE_GROUP 45 /* from <ws2ipdef.h> */
437 struct group_source_req
439 uint32_t gsr_interface;
440 struct sockaddr_storage gsr_group;
441 struct sockaddr_storage gsr_source;
446 * IP-agnostic multicast join,
447 * with fallback to old APIs, and fallback from SSM to ASM.
450 net_SourceSubscribe (vlc_object_t *obj, int fd,
451 const struct sockaddr *src, socklen_t srclen,
452 const struct sockaddr *grp, socklen_t grplen)
456 char *iface = var_CreateGetNonEmptyString (obj, "miface");
459 iid = if_nametoindex (iface);
462 msg_Err (obj, "invalid multicast interface: %s", iface);
469 switch (grp->sa_family)
474 if (((const struct sockaddr_in6 *)grp)->sin6_scope_id)
475 iid = ((const struct sockaddr_in6 *)grp)->sin6_scope_id;
484 errno = EAFNOSUPPORT;
489 switch (src->sa_family)
493 if (memcmp (&((const struct sockaddr_in6 *)src)->sin6_addr,
494 &in6addr_any, sizeof (in6addr_any)) == 0)
500 if (((const struct sockaddr_in *)src)->sin_addr.s_addr
507 /* Agnostic ASM/SSM multicast join */
508 #ifdef MCAST_JOIN_SOURCE_GROUP
512 struct group_source_req gsr;
516 memset (&opt, 0, sizeof (opt));
520 if ((grplen > sizeof (opt.gsr.gsr_group))
521 || (srclen > sizeof (opt.gsr.gsr_source)))
524 opt.gsr.gsr_interface = iid;
525 memcpy (&opt.gsr.gsr_source, src, srclen);
526 memcpy (&opt.gsr.gsr_group, grp, grplen);
527 optlen = sizeof (opt.gsr);
531 if (grplen > sizeof (opt.gr.gr_group))
534 opt.gr.gr_interface = iid;
535 memcpy (&opt.gr.gr_group, grp, grplen);
536 optlen = sizeof (opt.gr);
539 msg_Dbg (obj, "Multicast %sgroup join request", src ? "source " : "");
541 if (setsockopt (fd, level,
542 src ? MCAST_JOIN_SOURCE_GROUP : MCAST_JOIN_GROUP,
543 (void *)&opt, optlen) == 0)
547 /* Fallback to IPv-specific APIs */
548 if ((src != NULL) && (src->sa_family != grp->sa_family))
551 switch (grp->sa_family)
554 if ((grplen < sizeof (struct sockaddr_in))
555 || ((src != NULL) && (srclen < sizeof (struct sockaddr_in))))
558 if (net_IPv4Join (obj, fd, (const struct sockaddr_in *)src,
559 (const struct sockaddr_in *)grp) == 0)
565 if ((grplen < sizeof (struct sockaddr_in6))
566 || ((src != NULL) && (srclen < sizeof (struct sockaddr_in6))))
569 /* IPv6-specific SSM API does not exist. So if we're here
570 * it means IPv6 SSM is not supported on this OS and we
571 * directly fallback to ASM */
573 if (net_IPv6Join (obj, fd, (const struct sockaddr_in6 *)grp) == 0)
579 msg_Err (obj, "Multicast group join error (%m)");
583 msg_Warn (obj, "Trying ASM instead of SSM...");
584 return net_Subscribe (obj, fd, grp, grplen);
587 msg_Err (obj, "Multicast not supported");
592 int net_Subscribe (vlc_object_t *obj, int fd,
593 const struct sockaddr *addr, socklen_t addrlen)
595 return net_SourceSubscribe (obj, fd, NULL, 0, addr, addrlen);
599 static int net_SetDSCP( int fd, uint8_t dscp )
601 struct sockaddr_storage addr;
602 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
607 switch( addr.ss_family )
628 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
632 /*****************************************************************************
633 * __net_ConnectDgram:
634 *****************************************************************************
635 * Open a datagram socket to send data to a defined destination, with an
636 * optional hop limit.
637 *****************************************************************************/
638 int __net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
639 int i_hlim, int proto )
641 struct addrinfo hints, *res, *ptr;
642 int i_val, i_handle = -1;
643 bool b_unreach = false;
646 i_hlim = var_CreateGetInteger( p_this, "ttl" );
648 memset( &hints, 0, sizeof( hints ) );
649 hints.ai_socktype = SOCK_DGRAM;
651 msg_Dbg( p_this, "net: connecting to [%s]:%d", psz_host, i_port );
653 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
656 msg_Err( p_this, "cannot resolve [%s]:%d : %s", psz_host, i_port,
657 vlc_gai_strerror( i_val ) );
661 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
664 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
665 proto ?: ptr->ai_protocol);
669 #if !defined( SYS_BEOS )
670 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
671 * to avoid packet loss caused by scheduling problems */
672 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
673 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
675 /* Allow broadcast sending */
676 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
680 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
682 str = var_CreateGetNonEmptyString (p_this, "miface");
685 net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
689 str = var_CreateGetNonEmptyString (p_this, "miface-addr");
692 net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
696 net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
698 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
705 #if defined( WIN32 ) || defined( UNDER_CE )
706 if( WSAGetLastError () == WSAENETUNREACH )
708 if( errno == ENETUNREACH )
713 msg_Warn( p_this, "%s port %d : %m", psz_host, i_port);
719 vlc_freeaddrinfo( res );
724 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
733 /*****************************************************************************
735 *****************************************************************************
736 * OpenDgram a datagram socket and return a handle
737 *****************************************************************************/
738 int __net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
739 const char *psz_server, int i_server,
740 int family, int protocol )
742 if ((psz_server == NULL) || (psz_server[0] == '\0'))
743 return net_ListenSingle (obj, psz_bind, i_bind, family, protocol);
745 msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
746 psz_server, i_server, psz_bind, i_bind);
748 struct addrinfo hints, *loc, *rem;
751 memset (&hints, 0, sizeof (hints));
752 hints.ai_family = family;
753 hints.ai_socktype = SOCK_DGRAM;
755 val = vlc_getaddrinfo (obj, psz_server, i_server, &hints, &rem);
758 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
759 vlc_gai_strerror (val));
763 hints.ai_flags = AI_PASSIVE;
764 val = vlc_getaddrinfo (obj, psz_bind, i_bind, &hints, &loc);
767 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
768 vlc_gai_strerror (val));
769 vlc_freeaddrinfo (rem);
773 for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
775 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
776 protocol ?: ptr->ai_protocol);
778 continue; // usually, address family not supported
780 fd = net_SetupDgramSocket( obj, fd, ptr );
785 for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
787 if ((ptr2->ai_family != ptr->ai_family)
788 || (ptr2->ai_socktype != ptr->ai_socktype)
789 || (ptr2->ai_protocol != ptr->ai_protocol))
792 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
793 ? net_SourceSubscribe (obj, fd,
794 ptr2->ai_addr, ptr2->ai_addrlen,
795 ptr->ai_addr, ptr->ai_addrlen)
796 : connect (fd, ptr2->ai_addr, ptr2->ai_addrlen))
798 msg_Err (obj, "cannot connect to %s port %d: %m",
799 psz_server, i_server);
812 vlc_freeaddrinfo (rem);
813 vlc_freeaddrinfo (loc);
820 * Sets the send and receive checksum coverage of a socket:
822 * @param sendcov payload coverage of sent packets (bytes), -1 for full
823 * @param recvcov minimum payload coverage of received packets, -1 for full
825 int net_SetCSCov (int fd, int sendcov, int recvcov)
829 if (getsockopt (fd, SOL_SOCKET, SO_TYPE,
830 &type, &(socklen_t){ sizeof (type) }))
835 #ifdef UDPLITE_RECV_CSCOV
836 case SOCK_DGRAM: /* UDP-Lite */
840 sendcov += 8; /* partial */
841 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_SEND_CSCOV, &sendcov,
849 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
850 &recvcov, sizeof (recvcov)))
855 #ifdef DCCP_SOCKOPT_SEND_CSCOV
856 case SOCK_DCCP: /* DCCP and its ill-named socket type */
857 if ((sendcov == -1) || (sendcov > 56))
860 sendcov = (sendcov + 3) / 4;
861 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SEND_CSCOV,
862 &sendcov, sizeof (sendcov)))
865 if ((recvcov == -1) || (recvcov > 56))
868 recvcov = (recvcov + 3) / 4;
869 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_RECV_CSCOV,
870 &recvcov, sizeof (recvcov)))
876 #if !defined( UDPLITE_RECV_CSCOV ) && !defined( DCCP_SOCKOPT_SEND_CSCOV )