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