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