]> git.sesse.net Git - vlc/blob - src/network/udp.c
- Support for setting DSCP via dscp option
[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_SetMcastOutIface (int fd, int family, int scope)
115 {
116     switch (family)
117     {
118 #ifdef IPV6_MULTICAST_IF
119         case AF_INET6:
120             return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
121                                &scope, sizeof (scope));
122 #endif
123
124 #ifdef __linux__
125         case AF_INET:
126         {
127             struct ip_mreqn req = { .imr_ifindex = scope };
128
129             return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
130                                sizeof (req));
131         }
132 #endif
133     }
134
135     errno = EAFNOSUPPORT;
136     return -1;
137 }
138
139
140 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
141 {
142 #ifdef IP_MULTICAST_IF
143     return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
144 #else
145     errno = EAFNOSUPPORT;
146     return -1;
147 #endif
148 }
149
150
151 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
152                             const char *iface, const char *addr)
153 {
154     if (iface != NULL)
155     {
156         int scope = if_nametoindex (iface);
157         if (scope == 0)
158         {
159             msg_Err (p_this, "%s: invalid interface for multicast", iface);
160             return -1;
161         }
162
163         if (net_SetMcastOutIface (fd, family, scope) == 0)
164             return 0;
165
166         msg_Err (p_this, "%s: %s", iface, net_strerror (net_errno));
167     }
168
169     if (addr != NULL)
170     {
171         if (family == AF_INET)
172         {
173             struct in_addr ipv4;
174             if (inet_pton (AF_INET, addr, &ipv4) <= 0)
175             {
176                 msg_Err (p_this, "%s: invalid IPv4 address for multicast",
177                          addr);
178                 return -1;
179             }
180
181             if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
182                 return 0;
183
184             msg_Err (p_this, "%s: %s", addr, net_strerror (net_errno));
185         }
186     }
187
188     return -1;
189 }
190
191
192 int net_SetDSCP( int fd, uint8_t dscp )
193 {
194     struct sockaddr_storage addr;
195     if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
196         return -1;
197
198     int level, cmd;
199
200     switch( addr.ss_family )
201     {
202 #ifdef IPV6_TCLASS
203         case AF_INET6:
204             level = SOL_IPV6;
205             cmd = IPV6_TCLASS;
206             break;
207 #endif
208
209         case AF_INET:
210             level = SOL_IP;
211             cmd = IP_TOS;
212             break;
213
214         default:
215 #ifdef ENOPROTOOPT
216             errno = ENOPROTOOPT;
217 #endif
218             return -1;
219     }
220
221     return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
222 }
223
224
225 /*****************************************************************************
226  * __net_ConnectUDP:
227  *****************************************************************************
228  * Open a UDP socket to send data to a defined destination, with an optional
229  * hop limit.
230  *****************************************************************************/
231 int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
232                       int i_hlim )
233 {
234     struct addrinfo hints, *res, *ptr;
235     int             i_val, i_handle = -1;
236     vlc_bool_t      b_unreach = VLC_FALSE;
237
238     if( i_port == 0 )
239         i_port = 1234; /* historical VLC thing */
240
241     if( i_hlim < 1 )
242         i_hlim = var_CreateGetInteger( p_this, "ttl" );
243
244     memset( &hints, 0, sizeof( hints ) );
245     hints.ai_socktype = SOCK_DGRAM;
246
247     msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
248
249     i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
250     if( i_val )
251     {
252         msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
253                  vlc_gai_strerror( i_val ) );
254         return -1;
255     }
256
257     for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
258     {
259         char *str;
260         int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
261                              ptr->ai_protocol);
262         if (fd == -1)
263             continue;
264
265 #if !defined( SYS_BEOS )
266         /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
267         * to avoid packet loss caused by scheduling problems */
268         setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
269         setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
270
271         /* Allow broadcast sending */
272         setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
273 #endif
274
275         if( i_hlim > 0 )
276             net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
277
278         str = var_CreateGetString (p_this, "miface");
279         if (str != NULL)
280         {
281             net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
282             free (str);
283         }
284
285         str = var_CreateGetString (p_this, "miface-addr");
286         if (str != NULL)
287         {
288             net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
289             free (str);
290         }
291
292         net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
293
294         if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
295         {
296             /* success */
297             i_handle = fd;
298             break;
299         }
300
301 #if defined( WIN32 ) || defined( UNDER_CE )
302         if( WSAGetLastError () == WSAENETUNREACH )
303 #else
304         if( errno == ENETUNREACH )
305 #endif
306             b_unreach = VLC_TRUE;
307         else
308         {
309             msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
310                       strerror( errno ) );
311             net_Close( fd );
312             continue;
313         }
314     }
315
316     vlc_freeaddrinfo( res );
317
318     if( i_handle == -1 )
319     {
320         if( b_unreach )
321             msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
322                      i_port );
323         return -1;
324     }
325
326     return i_handle;
327 }
328
329 /*****************************************************************************
330  * __net_OpenUDP:
331  *****************************************************************************
332  * Open a UDP connection and return a handle
333  *****************************************************************************/
334 int __net_OpenUDP( vlc_object_t *p_this, const char *psz_bind, int i_bind,
335                    const char *psz_server, int i_server )
336 {
337     vlc_value_t      v4, v6;
338     void            *private;
339     network_socket_t sock;
340     module_t         *p_network = NULL;
341
342 /*    if( ( psz_server != NULL ) && ( psz_server[0] == '\0' ) )
343         msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
344                   "is obsolete - use net_ConnectUDP instead" );
345     if( i_server != 0 )
346         msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
347                   "port is obsolete - use __net_ConnectUDP instead" );*/
348
349     if( psz_server == NULL ) psz_server = "";
350     if( psz_bind == NULL ) psz_bind = "";
351
352     /* Prepare the network_socket_t structure */
353     sock.psz_bind_addr   = psz_bind;
354     sock.i_bind_port     = i_bind;
355     sock.psz_server_addr = psz_server;
356     sock.i_server_port   = i_server;
357     sock.i_ttl           = 0;
358     sock.v6only          = 0;
359     sock.i_handle        = -1;
360
361     msg_Dbg( p_this, "net: connecting to '[%s]:%d@[%s]:%d'",
362              psz_server, i_server, psz_bind, i_bind );
363
364     /* Check if we have force ipv4 or ipv6 */
365     var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
366     var_Get( p_this, "ipv4", &v4 );
367     var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
368     var_Get( p_this, "ipv6", &v6 );
369
370     if( !v4.b_bool )
371     {
372         if( v6.b_bool )
373             sock.v6only = 1;
374
375         /* try IPv6 first (unless IPv4 forced) */
376         private = p_this->p_private;
377         p_this->p_private = (void*)&sock;
378         p_network = module_Need( p_this, "network", "ipv6", VLC_TRUE );
379
380         if( p_network != NULL )
381             module_Unneed( p_this, p_network );
382
383         p_this->p_private = private;
384
385         /*
386          * Check if the IP stack can receive IPv4 packets on IPv6 sockets.
387          * If yes, then it is better to use the IPv6 socket.
388          * Otherwise, if we also get an IPv4, we have to choose, so we use
389          * IPv4 only.
390          */
391         if( ( sock.i_handle != -1 ) && ( ( sock.v6only == 0 ) || v6.b_bool ) )
392             return sock.i_handle;
393     }
394
395     if( !v6.b_bool )
396     {
397         int fd6 = sock.i_handle;
398
399         /* also try IPv4 (unless IPv6 forced) */
400         private = p_this->p_private;
401         p_this->p_private = (void*)&sock;
402         p_network = module_Need( p_this, "network", "ipv4", VLC_TRUE );
403
404         if( p_network != NULL )
405             module_Unneed( p_this, p_network );
406
407         p_this->p_private = private;
408
409         if( fd6 != -1 )
410         {
411             if( sock.i_handle != -1 )
412             {
413                 msg_Warn( p_this, "net: lame IPv6/IPv4 dual-stack present, "
414                                   "using only IPv4." );
415                 net_Close( fd6 );
416             }
417             else
418                 sock.i_handle = fd6;
419         }
420     }
421
422     if( sock.i_handle == -1 )
423         msg_Dbg( p_this, "net: connection to '[%s]:%d@[%s]:%d' failed",
424                 psz_server, i_server, psz_bind, i_bind );
425
426     return sock.i_handle;
427 }