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>
43 # define EAFNOSUPPORT WSAEAFNOSUPPORT
51 #ifdef HAVE_LINUX_DCCP_H
52 # include <linux/dccp.h>
53 # ifndef SOCK_DCCP /* provisional API */
59 # define SOL_IP IPPROTO_IP
62 # define SOL_IPV6 IPPROTO_IPV6
65 # define IPPROTO_IPV6 41 /* IANA */
68 # define SOL_DCCP IPPROTO_DCCP
71 # define IPPROTO_DCCP 33 /* IANA */
74 # define SOL_UDPLITE IPPROTO_UDPLITE
76 #ifndef IPPROTO_UDPLITE
77 # define IPPROTO_UDPLITE 136 /* IANA */
80 #if defined (HAVE_NETINET_UDPLITE_H)
81 # include <netinet/udplite.h>
82 #elif defined (__linux__)
83 /* still missing from glibc 2.6 */
84 # define UDPLITE_SEND_CSCOV 10
85 # define UDPLITE_RECV_CSCOV 11
88 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
92 static int net_SetupDgramSocket (vlc_object_t *p_obj, int fd,
93 const struct addrinfo *ptr)
96 setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
100 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
101 && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
103 // This works for IPv4 too - don't worry!
104 struct sockaddr_in6 dumb =
106 .sin6_family = ptr->ai_addr->sa_family,
107 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
110 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
114 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
116 msg_Err( p_obj, "socket bind error: %s", vlc_strerror_c(net_errno) );
124 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
127 struct addrinfo hints = {
128 .ai_socktype = SOCK_DGRAM,
129 .ai_protocol = protocol,
130 .ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_IDN,
136 msg_Dbg (obj, "net: opening %s datagram port %d",
137 host ? host : "any", port);
139 int val = vlc_getaddrinfo (host, port, &hints, &res);
142 msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
149 for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
151 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
155 msg_Dbg (obj, "socket error: %s", vlc_strerror_c(net_errno));
160 /* Try dual-mode IPv6 if available. */
161 if (ptr->ai_family == AF_INET6)
162 setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &(int){ 0 }, sizeof (int));
164 fd = net_SetupDgramSocket( obj, fd, ptr );
168 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
169 && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
184 static int net_SetMcastHopLimit( vlc_object_t *p_this,
185 int fd, int family, int hlim )
189 /* There is some confusion in the world whether IP_MULTICAST_TTL
190 * takes a byte or an int as an argument.
191 * BSD seems to indicate byte so we are going with that and use
192 * int as a fallback to be safe */
195 #ifdef IP_MULTICAST_TTL
198 cmd = IP_MULTICAST_TTL;
202 #ifdef IPV6_MULTICAST_HOPS
205 cmd = IPV6_MULTICAST_HOPS;
210 errno = EAFNOSUPPORT;
211 msg_Warn( p_this, "%s", vlc_strerror_c(EAFNOSUPPORT) );
215 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
217 /* BSD compatibility */
220 msg_Dbg( p_this, "cannot set hop limit (%d): %s", hlim,
221 vlc_strerror_c(net_errno) );
222 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
223 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
225 msg_Err( p_this, "cannot set hop limit (%d): %s", hlim,
226 vlc_strerror_c(net_errno) );
235 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
238 int scope = if_nametoindex (iface);
241 msg_Err (p_this, "invalid multicast interface: %s", iface);
247 #ifdef IPV6_MULTICAST_IF
249 if (setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
250 &scope, sizeof (scope)) == 0)
258 struct ip_mreqn req = { .imr_ifindex = scope };
259 if (setsockopt (fd, SOL_IP, IP_MULTICAST_IF,
260 &req, sizeof (req)) == 0)
266 errno = EAFNOSUPPORT;
268 msg_Err (p_this, "cannot force multicast interface %s: %s", iface,
269 vlc_strerror_c(errno));
274 static unsigned var_GetIfIndex (vlc_object_t *obj)
276 char *ifname = var_InheritString (obj, "miface");
280 unsigned ifindex = if_nametoindex (ifname);
282 msg_Err (obj, "invalid multicast interface: %s", ifname);
289 * IP-agnostic multicast join,
290 * with fallback to old APIs, and fallback from SSM to ASM.
293 net_SourceSubscribe (vlc_object_t *obj, int fd,
294 const struct sockaddr *src, socklen_t srclen,
295 const struct sockaddr *grp, socklen_t grplen)
297 /* MCAST_JOIN_SOURCE_GROUP was introduced to OS X in v10.7, but it doesn't work,
298 * so ignore it to use the same code path as on 10.5 or 10.6 */
299 #if defined (MCAST_JOIN_SOURCE_GROUP) && !defined (__APPLE__)
300 /* Agnostic SSM multicast join */
302 struct group_source_req gsr;
304 memset (&gsr, 0, sizeof (gsr));
305 gsr.gsr_interface = var_GetIfIndex (obj);
307 switch (grp->sa_family)
312 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
315 assert (grplen >= sizeof (struct sockaddr_in6));
316 if (g6->sin6_scope_id != 0)
317 gsr.gsr_interface = g6->sin6_scope_id;
325 errno = EAFNOSUPPORT;
329 assert (grplen <= sizeof (gsr.gsr_group));
330 memcpy (&gsr.gsr_source, src, srclen);
331 assert (srclen <= sizeof (gsr.gsr_source));
332 memcpy (&gsr.gsr_group, grp, grplen);
333 if (setsockopt (fd, level, MCAST_JOIN_SOURCE_GROUP,
334 &gsr, sizeof (gsr)) == 0)
338 if (src->sa_family != grp->sa_family)
340 errno = EAFNOSUPPORT;
344 switch (grp->sa_family)
346 # ifdef IP_ADD_SOURCE_MEMBERSHIP
347 /* IPv4-specific API */
350 struct ip_mreq_source imr;
352 memset (&imr, 0, sizeof (imr));
353 assert (grplen >= sizeof (struct sockaddr_in));
354 imr.imr_multiaddr = ((const struct sockaddr_in *)grp)->sin_addr;
355 assert (srclen >= sizeof (struct sockaddr_in));
356 imr.imr_sourceaddr = ((const struct sockaddr_in *)src)->sin_addr;
357 if (setsockopt (fd, SOL_IP, IP_ADD_SOURCE_MEMBERSHIP,
358 &imr, sizeof (imr)) == 0)
364 errno = EAFNOSUPPORT;
368 msg_Err (obj, "cannot join source multicast group: %s",
369 vlc_strerror_c(net_errno));
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: %s",
454 vlc_strerror_c(net_errno));
459 static int net_SetDSCP( int fd, uint8_t dscp )
461 struct sockaddr_storage addr;
462 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
467 switch( addr.ss_family )
488 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
491 #undef net_ConnectDgram
492 /*****************************************************************************
494 *****************************************************************************
495 * Open a datagram socket to send data to a defined destination, with an
496 * optional hop limit.
497 *****************************************************************************/
498 int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
499 int i_hlim, int proto )
501 struct addrinfo hints = {
502 .ai_socktype = SOCK_DGRAM,
503 .ai_protocol = proto,
504 .ai_flags = AI_NUMERICSERV | AI_IDN,
507 bool b_unreach = false;
510 i_hlim = var_InheritInteger( p_this, "ttl" );
512 msg_Dbg( p_this, "net: connecting to [%s]:%d", psz_host, i_port );
514 int val = vlc_getaddrinfo (psz_host, i_port, &hints, &res);
517 msg_Err (p_this, "cannot resolve [%s]:%d : %s", psz_host, i_port,
522 for (struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
525 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
530 /* Allow broadcast sending */
531 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
534 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
536 str = var_InheritString (p_this, "miface");
539 net_SetMcastOut (p_this, fd, ptr->ai_family, str);
543 net_SetDSCP (fd, var_InheritInteger (p_this, "dscp"));
545 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
552 #if defined( _WIN32 )
553 if( WSAGetLastError () == WSAENETUNREACH )
555 if( errno == ENETUNREACH )
559 msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
560 vlc_strerror_c(errno) );
569 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
578 /*****************************************************************************
580 *****************************************************************************
581 * OpenDgram a datagram socket and return a handle
582 *****************************************************************************/
583 int net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
584 const char *psz_server, int i_server, int protocol )
586 if ((psz_server == NULL) || (psz_server[0] == '\0'))
587 return net_ListenSingle (obj, psz_bind, i_bind, protocol);
589 msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
590 psz_server, i_server, psz_bind, i_bind);
592 struct addrinfo hints = {
593 .ai_socktype = SOCK_DGRAM,
594 .ai_protocol = protocol,
595 .ai_flags = AI_NUMERICSERV | AI_IDN,
598 int val = vlc_getaddrinfo (psz_server, i_server, &hints, &rem);
601 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
606 hints.ai_flags |= AI_PASSIVE;
607 val = vlc_getaddrinfo (psz_bind, i_bind, &hints, &loc);
610 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
617 for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
619 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
622 continue; // usually, address family not supported
624 fd = net_SetupDgramSocket( obj, fd, ptr );
628 for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
630 if ((ptr2->ai_family != ptr->ai_family)
631 || (ptr2->ai_socktype != ptr->ai_socktype)
632 || (ptr2->ai_protocol != ptr->ai_protocol))
635 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
636 ? net_SourceSubscribe (obj, fd,
637 ptr2->ai_addr, ptr2->ai_addrlen,
638 ptr->ai_addr, ptr->ai_addrlen)
639 : connect (fd, ptr2->ai_addr, ptr2->ai_addrlen))
641 msg_Err (obj, "cannot connect to %s port %d: %s",
642 psz_server, i_server, vlc_strerror_c(net_errno));
663 * Sets the send and receive checksum coverage of a socket:
665 * @param sendcov payload coverage of sent packets (bytes), -1 for full
666 * @param recvcov minimum payload coverage of received packets, -1 for full
668 int net_SetCSCov (int fd, int sendcov, int recvcov)
672 if (getsockopt (fd, SOL_SOCKET, SO_TYPE,
673 &type, &(socklen_t){ sizeof (type) }))
678 #ifdef UDPLITE_RECV_CSCOV
679 case SOCK_DGRAM: /* UDP-Lite */
683 sendcov += 8; /* partial */
684 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_SEND_CSCOV, &sendcov,
692 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
693 &recvcov, sizeof (recvcov)))
698 #ifdef DCCP_SOCKOPT_SEND_CSCOV
699 case SOCK_DCCP: /* DCCP and its ill-named socket type */
700 if ((sendcov == -1) || (sendcov > 56))
703 sendcov = (sendcov + 3) / 4;
704 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SEND_CSCOV,
705 &sendcov, sizeof (sendcov)))
708 if ((recvcov == -1) || (recvcov > 56))
711 recvcov = (recvcov + 3) / 4;
712 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_RECV_CSCOV,
713 &recvcov, sizeof (recvcov)))
719 #if !defined( UDPLITE_RECV_CSCOV ) && !defined( DCCP_SOCKOPT_SEND_CSCOV )