1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
5 * Copyright © 2006 Rémi Denis-Courmont
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
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
43 # if defined(UNDER_CE)
44 # undef IP_MULTICAST_TTL
45 # define IP_MULTICAST_TTL 3
46 # undef IP_ADD_MEMBERSHIP
47 # define IP_ADD_MEMBERSHIP 5
49 # define EAFNOSUPPORT WSAEAFNOSUPPORT
50 # define if_nametoindex( str ) atoi( str )
59 # define SOL_IP IPPROTO_IP
62 # define SOL_IPV6 IPPROTO_IPV6
65 # define IPPROTO_IPV6 41
68 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
72 static int net_SetMcastHopLimit( vlc_object_t *p_this,
73 int fd, int family, int hlim )
78 /* There is some confusion in the world whether IP_MULTICAST_TTL
79 * takes a byte or an int as an argument.
80 * BSD seems to indicate byte so we are going with that and use
81 * int as a fallback to be safe */
86 cmd = IP_MULTICAST_TTL;
89 #ifdef IPV6_MULTICAST_HOPS
92 cmd = IPV6_MULTICAST_HOPS;
97 msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
101 if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
103 /* BSD compatibility */
106 buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
107 if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
115 static int net_SetMcastOutIface (int fd, int family, int scope)
119 #ifdef IPV6_MULTICAST_IF
121 return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
122 &scope, sizeof (scope));
128 struct ip_mreqn req = { .imr_ifindex = scope };
130 return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
136 errno = EAFNOSUPPORT;
141 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
143 #ifdef IP_MULTICAST_IF
144 return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
146 errno = EAFNOSUPPORT;
152 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
153 const char *iface, const char *addr)
157 int scope = if_nametoindex (iface);
160 msg_Err (p_this, "%s: invalid interface for multicast", iface);
164 if (net_SetMcastOutIface (fd, family, scope) == 0)
167 msg_Err (p_this, "%s: %s", iface, net_strerror (net_errno));
172 if (family == AF_INET)
175 if (inet_pton (AF_INET, addr, &ipv4) <= 0)
177 msg_Err (p_this, "%s: invalid IPv4 address for multicast",
182 if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
185 msg_Err (p_this, "%s: %s", addr, net_strerror (net_errno));
193 int net_Subscribe (vlc_object_t *obj, int fd, const struct sockaddr *addr,
196 switch (addr->sa_family)
198 #ifdef IP_ADD_MEMBERSHIP
201 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
202 if (addrlen < sizeof (*v4))
206 memset (&imr, 0, sizeof (imr));
207 memcpy (&imr.imr_multiaddr, &v4->sin_addr, 4);
209 /* FIXME: should use a different option for in and out */
210 char *iif = var_CreateGetString (obj, "miface-addr");
213 if ((iif[0] != '\0') &&
214 (inet_pton (AF_INET, iif, &imr.imr_interface) <= 0))
216 msg_Err (obj, "invalid multicast interface address %s",
224 msg_Dbg (obj, "IP_ADD_MEMBERSHIP multicast request");
226 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr,
229 msg_Err (obj, "cannot join IPv4 multicast group (%s)",
230 net_strerror (net_errno));
237 #ifdef IPV6_JOIN_GROUP
240 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
241 if (addrlen < sizeof (*v6))
244 struct ipv6_mreq imr;
245 memset (&imr, 0, sizeof (imr));
246 imr.ipv6mr_interface = v6->sin6_scope_id;
247 memcpy (&imr.ipv6mr_multiaddr, &v6->sin6_addr, 16);
249 msg_Dbg (obj, "IPV6_JOIN_GROUP multicast request");
251 if (setsockopt (fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &imr,
254 msg_Err (obj, "cannot join IPv6 multicast group (%s)",
255 net_strerror (net_errno));
264 msg_Err (obj, "Multicast not supported");
269 int net_SetDSCP( int fd, uint8_t dscp )
271 struct sockaddr_storage addr;
272 if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
277 switch( addr.ss_family )
298 return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
302 /*****************************************************************************
304 *****************************************************************************
305 * Open a UDP socket to send data to a defined destination, with an optional
307 *****************************************************************************/
308 int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
311 struct addrinfo hints, *res, *ptr;
312 int i_val, i_handle = -1;
313 vlc_bool_t b_unreach = VLC_FALSE;
316 i_port = 1234; /* historical VLC thing */
319 i_hlim = var_CreateGetInteger( p_this, "ttl" );
321 memset( &hints, 0, sizeof( hints ) );
322 hints.ai_socktype = SOCK_DGRAM;
324 msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
326 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
329 msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
330 vlc_gai_strerror( i_val ) );
334 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
337 int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
342 #if !defined( SYS_BEOS )
343 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
344 * to avoid packet loss caused by scheduling problems */
345 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
346 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
348 /* Allow broadcast sending */
349 setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
353 net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
355 str = var_CreateGetString (p_this, "miface");
359 net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
363 str = var_CreateGetString (p_this, "miface-addr");
367 net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
371 net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
373 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
380 #if defined( WIN32 ) || defined( UNDER_CE )
381 if( WSAGetLastError () == WSAENETUNREACH )
383 if( errno == ENETUNREACH )
385 b_unreach = VLC_TRUE;
388 msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
395 vlc_freeaddrinfo( res );
400 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
409 /*****************************************************************************
411 *****************************************************************************
412 * Open a UDP connection and return a handle
413 *****************************************************************************/
414 int __net_OpenUDP( vlc_object_t *p_this, const char *psz_bind, int i_bind,
415 const char *psz_server, int i_server )
418 network_socket_t sock;
419 module_t *p_network = NULL;
421 if (((psz_bind == NULL) || (psz_bind[0] == '\0')) && (i_bind == 0))
424 "Obsolete net_OpenUDP with no local endpoint; "
425 "Use net_ConnectUDP instead");
426 return net_ConnectUDP (p_this, psz_server, i_server, -1);
429 if (((psz_server == NULL) || (psz_server[0] == '\0')) && (i_server == 0))
432 "Obsolete net_OpenUDP with no remote endpoint; "
433 "Use net_ListenUDP instead");
434 return net_ListenUDP1 (p_this, psz_bind, i_bind);
437 if( psz_server == NULL ) psz_server = "";
438 if( psz_bind == NULL ) psz_bind = "";
440 /* Prepare the network_socket_t structure */
441 sock.psz_bind_addr = psz_bind;
442 sock.i_bind_port = i_bind;
443 sock.psz_server_addr = psz_server;
444 sock.i_server_port = i_server;
448 msg_Dbg( p_this, "net: connecting to '[%s]:%d@[%s]:%d'",
449 psz_server, i_server, psz_bind, i_bind );
451 /* Check if we have force ipv4 or ipv6 */
452 vlc_bool_t v4 = var_CreateGetBool (p_this, "ipv4");
453 vlc_bool_t v6 = var_CreateGetBool (p_this, "ipv6");
457 /* try IPv6 first (unless IPv4 forced) */
458 private = p_this->p_private;
459 p_this->p_private = (void*)&sock;
460 p_network = module_Need( p_this, "network", "ipv6", VLC_TRUE );
462 if( p_network != NULL )
463 module_Unneed( p_this, p_network );
465 p_this->p_private = private;
468 if ((sock.i_handle == -1) && !v6)
470 /* also try IPv4 (unless IPv6 forced) */
471 private = p_this->p_private;
472 p_this->p_private = (void*)&sock;
473 p_network = module_Need( p_this, "network", "ipv4", VLC_TRUE );
475 if( p_network != NULL )
476 module_Unneed( p_this, p_network );
478 p_this->p_private = private;
481 if( sock.i_handle == -1 )
482 msg_Dbg( p_this, "net: connection to '[%s]:%d@[%s]:%d' failed",
483 psz_server, i_server, psz_bind, i_bind );
485 return sock.i_handle;