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