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