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 *****************************************************************************/
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.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
49 # define if_nametoindex( str ) atoi( str )
58 # define SOL_IP IPPROTO_IP
61 # define SOL_IPV6 IPPROTO_IPV6
64 # define IPPROTO_IPV6 41
67 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
70 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
71 int family, int protocol)
73 struct addrinfo hints, *res;
75 memset (&hints, 0, sizeof( hints ));
76 hints.ai_family = family;
77 hints.ai_socktype = SOCK_DGRAM;
78 hints.ai_flags = AI_PASSIVE;
83 msg_Dbg (obj, "net: opening %s datagram port %d", host ?: "any", port);
85 int val = vlc_getaddrinfo (obj, host, port, &hints, &res);
88 msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
89 vlc_gai_strerror (val));
95 for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
97 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
98 protocol ?: ptr->ai_protocol);
101 msg_Dbg (obj, "socket error: %s", net_strerror (net_errno));
105 if (ptr->ai_next != NULL)
108 if ((ptr->ai_family != AF_INET6)
109 || setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &(int){ 0 },
113 msg_Err (obj, "Multiple network protocols present");
114 msg_Err (obj, "Please select network protocol manually");
118 /* Bind the socket */
119 #if defined (WIN32) || defined (UNDER_CE)
120 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
121 && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
123 struct sockaddr_in6 dumb =
125 .sin6_family = ptr->ai_addr->sa_family,
126 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
128 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
132 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
134 msg_Err (obj, "socket bind error (%s)", net_strerror (net_errno));
139 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
140 && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
150 vlc_freeaddrinfo (res);
155 static int net_SetMcastHopLimit( vlc_object_t *p_this,
156 int fd, int family, int hlim )
160 /* There is some confusion in the world whether IP_MULTICAST_TTL
161 * takes a byte or an int as an argument.
162 * BSD seems to indicate byte so we are going with that and use
163 * int as a fallback to be safe */
166 #ifdef IP_MULTICAST_TTL
169 cmd = IP_MULTICAST_TTL;
173 #ifdef IPV6_MULTICAST_HOPS
176 cmd = IPV6_MULTICAST_HOPS;
181 msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
185 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
187 /* BSD compatibility */
190 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
191 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
199 static int net_SetMcastOutIface (int fd, int family, int scope)
203 #ifdef IPV6_MULTICAST_IF
205 return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
206 &scope, sizeof (scope));
212 struct ip_mreqn req = { .imr_ifindex = scope };
214 return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
220 errno = EAFNOSUPPORT;
225 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
227 #ifdef IP_MULTICAST_IF
228 return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
230 errno = EAFNOSUPPORT;
236 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
237 const char *iface, const char *addr)
241 int scope = if_nametoindex (iface);
244 msg_Err (p_this, "invalid multicast interface: %s", iface);
248 if (net_SetMcastOutIface (fd, family, scope) == 0)
251 msg_Err (p_this, "%s: %s", iface, net_strerror (net_errno));
256 if (family == AF_INET)
259 if (inet_pton (AF_INET, addr, &ipv4) <= 0)
261 msg_Err (p_this, "invalid IPv4 address for multicast: %s",
266 if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
269 msg_Err (p_this, "%s: %s", addr, net_strerror (net_errno));
278 * Old-style any-source multicast join.
279 * In use on Windows XP/2003 and older.
282 net_IPv4Join (vlc_object_t *obj, int fd,
283 const struct sockaddr_in *src, const struct sockaddr_in *grp)
285 #ifdef IP_ADD_MEMBERSHIP
289 # ifdef IP_ADD_SOURCE_MEMBERSHIP
290 struct ip_mreq_source gsr4;
294 struct in_addr id = { .s_addr = INADDR_ANY };
297 /* Multicast interface IPv4 address */
298 char *iface = var_CreateGetString (obj, "miface-addr");
302 && (inet_pton (AF_INET, iface, &id) <= 0))
304 msg_Err (obj, "invalid multicast interface address %s", iface);
310 memset (&opt, 0, sizeof (opt));
313 # ifdef IP_ADD_SOURCE_MEMBERSHIP
314 cmd = IP_ADD_SOURCE_MEMBERSHIP;
315 opt.gsr4.imr_multiaddr = grp->sin_addr;
316 opt.gsr4.imr_sourceaddr = src->sin_addr;
317 opt.gsr4.imr_interface = id;
318 optlen = sizeof (opt.gsr4);
326 cmd = IP_ADD_MEMBERSHIP;
327 opt.gr4.imr_multiaddr = grp->sin_addr;
328 opt.gr4.imr_interface = id;
329 optlen = sizeof (opt.gr4);
332 msg_Dbg (obj, "IP_ADD_%sMEMBERSHIP multicast request",
333 (src != NULL) ? "SOURCE_" : "");
335 if (setsockopt (fd, SOL_IP, cmd, &opt, optlen) == 0)
341 msg_Err (obj, "cannot join IPv4 multicast group (%s)",
342 net_strerror (net_errno));
348 net_IPv6Join (vlc_object_t *obj, int fd, const struct sockaddr_in6 *src)
350 #ifdef IPV6_JOIN_GROUP
351 struct ipv6_mreq gr6;
352 memset (&gr6, 0, sizeof (gr6));
353 gr6.ipv6mr_interface = src->sin6_scope_id;
354 memcpy (&gr6.ipv6mr_multiaddr, &src->sin6_addr, 16);
356 msg_Dbg (obj, "IPV6_JOIN_GROUP multicast request");
358 if (!setsockopt (fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &gr6, sizeof (gr6)))
364 msg_Err (obj, "cannot join IPv6 any-source multicast group (%s)",
365 net_strerror (net_errno));
370 #if defined (WIN32) && !defined (MCAST_JOIN_SOURCE_GROUP)
372 * I hate manual definitions: Error-prone. Portability hell.
373 * Developers shall use UP-TO-DATE compilers. Full point.
374 * If you remove the warning, you remove the whole ifndef.
376 # warning Your C headers are out-of-date. Please update.
378 # define MCAST_JOIN_GROUP 41
382 struct sockaddr_storage gr_group;
385 # define MCAST_JOIN_SOURCE_GROUP 45 /* from <ws2ipdef.h> */
386 struct group_source_req
388 uint32_t gsr_interface;
389 struct sockaddr_storage gsr_group;
390 struct sockaddr_storage gsr_source;
395 * IP-agnostic multicast join,
396 * with fallback to old APIs, and fallback from SSM to ASM.
399 net_SourceSubscribe (vlc_object_t *obj, int fd,
400 const struct sockaddr *src, socklen_t srclen,
401 const struct sockaddr *grp, socklen_t grplen)
405 char *iface = var_CreateGetString (obj, "miface");
410 iid = if_nametoindex (iface);
413 msg_Err (obj, "invalid multicast interface: %s", iface);
421 switch (grp->sa_family)
426 if (((const struct sockaddr_in6 *)grp)->sin6_scope_id)
427 iid = ((const struct sockaddr_in6 *)grp)->sin6_scope_id;
436 errno = EAFNOSUPPORT;
441 switch (src->sa_family)
445 if (memcmp (&((const struct sockaddr_in6 *)src)->sin6_addr,
446 &in6addr_any, sizeof (in6addr_any)) == 0)
452 if (((const struct sockaddr_in *)src)->sin_addr.s_addr
459 /* Agnostic ASM/SSM multicast join */
460 #ifdef MCAST_JOIN_SOURCE_GROUP
464 struct group_source_req gsr;
468 memset (&opt, 0, sizeof (opt));
472 if ((grplen > sizeof (opt.gsr.gsr_group))
473 || (srclen > sizeof (opt.gsr.gsr_source)))
476 opt.gsr.gsr_interface = iid;
477 memcpy (&opt.gsr.gsr_source, src, srclen);
478 memcpy (&opt.gsr.gsr_group, grp, grplen);
479 optlen = sizeof (opt.gsr);
483 if (grplen > sizeof (opt.gr.gr_group))
486 opt.gr.gr_interface = iid;
487 memcpy (&opt.gr.gr_group, grp, grplen);
488 optlen = sizeof (opt.gr);
491 msg_Dbg (obj, "Multicast %sgroup join request", src ? "source " : "");
493 if (setsockopt (fd, level,
494 src ? MCAST_JOIN_SOURCE_GROUP : MCAST_JOIN_GROUP,
495 (void *)&opt, optlen) == 0)
499 /* Fallback to IPv-specific APIs */
500 if ((src != NULL) && (src->sa_family != grp->sa_family))
503 switch (grp->sa_family)
506 if ((grplen < sizeof (struct sockaddr_in))
507 || ((src != NULL) && (srclen < sizeof (struct sockaddr_in))))
510 if (net_IPv4Join (obj, fd, (const struct sockaddr_in *)src,
511 (const struct sockaddr_in *)grp) == 0)
517 if ((grplen < sizeof (struct sockaddr_in6))
518 || ((src != NULL) && (srclen < sizeof (struct sockaddr_in6))))
521 /* IPv6-specific SSM API does not exist. So if we're here
522 * it means IPv6 SSM is not supported on this OS and we
523 * directly fallback to ASM */
525 if (net_IPv6Join (obj, fd, (const struct sockaddr_in6 *)grp) == 0)
531 msg_Err (obj, "Multicast group join error (%s)",
532 net_strerror (net_errno));
536 msg_Warn (obj, "Trying ASM instead of SSM...");
537 return net_Subscribe (obj, fd, grp, grplen);
540 msg_Err (obj, "Multicast not supported");
545 int net_Subscribe (vlc_object_t *obj, int fd,
546 const struct sockaddr *addr, socklen_t addrlen)
548 return net_SourceSubscribe (obj, fd, NULL, 0, addr, addrlen);
552 int net_SetDSCP( int fd, uint8_t dscp )
554 struct sockaddr_storage addr;
555 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
560 switch( addr.ss_family )
581 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
585 /*****************************************************************************
586 * __net_ConnectDgram:
587 *****************************************************************************
588 * Open a datagram socket to send data to a defined destination, with an
589 * optional hop limit.
590 *****************************************************************************/
591 int __net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
592 int i_hlim, int proto )
594 struct addrinfo hints, *res, *ptr;
595 int i_val, i_handle = -1;
596 vlc_bool_t b_unreach = VLC_FALSE;
599 i_port = 1234; /* historical VLC thing */
602 i_hlim = var_CreateGetInteger( p_this, "ttl" );
604 memset( &hints, 0, sizeof( hints ) );
605 hints.ai_socktype = SOCK_DGRAM;
607 msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
609 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
612 msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
613 vlc_gai_strerror( i_val ) );
617 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
620 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
621 proto ?: ptr->ai_protocol);
625 #if !defined( SYS_BEOS )
626 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
627 * to avoid packet loss caused by scheduling problems */
628 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
629 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
631 /* Allow broadcast sending */
632 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
636 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
638 str = var_CreateGetString (p_this, "miface");
642 net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
646 str = var_CreateGetString (p_this, "miface-addr");
650 net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
654 net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
656 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
663 #if defined( WIN32 ) || defined( UNDER_CE )
664 if( WSAGetLastError () == WSAENETUNREACH )
666 if( errno == ENETUNREACH )
668 b_unreach = VLC_TRUE;
671 msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
678 vlc_freeaddrinfo( res );
683 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
692 /*****************************************************************************
694 *****************************************************************************
695 * OpenDgram a datagram socket and return a handle
696 *****************************************************************************/
697 int __net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
698 const char *psz_server, int i_server,
699 int family, int protocol )
701 if ((psz_server == NULL) || (psz_server[0] == '\0'))
702 return net_ListenSingle (obj, psz_bind, i_bind, family, protocol);
704 msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
705 psz_server, i_server, psz_bind, i_bind);
707 struct addrinfo hints, *loc, *rem;
710 memset (&hints, 0, sizeof (hints));
711 hints.ai_family = family;
712 hints.ai_socktype = SOCK_DGRAM;
713 hints.ai_flags = AI_PASSIVE;
715 val = vlc_getaddrinfo (obj, psz_server, i_server, &hints, &rem);
718 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
719 vlc_gai_strerror (val));
723 val = vlc_getaddrinfo (obj, psz_bind, i_bind, &hints, &loc);
726 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
727 vlc_gai_strerror (val));
728 vlc_freeaddrinfo (rem);
732 for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
734 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
735 protocol ?: ptr->ai_protocol);
737 continue; // usually, address family not supported
740 setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
744 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
745 * to avoid packet loss caused in case of scheduling hiccups */
746 setsockopt (fd, SOL_SOCKET, SO_RCVBUF,
747 (void *)&(int){ 0x80000 }, sizeof (int));
748 setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
749 (void *)&(int){ 0x80000 }, sizeof (int));
752 #if defined (WIN32) || defined (UNDER_CE)
753 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
754 && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
756 // This works for IPv4 too - don't worry!
757 struct sockaddr_in6 dumb =
759 .sin6_family = ptr->ai_addr->sa_family,
760 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
763 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
767 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
774 for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
776 if ((ptr2->ai_family != ptr->ai_family)
777 || (ptr2->ai_socktype != ptr->ai_socktype)
778 || (ptr2->ai_protocol != ptr->ai_protocol))
781 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
782 ? net_SourceSubscribe (obj, fd,
783 ptr2->ai_addr, ptr2->ai_addrlen,
784 ptr->ai_addr, ptr->ai_addrlen)
785 : connect (fd, ptr2->ai_addr, ptr2->ai_addrlen))
787 msg_Err (obj, "cannot connect to %s port %d: %s",
788 psz_server, i_server, net_strerror (net_errno));
801 vlc_freeaddrinfo (rem);
802 vlc_freeaddrinfo (loc);