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