]> git.sesse.net Git - vlc/blob - src/network/udp.c
8b9f609474c9fe8912c98baaf43616268a91fd15
[vlc] / src / network / udp.c
1 /*****************************************************************************
2  * udp.c:
3  *****************************************************************************
4  * Copyright (C) 2004-2006 the VideoLAN team
5  * Copyright © 2006-2007 Rémi Denis-Courmont
6  *
7  * $Id$
8  *
9  * Authors: Laurent Aimar <fenrir@videolan.org>
10  *          Rémi Denis-Courmont <rem # videolan.org>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <vlc/vlc.h>
31
32 #include <errno.h>
33
34 #ifdef HAVE_SYS_TIME_H
35 #    include <sys/time.h>
36 #endif
37
38 #include <vlc_network.h>
39
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
55
56 #ifndef SOL_IP
57 # define SOL_IP IPPROTO_IP
58 #endif
59 #ifndef SOL_IPV6
60 # define SOL_IPV6 IPPROTO_IPV6
61 #endif
62 #ifndef IPPROTO_IPV6
63 # define IPPROTO_IPV6 41 /* IANA */
64 #endif
65 #ifndef SOL_DCCP
66 # define SOL_DCCP IPPROTO_DCCP
67 #endif
68 #ifndef IPPROTO_DCCP
69 # define IPPROTO_DCCP 33 /* IANA */
70 #endif
71 #ifndef SOL_UDPLITE
72 # define SOL_UDPLITE IPPROTO_UDPLITE
73 #endif
74 #ifndef IPPROTO_UDPLITE
75 # define IPPROTO_UDPLITE 136 /* IANA */
76 #endif
77
78 #ifdef HAVE_LINUX_DCCP_H
79 # include <linux/dccp.h>
80 # ifndef SOCK_DCCP /* provisional API */
81 #  define SOCK_DCCP 6
82 # endif
83 #endif
84
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
92
93 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
94                        int i_protocol );
95
96 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
97                              int family, int protocol)
98 {
99     struct addrinfo hints, *res;
100
101     memset (&hints, 0, sizeof( hints ));
102     hints.ai_family = family;
103     hints.ai_socktype = SOCK_DGRAM;
104     hints.ai_flags = AI_PASSIVE;
105
106     if (host && !*host)
107         host = NULL;
108
109     msg_Dbg (obj, "net: opening %s datagram port %d", host ?: "any", port);
110
111     int val = vlc_getaddrinfo (obj, host, port, &hints, &res);
112     if (val)
113     {
114         msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
115                  vlc_gai_strerror (val));
116         return -1;
117     }
118
119     val = -1;
120
121     for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
122     {
123         int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
124                              protocol ?: ptr->ai_protocol);
125         if (fd == -1)
126         {
127             msg_Dbg (obj, "socket error: %s", net_strerror (net_errno));
128             continue;
129         }
130
131         if (ptr->ai_next != NULL)
132         {
133 #ifdef IPV6_V6ONLY
134             if ((ptr->ai_family != AF_INET6)
135              || setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &(int){ 0 },
136                             sizeof (int)))
137 #endif
138             {
139                 msg_Err (obj, "Multiple network protocols present");
140                 msg_Err (obj, "Please select network protocol manually");
141             }
142         }
143
144         /* Bind the socket */
145 #if defined (WIN32) || defined (UNDER_CE)
146         if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
147          && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
148         {
149             struct sockaddr_in6 dumb =
150             {
151                 .sin6_family = ptr->ai_addr->sa_family,
152                 .sin6_port =  ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
153             };
154             bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
155         }
156         else
157 #endif
158         if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
159         {
160             msg_Err (obj, "socket bind error (%s)", net_strerror (net_errno));
161             net_Close (fd);
162             continue;
163         }
164
165         if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
166          && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
167         {
168             net_Close (fd);
169             continue;
170         }
171
172         val = fd;
173         break;
174     }
175
176     vlc_freeaddrinfo (res);
177     return val;
178 }
179
180
181 static int net_SetMcastHopLimit( vlc_object_t *p_this,
182                                  int fd, int family, int hlim )
183 {
184     int proto, cmd;
185
186     /* There is some confusion in the world whether IP_MULTICAST_TTL
187      * takes a byte or an int as an argument.
188      * BSD seems to indicate byte so we are going with that and use
189      * int as a fallback to be safe */
190     switch( family )
191     {
192 #ifdef IP_MULTICAST_TTL
193         case AF_INET:
194             proto = SOL_IP;
195             cmd = IP_MULTICAST_TTL;
196             break;
197 #endif
198
199 #ifdef IPV6_MULTICAST_HOPS
200         case AF_INET6:
201             proto = SOL_IPV6;
202             cmd = IPV6_MULTICAST_HOPS;
203             break;
204 #endif
205
206         default:
207             msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
208             return VLC_EGENERIC;
209     }
210
211     if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
212     {
213         /* BSD compatibility */
214         unsigned char buf;
215
216         buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
217         if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
218             return VLC_EGENERIC;
219     }
220
221     return VLC_SUCCESS;
222 }
223
224
225 static int net_SetMcastOutIface (int fd, int family, int scope)
226 {
227     switch (family)
228     {
229 #ifdef IPV6_MULTICAST_IF
230         case AF_INET6:
231             return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
232                                &scope, sizeof (scope));
233 #endif
234
235 #ifdef __linux__
236         case AF_INET:
237         {
238             struct ip_mreqn req = { .imr_ifindex = scope };
239
240             return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
241                                sizeof (req));
242         }
243 #endif
244     }
245
246     errno = EAFNOSUPPORT;
247     return -1;
248 }
249
250
251 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
252 {
253 #ifdef IP_MULTICAST_IF
254     return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
255 #else
256     errno = EAFNOSUPPORT;
257     return -1;
258 #endif
259 }
260
261
262 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
263                             const char *iface, const char *addr)
264 {
265     if (iface != NULL)
266     {
267         int scope = if_nametoindex (iface);
268         if (scope == 0)
269         {
270             msg_Err (p_this, "invalid multicast interface: %s", iface);
271             return -1;
272         }
273
274         if (net_SetMcastOutIface (fd, family, scope) == 0)
275             return 0;
276
277         msg_Err (p_this, "%s: %s", iface, net_strerror (net_errno));
278     }
279
280     if (addr != NULL)
281     {
282         if (family == AF_INET)
283         {
284             struct in_addr ipv4;
285             if (inet_pton (AF_INET, addr, &ipv4) <= 0)
286             {
287                 msg_Err (p_this, "invalid IPv4 address for multicast: %s",
288                          addr);
289                 return -1;
290             }
291
292             if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
293                 return 0;
294
295             msg_Err (p_this, "%s: %s", addr, net_strerror (net_errno));
296         }
297     }
298
299     return -1;
300 }
301
302
303 /**
304  * Old-style any-source multicast join.
305  * In use on Windows XP/2003 and older.
306  */
307 static int
308 net_IPv4Join (vlc_object_t *obj, int fd,
309               const struct sockaddr_in *src, const struct sockaddr_in *grp)
310 {
311 #ifdef IP_ADD_MEMBERSHIP
312     union
313     {
314         struct ip_mreq gr4;
315 # ifdef IP_ADD_SOURCE_MEMBERSHIP
316         struct ip_mreq_source gsr4;
317 # endif
318     } opt;
319     int cmd;
320     struct in_addr id = { .s_addr = INADDR_ANY };
321     socklen_t optlen;
322
323     /* Multicast interface IPv4 address */
324     char *iface = var_CreateGetNonEmptyString (obj, "miface-addr");
325     if ((iface != NULL)
326      && (inet_pton (AF_INET, iface, &id) <= 0))
327     {
328         msg_Err (obj, "invalid multicast interface address %s", iface);
329         free (iface);
330         goto error;
331     }
332     free (iface);
333
334     memset (&opt, 0, sizeof (opt));
335     if (src != NULL)
336     {
337 # ifdef IP_ADD_SOURCE_MEMBERSHIP
338         cmd = IP_ADD_SOURCE_MEMBERSHIP;
339         opt.gsr4.imr_multiaddr = grp->sin_addr;
340         opt.gsr4.imr_sourceaddr = src->sin_addr;
341         opt.gsr4.imr_interface = id;
342         optlen = sizeof (opt.gsr4);
343 # else
344         errno = ENOSYS;
345         goto error;
346 # endif
347     }
348     else
349     {
350         cmd = IP_ADD_MEMBERSHIP;
351         opt.gr4.imr_multiaddr = grp->sin_addr;
352         opt.gr4.imr_interface = id;
353         optlen = sizeof (opt.gr4);
354     }
355
356     msg_Dbg (obj, "IP_ADD_%sMEMBERSHIP multicast request",
357              (src != NULL) ? "SOURCE_" : "");
358
359     if (setsockopt (fd, SOL_IP, cmd, &opt, optlen) == 0)
360         return 0;
361
362 error:
363 #endif
364
365     msg_Err (obj, "cannot join IPv4 multicast group (%s)",
366              net_strerror (net_errno));
367     return -1;
368 }
369
370
371 static int
372 net_IPv6Join (vlc_object_t *obj, int fd, const struct sockaddr_in6 *src)
373 {
374 #ifdef IPV6_JOIN_GROUP
375     struct ipv6_mreq gr6;
376     memset (&gr6, 0, sizeof (gr6));
377     gr6.ipv6mr_interface = src->sin6_scope_id;
378     memcpy (&gr6.ipv6mr_multiaddr, &src->sin6_addr, 16);
379
380     msg_Dbg (obj, "IPV6_JOIN_GROUP multicast request");
381
382     if (!setsockopt (fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &gr6, sizeof (gr6)))
383         return 0;
384 #else
385     errno = ENOSYS;
386 #endif
387
388     msg_Err (obj, "cannot join IPv6 any-source multicast group (%s)",
389              net_strerror (net_errno));
390     return -1;
391 }
392
393
394 #if defined (WIN32) && !defined (MCAST_JOIN_SOURCE_GROUP)
395 /*
396  * I hate manual definitions: Error-prone. Portability hell.
397  * Developers shall use UP-TO-DATE compilers. Full point.
398  * If you remove the warning, you remove the whole ifndef.
399  */
400 #  warning Your C headers are out-of-date. Please update.
401
402 #  define MCAST_JOIN_GROUP 41
403 struct group_req
404 {
405     ULONG gr_interface;
406     struct sockaddr_storage gr_group;
407 };
408
409 #  define MCAST_JOIN_SOURCE_GROUP 45 /* from <ws2ipdef.h> */
410 struct group_source_req
411 {
412     uint32_t gsr_interface;
413     struct sockaddr_storage gsr_group;
414     struct sockaddr_storage gsr_source;
415 };
416 #endif
417
418 /**
419  * IP-agnostic multicast join,
420  * with fallback to old APIs, and fallback from SSM to ASM.
421  */
422 static int
423 net_SourceSubscribe (vlc_object_t *obj, int fd,
424                      const struct sockaddr *src, socklen_t srclen,
425                      const struct sockaddr *grp, socklen_t grplen)
426 {
427     int level, iid = 0;
428
429     char *iface = var_CreateGetNonEmptyString (obj, "miface");
430     if (iface != NULL)
431     {
432         iid = if_nametoindex (iface);
433         if (iid == 0)
434         {
435             msg_Err (obj, "invalid multicast interface: %s", iface);
436             free (iface);
437             return -1;
438         }
439         free (iface);
440     }
441
442     switch (grp->sa_family)
443     {
444 #ifdef AF_INET6
445         case AF_INET6:
446             level = SOL_IPV6;
447             if (((const struct sockaddr_in6 *)grp)->sin6_scope_id)
448                 iid = ((const struct sockaddr_in6 *)grp)->sin6_scope_id;
449             break;
450 #endif
451
452         case AF_INET:
453             level = SOL_IP;
454             break;
455
456         default:
457             errno = EAFNOSUPPORT;
458             return -1;
459     }
460
461     if (src != NULL)
462         switch (src->sa_family)
463         {
464 #ifdef AF_INET6
465             case AF_INET6:
466                 if (memcmp (&((const struct sockaddr_in6 *)src)->sin6_addr,
467                             &in6addr_any, sizeof (in6addr_any)) == 0)
468                     src = NULL;
469             break;
470 #endif
471
472             case AF_INET:
473                 if (((const struct sockaddr_in *)src)->sin_addr.s_addr
474                      == INADDR_ANY)
475                     src = NULL;
476                 break;
477         }
478
479
480     /* Agnostic ASM/SSM multicast join */
481 #ifdef MCAST_JOIN_SOURCE_GROUP
482     union
483     {
484         struct group_req gr;
485         struct group_source_req gsr;
486     } opt;
487     socklen_t optlen;
488
489     memset (&opt, 0, sizeof (opt));
490
491     if (src != NULL)
492     {
493         if ((grplen > sizeof (opt.gsr.gsr_group))
494          || (srclen > sizeof (opt.gsr.gsr_source)))
495             return -1;
496
497         opt.gsr.gsr_interface = iid;
498         memcpy (&opt.gsr.gsr_source, src, srclen);
499         memcpy (&opt.gsr.gsr_group,  grp, grplen);
500         optlen = sizeof (opt.gsr);
501     }
502     else
503     {
504         if (grplen > sizeof (opt.gr.gr_group))
505             return -1;
506
507         opt.gr.gr_interface = iid;
508         memcpy (&opt.gr.gr_group, grp, grplen);
509         optlen = sizeof (opt.gr);
510     }
511
512     msg_Dbg (obj, "Multicast %sgroup join request", src ? "source " : "");
513
514     if (setsockopt (fd, level,
515                     src ? MCAST_JOIN_SOURCE_GROUP : MCAST_JOIN_GROUP,
516                     (void *)&opt, optlen) == 0)
517         return 0;
518 #endif
519
520     /* Fallback to IPv-specific APIs */
521     if ((src != NULL) && (src->sa_family != grp->sa_family))
522         return -1;
523
524     switch (grp->sa_family)
525     {
526         case AF_INET:
527             if ((grplen < sizeof (struct sockaddr_in))
528              || ((src != NULL) && (srclen < sizeof (struct sockaddr_in))))
529                 return -1;
530
531             if (net_IPv4Join (obj, fd, (const struct sockaddr_in *)src,
532                               (const struct sockaddr_in *)grp) == 0)
533                 return 0;
534             break;
535
536 #ifdef AF_INET6
537         case AF_INET6:
538             if ((grplen < sizeof (struct sockaddr_in6))
539              || ((src != NULL) && (srclen < sizeof (struct sockaddr_in6))))
540                 return -1;
541
542             /* IPv6-specific SSM API does not exist. So if we're here
543              * it means IPv6 SSM is not supported on this OS and we
544              * directly fallback to ASM */
545
546             if (net_IPv6Join (obj, fd, (const struct sockaddr_in6 *)grp) == 0)
547                 return 0;
548             break;
549 #endif
550     }
551
552     msg_Err (obj, "Multicast group join error (%s)",
553              net_strerror (net_errno));
554
555     if (src != NULL)
556     {
557         msg_Warn (obj, "Trying ASM instead of SSM...");
558         return net_Subscribe (obj, fd, grp, grplen);
559     }
560
561     msg_Err (obj, "Multicast not supported");
562     return -1;
563 }
564
565
566 int net_Subscribe (vlc_object_t *obj, int fd,
567                    const struct sockaddr *addr, socklen_t addrlen)
568 {
569     return net_SourceSubscribe (obj, fd, NULL, 0, addr, addrlen);
570 }
571
572
573 static int net_SetDSCP( int fd, uint8_t dscp )
574 {
575     struct sockaddr_storage addr;
576     if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
577         return -1;
578
579     int level, cmd;
580
581     switch( addr.ss_family )
582     {
583 #ifdef IPV6_TCLASS
584         case AF_INET6:
585             level = SOL_IPV6;
586             cmd = IPV6_TCLASS;
587             break;
588 #endif
589
590         case AF_INET:
591             level = SOL_IP;
592             cmd = IP_TOS;
593             break;
594
595         default:
596 #ifdef ENOPROTOOPT
597             errno = ENOPROTOOPT;
598 #endif
599             return -1;
600     }
601
602     return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
603 }
604
605
606 /*****************************************************************************
607  * __net_ConnectDgram:
608  *****************************************************************************
609  * Open a datagram socket to send data to a defined destination, with an
610  * optional hop limit.
611  *****************************************************************************/
612 int __net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
613                         int i_hlim, int proto )
614 {
615     struct addrinfo hints, *res, *ptr;
616     int             i_val, i_handle = -1;
617     vlc_bool_t      b_unreach = VLC_FALSE;
618
619     if( i_port == 0 )
620         i_port = 1234; /* historical VLC thing */
621
622     if( i_hlim < 1 )
623         i_hlim = var_CreateGetInteger( p_this, "ttl" );
624
625     memset( &hints, 0, sizeof( hints ) );
626     hints.ai_socktype = SOCK_DGRAM;
627
628     msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
629
630     i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
631     if( i_val )
632     {
633         msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
634                  vlc_gai_strerror( i_val ) );
635         return -1;
636     }
637
638     for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
639     {
640         char *str;
641         int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
642                              proto ?: ptr->ai_protocol);
643         if (fd == -1)
644             continue;
645
646 #if !defined( SYS_BEOS )
647         /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
648         * to avoid packet loss caused by scheduling problems */
649         setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
650         setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
651
652         /* Allow broadcast sending */
653         setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
654 #endif
655
656         if( i_hlim > 0 )
657             net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
658
659         str = var_CreateGetNonEmptyString (p_this, "miface");
660         if (str != NULL)
661         {
662             net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
663             free (str);
664         }
665
666         str = var_CreateGetNonEmptyString (p_this, "miface-addr");
667         if (str != NULL)
668         {
669             net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
670             free (str);
671         }
672
673         net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
674
675         if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
676         {
677             /* success */
678             i_handle = fd;
679             break;
680         }
681
682 #if defined( WIN32 ) || defined( UNDER_CE )
683         if( WSAGetLastError () == WSAENETUNREACH )
684 #else
685         if( errno == ENETUNREACH )
686 #endif
687             b_unreach = VLC_TRUE;
688         else
689         {
690             msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
691                       strerror( errno ) );
692             net_Close( fd );
693             continue;
694         }
695     }
696
697     vlc_freeaddrinfo( res );
698
699     if( i_handle == -1 )
700     {
701         if( b_unreach )
702             msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
703                      i_port );
704         return -1;
705     }
706
707     return i_handle;
708 }
709
710
711 /*****************************************************************************
712  * __net_OpenDgram:
713  *****************************************************************************
714  * OpenDgram a datagram socket and return a handle
715  *****************************************************************************/
716 int __net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
717                      const char *psz_server, int i_server,
718                      int family, int protocol )
719 {
720     if ((psz_server == NULL) || (psz_server[0] == '\0'))
721         return net_ListenSingle (obj, psz_bind, i_bind, family, protocol);
722
723     msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
724              psz_server, i_server, psz_bind, i_bind);
725
726     struct addrinfo hints, *loc, *rem;
727     int val;
728
729     memset (&hints, 0, sizeof (hints));
730     hints.ai_family = family;
731     hints.ai_socktype = SOCK_DGRAM;
732
733     val = vlc_getaddrinfo (obj, psz_server, i_server, &hints, &rem);
734     if (val)
735     {
736         msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
737                  vlc_gai_strerror (val));
738         return -1;
739     }
740
741     hints.ai_flags = AI_PASSIVE;
742     val = vlc_getaddrinfo (obj, psz_bind, i_bind, &hints, &loc);
743     if (val)
744     {
745         msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
746                  vlc_gai_strerror (val));
747         vlc_freeaddrinfo (rem);
748         return -1;
749     }
750
751     for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
752     {
753         int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
754                              protocol ?: ptr->ai_protocol);
755         if (fd == -1)
756             continue; // usually, address family not supported
757
758 #ifdef SO_REUSEPORT
759         setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
760 #endif
761
762 #ifdef SO_RCVBUF
763         /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
764          * to avoid packet loss caused in case of scheduling hiccups */
765         setsockopt (fd, SOL_SOCKET, SO_RCVBUF,
766                     (void *)&(int){ 0x80000 }, sizeof (int));
767         setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
768                     (void *)&(int){ 0x80000 }, sizeof (int));
769 #endif
770
771 #if defined (WIN32) || defined (UNDER_CE)
772         if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
773          && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
774         {
775             // This works for IPv4 too - don't worry!
776             struct sockaddr_in6 dumb =
777             {
778                 .sin6_family = ptr->ai_addr->sa_family,
779                 .sin6_port =  ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
780             };
781
782             bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
783         }
784         else
785 #endif
786         if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
787         {
788             net_Close (fd);
789             continue;
790         }
791
792         val = -1;
793         for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
794         {
795             if ((ptr2->ai_family != ptr->ai_family)
796              || (ptr2->ai_socktype != ptr->ai_socktype)
797              || (ptr2->ai_protocol != ptr->ai_protocol))
798                 continue;
799
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))
805             {
806                 msg_Err (obj, "cannot connect to %s port %d: %s",
807                          psz_server, i_server, net_strerror (net_errno));
808                 continue;
809             }
810             val = fd;
811             break;
812         }
813
814         if (val != -1)
815             break;
816
817         close (fd);
818     }
819
820     vlc_freeaddrinfo (rem);
821     vlc_freeaddrinfo (loc);
822     return val;
823 }
824
825
826 /**
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
832  */
833 int net_SetCSCov (int fd, int sendcov, int recvcov)
834 {
835     int type;
836
837     if (getsockopt (fd, SOL_SOCKET, SO_TYPE,
838                     &type, &(socklen_t){ sizeof (type) }))
839         return VLC_EGENERIC;
840
841     switch (type)
842     {
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;
852
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;
860
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;
872
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;
880
881             return VLC_SUCCESS;
882 #endif
883     }
884
885     return VLC_EGENERIC;
886 }