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