]> git.sesse.net Git - vlc/blob - src/network/udp.c
Suppress the v6only kludge
[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 #include <assert.h>
35
36 #ifdef HAVE_SYS_TIME_H
37 #    include <sys/time.h>
38 #endif
39
40 #include "network.h"
41
42 #ifdef WIN32
43 #   if defined(UNDER_CE)
44 #       undef IP_MULTICAST_TTL
45 #       define IP_MULTICAST_TTL 3
46 #       undef IP_ADD_MEMBERSHIP
47 #       define IP_ADD_MEMBERSHIP 5
48 #   endif
49 #   define EAFNOSUPPORT WSAEAFNOSUPPORT
50 #   define if_nametoindex( str ) atoi( str )
51 #else
52 #   include <unistd.h>
53 #   ifdef HAVE_NET_IF_H
54 #       include <net/if.h>
55 #   endif
56 #endif
57
58 #ifndef SOL_IP
59 # define SOL_IP IPPROTO_IP
60 #endif
61 #ifndef SOL_IPV6
62 # define SOL_IPV6 IPPROTO_IPV6
63 #endif
64 #ifndef IPPROTO_IPV6
65 # define IPPROTO_IPV6 41
66 #endif
67
68 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
69                        int i_protocol );
70
71
72 static int net_SetMcastHopLimit( vlc_object_t *p_this,
73                                  int fd, int family, int hlim )
74 {
75 #ifndef SYS_BEOS
76     int proto, cmd;
77
78     /* There is some confusion in the world whether IP_MULTICAST_TTL 
79      * takes a byte or an int as an argument.
80      * BSD seems to indicate byte so we are going with that and use
81      * int as a fallback to be safe */
82     switch( family )
83     {
84         case AF_INET:
85             proto = SOL_IP;
86             cmd = IP_MULTICAST_TTL;
87             break;
88
89 #ifdef IPV6_MULTICAST_HOPS
90         case AF_INET6:
91             proto = SOL_IPV6;
92             cmd = IPV6_MULTICAST_HOPS;
93             break;
94 #endif
95
96         default:
97             msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
98             return VLC_EGENERIC;
99     }
100
101     if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
102     {
103         /* BSD compatibility */
104         unsigned char buf;
105
106         buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
107         if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
108             return VLC_EGENERIC;
109     }
110 #endif
111     return VLC_SUCCESS;
112 }
113
114
115 static int net_SetMcastOutIface (int fd, int family, int scope)
116 {
117     switch (family)
118     {
119 #ifdef IPV6_MULTICAST_IF
120         case AF_INET6:
121             return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
122                                &scope, sizeof (scope));
123 #endif
124
125 #ifdef __linux__
126         case AF_INET:
127         {
128             struct ip_mreqn req = { .imr_ifindex = scope };
129
130             return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
131                                sizeof (req));
132         }
133 #endif
134     }
135
136     errno = EAFNOSUPPORT;
137     return -1;
138 }
139
140
141 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
142 {
143 #ifdef IP_MULTICAST_IF
144     return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
145 #else
146     errno = EAFNOSUPPORT;
147     return -1;
148 #endif
149 }
150
151
152 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
153                             const char *iface, const char *addr)
154 {
155     if (iface != NULL)
156     {
157         int scope = if_nametoindex (iface);
158         if (scope == 0)
159         {
160             msg_Err (p_this, "%s: invalid interface for multicast", iface);
161             return -1;
162         }
163
164         if (net_SetMcastOutIface (fd, family, scope) == 0)
165             return 0;
166
167         msg_Err (p_this, "%s: %s", iface, net_strerror (net_errno));
168     }
169
170     if (addr != NULL)
171     {
172         if (family == AF_INET)
173         {
174             struct in_addr ipv4;
175             if (inet_pton (AF_INET, addr, &ipv4) <= 0)
176             {
177                 msg_Err (p_this, "%s: invalid IPv4 address for multicast",
178                          addr);
179                 return -1;
180             }
181
182             if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
183                 return 0;
184
185             msg_Err (p_this, "%s: %s", addr, net_strerror (net_errno));
186         }
187     }
188
189     return -1;
190 }
191
192
193 int net_Subscribe (vlc_object_t *obj, int fd, const struct sockaddr *addr,
194                    socklen_t addrlen)
195 {
196     switch (addr->sa_family)
197     {
198 #ifdef IP_ADD_MEMBERSHIP
199         case AF_INET:
200         {
201             const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
202             if (addrlen < sizeof (*v4))
203                 return -1;
204
205             struct ip_mreq imr;
206             memset (&imr, 0, sizeof (imr));
207             memcpy (&imr.imr_multiaddr, &v4->sin_addr, 4);
208
209             /* FIXME: should use a different option for in and out */
210             char *iif = var_CreateGetString (obj, "miface-addr");
211             if (iif != NULL)
212             {
213                 if ((iif[0] != '\0') &&
214                     (inet_pton (AF_INET, iif, &imr.imr_interface) <= 0))
215                 {
216                     msg_Err (obj, "invalid multicast interface address %s",
217                              iif);
218                     free (iif);
219                     return -1;
220                 }
221                 free (iif);
222             }
223
224             msg_Dbg (obj, "IP_ADD_MEMBERSHIP multicast request");
225
226             if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr,
227                             sizeof (imr)))
228             {
229                 msg_Err (obj, "cannot join IPv4 multicast group (%s)",
230                          net_strerror (net_errno));
231                 return -1;
232             }
233             return 0;
234         }
235 #endif
236
237 #ifdef IPV6_JOIN_GROUP
238         case AF_INET6:
239         {
240             const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
241             if (addrlen < sizeof (*v6))
242                 return -1;
243
244             struct ipv6_mreq imr;
245             memset (&imr, 0, sizeof (imr));
246             imr.ipv6mr_interface = v6->sin6_scope_id;
247             memcpy (&imr.ipv6mr_multiaddr, &v6->sin6_addr, 16);
248
249             msg_Dbg (obj, "IPV6_JOIN_GROUP multicast request");
250
251             if (setsockopt (fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &imr,
252                             sizeof (imr)))
253             {
254                 msg_Err (obj, "cannot join IPv6 multicast group (%s)",
255                          net_strerror (net_errno));
256                 return -1;
257             }
258
259             return 0;
260         }
261 #endif
262     }
263
264     msg_Err (obj, "Multicast not supported");
265     return -1;
266 }
267
268
269 int net_SetDSCP( int fd, uint8_t dscp )
270 {
271     struct sockaddr_storage addr;
272     if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
273         return -1;
274
275     int level, cmd;
276
277     switch( addr.ss_family )
278     {
279 #ifdef IPV6_TCLASS
280         case AF_INET6:
281             level = SOL_IPV6;
282             cmd = IPV6_TCLASS;
283             break;
284 #endif
285
286         case AF_INET:
287             level = SOL_IP;
288             cmd = IP_TOS;
289             break;
290
291         default:
292 #ifdef ENOPROTOOPT
293             errno = ENOPROTOOPT;
294 #endif
295             return -1;
296     }
297
298     return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
299 }
300
301
302 /*****************************************************************************
303  * __net_ConnectUDP:
304  *****************************************************************************
305  * Open a UDP socket to send data to a defined destination, with an optional
306  * hop limit.
307  *****************************************************************************/
308 int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
309                       int i_hlim )
310 {
311     struct addrinfo hints, *res, *ptr;
312     int             i_val, i_handle = -1;
313     vlc_bool_t      b_unreach = VLC_FALSE;
314
315     if( i_port == 0 )
316         i_port = 1234; /* historical VLC thing */
317
318     if( i_hlim < 1 )
319         i_hlim = var_CreateGetInteger( p_this, "ttl" );
320
321     memset( &hints, 0, sizeof( hints ) );
322     hints.ai_socktype = SOCK_DGRAM;
323
324     msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
325
326     i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
327     if( i_val )
328     {
329         msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
330                  vlc_gai_strerror( i_val ) );
331         return -1;
332     }
333
334     for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
335     {
336         char *str;
337         int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
338                              ptr->ai_protocol);
339         if (fd == -1)
340             continue;
341
342 #if !defined( SYS_BEOS )
343         /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
344         * to avoid packet loss caused by scheduling problems */
345         setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
346         setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
347
348         /* Allow broadcast sending */
349         setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
350 #endif
351
352         if( i_hlim > 0 )
353             net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
354
355         str = var_CreateGetString (p_this, "miface");
356         if (str != NULL)
357         {
358             if (*str)
359                 net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
360             free (str);
361         }
362
363         str = var_CreateGetString (p_this, "miface-addr");
364         if (str != NULL)
365         {
366             if (*str)
367                 net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
368             free (str);
369         }
370
371         net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
372
373         if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
374         {
375             /* success */
376             i_handle = fd;
377             break;
378         }
379
380 #if defined( WIN32 ) || defined( UNDER_CE )
381         if( WSAGetLastError () == WSAENETUNREACH )
382 #else
383         if( errno == ENETUNREACH )
384 #endif
385             b_unreach = VLC_TRUE;
386         else
387         {
388             msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
389                       strerror( errno ) );
390             net_Close( fd );
391             continue;
392         }
393     }
394
395     vlc_freeaddrinfo( res );
396
397     if( i_handle == -1 )
398     {
399         if( b_unreach )
400             msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
401                      i_port );
402         return -1;
403     }
404
405     return i_handle;
406 }
407
408
409 /*****************************************************************************
410  * __net_OpenUDP:
411  *****************************************************************************
412  * Open a UDP connection and return a handle
413  *****************************************************************************/
414 int __net_OpenUDP( vlc_object_t *p_this, const char *psz_bind, int i_bind,
415                    const char *psz_server, int i_server )
416 {
417     void            *private;
418     network_socket_t sock;
419     module_t         *p_network = NULL;
420
421     if (((psz_bind == NULL) || (psz_bind[0] == '\0')) && (i_bind == 0))
422     {
423         msg_Warn (p_this,
424                   "Obsolete net_OpenUDP with no local endpoint; "
425                   "Use net_ConnectUDP instead");
426         return net_ConnectUDP (p_this, psz_server, i_server, -1);
427     }
428
429     if (((psz_server == NULL) || (psz_server[0] == '\0')) && (i_server == 0))
430     {
431         msg_Warn (p_this,
432                   "Obsolete net_OpenUDP with no remote endpoint; "
433                   "Use net_ListenUDP instead");
434         return net_ListenUDP1 (p_this, psz_bind, i_bind);
435     }
436
437     if( psz_server == NULL ) psz_server = "";
438     if( psz_bind == NULL ) psz_bind = "";
439
440     /* Prepare the network_socket_t structure */
441     sock.psz_bind_addr   = psz_bind;
442     sock.i_bind_port     = i_bind;
443     sock.psz_server_addr = psz_server;
444     sock.i_server_port   = i_server;
445     sock.i_ttl           = 0;
446     sock.i_handle        = -1;
447
448     msg_Dbg( p_this, "net: connecting to '[%s]:%d@[%s]:%d'",
449              psz_server, i_server, psz_bind, i_bind );
450
451     /* Check if we have force ipv4 or ipv6 */
452     vlc_bool_t v4 = var_CreateGetBool (p_this, "ipv4");
453     vlc_bool_t v6 = var_CreateGetBool (p_this, "ipv6");
454
455     if( !v4 )
456     {
457         /* try IPv6 first (unless IPv4 forced) */
458         private = p_this->p_private;
459         p_this->p_private = (void*)&sock;
460         p_network = module_Need( p_this, "network", "ipv6", VLC_TRUE );
461
462         if( p_network != NULL )
463             module_Unneed( p_this, p_network );
464
465         p_this->p_private = private;
466     }
467
468     if ((sock.i_handle == -1) && !v6)
469     {
470         /* also try IPv4 (unless IPv6 forced) */
471         private = p_this->p_private;
472         p_this->p_private = (void*)&sock;
473         p_network = module_Need( p_this, "network", "ipv4", VLC_TRUE );
474
475         if( p_network != NULL )
476             module_Unneed( p_this, p_network );
477
478         p_this->p_private = private;
479     }
480
481     if( sock.i_handle == -1 )
482         msg_Dbg( p_this, "net: connection to '[%s]:%d@[%s]:%d' failed",
483                 psz_server, i_server, psz_bind, i_bind );
484
485     return sock.i_handle;
486 }