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