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 # define EAFNOSUPPORT WSAEAFNOSUPPORT
50 #ifdef HAVE_LINUX_DCCP_H
51 # include <linux/dccp.h>
52 # ifndef SOCK_DCCP /* provisional API */
58 # define SOL_IP IPPROTO_IP
61 # define SOL_IPV6 IPPROTO_IPV6
64 # define IPPROTO_IPV6 41 /* IANA */
67 # define SOL_DCCP IPPROTO_DCCP
70 # define IPPROTO_DCCP 33 /* IANA */
73 # define SOL_UDPLITE IPPROTO_UDPLITE
75 #ifndef IPPROTO_UDPLITE
76 # define IPPROTO_UDPLITE 136 /* IANA */
79 #if defined (HAVE_NETINET_UDPLITE_H)
80 # include <netinet/udplite.h>
81 #elif defined (__linux__)
82 /* still missing from glibc 2.6 */
83 # define UDPLITE_SEND_CSCOV 10
84 # define UDPLITE_RECV_CSCOV 11
87 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
91 static int net_SetupDgramSocket (vlc_object_t *p_obj, int fd,
92 const struct addrinfo *ptr)
95 setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
99 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
100 * to avoid packet loss caused in case of scheduling hiccups */
101 setsockopt (fd, SOL_SOCKET, SO_RCVBUF,
102 (void *)&(int){ 0x80000 }, sizeof (int));
103 setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
104 (void *)&(int){ 0x80000 }, sizeof (int));
108 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
109 && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
111 // This works for IPv4 too - don't worry!
112 struct sockaddr_in6 dumb =
114 .sin6_family = ptr->ai_addr->sa_family,
115 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
118 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
122 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
124 msg_Err( p_obj, "socket bind error (%m)" );
132 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
135 struct addrinfo hints = {
136 .ai_socktype = SOCK_DGRAM,
137 .ai_protocol = protocol,
138 .ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_IDN,
144 msg_Dbg (obj, "net: opening %s datagram port %d",
145 host ? host : "any", port);
147 int val = vlc_getaddrinfo (host, port, &hints, &res);
150 msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
157 for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
159 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
163 msg_Dbg (obj, "socket error: %m");
168 /* Try dual-mode IPv6 if available. */
169 if (ptr->ai_family == AF_INET6)
170 setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &(int){ 0 }, sizeof (int));
172 fd = net_SetupDgramSocket( obj, fd, ptr );
176 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
177 && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
192 static int net_SetMcastHopLimit( vlc_object_t *p_this,
193 int fd, int family, int hlim )
197 /* There is some confusion in the world whether IP_MULTICAST_TTL
198 * takes a byte or an int as an argument.
199 * BSD seems to indicate byte so we are going with that and use
200 * int as a fallback to be safe */
203 #ifdef IP_MULTICAST_TTL
206 cmd = IP_MULTICAST_TTL;
210 #ifdef IPV6_MULTICAST_HOPS
213 cmd = IPV6_MULTICAST_HOPS;
218 errno = EAFNOSUPPORT;
219 msg_Warn( p_this, "%m" );
223 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
225 /* BSD compatibility */
228 msg_Dbg( p_this, "cannot set hop limit (%d): %m", hlim );
229 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
230 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
232 msg_Err( p_this, "cannot set hop limit (%d): %m", hlim );
241 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
244 int scope = if_nametoindex (iface);
247 msg_Err (p_this, "invalid multicast interface: %s", iface);
253 #ifdef IPV6_MULTICAST_IF
255 if (setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
256 &scope, sizeof (scope)) == 0)
263 struct ip_mreqn req = { .imr_ifindex = scope };
264 if (setsockopt (fd, SOL_IP, IP_MULTICAST_IF,
265 &req, sizeof (req)) == 0)
270 errno = EAFNOSUPPORT;
272 msg_Err (p_this, "cannot force multicast interface %s: %m", iface);
277 static unsigned var_GetIfIndex (vlc_object_t *obj)
279 char *ifname = var_InheritString (obj, "miface");
283 unsigned ifindex = if_nametoindex (ifname);
285 msg_Err (obj, "invalid multicast interface: %s", ifname);
292 * IP-agnostic multicast join,
293 * with fallback to old APIs, and fallback from SSM to ASM.
296 net_SourceSubscribe (vlc_object_t *obj, int fd,
297 const struct sockaddr *src, socklen_t srclen,
298 const struct sockaddr *grp, socklen_t grplen)
300 #ifdef MCAST_JOIN_SOURCE_GROUP
301 /* Agnostic SSM multicast join */
303 struct group_source_req gsr;
305 memset (&gsr, 0, sizeof (gsr));
306 gsr.gsr_interface = var_GetIfIndex (obj);
308 switch (grp->sa_family)
313 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
316 assert (grplen >= sizeof (struct sockaddr_in6));
317 if (g6->sin6_scope_id != 0)
318 gsr.gsr_interface = g6->sin6_scope_id;
326 errno = EAFNOSUPPORT;
330 assert (grplen <= sizeof (gsr.gsr_group));
331 memcpy (&gsr.gsr_source, src, srclen);
332 assert (srclen <= sizeof (gsr.gsr_source));
333 memcpy (&gsr.gsr_group, grp, grplen);
334 if (setsockopt (fd, level, MCAST_JOIN_SOURCE_GROUP,
335 &gsr, sizeof (gsr)) == 0)
339 if (src->sa_family != grp->sa_family)
341 errno = EAFNOSUPPORT;
345 switch (grp->sa_family)
347 # ifdef IP_ADD_SOURCE_MEMBERSHIP
348 /* IPv4-specific API */
351 struct ip_mreq_source imr;
353 memset (&imr, 0, sizeof (imr));
354 assert (grplen >= sizeof (struct sockaddr_in));
355 imr.imr_multiaddr = ((const struct sockaddr_in *)grp)->sin_addr;
356 assert (srclen >= sizeof (struct sockaddr_in));
357 imr.imr_sourceaddr = ((const struct sockaddr_in *)src)->sin_addr;
358 if (setsockopt (fd, SOL_IP, IP_ADD_SOURCE_MEMBERSHIP,
359 &imr, sizeof (imr)) == 0)
365 errno = EAFNOSUPPORT;
369 msg_Err (obj, "cannot join source multicast group: %m");
370 msg_Warn (obj, "trying ASM instead of SSM...");
371 return net_Subscribe (obj, fd, grp, grplen);
375 int net_Subscribe (vlc_object_t *obj, int fd,
376 const struct sockaddr *grp, socklen_t grplen)
378 /* MCAST_JOIN_GROUP was introduced to OS X in v10.7, but it doesn't work,
379 * so ignore it to use the same code as on 10.5 or 10.6 */
380 #if defined (MCAST_JOIN_GROUP) && !defined (__APPLE__)
381 /* Agnostic SSM multicast join */
385 memset (&gr, 0, sizeof (gr));
386 gr.gr_interface = var_GetIfIndex (obj);
388 switch (grp->sa_family)
393 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
396 assert (grplen >= sizeof (struct sockaddr_in6));
397 if (g6->sin6_scope_id != 0)
398 gr.gr_interface = g6->sin6_scope_id;
406 errno = EAFNOSUPPORT;
410 assert (grplen <= sizeof (gr.gr_group));
411 memcpy (&gr.gr_group, grp, grplen);
412 if (setsockopt (fd, level, MCAST_JOIN_GROUP, &gr, sizeof (gr)) == 0)
416 switch (grp->sa_family)
418 # ifdef IPV6_JOIN_GROUP
421 struct ipv6_mreq ipv6mr;
422 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
424 memset (&ipv6mr, 0, sizeof (ipv6mr));
425 assert (grplen >= sizeof (struct sockaddr_in6));
426 ipv6mr.ipv6mr_multiaddr = g6->sin6_addr;
427 ipv6mr.ipv6mr_interface = g6->sin6_scope_id;
428 if (!setsockopt (fd, SOL_IPV6, IPV6_JOIN_GROUP,
429 &ipv6mr, sizeof (ipv6mr)))
434 # ifdef IP_ADD_MEMBERSHIP
439 memset (&imr, 0, sizeof (imr));
440 assert (grplen >= sizeof (struct sockaddr_in));
441 imr.imr_multiaddr = ((const struct sockaddr_in *)grp)->sin_addr;
442 if (setsockopt (fd, SOL_IP, IP_ADD_MEMBERSHIP,
443 &imr, sizeof (imr)) == 0)
449 errno = EAFNOSUPPORT;
453 msg_Err (obj, "cannot join multicast group: %m");
458 static 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));
490 #undef net_ConnectDgram
491 /*****************************************************************************
493 *****************************************************************************
494 * Open a datagram socket to send data to a defined destination, with an
495 * optional hop limit.
496 *****************************************************************************/
497 int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
498 int i_hlim, int proto )
500 struct addrinfo hints = {
501 .ai_socktype = SOCK_DGRAM,
502 .ai_protocol = proto,
503 .ai_flags = AI_NUMERICSERV | AI_IDN,
506 bool b_unreach = false;
509 i_hlim = var_InheritInteger( p_this, "ttl" );
511 msg_Dbg( p_this, "net: connecting to [%s]:%d", psz_host, i_port );
513 int val = vlc_getaddrinfo (psz_host, i_port, &hints, &res);
516 msg_Err (p_this, "cannot resolve [%s]:%d : %s", psz_host, i_port,
521 for (struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
524 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
529 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
530 * to avoid packet loss caused by scheduling problems */
531 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
532 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
534 /* Allow broadcast sending */
535 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
538 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
540 str = var_InheritString (p_this, "miface");
543 net_SetMcastOut (p_this, fd, ptr->ai_family, str);
547 net_SetDSCP (fd, var_InheritInteger (p_this, "dscp"));
549 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
556 #if defined( _WIN32 )
557 if( WSAGetLastError () == WSAENETUNREACH )
559 if( errno == ENETUNREACH )
563 msg_Warn( p_this, "%s port %d : %m", psz_host, i_port);
572 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
581 /*****************************************************************************
583 *****************************************************************************
584 * OpenDgram a datagram socket and return a handle
585 *****************************************************************************/
586 int net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
587 const char *psz_server, int i_server, int protocol )
589 if ((psz_server == NULL) || (psz_server[0] == '\0'))
590 return net_ListenSingle (obj, psz_bind, i_bind, protocol);
592 msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
593 psz_server, i_server, psz_bind, i_bind);
595 struct addrinfo hints = {
596 .ai_socktype = SOCK_DGRAM,
597 .ai_protocol = protocol,
598 .ai_flags = AI_NUMERICSERV | AI_IDN,
601 int val = vlc_getaddrinfo (psz_server, i_server, &hints, &rem);
604 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
609 hints.ai_flags |= AI_PASSIVE;
610 val = vlc_getaddrinfo (psz_bind, i_bind, &hints, &loc);
613 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
620 for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
622 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
625 continue; // usually, address family not supported
627 fd = net_SetupDgramSocket( obj, fd, ptr );
631 for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
633 if ((ptr2->ai_family != ptr->ai_family)
634 || (ptr2->ai_socktype != ptr->ai_socktype)
635 || (ptr2->ai_protocol != ptr->ai_protocol))
638 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
639 ? net_SourceSubscribe (obj, fd,
640 ptr2->ai_addr, ptr2->ai_addrlen,
641 ptr->ai_addr, ptr->ai_addrlen)
642 : connect (fd, ptr2->ai_addr, ptr2->ai_addrlen))
644 msg_Err (obj, "cannot connect to %s port %d: %m",
645 psz_server, i_server);
666 * Sets the send and receive checksum coverage of a socket:
668 * @param sendcov payload coverage of sent packets (bytes), -1 for full
669 * @param recvcov minimum payload coverage of received packets, -1 for full
671 int net_SetCSCov (int fd, int sendcov, int recvcov)
675 if (getsockopt (fd, SOL_SOCKET, SO_TYPE,
676 &type, &(socklen_t){ sizeof (type) }))
681 #ifdef UDPLITE_RECV_CSCOV
682 case SOCK_DGRAM: /* UDP-Lite */
686 sendcov += 8; /* partial */
687 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_SEND_CSCOV, &sendcov,
695 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
696 &recvcov, sizeof (recvcov)))
701 #ifdef DCCP_SOCKOPT_SEND_CSCOV
702 case SOCK_DCCP: /* DCCP and its ill-named socket type */
703 if ((sendcov == -1) || (sendcov > 56))
706 sendcov = (sendcov + 3) / 4;
707 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SEND_CSCOV,
708 &sendcov, sizeof (sendcov)))
711 if ((recvcov == -1) || (recvcov > 56))
714 recvcov = (recvcov + 3) / 4;
715 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_RECV_CSCOV,
716 &recvcov, sizeof (recvcov)))
722 #if !defined( UDPLITE_RECV_CSCOV ) && !defined( DCCP_SOCKOPT_SEND_CSCOV )