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