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