]> git.sesse.net Git - vlc/blob - src/network/udp.c
Implement multicast hop limit
[vlc] / src / network / udp.c
1 /*****************************************************************************
2  * udp.c:
3  *****************************************************************************
4  * Copyright (C) 2004-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@videolan.org>
8  *          RĂ©mi Denis-Courmont <rem # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <vlc/vlc.h>
30
31 #include <errno.h>
32
33 #ifdef HAVE_FCNTL_H
34 #   include <fcntl.h>
35 #endif
36 #ifdef HAVE_SYS_TIME_H
37 #    include <sys/time.h>
38 #endif
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #endif
42
43 #include "network.h"
44
45 #ifdef WIN32
46 #   if defined(UNDER_CE)
47 #       undef IP_MULTICAST_TTL
48 #       define IP_MULTICAST_TTL 3
49 #       undef IP_ADD_MEMBERSHIP
50 #       define IP_ADD_MEMBERSHIP 5
51 #   endif
52 #endif
53
54 #ifndef SOL_IP
55 # define SOL_IP IPPROTO_IP
56 #endif
57 #ifndef SOL_IPV6
58 # define SOL_IPV6 IPPROTO_IPV6
59 #endif
60 #ifndef IPPROTO_IPV6
61 # define IPPROTO_IPV6 41
62 #endif
63
64 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
65                        int i_protocol );
66
67
68 static void net_SetMcastHopLimit( int fd, int family, int hlim )
69 {
70     int proto, cmd;
71
72     /* There is some confusion in the world whether IP_MULTICAST_TTL 
73      * takes a byte or an int as an argument.
74      * BSD seems to indicate byte so we are going with that and use
75      * int as a fallback to be safe */
76     switch( family )
77     {
78         case AF_INET:
79             proto = SOL_IP;
80             cmd = IP_MULTICAST_TTL;
81             break;
82
83 #ifdef IPV6_MULTICAST_HOPS
84         case AF_INET6:
85             proto = SOL_IPV6;
86             cmd = IPV6_MULTICAST_HOPS;
87             break;
88 #endif
89
90         default:
91             return;
92     }
93
94     if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
95     {
96         /* BSD compatibility */
97         unsigned char buf;
98
99         buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
100         setsockopt( fd, proto, cmd, &buf, sizeof( buf ) );
101     }
102 }
103
104
105 /*****************************************************************************
106  * __net_ConnectUDP:
107  *****************************************************************************
108  * Open a UDP socket to send data to a defined destination, with an optional
109  * hop limit.
110  *****************************************************************************/
111 int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
112                       int i_hlim )
113 {
114     struct addrinfo hints, *res, *ptr;
115     int             i_val, i_handle = -1;
116     vlc_bool_t      b_unreach = VLC_FALSE;
117
118     if( i_port == 0 )
119         i_port = 1234; /* historical VLC thing */
120
121     if( i_hlim < 1 )
122     {
123         vlc_value_t val;
124
125         if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS )
126         {
127             var_Create( p_this, "ttl", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
128             var_Get( p_this, "ttl", &val );
129         }
130         i_hlim = val.i_int;
131
132         if( i_hlim < 1 ) i_hlim = 1;
133     }
134
135     memset( &hints, 0, sizeof( hints ) );
136     hints.ai_socktype = SOCK_DGRAM;
137
138     msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
139
140     i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
141     if( i_val )
142     {
143         msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
144                  vlc_gai_strerror( i_val ) );
145         return -1;
146     }
147
148     for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
149     {
150         int fd;
151
152         fd = net_Socket( p_this, ptr->ai_family, ptr->ai_socktype,
153                          ptr->ai_protocol );
154         if( fd == -1 )
155             continue;
156 #if !defined( SYS_BEOS )
157         else
158         {
159             int i_val;
160
161             /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
162              * to avoid packet loss caused by scheduling problems */
163             i_val = 0x80000;
164             setsockopt( fd, SOL_SOCKET, SO_RCVBUF, (void *)&i_val,
165                         sizeof( i_val ) );
166             i_val = 0x80000;
167             setsockopt( fd, SOL_SOCKET, SO_SNDBUF, (void *)&i_val,
168                         sizeof( i_val ) );
169         }
170 #endif
171
172         net_SetMcastHopLimit( i_handle, ptr->ai_family, i_hlim );
173
174         if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
175         {
176             /* success */
177             i_handle = fd;
178             break;
179         }
180
181 #if defined( WIN32 ) || defined( UNDER_CE )
182         if( WSAGetLastError () == WSAENETUNREACH )
183 #else
184         if( errno == ENETUNREACH )
185 #endif
186             b_unreach = VLC_TRUE;
187         else
188         {
189             msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
190                       strerror( errno ) );
191             net_Close( fd );
192             continue;
193         }
194     }
195
196     vlc_freeaddrinfo( res );
197
198     if( i_handle == -1 )
199     {
200         if( b_unreach )
201             msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
202                      i_port );
203         return -1;
204     }
205
206     return i_handle;
207 }
208
209 /*****************************************************************************
210  * __net_OpenUDP:
211  *****************************************************************************
212  * Open a UDP connection and return a handle
213  *****************************************************************************/
214 int __net_OpenUDP( vlc_object_t *p_this, const char *psz_bind, int i_bind,
215                    const char *psz_server, int i_server )
216 {
217     vlc_value_t      v4, v6;
218     void            *private;
219     network_socket_t sock;
220     module_t         *p_network = NULL;
221
222     if( ( psz_server != NULL ) && ( psz_server[0] == '\0' ) )
223         msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
224                   "is obsolete - use net_ConnectUDP instead" );
225     if( i_server != 0 )
226         msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
227                   "port is obsolete - use __net_ConnectUDP instead" );
228
229     if( psz_server == NULL ) psz_server = "";
230     if( psz_bind == NULL ) psz_bind = "";
231
232     /* Prepare the network_socket_t structure */
233     sock.psz_bind_addr   = psz_bind;
234     sock.i_bind_port     = i_bind;
235     sock.psz_server_addr = psz_server;
236     sock.i_server_port   = i_server;
237     sock.i_ttl           = 0;
238     sock.v6only          = 0;
239     sock.i_handle        = -1;
240
241     msg_Dbg( p_this, "net: connecting to '[%s]:%d@[%s]:%d'",
242              psz_server, i_server, psz_bind, i_bind );
243
244     /* Check if we have force ipv4 or ipv6 */
245     var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
246     var_Get( p_this, "ipv4", &v4 );
247     var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
248     var_Get( p_this, "ipv6", &v6 );
249
250     if( !v4.b_bool )
251     {
252         if( v6.b_bool )
253             sock.v6only = 1;
254
255         /* try IPv6 first (unless IPv4 forced) */
256         private = p_this->p_private;
257         p_this->p_private = (void*)&sock;
258         p_network = module_Need( p_this, "network", "ipv6", VLC_TRUE );
259
260         if( p_network != NULL )
261             module_Unneed( p_this, p_network );
262
263         p_this->p_private = private;
264
265         /*
266          * Check if the IP stack can receive IPv4 packets on IPv6 sockets.
267          * If yes, then it is better to use the IPv6 socket.
268          * Otherwise, if we also get an IPv4, we have to choose, so we use
269          * IPv4 only.
270          */
271         if( ( sock.i_handle != -1 ) && ( ( sock.v6only == 0 ) || v6.b_bool ) )
272             return sock.i_handle;
273     }
274
275     if( !v6.b_bool )
276     {
277         int fd6 = sock.i_handle;
278
279         /* also try IPv4 (unless IPv6 forced) */
280         private = p_this->p_private;
281         p_this->p_private = (void*)&sock;
282         p_network = module_Need( p_this, "network", "ipv4", VLC_TRUE );
283
284         if( p_network != NULL )
285             module_Unneed( p_this, p_network );
286
287         p_this->p_private = private;
288
289         if( fd6 != -1 )
290         {
291             if( sock.i_handle != -1 )
292             {
293                 msg_Warn( p_this, "net: lame IPv6/IPv4 dual-stack present. "
294                                   "Using only IPv4." );
295                 net_Close( fd6 );
296             }
297             else
298                 sock.i_handle = fd6;
299         }
300     }
301
302     if( sock.i_handle == -1 )
303         msg_Dbg( p_this, "net: connection to '[%s]:%d@[%s]:%d' failed",
304                 psz_server, i_server, psz_bind, i_bind );
305
306     return sock.i_handle;
307 }