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