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