Add encoder support for Dirac using the Schroedinger library.
[vlc/asuraparaju-public.git] / src / network / udp.c
blobe72c42610a045733e8fd861ab8f17ff4246f00b0
1 /*****************************************************************************
2 * udp.c:
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
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
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 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
36 #include <errno.h>
38 #include <vlc_network.h>
40 #ifdef WIN32
41 # if defined(UNDER_CE)
42 # undef IP_MULTICAST_TTL
43 # define IP_MULTICAST_TTL 3
44 # undef IP_ADD_MEMBERSHIP
45 # define IP_ADD_MEMBERSHIP 5
46 # endif
47 # define EAFNOSUPPORT WSAEAFNOSUPPORT
48 # define if_nametoindex( str ) atoi( str )
49 #else
50 # include <unistd.h>
51 # ifdef HAVE_NET_IF_H
52 # include <net/if.h>
53 # endif
54 #endif
56 #ifdef HAVE_LINUX_DCCP_H
57 # include <linux/dccp.h>
58 # ifndef SOCK_DCCP /* provisional API */
59 # define SOCK_DCCP 6
60 # endif
61 #endif
63 #ifndef SOL_IP
64 # define SOL_IP IPPROTO_IP
65 #endif
66 #ifndef SOL_IPV6
67 # define SOL_IPV6 IPPROTO_IPV6
68 #endif
69 #ifndef IPPROTO_IPV6
70 # define IPPROTO_IPV6 41 /* IANA */
71 #endif
72 #ifndef SOL_DCCP
73 # define SOL_DCCP IPPROTO_DCCP
74 #endif
75 #ifndef IPPROTO_DCCP
76 # define IPPROTO_DCCP 33 /* IANA */
77 #endif
78 #ifndef SOL_UDPLITE
79 # define SOL_UDPLITE IPPROTO_UDPLITE
80 #endif
81 #ifndef IPPROTO_UDPLITE
82 # define IPPROTO_UDPLITE 136 /* IANA */
83 #endif
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
91 #endif
93 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
94 int i_protocol );
96 /* */
97 static int net_SetupDgramSocket( vlc_object_t *p_obj, int fd, const struct addrinfo *ptr )
99 #ifdef SO_REUSEPORT
100 setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
101 #endif
103 #ifdef SO_RCVBUF
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));
110 #endif
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);
125 else
126 #endif
127 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
129 msg_Err( p_obj, "socket bind error (%m)" );
130 net_Close (fd);
131 return -1;
133 return fd;
136 /* */
137 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
138 int family, int protocol)
140 struct addrinfo hints, *res;
142 memset (&hints, 0, sizeof( hints ));
143 hints.ai_family = family;
144 hints.ai_socktype = SOCK_DGRAM;
145 hints.ai_protocol = protocol;
146 hints.ai_flags = AI_PASSIVE;
148 if (host && !*host)
149 host = NULL;
151 msg_Dbg (obj, "net: opening %s datagram port %d",
152 host ? host : "any", port);
154 int val = vlc_getaddrinfo (obj, host, port, &hints, &res);
155 if (val)
157 msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
158 gai_strerror (val));
159 return -1;
162 val = -1;
164 for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
166 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
167 ptr->ai_protocol);
168 if (fd == -1)
170 msg_Dbg (obj, "socket error: %m");
171 continue;
174 #ifdef IPV6_V6ONLY
175 /* If IPv6 was forced, set IPv6-only mode.
176 * If IPv4 was forced, do nothing extraordinary.
177 * If nothing was forced, try dual-mode IPv6. */
178 if (ptr->ai_family == AF_INET6)
180 int on = (family == AF_INET6);
181 setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &on, sizeof (on));
183 if (ptr->ai_family == AF_INET)
184 #endif
185 if (family == AF_UNSPEC && ptr->ai_next != NULL)
187 msg_Warn (obj, "ambiguous network protocol specification");
188 msg_Warn (obj, "please select IP version explicitly");
191 fd = net_SetupDgramSocket( obj, fd, ptr );
192 if( fd == -1 )
193 continue;
195 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
196 && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
198 net_Close (fd);
199 continue;
202 val = fd;
203 break;
206 freeaddrinfo (res);
207 return val;
211 static int net_SetMcastHopLimit( vlc_object_t *p_this,
212 int fd, int family, int hlim )
214 int proto, cmd;
216 /* There is some confusion in the world whether IP_MULTICAST_TTL
217 * takes a byte or an int as an argument.
218 * BSD seems to indicate byte so we are going with that and use
219 * int as a fallback to be safe */
220 switch( family )
222 #ifdef IP_MULTICAST_TTL
223 case AF_INET:
224 proto = SOL_IP;
225 cmd = IP_MULTICAST_TTL;
226 break;
227 #endif
229 #ifdef IPV6_MULTICAST_HOPS
230 case AF_INET6:
231 proto = SOL_IPV6;
232 cmd = IPV6_MULTICAST_HOPS;
233 break;
234 #endif
236 default:
237 errno = EAFNOSUPPORT;
238 msg_Warn( p_this, "%m" );
239 return VLC_EGENERIC;
242 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
244 /* BSD compatibility */
245 unsigned char buf;
247 msg_Dbg( p_this, "cannot set hop limit (%d): %m", hlim );
248 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
249 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
251 msg_Err( p_this, "cannot set hop limit (%d): %m", hlim );
252 return VLC_EGENERIC;
256 return VLC_SUCCESS;
260 static int net_SetMcastOutIface (int fd, int family, int scope)
262 switch (family)
264 #ifdef IPV6_MULTICAST_IF
265 case AF_INET6:
266 return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
267 &scope, sizeof (scope));
268 #endif
270 #ifdef __linux__
271 case AF_INET:
273 struct ip_mreqn req = { .imr_ifindex = scope };
275 return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
276 sizeof (req));
278 #endif
281 errno = EAFNOSUPPORT;
282 return -1;
286 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
288 #ifdef IP_MULTICAST_IF
289 return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
290 #else
291 errno = EAFNOSUPPORT;
292 return -1;
293 #endif
297 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
298 const char *iface, const char *addr)
300 if (iface != NULL)
302 int scope = if_nametoindex (iface);
303 if (scope == 0)
305 msg_Err (p_this, "invalid multicast interface: %s", iface);
306 return -1;
309 if (net_SetMcastOutIface (fd, family, scope) == 0)
310 return 0;
312 msg_Err (p_this, "%s: %m", iface);
315 if (addr != NULL)
317 if (family == AF_INET)
319 struct in_addr ipv4;
320 if (inet_pton (AF_INET, addr, &ipv4) <= 0)
322 msg_Err (p_this, "invalid IPv4 address for multicast: %s",
323 addr);
324 return -1;
327 if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
328 return 0;
330 msg_Err (p_this, "%s: %m", addr);
334 return -1;
339 * Old-style any-source multicast join.
340 * In use on Windows XP/2003 and older.
342 static int
343 net_IPv4Join (vlc_object_t *obj, int fd,
344 const struct sockaddr_in *src, const struct sockaddr_in *grp)
346 #ifdef IP_ADD_MEMBERSHIP
347 union
349 struct ip_mreq gr4;
350 # ifdef IP_ADD_SOURCE_MEMBERSHIP
351 struct ip_mreq_source gsr4;
352 # endif
353 } opt;
354 int cmd;
355 struct in_addr id = { .s_addr = INADDR_ANY };
356 socklen_t optlen;
358 /* Multicast interface IPv4 address */
359 char *iface = var_CreateGetNonEmptyString (obj, "miface-addr");
360 if ((iface != NULL)
361 && (inet_pton (AF_INET, iface, &id) <= 0))
363 msg_Err (obj, "invalid multicast interface address %s", iface);
364 free (iface);
365 goto error;
367 free (iface);
369 memset (&opt, 0, sizeof (opt));
370 if (src != NULL)
372 # ifdef IP_ADD_SOURCE_MEMBERSHIP
373 cmd = IP_ADD_SOURCE_MEMBERSHIP;
374 opt.gsr4.imr_multiaddr = grp->sin_addr;
375 opt.gsr4.imr_sourceaddr = src->sin_addr;
376 opt.gsr4.imr_interface = id;
377 optlen = sizeof (opt.gsr4);
378 # else
379 errno = ENOSYS;
380 goto error;
381 # endif
383 else
385 cmd = IP_ADD_MEMBERSHIP;
386 opt.gr4.imr_multiaddr = grp->sin_addr;
387 opt.gr4.imr_interface = id;
388 optlen = sizeof (opt.gr4);
391 msg_Dbg (obj, "IP_ADD_%sMEMBERSHIP multicast request",
392 (src != NULL) ? "SOURCE_" : "");
394 if (setsockopt (fd, SOL_IP, cmd, &opt, optlen) == 0)
395 return 0;
397 error:
398 #endif
400 msg_Err (obj, "cannot join IPv4 multicast group (%m)");
401 return -1;
405 static int
406 net_IPv6Join (vlc_object_t *obj, int fd, const struct sockaddr_in6 *src)
408 #ifdef IPV6_JOIN_GROUP
409 struct ipv6_mreq gr6;
410 memset (&gr6, 0, sizeof (gr6));
411 gr6.ipv6mr_interface = src->sin6_scope_id;
412 memcpy (&gr6.ipv6mr_multiaddr, &src->sin6_addr, 16);
414 msg_Dbg (obj, "IPV6_JOIN_GROUP multicast request");
416 if (!setsockopt (fd, SOL_IPV6, IPV6_JOIN_GROUP, &gr6, sizeof (gr6)))
417 return 0;
418 #else
419 errno = ENOSYS;
420 #endif
422 msg_Err (obj, "cannot join IPv6 any-source multicast group (%m)");
423 return -1;
427 #if defined (WIN32) && !defined (MCAST_JOIN_SOURCE_GROUP)
429 * I hate manual definitions: Error-prone. Portability hell.
430 * Developers shall use UP-TO-DATE compilers. Full point.
431 * If you remove the warning, you remove the whole ifndef.
433 # warning Your C headers are out-of-date. Please update.
435 # define MCAST_JOIN_GROUP 41
436 struct group_req
438 ULONG gr_interface;
439 struct sockaddr_storage gr_group;
442 # define MCAST_JOIN_SOURCE_GROUP 45 /* from <ws2ipdef.h> */
443 struct group_source_req
445 uint32_t gsr_interface;
446 struct sockaddr_storage gsr_group;
447 struct sockaddr_storage gsr_source;
449 #endif
452 * IP-agnostic multicast join,
453 * with fallback to old APIs, and fallback from SSM to ASM.
455 static int
456 net_SourceSubscribe (vlc_object_t *obj, int fd,
457 const struct sockaddr *src, socklen_t srclen,
458 const struct sockaddr *grp, socklen_t grplen)
460 int level, iid = 0;
462 char *iface = var_CreateGetNonEmptyString (obj, "miface");
463 if (iface != NULL)
465 iid = if_nametoindex (iface);
466 if (iid == 0)
468 msg_Err (obj, "invalid multicast interface: %s", iface);
469 free (iface);
470 return -1;
472 free (iface);
475 switch (grp->sa_family)
477 #ifdef AF_INET6
478 case AF_INET6:
479 level = SOL_IPV6;
480 if (((const struct sockaddr_in6 *)grp)->sin6_scope_id)
481 iid = ((const struct sockaddr_in6 *)grp)->sin6_scope_id;
482 break;
483 #endif
485 case AF_INET:
486 level = SOL_IP;
487 break;
489 default:
490 errno = EAFNOSUPPORT;
491 return -1;
494 if (src != NULL)
495 switch (src->sa_family)
497 #ifdef AF_INET6
498 case AF_INET6:
499 if (memcmp (&((const struct sockaddr_in6 *)src)->sin6_addr,
500 &in6addr_any, sizeof (in6addr_any)) == 0)
501 src = NULL;
502 break;
503 #endif
505 case AF_INET:
506 if (((const struct sockaddr_in *)src)->sin_addr.s_addr
507 == INADDR_ANY)
508 src = NULL;
509 break;
513 /* Agnostic ASM/SSM multicast join */
514 #ifdef MCAST_JOIN_SOURCE_GROUP
515 union
517 struct group_req gr;
518 struct group_source_req gsr;
519 } opt;
520 socklen_t optlen;
522 memset (&opt, 0, sizeof (opt));
524 if (src != NULL)
526 if ((grplen > sizeof (opt.gsr.gsr_group))
527 || (srclen > sizeof (opt.gsr.gsr_source)))
528 return -1;
530 opt.gsr.gsr_interface = iid;
531 memcpy (&opt.gsr.gsr_source, src, srclen);
532 memcpy (&opt.gsr.gsr_group, grp, grplen);
533 optlen = sizeof (opt.gsr);
535 else
537 if (grplen > sizeof (opt.gr.gr_group))
538 return -1;
540 opt.gr.gr_interface = iid;
541 memcpy (&opt.gr.gr_group, grp, grplen);
542 optlen = sizeof (opt.gr);
545 msg_Dbg (obj, "Multicast %sgroup join request", src ? "source " : "");
547 if (setsockopt (fd, level,
548 src ? MCAST_JOIN_SOURCE_GROUP : MCAST_JOIN_GROUP,
549 (void *)&opt, optlen) == 0)
550 return 0;
551 #endif
553 /* Fallback to IPv-specific APIs */
554 if ((src != NULL) && (src->sa_family != grp->sa_family))
555 return -1;
557 switch (grp->sa_family)
559 case AF_INET:
560 if ((grplen < sizeof (struct sockaddr_in))
561 || ((src != NULL) && (srclen < sizeof (struct sockaddr_in))))
562 return -1;
564 if (net_IPv4Join (obj, fd, (const struct sockaddr_in *)src,
565 (const struct sockaddr_in *)grp) == 0)
566 return 0;
567 break;
569 #ifdef AF_INET6
570 case AF_INET6:
571 if ((grplen < sizeof (struct sockaddr_in6))
572 || ((src != NULL) && (srclen < sizeof (struct sockaddr_in6))))
573 return -1;
575 /* IPv6-specific SSM API does not exist. So if we're here
576 * it means IPv6 SSM is not supported on this OS and we
577 * directly fallback to ASM */
579 if (net_IPv6Join (obj, fd, (const struct sockaddr_in6 *)grp) == 0)
580 return 0;
581 break;
582 #endif
585 msg_Err (obj, "Multicast group join error (%m)");
587 if (src != NULL)
589 msg_Warn (obj, "Trying ASM instead of SSM...");
590 return net_Subscribe (obj, fd, grp, grplen);
593 msg_Err (obj, "Multicast not supported");
594 return -1;
598 int net_Subscribe (vlc_object_t *obj, int fd,
599 const struct sockaddr *addr, socklen_t addrlen)
601 return net_SourceSubscribe (obj, fd, NULL, 0, addr, addrlen);
605 static int net_SetDSCP( int fd, uint8_t dscp )
607 struct sockaddr_storage addr;
608 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
609 return -1;
611 int level, cmd;
613 switch( addr.ss_family )
615 #ifdef IPV6_TCLASS
616 case AF_INET6:
617 level = SOL_IPV6;
618 cmd = IPV6_TCLASS;
619 break;
620 #endif
622 case AF_INET:
623 level = SOL_IP;
624 cmd = IP_TOS;
625 break;
627 default:
628 #ifdef ENOPROTOOPT
629 errno = ENOPROTOOPT;
630 #endif
631 return -1;
634 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
637 #undef net_ConnectDgram
638 /*****************************************************************************
639 * net_ConnectDgram:
640 *****************************************************************************
641 * Open a datagram socket to send data to a defined destination, with an
642 * optional hop limit.
643 *****************************************************************************/
644 int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
645 int i_hlim, int proto )
647 struct addrinfo hints, *res, *ptr;
648 int i_val, i_handle = -1;
649 bool b_unreach = false;
651 if( i_hlim < 0 )
652 i_hlim = var_CreateGetInteger( p_this, "ttl" );
654 memset( &hints, 0, sizeof( hints ) );
655 hints.ai_socktype = SOCK_DGRAM;
656 hints.ai_protocol = proto;
658 msg_Dbg( p_this, "net: connecting to [%s]:%d", psz_host, i_port );
660 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
661 if( i_val )
663 msg_Err( p_this, "cannot resolve [%s]:%d : %s", psz_host, i_port,
664 gai_strerror( i_val ) );
665 return -1;
668 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
670 char *str;
671 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
672 ptr->ai_protocol);
673 if (fd == -1)
674 continue;
676 #if !defined( SYS_BEOS )
677 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
678 * to avoid packet loss caused by scheduling problems */
679 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
680 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
682 /* Allow broadcast sending */
683 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
684 #endif
686 if( i_hlim >= 0 )
687 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
689 str = var_CreateGetNonEmptyString (p_this, "miface");
690 if (str != NULL)
692 net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
693 free (str);
696 str = var_CreateGetNonEmptyString (p_this, "miface-addr");
697 if (str != NULL)
699 net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
700 free (str);
703 net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
705 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
707 /* success */
708 i_handle = fd;
709 break;
712 #if defined( WIN32 ) || defined( UNDER_CE )
713 if( WSAGetLastError () == WSAENETUNREACH )
714 #else
715 if( errno == ENETUNREACH )
716 #endif
717 b_unreach = true;
718 else
720 msg_Warn( p_this, "%s port %d : %m", psz_host, i_port);
721 net_Close( fd );
722 continue;
726 freeaddrinfo( res );
728 if( i_handle == -1 )
730 if( b_unreach )
731 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
732 i_port );
733 return -1;
736 return i_handle;
739 #undef net_OpenDgram
740 /*****************************************************************************
741 * net_OpenDgram:
742 *****************************************************************************
743 * OpenDgram a datagram socket and return a handle
744 *****************************************************************************/
745 int net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
746 const char *psz_server, int i_server,
747 int family, int protocol )
749 if ((psz_server == NULL) || (psz_server[0] == '\0'))
750 return net_ListenSingle (obj, psz_bind, i_bind, family, protocol);
752 msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
753 psz_server, i_server, psz_bind, i_bind);
755 struct addrinfo hints, *loc, *rem;
756 int val;
758 memset (&hints, 0, sizeof (hints));
759 hints.ai_family = family;
760 hints.ai_socktype = SOCK_DGRAM;
761 hints.ai_protocol = protocol;
763 val = vlc_getaddrinfo (obj, psz_server, i_server, &hints, &rem);
764 if (val)
766 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
767 gai_strerror (val));
768 return -1;
771 hints.ai_flags = AI_PASSIVE;
772 val = vlc_getaddrinfo (obj, psz_bind, i_bind, &hints, &loc);
773 if (val)
775 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
776 gai_strerror (val));
777 freeaddrinfo (rem);
778 return -1;
781 val = -1;
782 for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
784 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
785 ptr->ai_protocol);
786 if (fd == -1)
787 continue; // usually, address family not supported
789 fd = net_SetupDgramSocket( obj, fd, ptr );
790 if( fd == -1 )
791 continue;
793 for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
795 if ((ptr2->ai_family != ptr->ai_family)
796 || (ptr2->ai_socktype != ptr->ai_socktype)
797 || (ptr2->ai_protocol != ptr->ai_protocol))
798 continue;
800 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
801 ? net_SourceSubscribe (obj, fd,
802 ptr2->ai_addr, ptr2->ai_addrlen,
803 ptr->ai_addr, ptr->ai_addrlen)
804 : connect (fd, ptr2->ai_addr, ptr2->ai_addrlen))
806 msg_Err (obj, "cannot connect to %s port %d: %m",
807 psz_server, i_server);
808 continue;
810 val = fd;
811 break;
814 if (val != -1)
815 break;
817 net_Close (fd);
820 freeaddrinfo (rem);
821 freeaddrinfo (loc);
822 return val;
827 * net_SetCSCov:
828 * Sets the send and receive checksum coverage of a socket:
829 * @param fd socket
830 * @param sendcov payload coverage of sent packets (bytes), -1 for full
831 * @param recvcov minimum payload coverage of received packets, -1 for full
833 int net_SetCSCov (int fd, int sendcov, int recvcov)
835 int type;
837 if (getsockopt (fd, SOL_SOCKET, SO_TYPE,
838 &type, &(socklen_t){ sizeof (type) }))
839 return VLC_EGENERIC;
841 switch (type)
843 #ifdef UDPLITE_RECV_CSCOV
844 case SOCK_DGRAM: /* UDP-Lite */
845 if (sendcov == -1)
846 sendcov = 0;
847 else
848 sendcov += 8; /* partial */
849 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_SEND_CSCOV, &sendcov,
850 sizeof (sendcov)))
851 return VLC_EGENERIC;
853 if (recvcov == -1)
854 recvcov = 0;
855 else
856 recvcov += 8;
857 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
858 &recvcov, sizeof (recvcov)))
859 return VLC_EGENERIC;
861 return VLC_SUCCESS;
862 #endif
863 #ifdef DCCP_SOCKOPT_SEND_CSCOV
864 case SOCK_DCCP: /* DCCP and its ill-named socket type */
865 if ((sendcov == -1) || (sendcov > 56))
866 sendcov = 0;
867 else
868 sendcov = (sendcov + 3) / 4;
869 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SEND_CSCOV,
870 &sendcov, sizeof (sendcov)))
871 return VLC_EGENERIC;
873 if ((recvcov == -1) || (recvcov > 56))
874 recvcov = 0;
875 else
876 recvcov = (recvcov + 3) / 4;
877 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_RECV_CSCOV,
878 &recvcov, sizeof (recvcov)))
879 return VLC_EGENERIC;
881 return VLC_SUCCESS;
882 #endif
884 #if !defined( UDPLITE_RECV_CSCOV ) && !defined( DCCP_SOCKOPT_SEND_CSCOV )
885 VLC_UNUSED(sendcov);
886 VLC_UNUSED(recvcov);
887 #endif
889 return VLC_EGENERIC;