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 #ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h>
38 #include <vlc_network.h>
41 # if defined(UNDER_CE)
42 # undef IP_MULTICAST_TTL
43 # define IP_MULTICAST_TTL 3
44 # undef IP_ADD_MEMBERSHIP
45 # define IP_ADD_MEMBERSHIP 5
47 # define EAFNOSUPPORT WSAEAFNOSUPPORT
48 # define if_nametoindex( str ) atoi( str )
57 # define SOL_IP IPPROTO_IP
60 # define SOL_IPV6 IPPROTO_IPV6
63 # define IPPROTO_IPV6 41 /* IANA */
66 # define SOL_DCCP IPPROTO_DCCP
69 # define IPPROTO_DCCP 33 /* IANA */
72 # define SOL_UDPLITE IPPROTO_UDPLITE
74 #ifndef IPPROTO_UDPLITE
75 # define IPPROTO_UDPLITE 136 /* IANA */
79 # include <linux/dccp.h>
80 # ifndef SOCK_DCCP /* provisional API */
85 #if defined (HAVE_NETINET_UDPLITE_H)
86 # include <netinet/udplite.h>
87 #elif defined (__linux__)
88 /* still missing from glibc 2.6 */
89 # define UDPLITE_SEND_CSCOV 10
90 # define UDPLITE_RECV_CSCOV 11
93 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
96 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
97 int family, int protocol)
99 struct addrinfo hints, *res;
101 memset (&hints, 0, sizeof( hints ));
102 hints.ai_family = family;
103 hints.ai_socktype = SOCK_DGRAM;
104 hints.ai_flags = AI_PASSIVE;
109 msg_Dbg (obj, "net: opening %s datagram port %d", host ?: "any", port);
111 int val = vlc_getaddrinfo (obj, host, port, &hints, &res);
114 msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
115 vlc_gai_strerror (val));
121 for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
123 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
124 protocol ?: ptr->ai_protocol);
127 msg_Dbg (obj, "socket error: %s", net_strerror (net_errno));
131 if (ptr->ai_next != NULL)
134 if ((ptr->ai_family != AF_INET6)
135 || setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &(int){ 0 },
139 msg_Err (obj, "Multiple network protocols present");
140 msg_Err (obj, "Please select network protocol manually");
144 /* Bind the socket */
145 #if defined (WIN32) || defined (UNDER_CE)
146 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
147 && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
149 struct sockaddr_in6 dumb =
151 .sin6_family = ptr->ai_addr->sa_family,
152 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
154 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
158 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
160 msg_Err (obj, "socket bind error (%s)", net_strerror (net_errno));
165 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
166 && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
176 vlc_freeaddrinfo (res);
181 static int net_SetMcastHopLimit( vlc_object_t *p_this,
182 int fd, int family, int hlim )
186 /* There is some confusion in the world whether IP_MULTICAST_TTL
187 * takes a byte or an int as an argument.
188 * BSD seems to indicate byte so we are going with that and use
189 * int as a fallback to be safe */
192 #ifdef IP_MULTICAST_TTL
195 cmd = IP_MULTICAST_TTL;
199 #ifdef IPV6_MULTICAST_HOPS
202 cmd = IPV6_MULTICAST_HOPS;
207 msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
211 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
213 /* BSD compatibility */
216 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
217 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
225 static int net_SetMcastOutIface (int fd, int family, int scope)
229 #ifdef IPV6_MULTICAST_IF
231 return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
232 &scope, sizeof (scope));
238 struct ip_mreqn req = { .imr_ifindex = scope };
240 return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
246 errno = EAFNOSUPPORT;
251 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
253 #ifdef IP_MULTICAST_IF
254 return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
256 errno = EAFNOSUPPORT;
262 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
263 const char *iface, const char *addr)
267 int scope = if_nametoindex (iface);
270 msg_Err (p_this, "invalid multicast interface: %s", iface);
274 if (net_SetMcastOutIface (fd, family, scope) == 0)
277 msg_Err (p_this, "%s: %s", iface, net_strerror (net_errno));
282 if (family == AF_INET)
285 if (inet_pton (AF_INET, addr, &ipv4) <= 0)
287 msg_Err (p_this, "invalid IPv4 address for multicast: %s",
292 if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
295 msg_Err (p_this, "%s: %s", addr, net_strerror (net_errno));
304 * Old-style any-source multicast join.
305 * In use on Windows XP/2003 and older.
308 net_IPv4Join (vlc_object_t *obj, int fd,
309 const struct sockaddr_in *src, const struct sockaddr_in *grp)
311 #ifdef IP_ADD_MEMBERSHIP
315 # ifdef IP_ADD_SOURCE_MEMBERSHIP
316 struct ip_mreq_source gsr4;
320 struct in_addr id = { .s_addr = INADDR_ANY };
323 /* Multicast interface IPv4 address */
324 char *iface = var_CreateGetString (obj, "miface-addr");
328 && (inet_pton (AF_INET, iface, &id) <= 0))
330 msg_Err (obj, "invalid multicast interface address %s", iface);
336 memset (&opt, 0, sizeof (opt));
339 # ifdef IP_ADD_SOURCE_MEMBERSHIP
340 cmd = IP_ADD_SOURCE_MEMBERSHIP;
341 opt.gsr4.imr_multiaddr = grp->sin_addr;
342 opt.gsr4.imr_sourceaddr = src->sin_addr;
343 opt.gsr4.imr_interface = id;
344 optlen = sizeof (opt.gsr4);
352 cmd = IP_ADD_MEMBERSHIP;
353 opt.gr4.imr_multiaddr = grp->sin_addr;
354 opt.gr4.imr_interface = id;
355 optlen = sizeof (opt.gr4);
358 msg_Dbg (obj, "IP_ADD_%sMEMBERSHIP multicast request",
359 (src != NULL) ? "SOURCE_" : "");
361 if (setsockopt (fd, SOL_IP, cmd, &opt, optlen) == 0)
367 msg_Err (obj, "cannot join IPv4 multicast group (%s)",
368 net_strerror (net_errno));
374 net_IPv6Join (vlc_object_t *obj, int fd, const struct sockaddr_in6 *src)
376 #ifdef IPV6_JOIN_GROUP
377 struct ipv6_mreq gr6;
378 memset (&gr6, 0, sizeof (gr6));
379 gr6.ipv6mr_interface = src->sin6_scope_id;
380 memcpy (&gr6.ipv6mr_multiaddr, &src->sin6_addr, 16);
382 msg_Dbg (obj, "IPV6_JOIN_GROUP multicast request");
384 if (!setsockopt (fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &gr6, sizeof (gr6)))
390 msg_Err (obj, "cannot join IPv6 any-source multicast group (%s)",
391 net_strerror (net_errno));
396 #if defined (WIN32) && !defined (MCAST_JOIN_SOURCE_GROUP)
398 * I hate manual definitions: Error-prone. Portability hell.
399 * Developers shall use UP-TO-DATE compilers. Full point.
400 * If you remove the warning, you remove the whole ifndef.
402 # warning Your C headers are out-of-date. Please update.
404 # define MCAST_JOIN_GROUP 41
408 struct sockaddr_storage gr_group;
411 # define MCAST_JOIN_SOURCE_GROUP 45 /* from <ws2ipdef.h> */
412 struct group_source_req
414 uint32_t gsr_interface;
415 struct sockaddr_storage gsr_group;
416 struct sockaddr_storage gsr_source;
421 * IP-agnostic multicast join,
422 * with fallback to old APIs, and fallback from SSM to ASM.
425 net_SourceSubscribe (vlc_object_t *obj, int fd,
426 const struct sockaddr *src, socklen_t srclen,
427 const struct sockaddr *grp, socklen_t grplen)
431 char *iface = var_CreateGetString (obj, "miface");
436 iid = if_nametoindex (iface);
439 msg_Err (obj, "invalid multicast interface: %s", iface);
447 switch (grp->sa_family)
452 if (((const struct sockaddr_in6 *)grp)->sin6_scope_id)
453 iid = ((const struct sockaddr_in6 *)grp)->sin6_scope_id;
462 errno = EAFNOSUPPORT;
467 switch (src->sa_family)
471 if (memcmp (&((const struct sockaddr_in6 *)src)->sin6_addr,
472 &in6addr_any, sizeof (in6addr_any)) == 0)
478 if (((const struct sockaddr_in *)src)->sin_addr.s_addr
485 /* Agnostic ASM/SSM multicast join */
486 #ifdef MCAST_JOIN_SOURCE_GROUP
490 struct group_source_req gsr;
494 memset (&opt, 0, sizeof (opt));
498 if ((grplen > sizeof (opt.gsr.gsr_group))
499 || (srclen > sizeof (opt.gsr.gsr_source)))
502 opt.gsr.gsr_interface = iid;
503 memcpy (&opt.gsr.gsr_source, src, srclen);
504 memcpy (&opt.gsr.gsr_group, grp, grplen);
505 optlen = sizeof (opt.gsr);
509 if (grplen > sizeof (opt.gr.gr_group))
512 opt.gr.gr_interface = iid;
513 memcpy (&opt.gr.gr_group, grp, grplen);
514 optlen = sizeof (opt.gr);
517 msg_Dbg (obj, "Multicast %sgroup join request", src ? "source " : "");
519 if (setsockopt (fd, level,
520 src ? MCAST_JOIN_SOURCE_GROUP : MCAST_JOIN_GROUP,
521 (void *)&opt, optlen) == 0)
525 /* Fallback to IPv-specific APIs */
526 if ((src != NULL) && (src->sa_family != grp->sa_family))
529 switch (grp->sa_family)
532 if ((grplen < sizeof (struct sockaddr_in))
533 || ((src != NULL) && (srclen < sizeof (struct sockaddr_in))))
536 if (net_IPv4Join (obj, fd, (const struct sockaddr_in *)src,
537 (const struct sockaddr_in *)grp) == 0)
543 if ((grplen < sizeof (struct sockaddr_in6))
544 || ((src != NULL) && (srclen < sizeof (struct sockaddr_in6))))
547 /* IPv6-specific SSM API does not exist. So if we're here
548 * it means IPv6 SSM is not supported on this OS and we
549 * directly fallback to ASM */
551 if (net_IPv6Join (obj, fd, (const struct sockaddr_in6 *)grp) == 0)
557 msg_Err (obj, "Multicast group join error (%s)",
558 net_strerror (net_errno));
562 msg_Warn (obj, "Trying ASM instead of SSM...");
563 return net_Subscribe (obj, fd, grp, grplen);
566 msg_Err (obj, "Multicast not supported");
571 int net_Subscribe (vlc_object_t *obj, int fd,
572 const struct sockaddr *addr, socklen_t addrlen)
574 return net_SourceSubscribe (obj, fd, NULL, 0, addr, addrlen);
578 static int net_SetDSCP( int fd, uint8_t dscp )
580 struct sockaddr_storage addr;
581 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
586 switch( addr.ss_family )
607 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
611 /*****************************************************************************
612 * __net_ConnectDgram:
613 *****************************************************************************
614 * Open a datagram socket to send data to a defined destination, with an
615 * optional hop limit.
616 *****************************************************************************/
617 int __net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
618 int i_hlim, int proto )
620 struct addrinfo hints, *res, *ptr;
621 int i_val, i_handle = -1;
622 vlc_bool_t b_unreach = VLC_FALSE;
625 i_port = 1234; /* historical VLC thing */
628 i_hlim = var_CreateGetInteger( p_this, "ttl" );
630 memset( &hints, 0, sizeof( hints ) );
631 hints.ai_socktype = SOCK_DGRAM;
633 msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
635 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
638 msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
639 vlc_gai_strerror( i_val ) );
643 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
646 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
647 proto ?: ptr->ai_protocol);
651 #if !defined( SYS_BEOS )
652 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
653 * to avoid packet loss caused by scheduling problems */
654 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
655 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
657 /* Allow broadcast sending */
658 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
662 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
664 str = var_CreateGetString (p_this, "miface");
668 net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
672 str = var_CreateGetString (p_this, "miface-addr");
676 net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
680 net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
682 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
689 #if defined( WIN32 ) || defined( UNDER_CE )
690 if( WSAGetLastError () == WSAENETUNREACH )
692 if( errno == ENETUNREACH )
694 b_unreach = VLC_TRUE;
697 msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
704 vlc_freeaddrinfo( res );
709 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
718 /*****************************************************************************
720 *****************************************************************************
721 * OpenDgram a datagram socket and return a handle
722 *****************************************************************************/
723 int __net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
724 const char *psz_server, int i_server,
725 int family, int protocol )
727 if ((psz_server == NULL) || (psz_server[0] == '\0'))
728 return net_ListenSingle (obj, psz_bind, i_bind, family, protocol);
730 msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
731 psz_server, i_server, psz_bind, i_bind);
733 struct addrinfo hints, *loc, *rem;
736 memset (&hints, 0, sizeof (hints));
737 hints.ai_family = family;
738 hints.ai_socktype = SOCK_DGRAM;
740 val = vlc_getaddrinfo (obj, psz_server, i_server, &hints, &rem);
743 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
744 vlc_gai_strerror (val));
748 hints.ai_flags = AI_PASSIVE;
749 val = vlc_getaddrinfo (obj, psz_bind, i_bind, &hints, &loc);
752 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
753 vlc_gai_strerror (val));
754 vlc_freeaddrinfo (rem);
758 for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
760 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
761 protocol ?: ptr->ai_protocol);
763 continue; // usually, address family not supported
766 setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
770 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
771 * to avoid packet loss caused in case of scheduling hiccups */
772 setsockopt (fd, SOL_SOCKET, SO_RCVBUF,
773 (void *)&(int){ 0x80000 }, sizeof (int));
774 setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
775 (void *)&(int){ 0x80000 }, sizeof (int));
778 #if defined (WIN32) || defined (UNDER_CE)
779 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
780 && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
782 // This works for IPv4 too - don't worry!
783 struct sockaddr_in6 dumb =
785 .sin6_family = ptr->ai_addr->sa_family,
786 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
789 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
793 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
800 for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
802 if ((ptr2->ai_family != ptr->ai_family)
803 || (ptr2->ai_socktype != ptr->ai_socktype)
804 || (ptr2->ai_protocol != ptr->ai_protocol))
807 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
808 ? net_SourceSubscribe (obj, fd,
809 ptr2->ai_addr, ptr2->ai_addrlen,
810 ptr->ai_addr, ptr->ai_addrlen)
811 : connect (fd, ptr2->ai_addr, ptr2->ai_addrlen))
813 msg_Err (obj, "cannot connect to %s port %d: %s",
814 psz_server, i_server, net_strerror (net_errno));
827 vlc_freeaddrinfo (rem);
828 vlc_freeaddrinfo (loc);
835 * Sets the send and receive checksum coverage of a socket:
837 * @param sendcov payload coverage of sent packets (bytes), -1 for full
838 * @param recvcov minimum payload coverage of received packets, -1 for full
840 int net_SetCSCov (int fd, int sendcov, int recvcov)
844 if (getsockopt (fd, SOL_SOCKET, SO_TYPE,
845 &type, &(socklen_t){ sizeof (type) }))
850 #ifdef UDPLITE_RECV_CSCOV
851 case SOCK_DGRAM: /* UDP-Lite */
855 sendcov += 8; /* partial */
856 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_SEND_CSCOV, &sendcov,
864 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
865 &recvcov, sizeof (recvcov)))
870 #ifdef DCCP_SOCKOPT_SEND_CSCOV
871 case SOCK_DCCP: /* DCCP and its ill-named socket type */
872 if ((sendcov == -1) || (sendcov > 56))
875 sendcov = (sendcov + 3) / 4;
876 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SEND_CSCOV,
877 &sendcov, sizeof (sendcov)))
880 if ((sendcov == -1) || (sendcov > 56))
883 sendcov = (sendcov + 3) / 4;
884 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_RECV_CSCOV,
885 &recvcov, sizeof (recvcov)))