1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 VLC authors and VideoLAN
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
39 #include <vlc_network.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
56 #ifdef HAVE_LINUX_DCCP_H
57 # include <linux/dccp.h>
58 # ifndef SOCK_DCCP /* provisional API */
64 # define SOL_IP IPPROTO_IP
67 # define SOL_IPV6 IPPROTO_IPV6
70 # define IPPROTO_IPV6 41 /* IANA */
73 # define SOL_DCCP IPPROTO_DCCP
76 # define IPPROTO_DCCP 33 /* IANA */
79 # define SOL_UDPLITE IPPROTO_UDPLITE
81 #ifndef IPPROTO_UDPLITE
82 # define IPPROTO_UDPLITE 136 /* IANA */
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,
97 static int net_SetupDgramSocket( vlc_object_t *p_obj, int fd, const struct addrinfo *ptr )
100 setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
104 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
105 * to avoid packet loss caused in case of scheduling hiccups */
106 setsockopt (fd, SOL_SOCKET, SO_RCVBUF,
107 (void *)&(int){ 0x80000 }, sizeof (int));
108 setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
109 (void *)&(int){ 0x80000 }, sizeof (int));
112 #if defined (WIN32) || defined (UNDER_CE)
113 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
114 && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
116 // This works for IPv4 too - don't worry!
117 struct sockaddr_in6 dumb =
119 .sin6_family = ptr->ai_addr->sa_family,
120 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
123 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
127 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
129 msg_Err( p_obj, "socket bind error (%m)" );
137 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
140 struct addrinfo hints, *res;
142 memset (&hints, 0, sizeof( hints ));
143 hints.ai_socktype = SOCK_DGRAM;
144 hints.ai_protocol = protocol;
145 hints.ai_flags = AI_PASSIVE;
150 msg_Dbg (obj, "net: opening %s datagram port %d",
151 host ? host : "any", port);
153 int val = vlc_getaddrinfo (obj, host, port, &hints, &res);
156 msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
163 for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
165 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
169 msg_Dbg (obj, "socket error: %m");
174 /* Try dual-mode IPv6 if available. */
175 if (ptr->ai_family == AF_INET6)
176 setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &(int){ 0 }, sizeof (int));
178 fd = net_SetupDgramSocket( obj, fd, ptr );
182 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
183 && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
198 static int net_SetMcastHopLimit( vlc_object_t *p_this,
199 int fd, int family, int hlim )
203 /* There is some confusion in the world whether IP_MULTICAST_TTL
204 * takes a byte or an int as an argument.
205 * BSD seems to indicate byte so we are going with that and use
206 * int as a fallback to be safe */
209 #ifdef IP_MULTICAST_TTL
212 cmd = IP_MULTICAST_TTL;
216 #ifdef IPV6_MULTICAST_HOPS
219 cmd = IPV6_MULTICAST_HOPS;
224 errno = EAFNOSUPPORT;
225 msg_Warn( p_this, "%m" );
229 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
231 /* BSD compatibility */
234 msg_Dbg( p_this, "cannot set hop limit (%d): %m", hlim );
235 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
236 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
238 msg_Err( p_this, "cannot set hop limit (%d): %m", hlim );
247 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
250 int scope = if_nametoindex (iface);
253 msg_Err (p_this, "invalid multicast interface: %s", iface);
259 #ifdef IPV6_MULTICAST_IF
261 if (setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
262 &scope, sizeof (scope) == 0))
269 struct ip_mreqn req = { .imr_ifindex = scope };
270 if (setsockopt (fd, SOL_IP, IP_MULTICAST_IF,
271 &req, sizeof (req)) == 0)
276 errno = EAFNOSUPPORT;
278 msg_Err (p_this, "cannot force multicast interface %s: %m", iface);
283 static unsigned var_GetIfIndex (vlc_object_t *obj)
285 char *ifname = var_InheritString (obj, "miface");
289 unsigned ifindex = if_nametoindex (ifname);
291 msg_Err (obj, "invalid multicast interface: %s", ifname);
298 * IP-agnostic multicast join,
299 * with fallback to old APIs, and fallback from SSM to ASM.
302 net_SourceSubscribe (vlc_object_t *obj, int fd,
303 const struct sockaddr *src, socklen_t srclen,
304 const struct sockaddr *grp, socklen_t grplen)
306 #ifdef MCAST_JOIN_SOURCE_GROUP
307 /* Agnostic SSM multicast join */
309 struct group_source_req gsr;
311 memset (&gsr, 0, sizeof (gsr));
312 gsr.gsr_interface = var_GetIfIndex (obj);
314 switch (grp->sa_family)
319 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
322 assert (grplen >= sizeof (struct sockaddr_in6));
323 if (g6->sin6_scope_id != 0)
324 gsr.gsr_interface = g6->sin6_scope_id;
332 errno = EAFNOSUPPORT;
336 assert (grplen <= sizeof (gsr.gsr_group));
337 memcpy (&gsr.gsr_source, src, srclen);
338 assert (srclen <= sizeof (gsr.gsr_source));
339 memcpy (&gsr.gsr_group, grp, grplen);
340 if (setsockopt (fd, level, MCAST_JOIN_SOURCE_GROUP,
341 &gsr, sizeof (gsr)) == 0)
345 if (src->sa_family != grp->sa_family)
347 errno = EAFNOSUPPORT;
351 switch (grp->sa_family)
353 # ifdef IP_ADD_SOURCE_MEMBERSHIP
354 /* IPv4-specific API */
357 struct ip_mreq_source imr;
359 memset (&imr, 0, sizeof (imr));
360 assert (grplen >= sizeof (struct sockaddr_in));
361 imr.imr_multiaddr = ((const struct sockaddr_in *)grp)->sin_addr;
362 assert (srclen >= sizeof (struct sockaddr_in));
363 imr.imr_sourceaddr = ((const struct sockaddr_in *)src)->sin_addr;
364 if (setsockopt (fd, SOL_IP, IP_ADD_SOURCE_MEMBERSHIP,
365 &imr, sizeof (imr)) == 0)
371 errno = EAFNOSUPPORT;
375 msg_Err (obj, "cannot join source multicast group: %m");
376 msg_Warn (obj, "trying ASM instead of SSM...");
377 return net_Subscribe (obj, fd, grp, grplen);
381 int net_Subscribe (vlc_object_t *obj, int fd,
382 const struct sockaddr *grp, socklen_t grplen)
384 #ifdef MCAST_JOIN_GROUP
385 /* Agnostic SSM multicast join */
389 memset (&gr, 0, sizeof (gr));
390 gr.gr_interface = var_GetIfIndex (obj);
392 switch (grp->sa_family)
397 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
400 assert (grplen >= sizeof (struct sockaddr_in6));
401 if (g6->sin6_scope_id != 0)
402 gr.gr_interface = g6->sin6_scope_id;
410 errno = EAFNOSUPPORT;
414 assert (grplen <= sizeof (gr.gr_group));
415 memcpy (&gr.gr_group, grp, grplen);
416 if (setsockopt (fd, level, MCAST_JOIN_GROUP, &gr, sizeof (gr)) == 0)
420 switch (grp->sa_family)
422 # ifdef IPV6_JOIN_GROUP
425 struct ipv6_mreq ipv6mr;
426 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
428 memset (&ipv6mr, 0, sizeof (ipv6mr));
429 assert (grplen >= sizeof (struct sockaddr_in6));
430 ipv6mr.ipv6mr_multiaddr = g6->sin6_addr;
431 ipv6mr.ipv6mr_interface = g6->sin6_scope_id;
432 if (!setsockopt (fd, SOL_IPV6, IPV6_JOIN_GROUP,
433 &ipv6mr, sizeof (ipv6mr)))
438 # ifdef IP_ADD_MEMBERSHIP
443 memset (&imr, 0, sizeof (imr));
444 assert (grplen >= sizeof (struct sockaddr_in));
445 imr.imr_multiaddr = ((const struct sockaddr_in *)grp)->sin_addr;
446 if (setsockopt (fd, SOL_IP, IP_ADD_MEMBERSHIP,
447 &imr, sizeof (imr)) == 0)
453 errno = EAFNOSUPPORT;
457 msg_Err (obj, "cannot join multicast group: %m");
462 static int net_SetDSCP( int fd, uint8_t dscp )
464 struct sockaddr_storage addr;
465 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
470 switch( addr.ss_family )
491 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
494 #undef net_ConnectDgram
495 /*****************************************************************************
497 *****************************************************************************
498 * Open a datagram socket to send data to a defined destination, with an
499 * optional hop limit.
500 *****************************************************************************/
501 int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
502 int i_hlim, int proto )
504 struct addrinfo hints, *res, *ptr;
505 int i_val, i_handle = -1;
506 bool b_unreach = false;
509 i_hlim = var_InheritInteger( p_this, "ttl" );
511 memset( &hints, 0, sizeof( hints ) );
512 hints.ai_socktype = SOCK_DGRAM;
513 hints.ai_protocol = proto;
515 msg_Dbg( p_this, "net: connecting to [%s]:%d", psz_host, i_port );
517 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
520 msg_Err( p_this, "cannot resolve [%s]:%d : %s", psz_host, i_port,
521 gai_strerror( i_val ) );
525 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
528 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
533 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
534 * to avoid packet loss caused by scheduling problems */
535 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
536 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
538 /* Allow broadcast sending */
539 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
542 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
544 str = var_InheritString (p_this, "miface");
547 net_SetMcastOut (p_this, fd, ptr->ai_family, str);
551 net_SetDSCP (fd, var_InheritInteger (p_this, "dscp"));
553 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
560 #if defined( WIN32 ) || defined( UNDER_CE )
561 if( WSAGetLastError () == WSAENETUNREACH )
563 if( errno == ENETUNREACH )
568 msg_Warn( p_this, "%s port %d : %m", psz_host, i_port);
579 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
588 /*****************************************************************************
590 *****************************************************************************
591 * OpenDgram a datagram socket and return a handle
592 *****************************************************************************/
593 int net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
594 const char *psz_server, int i_server, int protocol )
596 if ((psz_server == NULL) || (psz_server[0] == '\0'))
597 return net_ListenSingle (obj, psz_bind, i_bind, protocol);
599 msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
600 psz_server, i_server, psz_bind, i_bind);
602 struct addrinfo hints, *loc, *rem;
605 memset (&hints, 0, sizeof (hints));
606 hints.ai_socktype = SOCK_DGRAM;
607 hints.ai_protocol = protocol;
609 val = vlc_getaddrinfo (obj, psz_server, i_server, &hints, &rem);
612 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
617 hints.ai_flags = AI_PASSIVE;
618 val = vlc_getaddrinfo (obj, psz_bind, i_bind, &hints, &loc);
621 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
628 for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
630 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
633 continue; // usually, address family not supported
635 fd = net_SetupDgramSocket( obj, fd, ptr );
639 for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
641 if ((ptr2->ai_family != ptr->ai_family)
642 || (ptr2->ai_socktype != ptr->ai_socktype)
643 || (ptr2->ai_protocol != ptr->ai_protocol))
646 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
647 ? net_SourceSubscribe (obj, fd,
648 ptr2->ai_addr, ptr2->ai_addrlen,
649 ptr->ai_addr, ptr->ai_addrlen)
650 : connect (fd, ptr2->ai_addr, ptr2->ai_addrlen))
652 msg_Err (obj, "cannot connect to %s port %d: %m",
653 psz_server, i_server);
674 * Sets the send and receive checksum coverage of a socket:
676 * @param sendcov payload coverage of sent packets (bytes), -1 for full
677 * @param recvcov minimum payload coverage of received packets, -1 for full
679 int net_SetCSCov (int fd, int sendcov, int recvcov)
683 if (getsockopt (fd, SOL_SOCKET, SO_TYPE,
684 &type, &(socklen_t){ sizeof (type) }))
689 #ifdef UDPLITE_RECV_CSCOV
690 case SOCK_DGRAM: /* UDP-Lite */
694 sendcov += 8; /* partial */
695 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_SEND_CSCOV, &sendcov,
703 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
704 &recvcov, sizeof (recvcov)))
709 #ifdef DCCP_SOCKOPT_SEND_CSCOV
710 case SOCK_DCCP: /* DCCP and its ill-named socket type */
711 if ((sendcov == -1) || (sendcov > 56))
714 sendcov = (sendcov + 3) / 4;
715 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SEND_CSCOV,
716 &sendcov, sizeof (sendcov)))
719 if ((recvcov == -1) || (recvcov > 56))
722 recvcov = (recvcov + 3) / 4;
723 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_RECV_CSCOV,
724 &recvcov, sizeof (recvcov)))
730 #if !defined( UDPLITE_RECV_CSCOV ) && !defined( DCCP_SOCKOPT_SEND_CSCOV )