Fixed a crash caused by yadif deinterlacer on Windows XP
[vlc/solaris.git] / src / network / udp.c
blob813c1b717cd67d949e0189b65c091ca76f95ca9a
1 /*****************************************************************************
2 * udp.c:
3 *****************************************************************************
4 * Copyright (C) 2004-2006 VLC authors and VideoLAN
5 * Copyright © 2006-2007 Rémi Denis-Courmont
7 * $Id$
9 * Authors: Laurent Aimar <fenrir@videolan.org>
10 * Rémi Denis-Courmont <rem # videolan.org>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
36 #include <errno.h>
37 #include <assert.h>
39 #include <vlc_network.h>
41 #ifdef WIN32
42 # if defined(UNDER_CE)
43 # undef IP_MULTICAST_TTL
44 # define IP_MULTICAST_TTL 3
45 # undef IP_ADD_MEMBERSHIP
46 # define IP_ADD_MEMBERSHIP 5
47 # endif
48 # define EAFNOSUPPORT WSAEAFNOSUPPORT
49 #else
50 # include <unistd.h>
51 # ifdef HAVE_NET_IF_H
52 # include <net/if.h>
53 # endif
54 #endif
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
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
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
93 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
94 int i_protocol );
96 /* */
97 static int net_SetupDgramSocket( vlc_object_t *p_obj, int fd, const struct addrinfo *ptr )
99 #ifdef SO_REUSEPORT
100 setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
101 #endif
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
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))
116 // This works for IPv4 too - don't worry!
117 struct sockaddr_in6 dumb =
119 .sin6_family = ptr->ai_addr->sa_family,
120 .sin6_port = ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
123 bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
125 else
126 #endif
127 if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
129 msg_Err( p_obj, "socket bind error (%m)" );
130 net_Close (fd);
131 return -1;
133 return fd;
136 /* */
137 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
138 int protocol)
140 struct addrinfo hints, *res;
142 memset (&hints, 0, sizeof( hints ));
143 hints.ai_socktype = SOCK_DGRAM;
144 hints.ai_protocol = protocol;
145 hints.ai_flags = AI_PASSIVE;
147 if (host && !*host)
148 host = NULL;
150 msg_Dbg (obj, "net: opening %s datagram port %d",
151 host ? host : "any", port);
153 int val = vlc_getaddrinfo (obj, host, port, &hints, &res);
154 if (val)
156 msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
157 gai_strerror (val));
158 return -1;
161 val = -1;
163 for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
165 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
166 ptr->ai_protocol);
167 if (fd == -1)
169 msg_Dbg (obj, "socket error: %m");
170 continue;
173 #ifdef IPV6_V6ONLY
174 /* Try dual-mode IPv6 if available. */
175 if (ptr->ai_family == AF_INET6)
176 setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &(int){ 0 }, sizeof (int));
177 #endif
178 fd = net_SetupDgramSocket( obj, fd, ptr );
179 if( fd == -1 )
180 continue;
182 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
183 && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
185 net_Close (fd);
186 continue;
189 val = fd;
190 break;
193 freeaddrinfo (res);
194 return val;
198 static int net_SetMcastHopLimit( vlc_object_t *p_this,
199 int fd, int family, int hlim )
201 int proto, cmd;
203 /* There is some confusion in the world whether IP_MULTICAST_TTL
204 * takes a byte or an int as an argument.
205 * BSD seems to indicate byte so we are going with that and use
206 * int as a fallback to be safe */
207 switch( family )
209 #ifdef IP_MULTICAST_TTL
210 case AF_INET:
211 proto = SOL_IP;
212 cmd = IP_MULTICAST_TTL;
213 break;
214 #endif
216 #ifdef IPV6_MULTICAST_HOPS
217 case AF_INET6:
218 proto = SOL_IPV6;
219 cmd = IPV6_MULTICAST_HOPS;
220 break;
221 #endif
223 default:
224 errno = EAFNOSUPPORT;
225 msg_Warn( p_this, "%m" );
226 return VLC_EGENERIC;
229 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
231 /* BSD compatibility */
232 unsigned char buf;
234 msg_Dbg( p_this, "cannot set hop limit (%d): %m", hlim );
235 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
236 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
238 msg_Err( p_this, "cannot set hop limit (%d): %m", hlim );
239 return VLC_EGENERIC;
243 return VLC_SUCCESS;
247 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
248 const char *iface)
250 int scope = if_nametoindex (iface);
251 if (scope == 0)
253 msg_Err (p_this, "invalid multicast interface: %s", iface);
254 return -1;
257 switch (family)
259 #ifdef IPV6_MULTICAST_IF
260 case AF_INET6:
261 if (setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
262 &scope, sizeof (scope) == 0))
263 return 0;
264 #endif
266 #ifdef __linux__
267 case AF_INET:
269 struct ip_mreqn req = { .imr_ifindex = scope };
270 if (setsockopt (fd, SOL_IP, IP_MULTICAST_IF,
271 &req, sizeof (req)) == 0)
272 return 0;
274 #endif
275 default:
276 errno = EAFNOSUPPORT;
278 msg_Err (p_this, "cannot force multicast interface %s: %m", iface);
279 return -1;
283 static unsigned var_GetIfIndex (vlc_object_t *obj)
285 char *ifname = var_InheritString (obj, "miface");
286 if (ifname == NULL)
287 return 0;
289 unsigned ifindex = if_nametoindex (ifname);
290 if (ifindex == 0)
291 msg_Err (obj, "invalid multicast interface: %s", ifname);
292 free (ifname);
293 return ifindex;
298 * IP-agnostic multicast join,
299 * with fallback to old APIs, and fallback from SSM to ASM.
301 static int
302 net_SourceSubscribe (vlc_object_t *obj, int fd,
303 const struct sockaddr *src, socklen_t srclen,
304 const struct sockaddr *grp, socklen_t grplen)
306 #ifdef MCAST_JOIN_SOURCE_GROUP
307 /* Agnostic SSM multicast join */
308 int level;
309 struct group_source_req gsr;
311 memset (&gsr, 0, sizeof (gsr));
312 gsr.gsr_interface = var_GetIfIndex (obj);
314 switch (grp->sa_family)
316 #ifdef AF_INET6
317 case AF_INET6:
319 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
321 level = SOL_IPV6;
322 assert (grplen >= sizeof (struct sockaddr_in6));
323 if (g6->sin6_scope_id != 0)
324 gsr.gsr_interface = g6->sin6_scope_id;
325 break;
327 #endif
328 case AF_INET:
329 level = SOL_IP;
330 break;
331 default:
332 errno = EAFNOSUPPORT;
333 return -1;
336 assert (grplen <= sizeof (gsr.gsr_group));
337 memcpy (&gsr.gsr_source, src, srclen);
338 assert (srclen <= sizeof (gsr.gsr_source));
339 memcpy (&gsr.gsr_group, grp, grplen);
340 if (setsockopt (fd, level, MCAST_JOIN_SOURCE_GROUP,
341 &gsr, sizeof (gsr)) == 0)
342 return 0;
344 #else
345 if (src->sa_family != grp->sa_family)
347 errno = EAFNOSUPPORT;
348 return -1;
351 switch (grp->sa_family)
353 # ifdef IP_ADD_SOURCE_MEMBERSHIP
354 /* IPv4-specific API */
355 case AF_INET:
357 struct ip_mreq_source imr;
359 memset (&imr, 0, sizeof (imr));
360 assert (grplen >= sizeof (struct sockaddr_in));
361 imr.imr_multiaddr = ((const struct sockaddr_in *)grp)->sin_addr;
362 assert (srclen >= sizeof (struct sockaddr_in));
363 imr.imr_sourceaddr = ((const struct sockaddr_in *)src)->sin_addr;
364 if (setsockopt (fd, SOL_IP, IP_ADD_SOURCE_MEMBERSHIP,
365 &imr, sizeof (imr)) == 0)
366 return 0;
367 break;
369 # endif
370 default:
371 errno = EAFNOSUPPORT;
374 #endif
375 msg_Err (obj, "cannot join source multicast group: %m");
376 msg_Warn (obj, "trying ASM instead of SSM...");
377 return net_Subscribe (obj, fd, grp, grplen);
381 int net_Subscribe (vlc_object_t *obj, int fd,
382 const struct sockaddr *grp, socklen_t grplen)
384 #ifdef MCAST_JOIN_GROUP
385 /* Agnostic SSM multicast join */
386 int level;
387 struct group_req gr;
389 memset (&gr, 0, sizeof (gr));
390 gr.gr_interface = var_GetIfIndex (obj);
392 switch (grp->sa_family)
394 #ifdef AF_INET6
395 case AF_INET6:
397 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
399 level = SOL_IPV6;
400 assert (grplen >= sizeof (struct sockaddr_in6));
401 if (g6->sin6_scope_id != 0)
402 gr.gr_interface = g6->sin6_scope_id;
403 break;
405 #endif
406 case AF_INET:
407 level = SOL_IP;
408 break;
409 default:
410 errno = EAFNOSUPPORT;
411 return -1;
414 assert (grplen <= sizeof (gr.gr_group));
415 memcpy (&gr.gr_group, grp, grplen);
416 if (setsockopt (fd, level, MCAST_JOIN_GROUP, &gr, sizeof (gr)) == 0)
417 return 0;
419 #else
420 switch (grp->sa_family)
422 # ifdef IPV6_JOIN_GROUP
423 case AF_INET6:
425 struct ipv6_mreq ipv6mr;
426 const struct sockaddr_in6 *g6 = (const struct sockaddr_in6 *)grp;
428 memset (&ipv6mr, 0, sizeof (ipv6mr));
429 assert (grplen >= sizeof (struct sockaddr_in6));
430 ipv6mr.ipv6mr_multiaddr = g6->sin6_addr;
431 ipv6mr.ipv6mr_interface = g6->sin6_scope_id;
432 if (!setsockopt (fd, SOL_IPV6, IPV6_JOIN_GROUP,
433 &ipv6mr, sizeof (ipv6mr)))
434 return 0;
435 break;
437 # endif
438 # ifdef IP_ADD_MEMBERSHIP
439 case AF_INET:
441 struct ip_mreq imr;
443 memset (&imr, 0, sizeof (imr));
444 assert (grplen >= sizeof (struct sockaddr_in));
445 imr.imr_multiaddr = ((const struct sockaddr_in *)grp)->sin_addr;
446 if (setsockopt (fd, SOL_IP, IP_ADD_MEMBERSHIP,
447 &imr, sizeof (imr)) == 0)
448 return 0;
449 break;
451 # endif
452 default:
453 errno = EAFNOSUPPORT;
456 #endif
457 msg_Err (obj, "cannot join multicast group: %m");
458 return -1;
462 static int net_SetDSCP( int fd, uint8_t dscp )
464 struct sockaddr_storage addr;
465 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
466 return -1;
468 int level, cmd;
470 switch( addr.ss_family )
472 #ifdef IPV6_TCLASS
473 case AF_INET6:
474 level = SOL_IPV6;
475 cmd = IPV6_TCLASS;
476 break;
477 #endif
479 case AF_INET:
480 level = SOL_IP;
481 cmd = IP_TOS;
482 break;
484 default:
485 #ifdef ENOPROTOOPT
486 errno = ENOPROTOOPT;
487 #endif
488 return -1;
491 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
494 #undef net_ConnectDgram
495 /*****************************************************************************
496 * net_ConnectDgram:
497 *****************************************************************************
498 * Open a datagram socket to send data to a defined destination, with an
499 * optional hop limit.
500 *****************************************************************************/
501 int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
502 int i_hlim, int proto )
504 struct addrinfo hints, *res, *ptr;
505 int i_val, i_handle = -1;
506 bool b_unreach = false;
508 if( i_hlim < 0 )
509 i_hlim = var_InheritInteger( p_this, "ttl" );
511 memset( &hints, 0, sizeof( hints ) );
512 hints.ai_socktype = SOCK_DGRAM;
513 hints.ai_protocol = proto;
515 msg_Dbg( p_this, "net: connecting to [%s]:%d", psz_host, i_port );
517 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
518 if( i_val )
520 msg_Err( p_this, "cannot resolve [%s]:%d : %s", psz_host, i_port,
521 gai_strerror( i_val ) );
522 return -1;
525 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
527 char *str;
528 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
529 ptr->ai_protocol);
530 if (fd == -1)
531 continue;
533 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
534 * to avoid packet loss caused by scheduling problems */
535 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
536 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
538 /* Allow broadcast sending */
539 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
541 if( i_hlim >= 0 )
542 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
544 str = var_InheritString (p_this, "miface");
545 if (str != NULL)
547 net_SetMcastOut (p_this, fd, ptr->ai_family, str);
548 free (str);
551 net_SetDSCP (fd, var_InheritInteger (p_this, "dscp"));
553 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
555 /* success */
556 i_handle = fd;
557 break;
560 #if defined( WIN32 ) || defined( UNDER_CE )
561 if( WSAGetLastError () == WSAENETUNREACH )
562 #else
563 if( errno == ENETUNREACH )
564 #endif
565 b_unreach = true;
566 else
568 msg_Warn( p_this, "%s port %d : %m", psz_host, i_port);
569 net_Close( fd );
570 continue;
574 freeaddrinfo( res );
576 if( i_handle == -1 )
578 if( b_unreach )
579 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
580 i_port );
581 return -1;
584 return i_handle;
587 #undef net_OpenDgram
588 /*****************************************************************************
589 * net_OpenDgram:
590 *****************************************************************************
591 * OpenDgram a datagram socket and return a handle
592 *****************************************************************************/
593 int net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
594 const char *psz_server, int i_server, int protocol )
596 if ((psz_server == NULL) || (psz_server[0] == '\0'))
597 return net_ListenSingle (obj, psz_bind, i_bind, protocol);
599 msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
600 psz_server, i_server, psz_bind, i_bind);
602 struct addrinfo hints, *loc, *rem;
603 int val;
605 memset (&hints, 0, sizeof (hints));
606 hints.ai_socktype = SOCK_DGRAM;
607 hints.ai_protocol = protocol;
609 val = vlc_getaddrinfo (obj, psz_server, i_server, &hints, &rem);
610 if (val)
612 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
613 gai_strerror (val));
614 return -1;
617 hints.ai_flags = AI_PASSIVE;
618 val = vlc_getaddrinfo (obj, psz_bind, i_bind, &hints, &loc);
619 if (val)
621 msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
622 gai_strerror (val));
623 freeaddrinfo (rem);
624 return -1;
627 val = -1;
628 for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
630 int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
631 ptr->ai_protocol);
632 if (fd == -1)
633 continue; // usually, address family not supported
635 fd = net_SetupDgramSocket( obj, fd, ptr );
636 if( fd == -1 )
637 continue;
639 for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
641 if ((ptr2->ai_family != ptr->ai_family)
642 || (ptr2->ai_socktype != ptr->ai_socktype)
643 || (ptr2->ai_protocol != ptr->ai_protocol))
644 continue;
646 if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
647 ? net_SourceSubscribe (obj, fd,
648 ptr2->ai_addr, ptr2->ai_addrlen,
649 ptr->ai_addr, ptr->ai_addrlen)
650 : connect (fd, ptr2->ai_addr, ptr2->ai_addrlen))
652 msg_Err (obj, "cannot connect to %s port %d: %m",
653 psz_server, i_server);
654 continue;
656 val = fd;
657 break;
660 if (val != -1)
661 break;
663 net_Close (fd);
666 freeaddrinfo (rem);
667 freeaddrinfo (loc);
668 return val;
673 * net_SetCSCov:
674 * Sets the send and receive checksum coverage of a socket:
675 * @param fd socket
676 * @param sendcov payload coverage of sent packets (bytes), -1 for full
677 * @param recvcov minimum payload coverage of received packets, -1 for full
679 int net_SetCSCov (int fd, int sendcov, int recvcov)
681 int type;
683 if (getsockopt (fd, SOL_SOCKET, SO_TYPE,
684 &type, &(socklen_t){ sizeof (type) }))
685 return VLC_EGENERIC;
687 switch (type)
689 #ifdef UDPLITE_RECV_CSCOV
690 case SOCK_DGRAM: /* UDP-Lite */
691 if (sendcov == -1)
692 sendcov = 0;
693 else
694 sendcov += 8; /* partial */
695 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_SEND_CSCOV, &sendcov,
696 sizeof (sendcov)))
697 return VLC_EGENERIC;
699 if (recvcov == -1)
700 recvcov = 0;
701 else
702 recvcov += 8;
703 if (setsockopt (fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
704 &recvcov, sizeof (recvcov)))
705 return VLC_EGENERIC;
707 return VLC_SUCCESS;
708 #endif
709 #ifdef DCCP_SOCKOPT_SEND_CSCOV
710 case SOCK_DCCP: /* DCCP and its ill-named socket type */
711 if ((sendcov == -1) || (sendcov > 56))
712 sendcov = 0;
713 else
714 sendcov = (sendcov + 3) / 4;
715 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SEND_CSCOV,
716 &sendcov, sizeof (sendcov)))
717 return VLC_EGENERIC;
719 if ((recvcov == -1) || (recvcov > 56))
720 recvcov = 0;
721 else
722 recvcov = (recvcov + 3) / 4;
723 if (setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_RECV_CSCOV,
724 &recvcov, sizeof (recvcov)))
725 return VLC_EGENERIC;
727 return VLC_SUCCESS;
728 #endif
730 #if !defined( UDPLITE_RECV_CSCOV ) && !defined( DCCP_SOCKOPT_SEND_CSCOV )
731 VLC_UNUSED(sendcov);
732 VLC_UNUSED(recvcov);
733 #endif
735 return VLC_EGENERIC;