screen: disable compilation for non-Mac Darwin flavors
[vlc/vlc-acra.git] / src / network / udp.c
blob6763f10a399c4f94529a14e3d512cca0b73fd059
1 /*****************************************************************************
2 * udp.c:
3 *****************************************************************************
4 * Copyright (C) 2004-2006 VLC authors and VideoLAN
5 * Copyright © 2006-2007 Rémi Denis-Courmont
7 * $Id$
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 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
36 #include <errno.h>
37 #include <assert.h>
39 #include <vlc_network.h>
41 #ifdef WIN32
42 # define EAFNOSUPPORT WSAEAFNOSUPPORT
43 #else
44 # include <unistd.h>
45 # ifdef HAVE_NET_IF_H
46 # include <net/if.h>
47 # endif
48 #endif
50 #ifdef HAVE_LINUX_DCCP_H
51 # include <linux/dccp.h>
52 # ifndef SOCK_DCCP /* provisional API */
53 # define SOCK_DCCP 6
54 # endif
55 #endif
57 #ifndef SOL_IP
58 # define SOL_IP IPPROTO_IP
59 #endif
60 #ifndef SOL_IPV6
61 # define SOL_IPV6 IPPROTO_IPV6
62 #endif
63 #ifndef IPPROTO_IPV6
64 # define IPPROTO_IPV6 41 /* IANA */
65 #endif
66 #ifndef SOL_DCCP
67 # define SOL_DCCP IPPROTO_DCCP
68 #endif
69 #ifndef IPPROTO_DCCP
70 # define IPPROTO_DCCP 33 /* IANA */
71 #endif
72 #ifndef SOL_UDPLITE
73 # define SOL_UDPLITE IPPROTO_UDPLITE
74 #endif
75 #ifndef IPPROTO_UDPLITE
76 # define IPPROTO_UDPLITE 136 /* IANA */
77 #endif
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
85 #endif
87 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
88 int i_protocol );
90 /* */
91 static int net_SetupDgramSocket (vlc_object_t *p_obj, int fd,
92 const struct addrinfo *ptr)
94 #ifdef SO_REUSEPORT
95 setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
96 #endif
98 #ifdef SO_RCVBUF
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));
105 #endif
107 #if defined (WIN32)
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);
120 else
121 #endif
122 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
124 msg_Err( p_obj, "socket bind error (%m)" );
125 net_Close (fd);
126 return -1;
128 return fd;
131 /* */
132 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
133 int protocol)
135 struct addrinfo hints = {
136 .ai_socktype = SOCK_DGRAM,
137 .ai_protocol = protocol,
138 .ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_IDN,
139 }, *res;
141 if (host && !*host)
142 host = NULL;
144 msg_Dbg (obj, "net: opening %s datagram port %d",
145 host ? host : "any", port);
147 int val = vlc_getaddrinfo (host, port, &hints, &res);
148 if (val)
150 msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
151 gai_strerror (val));
152 return -1;
155 val = -1;
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,
160 ptr->ai_protocol);
161 if (fd == -1)
163 msg_Dbg (obj, "socket error: %m");
164 continue;
167 #ifdef IPV6_V6ONLY
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));
171 #endif
172 fd = net_SetupDgramSocket( obj, fd, ptr );
173 if( fd == -1 )
174 continue;
176 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
177 && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
179 net_Close (fd);
180 continue;
183 val = fd;
184 break;
187 freeaddrinfo (res);
188 return val;
192 static int net_SetMcastHopLimit( vlc_object_t *p_this,
193 int fd, int family, int hlim )
195 int proto, cmd;
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 */
201 switch( family )
203 #ifdef IP_MULTICAST_TTL
204 case AF_INET:
205 proto = SOL_IP;
206 cmd = IP_MULTICAST_TTL;
207 break;
208 #endif
210 #ifdef IPV6_MULTICAST_HOPS
211 case AF_INET6:
212 proto = SOL_IPV6;
213 cmd = IPV6_MULTICAST_HOPS;
214 break;
215 #endif
217 default:
218 errno = EAFNOSUPPORT;
219 msg_Warn( p_this, "%m" );
220 return VLC_EGENERIC;
223 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
225 /* BSD compatibility */
226 unsigned char buf;
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 );
233 return VLC_EGENERIC;
237 return VLC_SUCCESS;
241 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
242 const char *iface)
244 int scope = if_nametoindex (iface);
245 if (scope == 0)
247 msg_Err (p_this, "invalid multicast interface: %s", iface);
248 return -1;
251 switch (family)
253 #ifdef IPV6_MULTICAST_IF
254 case AF_INET6:
255 if (setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
256 &scope, sizeof (scope)) == 0)
257 return 0;
258 #endif
260 #ifdef __linux__
261 case AF_INET:
263 struct ip_mreqn req = { .imr_ifindex = scope };
264 if (setsockopt (fd, SOL_IP, IP_MULTICAST_IF,
265 &req, sizeof (req)) == 0)
266 return 0;
268 #endif
269 default:
270 errno = EAFNOSUPPORT;
272 msg_Err (p_this, "cannot force multicast interface %s: %m", iface);
273 return -1;
277 static unsigned var_GetIfIndex (vlc_object_t *obj)
279 char *ifname = var_InheritString (obj, "miface");
280 if (ifname == NULL)
281 return 0;
283 unsigned ifindex = if_nametoindex (ifname);
284 if (ifindex == 0)
285 msg_Err (obj, "invalid multicast interface: %s", ifname);
286 free (ifname);
287 return ifindex;
292 * IP-agnostic multicast join,
293 * with fallback to old APIs, and fallback from SSM to ASM.
295 static int
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 */
302 int level;
303 struct group_source_req gsr;
305 memset (&gsr, 0, sizeof (gsr));
306 gsr.gsr_interface = var_GetIfIndex (obj);
308 switch (grp->sa_family)
310 #ifdef AF_INET6
311 case AF_INET6:
313 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
315 level = SOL_IPV6;
316 assert (grplen >= sizeof (struct sockaddr_in6));
317 if (g6->sin6_scope_id != 0)
318 gsr.gsr_interface = g6->sin6_scope_id;
319 break;
321 #endif
322 case AF_INET:
323 level = SOL_IP;
324 break;
325 default:
326 errno = EAFNOSUPPORT;
327 return -1;
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)
336 return 0;
338 #else
339 if (src->sa_family != grp->sa_family)
341 errno = EAFNOSUPPORT;
342 return -1;
345 switch (grp->sa_family)
347 # ifdef IP_ADD_SOURCE_MEMBERSHIP
348 /* IPv4-specific API */
349 case AF_INET:
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)
360 return 0;
361 break;
363 # endif
364 default:
365 errno = EAFNOSUPPORT;
368 #endif
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 */
382 int level;
383 struct group_req gr;
385 memset (&gr, 0, sizeof (gr));
386 gr.gr_interface = var_GetIfIndex (obj);
388 switch (grp->sa_family)
390 #ifdef AF_INET6
391 case AF_INET6:
393 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
395 level = SOL_IPV6;
396 assert (grplen >= sizeof (struct sockaddr_in6));
397 if (g6->sin6_scope_id != 0)
398 gr.gr_interface = g6->sin6_scope_id;
399 break;
401 #endif
402 case AF_INET:
403 level = SOL_IP;
404 break;
405 default:
406 errno = EAFNOSUPPORT;
407 return -1;
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)
413 return 0;
415 #else
416 switch (grp->sa_family)
418 # ifdef IPV6_JOIN_GROUP
419 case AF_INET6:
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)))
430 return 0;
431 break;
433 # endif
434 # ifdef IP_ADD_MEMBERSHIP
435 case AF_INET:
437 struct ip_mreq imr;
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)
444 return 0;
445 break;
447 # endif
448 default:
449 errno = EAFNOSUPPORT;
452 #endif
453 msg_Err (obj, "cannot join multicast group: %m");
454 return -1;
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) }) )
462 return -1;
464 int level, cmd;
466 switch( addr.ss_family )
468 #ifdef IPV6_TCLASS
469 case AF_INET6:
470 level = SOL_IPV6;
471 cmd = IPV6_TCLASS;
472 break;
473 #endif
475 case AF_INET:
476 level = SOL_IP;
477 cmd = IP_TOS;
478 break;
480 default:
481 #ifdef ENOPROTOOPT
482 errno = ENOPROTOOPT;
483 #endif
484 return -1;
487 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
490 #undef net_ConnectDgram
491 /*****************************************************************************
492 * net_ConnectDgram:
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,
504 }, *res;
505 int i_handle = -1;
506 bool b_unreach = false;
508 if( i_hlim < 0 )
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);
514 if (val)
516 msg_Err (p_this, "cannot resolve [%s]:%d : %s", psz_host, i_port,
517 gai_strerror (val));
518 return -1;
521 for (struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
523 char *str;
524 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
525 ptr->ai_protocol);
526 if (fd == -1)
527 continue;
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));
537 if( i_hlim >= 0 )
538 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
540 str = var_InheritString (p_this, "miface");
541 if (str != NULL)
543 net_SetMcastOut (p_this, fd, ptr->ai_family, str);
544 free (str);
547 net_SetDSCP (fd, var_InheritInteger (p_this, "dscp"));
549 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
551 /* success */
552 i_handle = fd;
553 break;
556 #if defined( WIN32 )
557 if( WSAGetLastError () == WSAENETUNREACH )
558 #else
559 if( errno == ENETUNREACH )
560 #endif
561 b_unreach = true;
562 else
564 msg_Warn( p_this, "%s port %d : %m", psz_host, i_port);
565 net_Close( fd );
566 continue;
570 freeaddrinfo( res );
572 if( i_handle == -1 )
574 if( b_unreach )
575 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
576 i_port );
577 return -1;
580 return i_handle;
583 #undef net_OpenDgram
584 /*****************************************************************************
585 * net_OpenDgram:
586 *****************************************************************************
587 * OpenDgram a datagram socket and return a handle
588 *****************************************************************************/
589 int net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
590 const char *psz_server, int i_server, int protocol )
592 if ((psz_server == NULL) || (psz_server[0] == '\0'))
593 return net_ListenSingle (obj, psz_bind, i_bind, protocol);
595 msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
596 psz_server, i_server, psz_bind, i_bind);
598 struct addrinfo hints = {
599 .ai_socktype = SOCK_DGRAM,
600 .ai_protocol = protocol,
601 .ai_flags = AI_NUMERICSERV | AI_IDN,
602 }, *loc, *rem;
604 int val = vlc_getaddrinfo (psz_server, i_server, &hints, &rem);
605 if (val)
607 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
608 gai_strerror (val));
609 return -1;
612 hints.ai_flags |= AI_PASSIVE;
613 val = vlc_getaddrinfo (psz_bind, i_bind, &hints, &loc);
614 if (val)
616 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
617 gai_strerror (val));
618 freeaddrinfo (rem);
619 return -1;
622 val = -1;
623 for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
625 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
626 ptr->ai_protocol);
627 if (fd == -1)
628 continue; // usually, address family not supported
630 fd = net_SetupDgramSocket( obj, fd, ptr );
631 if( fd == -1 )
632 continue;
634 for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
636 if ((ptr2->ai_family != ptr->ai_family)
637 || (ptr2->ai_socktype != ptr->ai_socktype)
638 || (ptr2->ai_protocol != ptr->ai_protocol))
639 continue;
641 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
642 ? net_SourceSubscribe (obj, fd,
643 ptr2->ai_addr, ptr2->ai_addrlen,
644 ptr->ai_addr, ptr->ai_addrlen)
645 : connect (fd, ptr2->ai_addr, ptr2->ai_addrlen))
647 msg_Err (obj, "cannot connect to %s port %d: %m",
648 psz_server, i_server);
649 continue;
651 val = fd;
652 break;
655 if (val != -1)
656 break;
658 net_Close (fd);
661 freeaddrinfo (rem);
662 freeaddrinfo (loc);
663 return val;
668 * net_SetCSCov:
669 * Sets the send and receive checksum coverage of a socket:
670 * @param fd socket
671 * @param sendcov payload coverage of sent packets (bytes), -1 for full
672 * @param recvcov minimum payload coverage of received packets, -1 for full
674 int net_SetCSCov (int fd, int sendcov, int recvcov)
676 int type;
678 if (getsockopt (fd, SOL_SOCKET, SO_TYPE,
679 &type, &(socklen_t){ sizeof (type) }))
680 return VLC_EGENERIC;
682 switch (type)
684 #ifdef UDPLITE_RECV_CSCOV
685 case SOCK_DGRAM: /* UDP-Lite */
686 if (sendcov == -1)
687 sendcov = 0;
688 else
689 sendcov += 8; /* partial */
690 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_SEND_CSCOV, &sendcov,
691 sizeof (sendcov)))
692 return VLC_EGENERIC;
694 if (recvcov == -1)
695 recvcov = 0;
696 else
697 recvcov += 8;
698 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
699 &recvcov, sizeof (recvcov)))
700 return VLC_EGENERIC;
702 return VLC_SUCCESS;
703 #endif
704 #ifdef DCCP_SOCKOPT_SEND_CSCOV
705 case SOCK_DCCP: /* DCCP and its ill-named socket type */
706 if ((sendcov == -1) || (sendcov > 56))
707 sendcov = 0;
708 else
709 sendcov = (sendcov + 3) / 4;
710 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SEND_CSCOV,
711 &sendcov, sizeof (sendcov)))
712 return VLC_EGENERIC;
714 if ((recvcov == -1) || (recvcov > 56))
715 recvcov = 0;
716 else
717 recvcov = (recvcov + 3) / 4;
718 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_RECV_CSCOV,
719 &recvcov, sizeof (recvcov)))
720 return VLC_EGENERIC;
722 return VLC_SUCCESS;
723 #endif
725 #if !defined( UDPLITE_RECV_CSCOV ) && !defined( DCCP_SOCKOPT_SEND_CSCOV )
726 VLC_UNUSED(sendcov);
727 VLC_UNUSED(recvcov);
728 #endif
730 return VLC_EGENERIC;