]> git.sesse.net Git - vlc/blob - src/network/udp.c
Use net_ConnectUDP
[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 #   define EAFNOSUPPORT WSAEAFNOSUPPORT
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_SetMcastSource( vlc_object_t *p_this,
113                                 int fd, int family, const char *str )
114 {
115 #ifndef SYS_BEOS
116     switch( family )
117     {
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",
125                          str );
126                 return VLC_EGENERIC;
127             }
128
129             if( setsockopt( fd, IPPROTO_IP, IP_MULTICAST_IF, &addr,
130                             sizeof( addr ) ) < 0 )
131             {
132                 msg_Dbg( p_this, "Cannot set multicast interface (%s)",
133                          strerror(errno) );
134                 return VLC_EGENERIC;
135             }
136             break;
137         }
138
139 #ifdef IPV6_MULTICAST_IF
140 /* FIXME: TODO */
141 #endif
142         default:
143             msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
144             return VLC_EGENERIC;
145     }
146
147 #endif
148     return VLC_SUCCESS;
149 }
150
151 /*****************************************************************************
152  * __net_ConnectUDP:
153  *****************************************************************************
154  * Open a UDP socket to send data to a defined destination, with an optional
155  * hop limit.
156  *****************************************************************************/
157 int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
158                       int i_hlim )
159 {
160     struct addrinfo hints, *res, *ptr;
161     int             i_val, i_handle = -1;
162     vlc_bool_t      b_unreach = VLC_FALSE;
163
164     if( i_port == 0 )
165         i_port = 1234; /* historical VLC thing */
166
167     if( i_hlim < 1 )
168     {
169         i_hlim = var_CreateGetInteger( p_this, "ttl" );
170         if( i_hlim < 1 )
171             i_hlim = 1;
172     }
173
174     memset( &hints, 0, sizeof( hints ) );
175     hints.ai_socktype = SOCK_DGRAM;
176
177     msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
178
179     i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
180     if( i_val )
181     {
182         msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
183                  vlc_gai_strerror( i_val ) );
184         return -1;
185     }
186
187     for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
188     {
189         int fd;
190         char *psz_mif_addr;
191
192         fd = net_Socket( p_this, ptr->ai_family, ptr->ai_socktype,
193                          ptr->ai_protocol );
194         if( fd == -1 )
195             continue;
196 #if !defined( SYS_BEOS )
197         else
198         {
199             int i_val;
200
201             /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
202              * to avoid packet loss caused by scheduling problems */
203             i_val = 0x80000;
204             setsockopt( fd, SOL_SOCKET, SO_RCVBUF, (void *)&i_val,
205                         sizeof( i_val ) );
206             i_val = 0x80000;
207             setsockopt( fd, SOL_SOCKET, SO_SNDBUF, (void *)&i_val,
208                         sizeof( i_val ) );
209
210             /* Allow broadcast sending */
211             i_val = 1;
212             setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*)&i_val,
213                         sizeof( i_val ) );
214         }
215 #endif
216
217         net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
218         psz_mif_addr = config_GetPsz( p_this, "miface-addr" );
219         if( psz_mif_addr != NULL )
220         {
221             net_SetMcastSource( p_this, fd, ptr->ai_family, psz_mif_addr );
222             free( psz_mif_addr );
223         }
224
225         if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
226         {
227             /* success */
228             i_handle = fd;
229             break;
230         }
231
232 #if defined( WIN32 ) || defined( UNDER_CE )
233         if( WSAGetLastError () == WSAENETUNREACH )
234 #else
235         if( errno == ENETUNREACH )
236 #endif
237             b_unreach = VLC_TRUE;
238         else
239         {
240             msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
241                       strerror( errno ) );
242             net_Close( fd );
243             continue;
244         }
245     }
246
247     vlc_freeaddrinfo( res );
248
249     if( i_handle == -1 )
250     {
251         if( b_unreach )
252             msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
253                      i_port );
254         return -1;
255     }
256
257     return i_handle;
258 }
259
260 /*****************************************************************************
261  * __net_OpenUDP:
262  *****************************************************************************
263  * Open a UDP connection and return a handle
264  *****************************************************************************/
265 int __net_OpenUDP( vlc_object_t *p_this, const char *psz_bind, int i_bind,
266                    const char *psz_server, int i_server )
267 {
268     vlc_value_t      v4, v6;
269     void            *private;
270     network_socket_t sock;
271     module_t         *p_network = NULL;
272
273     if( ( psz_server != NULL ) && ( psz_server[0] == '\0' ) )
274         msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
275                   "is obsolete - use net_ConnectUDP instead" );
276     if( i_server != 0 )
277         msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
278                   "port is obsolete - use __net_ConnectUDP instead" );
279
280     if( psz_server == NULL ) psz_server = "";
281     if( psz_bind == NULL ) psz_bind = "";
282
283     /* Prepare the network_socket_t structure */
284     sock.psz_bind_addr   = psz_bind;
285     sock.i_bind_port     = i_bind;
286     sock.psz_server_addr = psz_server;
287     sock.i_server_port   = i_server;
288     sock.i_ttl           = 0;
289     sock.v6only          = 0;
290     sock.i_handle        = -1;
291
292     msg_Dbg( p_this, "net: connecting to '[%s]:%d@[%s]:%d'",
293              psz_server, i_server, psz_bind, i_bind );
294
295     /* Check if we have force ipv4 or ipv6 */
296     var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
297     var_Get( p_this, "ipv4", &v4 );
298     var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
299     var_Get( p_this, "ipv6", &v6 );
300
301     if( !v4.b_bool )
302     {
303         if( v6.b_bool )
304             sock.v6only = 1;
305
306         /* try IPv6 first (unless IPv4 forced) */
307         private = p_this->p_private;
308         p_this->p_private = (void*)&sock;
309         p_network = module_Need( p_this, "network", "ipv6", VLC_TRUE );
310
311         if( p_network != NULL )
312             module_Unneed( p_this, p_network );
313
314         p_this->p_private = private;
315
316         /*
317          * Check if the IP stack can receive IPv4 packets on IPv6 sockets.
318          * If yes, then it is better to use the IPv6 socket.
319          * Otherwise, if we also get an IPv4, we have to choose, so we use
320          * IPv4 only.
321          */
322         if( ( sock.i_handle != -1 ) && ( ( sock.v6only == 0 ) || v6.b_bool ) )
323             return sock.i_handle;
324     }
325
326     if( !v6.b_bool )
327     {
328         int fd6 = sock.i_handle;
329
330         /* also try IPv4 (unless IPv6 forced) */
331         private = p_this->p_private;
332         p_this->p_private = (void*)&sock;
333         p_network = module_Need( p_this, "network", "ipv4", VLC_TRUE );
334
335         if( p_network != NULL )
336             module_Unneed( p_this, p_network );
337
338         p_this->p_private = private;
339
340         if( fd6 != -1 )
341         {
342             if( sock.i_handle != -1 )
343             {
344                 msg_Warn( p_this, "net: lame IPv6/IPv4 dual-stack present. "
345                                   "Using only IPv4." );
346                 net_Close( fd6 );
347             }
348             else
349                 sock.i_handle = fd6;
350         }
351     }
352
353     if( sock.i_handle == -1 )
354         msg_Dbg( p_this, "net: connection to '[%s]:%d@[%s]:%d' failed",
355                 psz_server, i_server, psz_bind, i_bind );
356
357     return sock.i_handle;
358 }