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