]> git.sesse.net Git - vlc/blob - src/network/udp.c
BeOS fix
[vlc] / src / network / udp.c
1 /*****************************************************************************
2  * udp.c:
3  *****************************************************************************
4  * Copyright (C) 2004-2006 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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_SYS_TIME_H
34 #    include <sys/time.h>
35 #endif
36
37 #include "network.h"
38
39 #ifdef WIN32
40 #   if defined(UNDER_CE)
41 #       undef IP_MULTICAST_TTL
42 #       define IP_MULTICAST_TTL 3
43 #       undef IP_ADD_MEMBERSHIP
44 #       define IP_ADD_MEMBERSHIP 5
45 #   endif
46 #   define EAFNOSUPPORT WSAEAFNOSUPPORT
47 #   define if_nametoindex( str ) atoi( str )
48 #else
49 #   include <unistd.h>
50 #   ifdef HAVE_NET_IF_H
51 #       include <net/if.h>
52 #   endif
53 #endif
54
55 #ifndef SOL_IP
56 # define SOL_IP IPPROTO_IP
57 #endif
58 #ifndef SOL_IPV6
59 # define SOL_IPV6 IPPROTO_IPV6
60 #endif
61 #ifndef IPPROTO_IPV6
62 # define IPPROTO_IPV6 41
63 #endif
64
65 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
66                        int i_protocol );
67
68
69 static int net_SetMcastHopLimit( vlc_object_t *p_this,
70                                  int fd, int family, int hlim )
71 {
72 #ifndef SYS_BEOS
73     int proto, cmd;
74
75     /* There is some confusion in the world whether IP_MULTICAST_TTL 
76      * takes a byte or an int as an argument.
77      * BSD seems to indicate byte so we are going with that and use
78      * int as a fallback to be safe */
79     switch( family )
80     {
81         case AF_INET:
82             proto = SOL_IP;
83             cmd = IP_MULTICAST_TTL;
84             break;
85
86 #ifdef IPV6_MULTICAST_HOPS
87         case AF_INET6:
88             proto = SOL_IPV6;
89             cmd = IPV6_MULTICAST_HOPS;
90             break;
91 #endif
92
93         default:
94             msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
95             return VLC_EGENERIC;
96     }
97
98     if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
99     {
100         /* BSD compatibility */
101         unsigned char buf;
102
103         buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
104         if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
105             return VLC_EGENERIC;
106     }
107 #endif
108     return VLC_SUCCESS;
109 }
110
111
112 static int net_SetMcastIface( vlc_object_t *p_this,
113                               int fd, int family, const char *str )
114 {
115     switch( family )
116     {
117 #ifndef SYS_BEOS
118         case AF_INET:
119         {
120             struct in_addr addr;
121
122             if( inet_pton( AF_INET, str, &addr) <= 0 )
123             {
124                 msg_Err( p_this, "Invalid multicast interface %s", str );
125                 return VLC_EGENERIC;
126             }
127
128             if( setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &addr,
129                             sizeof( addr ) ) < 0 )
130             {
131                 msg_Err( p_this, "Cannot use %s as multicast interface: %s",
132                          str, strerror(errno) );
133                 return VLC_EGENERIC;
134             }
135             break;
136         }
137 #endif /* SYS_BEOS */
138
139 #ifdef IPV6_MULTICAST_IF
140         case AF_INET6:
141         {
142             int scope = if_nametoindex( str );
143
144             if( scope == 0 )
145             {
146                 msg_Err( p_this, "Invalid multicast interface %s", str );
147                 return VLC_EGENERIC;
148             }
149
150             if( setsockopt( fd, SOL_IPV6, IPV6_MULTICAST_IF,
151                             &scope, sizeof( scope ) ) < 0 )
152             {
153                 msg_Err( p_this, "Cannot use %s as multicast interface: %s",
154                          str, strerror( errno ) );
155                 return VLC_EGENERIC;
156             }
157             break;
158         }
159 #endif
160
161         default:
162             msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
163             return VLC_EGENERIC;
164     }
165
166     return VLC_SUCCESS;
167 }
168
169 /*****************************************************************************
170  * __net_ConnectUDP:
171  *****************************************************************************
172  * Open a UDP socket to send data to a defined destination, with an optional
173  * hop limit.
174  *****************************************************************************/
175 int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
176                       int i_hlim )
177 {
178     struct addrinfo hints, *res, *ptr;
179     int             i_val, i_handle = -1;
180     vlc_bool_t      b_unreach = VLC_FALSE;
181
182     if( i_port == 0 )
183         i_port = 1234; /* historical VLC thing */
184
185     if( i_hlim < 1 )
186         i_hlim = var_CreateGetInteger( p_this, "ttl" );
187
188     memset( &hints, 0, sizeof( hints ) );
189     hints.ai_socktype = SOCK_DGRAM;
190
191     msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
192
193     i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
194     if( i_val )
195     {
196         msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
197                  vlc_gai_strerror( i_val ) );
198         return -1;
199     }
200
201     for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
202     {
203         int fd;
204         char *psz_mif;
205
206         fd = net_Socket( p_this, ptr->ai_family, ptr->ai_socktype,
207                          ptr->ai_protocol );
208         if( fd == -1 )
209             continue;
210 #if !defined( SYS_BEOS )
211         else
212         {
213             int i_val;
214
215             /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
216              * to avoid packet loss caused by scheduling problems */
217             i_val = 0x80000;
218             setsockopt( fd, SOL_SOCKET, SO_RCVBUF, (void *)&i_val,
219                         sizeof( i_val ) );
220             i_val = 0x80000;
221             setsockopt( fd, SOL_SOCKET, SO_SNDBUF, (void *)&i_val,
222                         sizeof( i_val ) );
223
224             /* Allow broadcast sending */
225             i_val = 1;
226             setsockopt( fd, SOL_SOCKET, SO_BROADCAST, (void*)&i_val,
227                         sizeof( i_val ) );
228         }
229 #endif
230
231         if( i_hlim > 0 )
232             net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
233         psz_mif = config_GetPsz( p_this, (ptr->ai_family != AF_INET)
234                                             ? "miface" : "miface-addr" );
235         if( psz_mif != NULL )
236         {
237             net_SetMcastIface( p_this, fd, ptr->ai_family, psz_mif );
238             free( psz_mif );
239         }
240
241         if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
242         {
243             /* success */
244             i_handle = fd;
245             break;
246         }
247
248 #if defined( WIN32 ) || defined( UNDER_CE )
249         if( WSAGetLastError () == WSAENETUNREACH )
250 #else
251         if( errno == ENETUNREACH )
252 #endif
253             b_unreach = VLC_TRUE;
254         else
255         {
256             msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
257                       strerror( errno ) );
258             net_Close( fd );
259             continue;
260         }
261     }
262
263     vlc_freeaddrinfo( res );
264
265     if( i_handle == -1 )
266     {
267         if( b_unreach )
268             msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
269                      i_port );
270         return -1;
271     }
272
273     return i_handle;
274 }
275
276 /*****************************************************************************
277  * __net_OpenUDP:
278  *****************************************************************************
279  * Open a UDP connection and return a handle
280  *****************************************************************************/
281 int __net_OpenUDP( vlc_object_t *p_this, const char *psz_bind, int i_bind,
282                    const char *psz_server, int i_server )
283 {
284     vlc_value_t      v4, v6;
285     void            *private;
286     network_socket_t sock;
287     module_t         *p_network = NULL;
288
289     if( ( psz_server != NULL ) && ( psz_server[0] == '\0' ) )
290         msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
291                   "is obsolete - use net_ConnectUDP instead" );
292     if( i_server != 0 )
293         msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
294                   "port is obsolete - use __net_ConnectUDP instead" );
295
296     if( psz_server == NULL ) psz_server = "";
297     if( psz_bind == NULL ) psz_bind = "";
298
299     /* Prepare the network_socket_t structure */
300     sock.psz_bind_addr   = psz_bind;
301     sock.i_bind_port     = i_bind;
302     sock.psz_server_addr = psz_server;
303     sock.i_server_port   = i_server;
304     sock.i_ttl           = 0;
305     sock.v6only          = 0;
306     sock.i_handle        = -1;
307
308     msg_Dbg( p_this, "net: connecting to '[%s]:%d@[%s]:%d'",
309              psz_server, i_server, psz_bind, i_bind );
310
311     /* Check if we have force ipv4 or ipv6 */
312     var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
313     var_Get( p_this, "ipv4", &v4 );
314     var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
315     var_Get( p_this, "ipv6", &v6 );
316
317     if( !v4.b_bool )
318     {
319         if( v6.b_bool )
320             sock.v6only = 1;
321
322         /* try IPv6 first (unless IPv4 forced) */
323         private = p_this->p_private;
324         p_this->p_private = (void*)&sock;
325         p_network = module_Need( p_this, "network", "ipv6", VLC_TRUE );
326
327         if( p_network != NULL )
328             module_Unneed( p_this, p_network );
329
330         p_this->p_private = private;
331
332         /*
333          * Check if the IP stack can receive IPv4 packets on IPv6 sockets.
334          * If yes, then it is better to use the IPv6 socket.
335          * Otherwise, if we also get an IPv4, we have to choose, so we use
336          * IPv4 only.
337          */
338         if( ( sock.i_handle != -1 ) && ( ( sock.v6only == 0 ) || v6.b_bool ) )
339             return sock.i_handle;
340     }
341
342     if( !v6.b_bool )
343     {
344         int fd6 = sock.i_handle;
345
346         /* also try IPv4 (unless IPv6 forced) */
347         private = p_this->p_private;
348         p_this->p_private = (void*)&sock;
349         p_network = module_Need( p_this, "network", "ipv4", VLC_TRUE );
350
351         if( p_network != NULL )
352             module_Unneed( p_this, p_network );
353
354         p_this->p_private = private;
355
356         if( fd6 != -1 )
357         {
358             if( sock.i_handle != -1 )
359             {
360                 msg_Warn( p_this, "net: lame IPv6/IPv4 dual-stack present. "
361                                   "Using only IPv4." );
362                 net_Close( fd6 );
363             }
364             else
365                 sock.i_handle = fd6;
366         }
367     }
368
369     if( sock.i_handle == -1 )
370         msg_Dbg( p_this, "net: connection to '[%s]:%d@[%s]:%d' failed",
371                 psz_server, i_server, psz_bind, i_bind );
372
373     return sock.i_handle;
374 }