1 /*****************************************************************************
2 * udp.c:
3 *****************************************************************************
4 * Copyright (C) 2004-2006 VLC authors and VideoLAN
5 * Copyright © 2006-2007 Rémi Denis-Courmont
6 *
7 * $Id: 2c1bc722de07a3f782a0bc98d6cf37ea78bff00c $
8 *
9 * Authors: Laurent Aimar <fenrir@videolan.org>
10 * Rémi Denis-Courmont
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * 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 #include <assert.h>
38
39 #include <vlc_network.h>
40
41 #ifdef _WIN32
42 # undef EAFNOSUPPORT
43 # define EAFNOSUPPORT WSAEAFNOSUPPORT
44 # include <iphlpapi.h>
45 #else
46 # include <unistd.h>
47 # ifdef HAVE_NET_IF_H
48 # include <net/if.h>
49 # endif
50 #endif
51
52 #ifdef HAVE_LINUX_DCCP_H
53 # include <linux/dccp.h>
54 # ifndef SOCK_DCCP /* provisional API */
55 # define SOCK_DCCP 6
56 # endif
57 #endif
58
59 #ifndef SOL_IP
60 # define SOL_IP IPPROTO_IP
61 #endif
62 #ifndef SOL_IPV6
63 # define SOL_IPV6 IPPROTO_IPV6
64 #endif
65 #ifndef IPPROTO_IPV6
66 # define IPPROTO_IPV6 41 /* IANA */
67 #endif
68 #ifndef SOL_DCCP
69 # define SOL_DCCP IPPROTO_DCCP
70 #endif
71 #ifndef IPPROTO_DCCP
72 # define IPPROTO_DCCP 33 /* IANA */
73 #endif
74 #ifndef SOL_UDPLITE
75 # define SOL_UDPLITE IPPROTO_UDPLITE
76 #endif
77 #ifndef IPPROTO_UDPLITE
78 # define IPPROTO_UDPLITE 136 /* IANA */
79 #endif
80
81 #if defined (HAVE_NETINET_UDPLITE_H)
82 # include <netinet/udplite.h>
83 #elif defined (__linux__)
84 /* still missing from glibc 2.6 */
85 # define UDPLITE_SEND_CSCOV 10
86 # define UDPLITE_RECV_CSCOV 11
87 #endif
88
89 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
90 int i_protocol );
91
92 /* */
net_SetupDgramSocket(vlc_object_t * p_obj,int fd,const struct addrinfo * ptr)93 static int net_SetupDgramSocket (vlc_object_t *p_obj, int fd,
94 const struct addrinfo *ptr)
95 {
96 #if defined (SO_REUSEPORT) && !defined (__linux__)
97 setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
98 #endif
99
100 #if defined (_WIN32)
101
102 /* Check windows version so we know if we need to increase receive buffers
103 * for Windows 7 and earlier
104
105 * SetSocketMediaStreamingMode is present in win 8 and later, so we set
106 * receive buffer if that isn't present
107 */
108 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
109 HINSTANCE h_Network = LoadLibrary(TEXT("Windows.Networking.dll"));
110 if( (h_Network == NULL) ||
111 (GetProcAddress( h_Network, "SetSocketMediaStreamingMode" ) == NULL ) )
112 {
113 setsockopt (fd, SOL_SOCKET, SO_RCVBUF,
114 (void *)&(int){ 0x80000 }, sizeof (int));
115 }
116 if( h_Network )
117 FreeLibrary( h_Network );
118 #endif
119
120 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
121 && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
122 {
123 // This works for IPv4 too - don't worry!
124 struct sockaddr_in6 dumb =
125 {
126 .sin6_family = ptr->ai_addr->sa_family,
127 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
128 };
129
130 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
131 }
132 else
133 #endif
134 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
135 {
136 msg_Err( p_obj, "socket bind error: %s", vlc_strerror_c(net_errno) );
137 net_Close (fd);
138 return -1;
139 }
140 return fd;
141 }
142
143 /* */
net_ListenSingle(vlc_object_t * obj,const char * host,int port,int protocol)144 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
145 int protocol)
146 {
147 struct addrinfo hints = {
148 .ai_socktype = SOCK_DGRAM,
149 .ai_protocol = protocol,
150 .ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_IDN,
151 }, *res;
152
153 if (host && !*host)
154 host = NULL;
155
156 msg_Dbg (obj, "net: opening %s datagram port %d",
157 host ? host : "any", port);
158
159 int val = vlc_getaddrinfo (host, port, &hints, &res);
160 if (val)
161 {
162 msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
163 gai_strerror (val));
164 return -1;
165 }
166
167 val = -1;
168
169 for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
170 {
171 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
172 ptr->ai_protocol);
173 if (fd == -1)
174 {
175 msg_Dbg (obj, "socket error: %s", vlc_strerror_c(net_errno));
176 continue;
177 }
178
179 #ifdef IPV6_V6ONLY
180 /* Try dual-mode IPv6 if available. */
181 if (ptr->ai_family == AF_INET6)
182 setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &(int){ 0 }, sizeof (int));
183 #endif
184 fd = net_SetupDgramSocket( obj, fd, ptr );
185 if( fd == -1 )
186 continue;
187
188 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
189 && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
190 {
191 net_Close (fd);
192 continue;
193 }
194
195 val = fd;
196 break;
197 }
198
199 freeaddrinfo (res);
200 return val;
201 }
202
203
net_SetMcastHopLimit(vlc_object_t * p_this,int fd,int family,int hlim)204 static int net_SetMcastHopLimit( vlc_object_t *p_this,
205 int fd, int family, int hlim )
206 {
207 int proto, cmd;
208
209 /* There is some confusion in the world whether IP_MULTICAST_TTL
210 * takes a byte or an int as an argument.
211 * BSD seems to indicate byte so we are going with that and use
212 * int as a fallback to be safe */
213 switch( family )
214 {
215 #ifdef IP_MULTICAST_TTL
216 case AF_INET:
217 proto = SOL_IP;
218 cmd = IP_MULTICAST_TTL;
219 break;
220 #endif
221
222 #ifdef IPV6_MULTICAST_HOPS
223 case AF_INET6:
224 proto = SOL_IPV6;
225 cmd = IPV6_MULTICAST_HOPS;
226 break;
227 #endif
228
229 default:
230 errno = EAFNOSUPPORT;
231 msg_Warn( p_this, "%s", vlc_strerror_c(EAFNOSUPPORT) );
232 return VLC_EGENERIC;
233 }
234
235 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
236 {
237 /* BSD compatibility */
238 unsigned char buf;
239
240 msg_Dbg( p_this, "cannot set hop limit (%d): %s", hlim,
241 vlc_strerror_c(net_errno) );
242 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
243 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
244 {
245 msg_Err( p_this, "cannot set hop limit (%d): %s", hlim,
246 vlc_strerror_c(net_errno) );
247 return VLC_EGENERIC;
248 }
249 }
250
251 return VLC_SUCCESS;
252 }
253
254
net_SetMcastOut(vlc_object_t * p_this,int fd,int family,const char * iface)255 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
256 const char *iface)
257 {
258 int scope = if_nametoindex (iface);
259 if (scope == 0)
260 {
261 msg_Err (p_this, "invalid multicast interface: %s", iface);
262 return -1;
263 }
264
265 switch (family)
266 {
267 #ifdef IPV6_MULTICAST_IF
268 case AF_INET6:
269 if (setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
270 &scope, sizeof (scope)) == 0)
271 return 0;
272 break;
273 #endif
274
275 #ifdef __linux__
276 case AF_INET:
277 {
278 struct ip_mreqn req = { .imr_ifindex = scope };
279 if (setsockopt (fd, SOL_IP, IP_MULTICAST_IF,
280 &req, sizeof (req)) == 0)
281 return 0;
282 break;
283 }
284 #endif
285 default:
286 errno = EAFNOSUPPORT;
287 }
288 msg_Err (p_this, "cannot force multicast interface %s: %s", iface,
289 vlc_strerror_c(errno));
290 return -1;
291 }
292
293
var_GetIfIndex(vlc_object_t * obj)294 static unsigned var_GetIfIndex (vlc_object_t *obj)
295 {
296 char *ifname = var_InheritString (obj, "miface");
297 if (ifname == NULL)
298 return 0;
299
300 unsigned ifindex = if_nametoindex (ifname);
301 if (ifindex == 0)
302 msg_Err (obj, "invalid multicast interface: %s", ifname);
303 free (ifname);
304 return ifindex;
305 }
306
307
308 /**
309 * IP-agnostic multicast join,
310 * with fallback to old APIs, and fallback from SSM to ASM.
311 */
312 static int
net_SourceSubscribe(vlc_object_t * obj,int fd,const struct sockaddr * src,socklen_t srclen,const struct sockaddr * grp,socklen_t grplen)313 net_SourceSubscribe (vlc_object_t *obj, int fd,
314 const struct sockaddr *src, socklen_t srclen,
315 const struct sockaddr *grp, socklen_t grplen)
316 {
317 /* MCAST_JOIN_SOURCE_GROUP was introduced to OS X in v10.7, but it doesn't work,
318 * so ignore it to use the same code path as on 10.5 or 10.6 */
319 #if defined (MCAST_JOIN_SOURCE_GROUP) && !defined (__APPLE__) && !defined (__FreeBSD__)
320 /* Family-agnostic Source-Specific Multicast join */
321 int level;
322 struct group_source_req gsr;
323
324 memset (&gsr, 0, sizeof (gsr));
325 gsr.gsr_interface = var_GetIfIndex (obj);
326
327 switch (grp->sa_family)
328 {
329 #ifdef AF_INET6
330 case AF_INET6:
331 {
332 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
333
334 level = SOL_IPV6;
335 assert (grplen >= sizeof (struct sockaddr_in6));
336 if (g6->sin6_scope_id != 0)
337 gsr.gsr_interface = g6->sin6_scope_id;
338 break;
339 }
340 #endif
341 case AF_INET:
342 level = SOL_IP;
343 break;
344 default:
345 errno = EAFNOSUPPORT;
346 return -1;
347 }
348
349 assert (grplen <= sizeof (gsr.gsr_group));
350 memcpy (&gsr.gsr_source, src, srclen);
351 assert (srclen <= sizeof (gsr.gsr_source));
352 memcpy (&gsr.gsr_group, grp, grplen);
353 if (setsockopt (fd, level, MCAST_JOIN_SOURCE_GROUP,
354 &gsr, sizeof (gsr)) == 0)
355 return 0;
356
357 #else
358 if (src->sa_family != grp->sa_family)
359 {
360 errno = EAFNOSUPPORT;
361 return -1;
362 }
363
364 switch (grp->sa_family)
365 {
366 # ifdef IP_ADD_SOURCE_MEMBERSHIP
367 /* IPv4-specific API */
368 case AF_INET:
369 {
370 struct ip_mreq_source imr;
371
372 memset (&imr, 0, sizeof (imr));
373 assert (grplen >= sizeof (struct sockaddr_in));
374 imr.imr_multiaddr = ((const struct sockaddr_in *)grp)->sin_addr;
375 assert (srclen >= sizeof (struct sockaddr_in));
376 imr.imr_sourceaddr = ((const struct sockaddr_in *)src)->sin_addr;
377 if (setsockopt (fd, SOL_IP, IP_ADD_SOURCE_MEMBERSHIP,
378 &imr, sizeof (imr)) == 0)
379 return 0;
380 break;
381 }
382 # endif
383 default:
384 errno = EAFNOSUPPORT;
385 }
386
387 #endif
388 msg_Err (obj, "cannot join source multicast group: %s",
389 vlc_strerror_c(net_errno));
390 msg_Warn (obj, "trying ASM instead of SSM...");
391 return net_Subscribe (obj, fd, grp, grplen);
392 }
393
394
net_Subscribe(vlc_object_t * obj,int fd,const struct sockaddr * grp,socklen_t grplen)395 int net_Subscribe (vlc_object_t *obj, int fd,
396 const struct sockaddr *grp, socklen_t grplen)
397 {
398 /* MCAST_JOIN_GROUP was introduced to OS X in v10.7, but it doesn't work,
399 * so ignore it to use the same code as on 10.5 or 10.6 */
400 #if defined (MCAST_JOIN_GROUP) && !defined (__APPLE__)
401 /* Family-agnostic Any-Source Multicast join */
402 int level;
403 struct group_req gr;
404
405 memset (&gr, 0, sizeof (gr));
406 gr.gr_interface = var_GetIfIndex (obj);
407
408 switch (grp->sa_family)
409 {
410 #ifdef AF_INET6
411 case AF_INET6:
412 {
413 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
414
415 level = SOL_IPV6;
416 assert (grplen >= sizeof (struct sockaddr_in6));
417 if (g6->sin6_scope_id != 0)
418 gr.gr_interface = g6->sin6_scope_id;
419 break;
420 }
421 #endif
422 case AF_INET:
423 level = SOL_IP;
424 break;
425 default:
426 errno = EAFNOSUPPORT;
427 return -1;
428 }
429
430 assert (grplen <= sizeof (gr.gr_group));
431 memcpy (&gr.gr_group, grp, grplen);
432 if (setsockopt (fd, level, MCAST_JOIN_GROUP, &gr, sizeof (gr)) == 0)
433 return 0;
434
435 #else
436 switch (grp->sa_family)
437 {
438 # ifdef IPV6_JOIN_GROUP
439 case AF_INET6:
440 {
441 struct ipv6_mreq ipv6mr;
442 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
443
444 memset (&ipv6mr, 0, sizeof (ipv6mr));
445 assert (grplen >= sizeof (struct sockaddr_in6));
446 ipv6mr.ipv6mr_multiaddr = g6->sin6_addr;
447 ipv6mr.ipv6mr_interface = g6->sin6_scope_id;
448 if (!setsockopt (fd, SOL_IPV6, IPV6_JOIN_GROUP,
449 &ipv6mr, sizeof (ipv6mr)))
450 return 0;
451 break;
452 }
453 # endif
454 # ifdef IP_ADD_MEMBERSHIP
455 case AF_INET:
456 {
457 struct ip_mreq imr;
458
459 memset (&imr, 0, sizeof (imr));
460 assert (grplen >= sizeof (struct sockaddr_in));
461 imr.imr_multiaddr = ((const struct sockaddr_in *)grp)->sin_addr;
462 if (setsockopt (fd, SOL_IP, IP_ADD_MEMBERSHIP,
463 &imr, sizeof (imr)) == 0)
464 return 0;
465 break;
466 }
467 # endif
468 default:
469 errno = EAFNOSUPPORT;
470 }
471
472 #endif
473 msg_Err (obj, "cannot join multicast group: %s",
474 vlc_strerror_c(net_errno));
475 return -1;
476 }
477
478
net_SetDSCP(int fd,uint8_t dscp)479 static int net_SetDSCP( int fd, uint8_t dscp )
480 {
481 struct sockaddr_storage addr;
482 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
483 return -1;
484
485 int level, cmd;
486
487 switch( addr.ss_family )
488 {
489 #ifdef IPV6_TCLASS
490 case AF_INET6:
491 level = SOL_IPV6;
492 cmd = IPV6_TCLASS;
493 break;
494 #endif
495
496 case AF_INET:
497 level = SOL_IP;
498 cmd = IP_TOS;
499 break;
500
501 default:
502 #ifdef ENOPROTOOPT
503 errno = ENOPROTOOPT;
504 #endif
505 return -1;
506 }
507
508 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
509 }
510
511 #undef net_ConnectDgram
512 /*****************************************************************************
513 * net_ConnectDgram:
514 *****************************************************************************
515 * Open a datagram socket to send data to a defined destination, with an
516 * optional hop limit.
517 *****************************************************************************/
net_ConnectDgram(vlc_object_t * p_this,const char * psz_host,int i_port,int i_hlim,int proto)518 int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
519 int i_hlim, int proto )
520 {
521 struct addrinfo hints = {
522 .ai_socktype = SOCK_DGRAM,
523 .ai_protocol = proto,
524 .ai_flags = AI_NUMERICSERV | AI_IDN,
525 }, *res;
526 int i_handle = -1;
527 bool b_unreach = false;
528
529 if( i_hlim < 0 )
530 i_hlim = var_InheritInteger( p_this, "ttl" );
531
532 msg_Dbg( p_this, "net: connecting to [%s]:%d", psz_host, i_port );
533
534 int val = vlc_getaddrinfo (psz_host, i_port, &hints, &res);
535 if (val)
536 {
537 msg_Err (p_this, "cannot resolve [%s]:%d : %s", psz_host, i_port,
538 gai_strerror (val));
539 return -1;
540 }
541
542 for (struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
543 {
544 char *str;
545 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
546 ptr->ai_protocol);
547 if (fd == -1)
548 continue;
549
550 /* Allow broadcast sending */
551 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
552
553 if( i_hlim >= 0 )
554 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
555
556 str = var_InheritString (p_this, "miface");
557 if (str != NULL)
558 {
559 net_SetMcastOut (p_this, fd, ptr->ai_family, str);
560 free (str);
561 }
562
563 net_SetDSCP (fd, var_InheritInteger (p_this, "dscp"));
564
565 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
566 {
567 /* success */
568 i_handle = fd;
569 break;
570 }
571
572 #if defined( _WIN32 )
573 if( WSAGetLastError () == WSAENETUNREACH )
574 #else
575 if( errno == ENETUNREACH )
576 #endif
577 b_unreach = true;
578 else
579 msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
580 vlc_strerror_c(errno) );
581 net_Close( fd );
582 }
583
584 freeaddrinfo( res );
585
586 if( i_handle == -1 )
587 {
588 if( b_unreach )
589 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
590 i_port );
591 return -1;
592 }
593
594 return i_handle;
595 }
596
597 #undef net_OpenDgram
598 /*****************************************************************************
599 * net_OpenDgram:
600 *****************************************************************************
601 * OpenDgram a datagram socket and return a handle
602 *****************************************************************************/
net_OpenDgram(vlc_object_t * obj,const char * psz_bind,int i_bind,const char * psz_server,int i_server,int protocol)603 int net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
604 const char *psz_server, int i_server, int protocol )
605 {
606 if ((psz_server == NULL) || (psz_server[0] == '\0'))
607 return net_ListenSingle (obj, psz_bind, i_bind, protocol);
608
609 msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
610 psz_server, i_server, psz_bind, i_bind);
611
612 struct addrinfo hints = {
613 .ai_socktype = SOCK_DGRAM,
614 .ai_protocol = protocol,
615 .ai_flags = AI_NUMERICSERV | AI_IDN,
616 }, *loc, *rem;
617
618 int val = vlc_getaddrinfo (psz_server, i_server, &hints, &rem);
619 if (val)
620 {
621 msg_Err (obj, "cannot resolve %s port %d : %s", psz_server, i_server,
622 gai_strerror (val));
623 return -1;
624 }
625
626 hints.ai_flags |= AI_PASSIVE;
627 val = vlc_getaddrinfo (psz_bind, i_bind, &hints, &loc);
628 if (val)
629 {
630 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
631 gai_strerror (val));
632 freeaddrinfo (rem);
633 return -1;
634 }
635
636 val = -1;
637 for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
638 {
639 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
640 ptr->ai_protocol);
641 if (fd == -1)
642 continue; // usually, address family not supported
643
644 fd = net_SetupDgramSocket( obj, fd, ptr );
645 if( fd == -1 )
646 continue;
647
648 for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
649 {
650 if ((ptr2->ai_family != ptr->ai_family)
651 || (ptr2->ai_socktype != ptr->ai_socktype)
652 || (ptr2->ai_protocol != ptr->ai_protocol))
653 continue;
654
655 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
656 ? net_SourceSubscribe (obj, fd,
657 ptr2->ai_addr, ptr2->ai_addrlen,
658 ptr->ai_addr, ptr->ai_addrlen)
659 : connect (fd, ptr2->ai_addr, ptr2->ai_addrlen))
660 {
661 msg_Err (obj, "cannot connect to %s port %d: %s",
662 psz_server, i_server, vlc_strerror_c(net_errno));
663 continue;
664 }
665 val = fd;
666 break;
667 }
668
669 if (val != -1)
670 break;
671
672 net_Close (fd);
673 }
674
675 freeaddrinfo (rem);
676 freeaddrinfo (loc);
677 return val;
678 }
679
680
681 /**
682 * net_SetCSCov:
683 * Sets the send and receive checksum coverage of a socket:
684 * @param fd socket
685 * @param sendcov payload coverage of sent packets (bytes), -1 for full
686 * @param recvcov minimum payload coverage of received packets, -1 for full
687 */
net_SetCSCov(int fd,int sendcov,int recvcov)688 int net_SetCSCov (int fd, int sendcov, int recvcov)
689 {
690 int type;
691
692 if (getsockopt (fd, SOL_SOCKET, SO_TYPE,
693 &type, &(socklen_t){ sizeof (type) }))
694 return VLC_EGENERIC;
695
696 switch (type)
697 {
698 #ifdef UDPLITE_RECV_CSCOV
699 case SOCK_DGRAM: /* UDP-Lite */
700 if (sendcov == -1)
701 sendcov = 0;
702 else
703 sendcov += 8; /* partial */
704 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_SEND_CSCOV, &sendcov,
705 sizeof (sendcov)))
706 return VLC_EGENERIC;
707
708 if (recvcov == -1)
709 recvcov = 0;
710 else
711 recvcov += 8;
712 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
713 &recvcov, sizeof (recvcov)))
714 return VLC_EGENERIC;
715
716 return VLC_SUCCESS;
717 #endif
718 #ifdef DCCP_SOCKOPT_SEND_CSCOV
719 case SOCK_DCCP: /* DCCP and its ill-named socket type */
720 if ((sendcov == -1) || (sendcov > 56))
721 sendcov = 0;
722 else
723 sendcov = (sendcov + 3) / 4;
724 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SEND_CSCOV,
725 &sendcov, sizeof (sendcov)))
726 return VLC_EGENERIC;
727
728 if ((recvcov == -1) || (recvcov > 56))
729 recvcov = 0;
730 else
731 recvcov = (recvcov + 3) / 4;
732 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_RECV_CSCOV,
733 &recvcov, sizeof (recvcov)))
734 return VLC_EGENERIC;
735
736 return VLC_SUCCESS;
737 #endif
738 }
739 #if !defined( UDPLITE_RECV_CSCOV ) && !defined( DCCP_SOCKOPT_SEND_CSCOV )
740 VLC_UNUSED(sendcov);
741 VLC_UNUSED(recvcov);
742 #endif
743
744 return VLC_EGENERIC;
745 }
746