]> git.sesse.net Git - vlc/blob - src/network/udp.c
- Only use IPV6_V6ONLY when it makes sense (i.e. receiving datagram socket)
[vlc] / src / network / udp.c
1 /*****************************************************************************
2  * udp.c:
3  *****************************************************************************
4  * Copyright (C) 2004-2006 the VideoLAN team
5  * Copyright © 2006-2007 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 <vlc_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 static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
71                              int family, int protocol)
72 {
73     struct addrinfo hints, *res;
74
75     memset (&hints, 0, sizeof( hints ));
76     hints.ai_family = family;
77     hints.ai_socktype = SOCK_DGRAM;
78     hints.ai_flags = AI_PASSIVE;
79
80     if (host && !*host)
81         host = NULL;
82
83     msg_Dbg (obj, "net: opening %s datagram port %d", host ?: "any", port);
84
85     int val = vlc_getaddrinfo (obj, host, port, &hints, &res);
86     if (val)
87     {
88         msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
89                  vlc_gai_strerror (val));
90         return -1;
91     }
92
93     val = -1;
94
95     for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
96     {
97         int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
98                              protocol ?: ptr->ai_protocol);
99         if (fd == -1)
100         {
101             msg_Dbg (obj, "socket error: %s", net_strerror (net_errno));
102             continue;
103         }
104
105         if (ptr->ai_next != NULL)
106         {
107 #ifdef IPV6_V6ONLY
108             if ((ptr->ai_family != AF_INET6)
109              || setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &(int){ 0 },
110                             sizeof (int)))
111 #endif
112             {
113                 msg_Err (obj, "Multiple network protocols present");
114                 msg_Err (obj, "Please select network protocol manually");
115             }
116         }
117
118         /* Bind the socket */
119 #if defined (WIN32) || defined (UNDER_CE)
120         if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
121          && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
122         {
123             struct sockaddr_in6 dumb =
124             {
125                 .sin6_family = ptr->ai_addr->sa_family,
126                 .sin6_port =  ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
127             };
128             bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
129         }
130         else
131 #endif
132         if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
133         {
134             msg_Err (obj, "socket bind error (%s)", net_strerror (net_errno));
135             net_Close (fd);
136             continue;
137         }
138
139         if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
140          && net_Subscribe (obj, fd, ptr->ai_addr, ptr->ai_addrlen))
141         {
142             net_Close (fd);
143             continue;
144         }
145
146         val = fd;
147         break;
148     }
149
150     vlc_freeaddrinfo (res);
151     return val;
152 }
153
154
155 static int net_SetMcastHopLimit( vlc_object_t *p_this,
156                                  int fd, int family, int hlim )
157 {
158     int proto, cmd;
159
160     /* There is some confusion in the world whether IP_MULTICAST_TTL 
161      * takes a byte or an int as an argument.
162      * BSD seems to indicate byte so we are going with that and use
163      * int as a fallback to be safe */
164     switch( family )
165     {
166 #ifdef IP_MULTICAST_TTL
167         case AF_INET:
168             proto = SOL_IP;
169             cmd = IP_MULTICAST_TTL;
170             break;
171 #endif
172
173 #ifdef IPV6_MULTICAST_HOPS
174         case AF_INET6:
175             proto = SOL_IPV6;
176             cmd = IPV6_MULTICAST_HOPS;
177             break;
178 #endif
179
180         default:
181             msg_Warn( p_this, "%s", strerror( EAFNOSUPPORT ) );
182             return VLC_EGENERIC;
183     }
184
185     if( setsockopt( fd, proto, cmd, &hlim, sizeof( hlim ) ) < 0 )
186     {
187         /* BSD compatibility */
188         unsigned char buf;
189
190         buf = (unsigned char)(( hlim > 255 ) ? 255 : hlim);
191         if( setsockopt( fd, proto, cmd, &buf, sizeof( buf ) ) )
192             return VLC_EGENERIC;
193     }
194
195     return VLC_SUCCESS;
196 }
197
198
199 static int net_SetMcastOutIface (int fd, int family, int scope)
200 {
201     switch (family)
202     {
203 #ifdef IPV6_MULTICAST_IF
204         case AF_INET6:
205             return setsockopt (fd, SOL_IPV6, IPV6_MULTICAST_IF,
206                                &scope, sizeof (scope));
207 #endif
208
209 #ifdef __linux__
210         case AF_INET:
211         {
212             struct ip_mreqn req = { .imr_ifindex = scope };
213
214             return setsockopt (fd, SOL_IP, IP_MULTICAST_IF, &req,
215                                sizeof (req));
216         }
217 #endif
218     }
219
220     errno = EAFNOSUPPORT;
221     return -1;
222 }
223
224
225 static inline int net_SetMcastOutIPv4 (int fd, struct in_addr ipv4)
226 {
227 #ifdef IP_MULTICAST_IF
228     return setsockopt( fd, SOL_IP, IP_MULTICAST_IF, &ipv4, sizeof (ipv4));
229 #else
230     errno = EAFNOSUPPORT;
231     return -1;
232 #endif
233 }
234
235
236 static int net_SetMcastOut (vlc_object_t *p_this, int fd, int family,
237                             const char *iface, const char *addr)
238 {
239     if (iface != NULL)
240     {
241         int scope = if_nametoindex (iface);
242         if (scope == 0)
243         {
244             msg_Err (p_this, "invalid multicast interface: %s", iface);
245             return -1;
246         }
247
248         if (net_SetMcastOutIface (fd, family, scope) == 0)
249             return 0;
250
251         msg_Err (p_this, "%s: %s", iface, net_strerror (net_errno));
252     }
253
254     if (addr != NULL)
255     {
256         if (family == AF_INET)
257         {
258             struct in_addr ipv4;
259             if (inet_pton (AF_INET, addr, &ipv4) <= 0)
260             {
261                 msg_Err (p_this, "invalid IPv4 address for multicast: %s",
262                          addr);
263                 return -1;
264             }
265
266             if (net_SetMcastOutIPv4 (fd, ipv4) == 0)
267                 return 0;
268
269             msg_Err (p_this, "%s: %s", addr, net_strerror (net_errno));
270         }
271     }
272
273     return -1;
274 }
275
276
277 /**
278  * Old-style any-source multicast join.
279  * In use on Windows XP/2003 and older.
280  */
281 static int
282 net_IPv4Join (vlc_object_t *obj, int fd,
283               const struct sockaddr_in *src, const struct sockaddr_in *grp)
284 {
285 #ifdef IP_ADD_MEMBERSHIP
286     union
287     {
288         struct ip_mreq gr4;
289 # ifdef IP_ADD_SOURCE_MEMBERSHIP
290         struct ip_mreq_source gsr4;
291 # endif
292     } opt;
293     int cmd;
294     struct in_addr id = { .s_addr = INADDR_ANY };
295     socklen_t optlen;
296
297     /* Multicast interface IPv4 address */
298     char *iface = var_CreateGetString (obj, "miface-addr");
299     if (iface != NULL)
300     {
301         if ((*iface)
302          && (inet_pton (AF_INET, iface, &id) <= 0))
303         {
304             msg_Err (obj, "invalid multicast interface address %s", iface);
305             free (iface);
306             goto error;
307         }
308     }
309
310     memset (&opt, 0, sizeof (opt));
311     if (src != NULL)
312     {
313 # ifdef IP_ADD_SOURCE_MEMBERSHIP
314         cmd = IP_ADD_SOURCE_MEMBERSHIP;
315         opt.gsr4.imr_multiaddr = grp->sin_addr;
316         opt.gsr4.imr_sourceaddr = src->sin_addr;
317         opt.gsr4.imr_interface = id;
318         optlen = sizeof (opt.gsr4);
319 # else
320         errno = ENOSYS;
321         goto error;
322 # endif
323     }
324     else
325     {
326         cmd = IP_ADD_MEMBERSHIP;
327         opt.gr4.imr_multiaddr = grp->sin_addr;
328         opt.gr4.imr_interface = id;
329         optlen = sizeof (opt.gr4);
330     }
331
332     msg_Dbg (obj, "IP_ADD_%sMEMBERSHIP multicast request",
333              (src != NULL) ? "SOURCE_" : "");
334
335     if (setsockopt (fd, SOL_IP, cmd, &opt, optlen) == 0)
336         return 0;
337
338 error:
339 #endif
340
341     msg_Err (obj, "cannot join IPv4 multicast group (%s)",
342              net_strerror (net_errno));
343     return -1;
344 }
345
346
347 static int
348 net_IPv6Join (vlc_object_t *obj, int fd, const struct sockaddr_in6 *src)
349 {
350 #ifdef IPV6_JOIN_GROUP
351     struct ipv6_mreq gr6;
352     memset (&gr6, 0, sizeof (gr6));
353     gr6.ipv6mr_interface = src->sin6_scope_id;
354     memcpy (&gr6.ipv6mr_multiaddr, &src->sin6_addr, 16);
355
356     msg_Dbg (obj, "IPV6_JOIN_GROUP multicast request");
357
358     if (!setsockopt (fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &gr6, sizeof (gr6)))
359         return 0;
360 #else
361     errno = ENOSYS;
362 #endif
363
364     msg_Err (obj, "cannot join IPv6 any-source multicast group (%s)",
365              net_strerror (net_errno));
366     return -1;
367 }
368
369
370 #if defined (WIN32) && !defined (MCAST_JOIN_SOURCE_GROUP)
371 /*
372  * I hate manual definitions: Error-prone. Portability hell.
373  * Developers shall use UP-TO-DATE compilers. Full point.
374  * If you remove the warning, you remove the whole ifndef.
375  */
376 #  warning Your C headers are out-of-date. Please update.
377
378 #  define MCAST_JOIN_GROUP 41
379 struct group_req
380 {
381     ULONG gr_interface;
382     struct sockaddr_storage gr_group;
383 };
384
385 #  define MCAST_JOIN_SOURCE_GROUP 45 /* from <ws2ipdef.h> */
386 struct group_source_req
387 {
388     uint32_t gsr_interface;
389     struct sockaddr_storage gsr_group;
390     struct sockaddr_storage gsr_source;
391 };
392 #endif
393
394 /**
395  * IP-agnostic multicast join,
396  * with fallback to old APIs, and fallback from SSM to ASM.
397  */
398 static int
399 net_SourceSubscribe (vlc_object_t *obj, int fd,
400                      const struct sockaddr *src, socklen_t srclen,
401                      const struct sockaddr *grp, socklen_t grplen)
402 {
403     int level, iid = 0;
404
405     char *iface = var_CreateGetString (obj, "miface");
406     if (iface != NULL)
407     {
408         if (*iface)
409         {
410             iid = if_nametoindex (iface);
411             if (iid == 0)
412             {
413                 msg_Err (obj, "invalid multicast interface: %s", iface);
414                 free (iface);
415                 return -1;
416             }
417         }
418         free (iface);
419     }
420
421     switch (grp->sa_family)
422     {
423 #ifdef AF_INET6
424         case AF_INET6:
425             level = SOL_IPV6;
426             if (((const struct sockaddr_in6 *)grp)->sin6_scope_id)
427                 iid = ((const struct sockaddr_in6 *)grp)->sin6_scope_id;
428             break;
429 #endif
430
431         case AF_INET:
432             level = SOL_IP;
433             break;
434
435         default:
436             errno = EAFNOSUPPORT;
437             return -1;
438     }
439
440     if (src != NULL)
441         switch (src->sa_family)
442         {
443 #ifdef AF_INET6
444             case AF_INET6:
445                 if (memcmp (&((const struct sockaddr_in6 *)src)->sin6_addr,
446                             &in6addr_any, sizeof (in6addr_any)) == 0)
447                     src = NULL;
448             break;
449 #endif
450
451             case AF_INET:
452                 if (((const struct sockaddr_in *)src)->sin_addr.s_addr
453                      == INADDR_ANY)
454                     src = NULL;
455                 break;
456         }
457
458
459     /* Agnostic ASM/SSM multicast join */
460 #ifdef MCAST_JOIN_SOURCE_GROUP
461     union
462     {
463         struct group_req gr;
464         struct group_source_req gsr;
465     } opt;
466     socklen_t optlen;
467
468     memset (&opt, 0, sizeof (opt));
469
470     if (src != NULL)
471     {
472         if ((grplen > sizeof (opt.gsr.gsr_group))
473          || (srclen > sizeof (opt.gsr.gsr_source)))
474             return -1;
475
476         opt.gsr.gsr_interface = iid;
477         memcpy (&opt.gsr.gsr_source, src, srclen);
478         memcpy (&opt.gsr.gsr_group,  grp, grplen);
479         optlen = sizeof (opt.gsr);
480     }
481     else
482     {
483         if (grplen > sizeof (opt.gr.gr_group))
484             return -1;
485
486         opt.gr.gr_interface = iid;
487         memcpy (&opt.gr.gr_group, grp, grplen);
488         optlen = sizeof (opt.gr);
489     }
490
491     msg_Dbg (obj, "Multicast %sgroup join request", src ? "source " : "");
492
493     if (setsockopt (fd, level, 
494                     src ? MCAST_JOIN_SOURCE_GROUP : MCAST_JOIN_GROUP,
495                     (void *)&opt, optlen) == 0)
496         return 0;
497 #endif
498
499     /* Fallback to IPv-specific APIs */
500     if ((src != NULL) && (src->sa_family != grp->sa_family))
501         return -1;
502
503     switch (grp->sa_family)
504     {
505         case AF_INET:
506             if ((grplen < sizeof (struct sockaddr_in))
507              || ((src != NULL) && (srclen < sizeof (struct sockaddr_in))))
508                 return -1;
509
510             if (net_IPv4Join (obj, fd, (const struct sockaddr_in *)src,
511                               (const struct sockaddr_in *)grp) == 0)
512                 return 0;
513             break;
514
515 #ifdef AF_INET6
516         case AF_INET6:
517             if ((grplen < sizeof (struct sockaddr_in6))
518              || ((src != NULL) && (srclen < sizeof (struct sockaddr_in6))))
519                 return -1;
520
521             /* IPv6-specific SSM API does not exist. So if we're here
522              * it means IPv6 SSM is not supported on this OS and we
523              * directly fallback to ASM */
524
525             if (net_IPv6Join (obj, fd, (const struct sockaddr_in6 *)grp) == 0)
526                 return 0;
527             break;
528 #endif
529     }
530
531     msg_Err (obj, "Multicast group join error (%s)",
532              net_strerror (net_errno));
533
534     if (src != NULL)
535     {
536         msg_Warn (obj, "Trying ASM instead of SSM...");
537         return net_Subscribe (obj, fd, grp, grplen);
538     }
539
540     msg_Err (obj, "Multicast not supported");
541     return -1;
542 }
543
544
545 int net_Subscribe (vlc_object_t *obj, int fd,
546                    const struct sockaddr *addr, socklen_t addrlen)
547 {
548     return net_SourceSubscribe (obj, fd, NULL, 0, addr, addrlen);
549 }
550
551
552 int net_SetDSCP( int fd, uint8_t dscp )
553 {
554     struct sockaddr_storage addr;
555     if( getsockname( fd, (struct sockaddr *)&addr, &(socklen_t){ sizeof (addr) }) )
556         return -1;
557
558     int level, cmd;
559
560     switch( addr.ss_family )
561     {
562 #ifdef IPV6_TCLASS
563         case AF_INET6:
564             level = SOL_IPV6;
565             cmd = IPV6_TCLASS;
566             break;
567 #endif
568
569         case AF_INET:
570             level = SOL_IP;
571             cmd = IP_TOS;
572             break;
573
574         default:
575 #ifdef ENOPROTOOPT
576             errno = ENOPROTOOPT;
577 #endif
578             return -1;
579     }
580
581     return setsockopt( fd, level, cmd, &(int){ dscp }, sizeof (int));
582 }
583
584
585 /*****************************************************************************
586  * __net_ConnectDgram:
587  *****************************************************************************
588  * Open a datagram socket to send data to a defined destination, with an
589  * optional hop limit.
590  *****************************************************************************/
591 int __net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
592                         int i_hlim, int proto )
593 {
594     struct addrinfo hints, *res, *ptr;
595     int             i_val, i_handle = -1;
596     vlc_bool_t      b_unreach = VLC_FALSE;
597
598     if( i_port == 0 )
599         i_port = 1234; /* historical VLC thing */
600
601     if( i_hlim < 1 )
602         i_hlim = var_CreateGetInteger( p_this, "ttl" );
603
604     memset( &hints, 0, sizeof( hints ) );
605     hints.ai_socktype = SOCK_DGRAM;
606
607     msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
608
609     i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
610     if( i_val )
611     {
612         msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
613                  vlc_gai_strerror( i_val ) );
614         return -1;
615     }
616
617     for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
618     {
619         char *str;
620         int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
621                              proto ?: ptr->ai_protocol);
622         if (fd == -1)
623             continue;
624
625 #if !defined( SYS_BEOS )
626         /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
627         * to avoid packet loss caused by scheduling problems */
628         setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0x80000 }, sizeof (int));
629         setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &(int){ 0x80000 }, sizeof (int));
630
631         /* Allow broadcast sending */
632         setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &(int){ 1 }, sizeof (int));
633 #endif
634
635         if( i_hlim > 0 )
636             net_SetMcastHopLimit( p_this, fd, ptr->ai_family, i_hlim );
637
638         str = var_CreateGetString (p_this, "miface");
639         if (str != NULL)
640         {
641             if (*str)
642                 net_SetMcastOut (p_this, fd, ptr->ai_family, str, NULL);
643             free (str);
644         }
645
646         str = var_CreateGetString (p_this, "miface-addr");
647         if (str != NULL)
648         {
649             if (*str)
650                 net_SetMcastOut (p_this, fd, ptr->ai_family, NULL, str);
651             free (str);
652         }
653
654         net_SetDSCP (fd, var_CreateGetInteger (p_this, "dscp"));
655
656         if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
657         {
658             /* success */
659             i_handle = fd;
660             break;
661         }
662
663 #if defined( WIN32 ) || defined( UNDER_CE )
664         if( WSAGetLastError () == WSAENETUNREACH )
665 #else
666         if( errno == ENETUNREACH )
667 #endif
668             b_unreach = VLC_TRUE;
669         else
670         {
671             msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
672                       strerror( errno ) );
673             net_Close( fd );
674             continue;
675         }
676     }
677
678     vlc_freeaddrinfo( res );
679
680     if( i_handle == -1 )
681     {
682         if( b_unreach )
683             msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
684                      i_port );
685         return -1;
686     }
687
688     return i_handle;
689 }
690
691
692 /*****************************************************************************
693  * __net_OpenDgram:
694  *****************************************************************************
695  * OpenDgram a datagram socket and return a handle
696  *****************************************************************************/
697 int __net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
698                      const char *psz_server, int i_server,
699                      int family, int protocol )
700 {
701     if ((psz_server == NULL) || (psz_server[0] == '\0'))
702         return net_ListenSingle (obj, psz_bind, i_bind, family, protocol);
703
704     msg_Dbg (obj, "net: connecting to [%s]:%d from [%s]:%d",
705              psz_server, i_server, psz_bind, i_bind);
706
707     struct addrinfo hints, *loc, *rem;
708     int val;
709
710     memset (&hints, 0, sizeof (hints));
711     hints.ai_family = family;
712     hints.ai_socktype = SOCK_DGRAM;
713     hints.ai_flags = AI_PASSIVE;
714
715     val = vlc_getaddrinfo (obj, psz_server, i_server, &hints, &rem);
716     if (val)
717     {
718         msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
719                  vlc_gai_strerror (val));
720         return -1;
721     }
722
723     val = vlc_getaddrinfo (obj, psz_bind, i_bind, &hints, &loc);
724     if (val)
725     {
726         msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
727                  vlc_gai_strerror (val));
728         vlc_freeaddrinfo (rem);
729         return -1;
730     }
731
732     for (struct addrinfo *ptr = loc; ptr != NULL; ptr = ptr->ai_next)
733     {
734         int fd = net_Socket (obj, ptr->ai_family, ptr->ai_socktype,
735                              protocol ?: ptr->ai_protocol);
736         if (fd == -1)
737             continue; // usually, address family not supported
738
739 #ifdef SO_REUSEPORT
740         setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof (int));
741 #endif
742
743 #ifdef SO_RCVBUF
744         /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s)
745          * to avoid packet loss caused in case of scheduling hiccups */
746         setsockopt (fd, SOL_SOCKET, SO_RCVBUF,
747                     (void *)&(int){ 0x80000 }, sizeof (int));
748         setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
749                     (void *)&(int){ 0x80000 }, sizeof (int));
750 #endif
751
752 #if defined (WIN32) || defined (UNDER_CE)
753         if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
754          && (sizeof (struct sockaddr_storage) >= ptr->ai_addrlen))
755         {
756             // This works for IPv4 too - don't worry!
757             struct sockaddr_in6 dumb =
758             {
759                 .sin6_family = ptr->ai_addr->sa_family,
760                 .sin6_port =  ((struct sockaddr_in *)(ptr->ai_addr))->sin_port
761             };
762
763             bind (fd, (struct sockaddr *)&dumb, ptr->ai_addrlen);
764         }
765         else
766 #endif
767         if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
768         {
769             net_Close (fd);
770             continue;
771         }
772
773         val = -1;
774         for (struct addrinfo *ptr2 = rem; ptr2 != NULL; ptr2 = ptr2->ai_next)
775         {
776             if ((ptr2->ai_family != ptr->ai_family)
777              || (ptr2->ai_socktype != ptr->ai_socktype)
778              || (ptr2->ai_protocol != ptr->ai_protocol))
779                 continue;
780
781             if (net_SockAddrIsMulticast (ptr->ai_addr, ptr->ai_addrlen)
782               ? net_SourceSubscribe (obj, fd,
783                                      ptr2->ai_addr, ptr2->ai_addrlen,
784                                      ptr->ai_addr, ptr->ai_addrlen)
785               : connect (fd, ptr2->ai_addr, ptr2->ai_addrlen))
786             {
787                 msg_Err (obj, "cannot connect to %s port %d: %s",
788                          psz_server, i_server, net_strerror (net_errno));
789                 continue;
790             }
791             val = fd;
792             break;
793         }
794
795         if (val != -1)
796             break;
797
798         close (fd);
799     }
800
801     vlc_freeaddrinfo (rem);
802     vlc_freeaddrinfo (loc);
803     return val;
804 }
805