]> git.sesse.net Git - vlc/blob - modules/misc/network/ipv4.c
* backport of [11386]
[vlc] / modules / misc / network / ipv4.c
1 /*****************************************************************************
2  * ipv4.c: IPv4 network abstraction layer
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Mathias Kretschmer <mathias@research.att.com>
9  *          Alexis de Lattre <alexis@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <vlc/vlc.h>
33 #include <errno.h>
34
35 #ifdef HAVE_SYS_TYPES_H
36 #   include <sys/types.h>
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #   include <sys/stat.h>
40 #endif
41 #ifdef HAVE_FCNTL_H
42 #   include <fcntl.h>
43 #endif
44
45 #ifdef HAVE_UNISTD_H
46 #   include <unistd.h>
47 #endif
48
49 #if defined(WIN32) || defined(UNDER_CE)
50 #   if defined(UNDER_CE) && defined(sockaddr_storage)
51 #       undef sockaddr_storage
52 #   endif
53 #   include <winsock2.h>
54 #   include <ws2tcpip.h>
55 #   define close closesocket
56 #   if defined(UNDER_CE)
57 #       undef IP_MULTICAST_TTL
58 #       define IP_MULTICAST_TTL 3
59 #       undef IP_ADD_MEMBERSHIP
60 #       define IP_ADD_MEMBERSHIP 5
61 #   endif
62 #else
63 #   include <netdb.h>                                         /* hostent ... */
64 #   include <sys/socket.h>
65 #   include <netinet/in.h>
66 #   ifdef HAVE_ARPA_INET_H
67 #       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
68 #   endif
69 #endif
70
71 #include "network.h"
72
73 #ifndef INADDR_ANY
74 #   define INADDR_ANY  0x00000000
75 #endif
76 #ifndef INADDR_NONE
77 #   define INADDR_NONE 0xFFFFFFFF
78 #endif
79 #ifndef IN_MULTICAST
80 #   define IN_MULTICAST(a) IN_CLASSD(a)
81 #endif
82 #ifndef PF_INET
83 #    define PF_INET AF_INET                                          /* BeOS */
84 #endif
85
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90 static int NetOpen( vlc_object_t * );
91
92 /*****************************************************************************
93  * Module descriptor
94  *****************************************************************************/
95 #define TIMEOUT_TEXT N_("TCP connection timeout in ms")
96 #define TIMEOUT_LONGTEXT N_( \
97     "Allows you to modify the default TCP connection timeout. This " \
98     "value should be set in millisecond units." )
99
100 #define MIFACE_TEXT N_("Multicast output interface")
101 #define MIFACE_LONGTEXT N_( \
102     "Indicate here the multicast output interface. " \
103     "This overrides the routing table.")
104
105 vlc_module_begin();
106     set_description( _("IPv4 network abstraction layer") );
107     set_capability( "network", 50 );
108     set_category( CAT_INPUT );
109     set_subcategory( SUBCAT_INPUT_ADVANCED );
110     set_callbacks( NetOpen, NULL );
111
112     add_integer( "ipv4-timeout", 5 * 1000, NULL, TIMEOUT_TEXT,
113                  TIMEOUT_LONGTEXT, VLC_TRUE );
114     add_string( "miface-addr", NULL, NULL, MIFACE_TEXT, MIFACE_LONGTEXT, VLC_TRUE );
115 vlc_module_end();
116
117 /*****************************************************************************
118  * BuildAddr: utility function to build a struct sockaddr_in
119  *****************************************************************************/
120 static int BuildAddr( struct sockaddr_in * p_socket,
121                       const char * psz_address, int i_port )
122 {
123     /* Reset struct */
124     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
125     p_socket->sin_family = AF_INET;                                /* family */
126     p_socket->sin_port = htons( (uint16_t)i_port );
127     if( !*psz_address )
128     {
129         p_socket->sin_addr.s_addr = INADDR_ANY;
130     }
131     else
132     {
133         struct hostent    * p_hostent;
134
135         /* Try to convert address directly from in_addr - this will work if
136          * psz_address is dotted decimal. */
137 #ifdef HAVE_ARPA_INET_H
138         if( !inet_aton( psz_address, &p_socket->sin_addr ) )
139 #else
140         p_socket->sin_addr.s_addr = inet_addr( psz_address );
141         if( p_socket->sin_addr.s_addr == INADDR_NONE )
142 #endif
143         {
144             /* We have a fqdn, try to find its address */
145             if ( (p_hostent = gethostbyname( psz_address )) == NULL )
146             {
147                 return( -1 );
148             }
149
150             /* Copy the first address of the host in the socket address */
151             memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
152                      p_hostent->h_length );
153         }
154     }
155     return( 0 );
156 }
157
158 /*****************************************************************************
159  * OpenUDP: open a UDP socket
160  *****************************************************************************
161  * psz_bind_addr, i_bind_port : address and port used for the bind()
162  *   system call. If psz_bind_addr == "", the socket is bound to
163  *   INADDR_ANY and broadcast reception is enabled. If i_bind_port == 0,
164  *   1234 is used. If psz_bind_addr is a multicast (class D) address,
165  *   join the multicast group.
166  * psz_server_addr, i_server_port : address and port used for the connect()
167  *   system call. It can avoid receiving packets from unauthorized IPs.
168  *   Its use leads to great confusion and is currently discouraged.
169  * This function returns -1 in case of error.
170  *****************************************************************************/
171 static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
172 {
173     char * psz_bind_addr = p_socket->psz_bind_addr;
174     int i_bind_port = p_socket->i_bind_port;
175     char * psz_server_addr = p_socket->psz_server_addr;
176     int i_server_port = p_socket->i_server_port;
177
178     int i_handle, i_opt;
179     socklen_t i_opt_size;
180     struct sockaddr_in sock;
181     vlc_value_t val;
182
183     /* If IP_ADD_SOURCE_MEMBERSHIP is not defined in the headers
184        (because it's not in glibc for example), we have to define the
185        headers required for IGMPv3 here */
186 #ifndef IP_ADD_SOURCE_MEMBERSHIP
187     #define IP_ADD_SOURCE_MEMBERSHIP  39
188     struct ip_mreq_source {
189         struct in_addr  imr_multiaddr;
190         struct in_addr  imr_interface;
191         struct in_addr  imr_sourceaddr;
192      };
193 #endif
194
195     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
196      * protocol */
197     if( (i_handle = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
198     {
199 #if defined(WIN32) || defined(UNDER_CE)
200         msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
201 #else
202         msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
203 #endif
204         return( -1 );
205     }
206
207     /* We may want to reuse an already used socket */
208     i_opt = 1;
209     if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
210                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
211     {
212 #if defined(WIN32) || defined(UNDER_CE)
213         msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %i)",
214                   WSAGetLastError() );
215 #else
216         msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR: %s)",
217                           strerror(errno));
218 #endif
219         close( i_handle );
220         return( -1 );
221     }
222
223 #ifdef SO_REUSEPORT
224     i_opt = 1;
225     if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEPORT,
226                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
227     {
228         msg_Warn( p_this, "cannot configure socket (SO_REUSEPORT)" );
229     }
230 #endif
231
232     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
233      * packet loss caused by scheduling problems */
234     i_opt = 0x80000;
235 #if !defined( SYS_BEOS )
236     if( setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *) &i_opt, sizeof( i_opt ) ) == -1 )
237     {
238 #if defined(WIN32) || defined(UNDER_CE)
239         msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %i)",
240                  WSAGetLastError() );
241 #else
242         msg_Dbg( p_this, "cannot configure socket (SO_RCVBUF: %s)",
243                           strerror(errno));
244 #endif
245     }
246 #endif
247
248 #if !defined( SYS_BEOS )
249     /* Check if we really got what we have asked for, because Linux, etc.
250      * will silently limit the max buffer size to net.core.rmem_max which
251      * is typically only 65535 bytes */
252     i_opt = 0;
253     i_opt_size = sizeof( i_opt );
254     if( getsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void*) &i_opt, &i_opt_size ) == -1 )
255     {
256 #if defined(WIN32) || defined(UNDER_CE)
257         msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %i)",
258                   WSAGetLastError() );
259 #else
260         msg_Warn( p_this, "cannot query socket (SO_RCVBUF: %s)",
261                           strerror(errno) );
262 #endif
263     }
264     else if( i_opt < 0x80000 )
265     {
266         msg_Dbg( p_this, "socket buffer size is 0x%x instead of 0x%x",
267                          i_opt, 0x80000 );
268     }
269 #endif
270
271
272     /* Build the local socket */
273
274 #if defined( WIN32 ) || defined( UNDER_CE )
275     /* Under Win32 and for multicasting, we bind to INADDR_ANY,
276      * so let's call BuildAddr with "" instead of psz_bind_addr */
277     if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
278                    "" : psz_bind_addr, i_bind_port ) == -1 )
279 #else
280     if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
281 #endif
282     {
283         msg_Dbg( p_this, "could not build local address" );
284         close( i_handle );
285         return( -1 );
286     }
287
288     /* Bind it */
289     if( bind( i_handle, (struct sockaddr *)&sock, sizeof( sock ) ) < 0 )
290     {
291 #if defined(WIN32) || defined(UNDER_CE)
292         msg_Warn( p_this, "cannot bind socket (%i)", WSAGetLastError() );
293 #else
294         msg_Warn( p_this, "cannot bind socket (%s)", strerror(errno) );
295 #endif
296         close( i_handle );
297         return( -1 );
298     }
299
300 #if defined( WIN32 ) || defined( UNDER_CE )
301     /* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
302     if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
303     {
304         if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
305         {
306             msg_Dbg( p_this, "could not build local address" );
307             close( i_handle );
308             return( -1 );
309         }
310     }
311 #endif
312
313 #if !defined( SYS_BEOS )
314     /* Allow broadcast reception if we bound on INADDR_ANY */
315     if( !*psz_bind_addr )
316     {
317         i_opt = 1;
318         if( setsockopt( i_handle, SOL_SOCKET, SO_BROADCAST, (void*) &i_opt, sizeof( i_opt ) ) == -1 )
319         {
320 #if defined(WIN32) || defined(UNDER_CE)
321             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %i)",
322                       WSAGetLastError() );
323 #else
324             msg_Warn( p_this, "cannot configure socket (SO_BROADCAST: %s)",
325                        strerror(errno) );
326 #endif
327         }
328     }
329 #endif
330
331 #if !defined( SYS_BEOS )
332     /* Join the multicast group if the socket is a multicast address */
333     if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
334     {
335         /* Determine interface to be used for multicast */
336         char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
337
338         /* If we have a source address, we use IP_ADD_SOURCE_MEMBERSHIP
339            so that IGMPv3 aware OSes running on IGMPv3 aware networks
340            will do an IGMPv3 query on the network */
341         if( *psz_server_addr )
342         {
343             struct ip_mreq_source imr;
344
345             imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
346             imr.imr_sourceaddr.s_addr = inet_addr(psz_server_addr);
347
348             if( psz_if_addr != NULL && *psz_if_addr
349                 && inet_addr(psz_if_addr) != INADDR_NONE )
350             {
351                 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
352             }
353             else
354             {
355                 imr.imr_interface.s_addr = INADDR_ANY;
356             }
357             if( psz_if_addr != NULL ) free( psz_if_addr );
358
359             msg_Dbg( p_this, "IP_ADD_SOURCE_MEMBERSHIP multicast request" );
360             /* Join Multicast group with source filter */
361             if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
362                          (char*)&imr,
363                          sizeof(struct ip_mreq_source) ) == -1 )
364             {
365 #if defined(WIN32) || defined(UNDER_CE)
366                 msg_Err( p_this, "failed to join IP multicast group (%i)",
367                          WSAGetLastError() );
368 #else
369                 msg_Err( p_this, "failed to join IP multicast group (%s)",
370                                   strerror(errno) );
371 #endif
372                 msg_Err( p_this, "are you sure your OS supports IGMPv3?" );
373                 close( i_handle );
374                 return( -1 );
375             }
376          }
377          /* If there is no source address, we use IP_ADD_MEMBERSHIP */
378          else
379          {
380              struct ip_mreq imr;
381
382              imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
383              if( psz_if_addr != NULL && *psz_if_addr
384                 && inet_addr(psz_if_addr) != INADDR_NONE )
385             {
386                 imr.imr_interface.s_addr = inet_addr(psz_if_addr);
387             }
388             else
389             {
390                 imr.imr_interface.s_addr = INADDR_ANY;
391             }
392             if( psz_if_addr != NULL ) free( psz_if_addr );
393
394             msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" );
395             /* Join Multicast group without source filter */
396             if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
397                             (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
398             {
399 #if defined(WIN32) || defined(UNDER_CE)
400                 msg_Err( p_this, "failed to join IP multicast group (%i)",
401                          WSAGetLastError() );
402 #else
403                 msg_Err( p_this, "failed to join IP multicast group (%s)",
404                                   strerror(errno) );
405 #endif
406                 close( i_handle );
407                 return( -1 );
408             }
409          }
410     }
411 #endif
412
413     if( *psz_server_addr )
414     {
415         /* Build socket for remote connection */
416         if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
417         {
418             msg_Warn( p_this, "cannot build remote address" );
419             close( i_handle );
420             return( -1 );
421         }
422
423         /* Connect the socket */
424         if( connect( i_handle, (struct sockaddr *) &sock,
425                      sizeof( sock ) ) == (-1) )
426         {
427 #if defined(WIN32) || defined(UNDER_CE)
428             msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
429 #else
430             msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
431 #endif
432             close( i_handle );
433             return( -1 );
434         }
435
436 #if !defined( SYS_BEOS )
437         if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
438         {
439             /* set the time-to-live */
440             int i_ttl = p_socket->i_ttl;
441             unsigned char ttl;
442             
443             /* set the multicast interface */
444             char * psz_mif_addr = config_GetPsz( p_this, "miface-addr" );
445             if( psz_mif_addr )
446             {
447                 struct in_addr intf;
448                 intf.s_addr = inet_addr(psz_mif_addr);
449                 free( psz_mif_addr  );
450
451                 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_IF,
452                                 &intf, sizeof( intf ) ) < 0 )
453                 {
454                     msg_Dbg( p_this, "failed to set multicast interface (%s).", strerror(errno) );
455                     close( i_handle );
456                     return ( -1 );
457                 }
458             }
459
460             if( i_ttl < 1 )
461             {
462                 if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS )
463                 {
464                     var_Create( p_this, "ttl",
465                                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
466                     var_Get( p_this, "ttl", &val );
467                 }
468                 i_ttl = val.i_int;
469             }
470             if( i_ttl < 1 ) i_ttl = 1;
471             ttl = (unsigned char) i_ttl;
472
473             /* There is some confusion in the world whether IP_MULTICAST_TTL 
474              * takes a byte or an int as an argument.
475              * BSD seems to indicate byte so we are going with that and use
476              * int as a fallback to be safe */
477             if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
478                             &ttl, sizeof( ttl ) ) < 0 )
479             {
480                 msg_Dbg( p_this, "failed to set ttl (%s). Let's try it "
481                          "the integer way.", strerror(errno) );
482                 if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
483                                 &i_ttl, sizeof( i_ttl ) ) <0 )
484                 {
485                     msg_Err( p_this, "failed to set ttl (%s)",
486                              strerror(errno) );
487                     close( i_handle );
488                     return( -1 );
489                 }
490             }
491         }
492 #endif
493     }
494
495     p_socket->i_handle = i_handle;
496
497     if( var_Get( p_this, "mtu", &val ) != VLC_SUCCESS )
498     {
499         var_Create( p_this, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
500         var_Get( p_this, "mtu", &val );
501     }
502     p_socket->i_mtu = val.i_int;
503     return( 0 );
504 }
505
506 /*****************************************************************************
507  * SocketTCP: create a TCP socket
508  *****************************************************************************
509  * This function returns -1 in case of error.
510  *****************************************************************************/
511 static int SocketTCP( vlc_object_t * p_this )
512 {
513     int i_handle;
514     
515     /* Open a SOCK_STREAM (TCP) socket, in the PF_INET domain, automatic (0)
516      * protocol */
517     if( (i_handle = socket( PF_INET, SOCK_STREAM, 0 )) == -1 )
518     {
519 #if defined(WIN32) || defined(UNDER_CE)
520         msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );
521 #else
522         msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );
523 #endif
524         return -1;
525     }
526
527     /* Set to non-blocking */
528 #if defined( WIN32 ) || defined( UNDER_CE )
529     {
530         unsigned long i_dummy = 1;
531         if( ioctlsocket( i_handle, FIONBIO, &i_dummy ) != 0 )
532         {
533             msg_Err( p_this, "cannot set socket to non-blocking mode" );
534         }
535     }
536 #else
537     {
538         int i_flags;
539         if( ( i_flags = fcntl( i_handle, F_GETFL, 0 ) ) < 0 ||
540             fcntl( i_handle, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
541         {
542             msg_Err( p_this, "cannot set socket to non-blocking mode" );
543         }
544     }
545 #endif
546
547     return i_handle;
548 }
549
550 /*****************************************************************************
551  * OpenTCP: open a TCP socket
552  *****************************************************************************
553  * psz_server_addr, i_server_port : address and port used for the connect()
554  *   system call. If i_server_port == 0, 80 is used.
555  * Other parameters are ignored.
556  * This function returns -1 in case of error.
557  *****************************************************************************/
558 static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
559 {
560     char * psz_server_addr = p_socket->psz_server_addr;
561     int i_server_port = p_socket->i_server_port;
562
563     int i_handle;
564     struct sockaddr_in sock;
565
566     if( i_server_port == 0 )
567     {
568         i_server_port = 80;
569     }
570
571     if( (i_handle = SocketTCP( p_this )) == -1 )
572         return VLC_EGENERIC;
573
574     /* Build remote address */
575     if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
576     {
577         msg_Dbg( p_this, "could not build local address" );
578         goto error;
579     }
580
581     /* Connect the socket */
582     if( connect( i_handle, (struct sockaddr *) &sock, sizeof( sock ) ) == -1 )
583     {
584 #if defined( WIN32 ) || defined( UNDER_CE )
585         if( WSAGetLastError() == WSAEWOULDBLOCK )
586 #else
587         if( errno == EINPROGRESS )
588 #endif
589         {
590             int i_ret, i_opt, i_opt_size = sizeof( i_opt ), i_max_count;
591             struct timeval timeout;
592             vlc_value_t val;
593             fd_set fds;
594
595             if( var_Get( p_this, "ipv4-timeout", &val ) != VLC_SUCCESS )
596             {
597                 var_Create( p_this, "ipv4-timeout",
598                             VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
599                 var_Get( p_this, "ipv4-timeout", &val );
600             }
601             i_max_count = val.i_int * 1000 / 100000 /* timeout.tv_usec */;
602
603             msg_Dbg( p_this, "connection in progress" );
604             do
605             {
606                 if( p_this->b_die || i_max_count <= 0 )
607                 {
608                     msg_Dbg( p_this, "connection aborted" );
609                     goto error;
610                 }
611
612                 i_max_count--;
613
614                 /* Initialize file descriptor set */
615                 FD_ZERO( &fds );
616                 FD_SET( i_handle, &fds );
617
618                 /* We'll wait 0.1 second if nothing happens */
619                 timeout.tv_sec = 0;
620                 timeout.tv_usec = 100000;
621
622             } while( ( i_ret = select( i_handle + 1, NULL, &fds, NULL,
623                                        &timeout ) ) == 0 ||
624 #if defined( WIN32 ) || defined( UNDER_CE )
625                      ( i_ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK ) );
626 #else
627                      ( i_ret < 0 && errno == EINTR ) );
628 #endif
629
630             if( i_ret < 0 )
631             {
632                 msg_Warn( p_this, "cannot connect socket (select failed)" );
633                 goto error;
634             }
635
636 #if !defined( SYS_BEOS ) && !defined( UNDER_CE )
637             if( getsockopt( i_handle, SOL_SOCKET, SO_ERROR, (void*)&i_opt,
638                             &i_opt_size ) == -1 || i_opt != 0 )
639             {
640                 msg_Warn( p_this, "cannot connect socket (SO_ERROR)" );
641                 goto error;
642             }
643 #endif
644         }
645         else
646         {
647 #if defined(WIN32) || defined(UNDER_CE)
648             msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());
649 #else
650             msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );
651 #endif
652             goto error;
653         }
654     }
655
656     p_socket->i_handle = i_handle;
657     p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
658     return VLC_SUCCESS;
659
660 error:
661     close( i_handle );
662     return VLC_EGENERIC;
663 }
664
665 /*****************************************************************************
666  * ListenTCP: open a TCP passive socket (server-side)
667  *****************************************************************************
668  * psz_server_addr, i_server_port : address and port used for the bind()
669  *   system call. If i_server_port == 0, 80 is used.
670  * Other parameters are ignored.
671  * This function returns -1 in case of error.
672  *****************************************************************************/
673 static int ListenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
674 {
675     char * psz_server_addr = p_socket->psz_server_addr;
676     int i_server_port = p_socket->i_server_port;
677
678     int i_handle, i_dummy = 1;
679     struct sockaddr_in sock;
680
681     if( (i_handle = SocketTCP( p_this )) == -1 )
682         return VLC_EGENERIC;
683
684     if ( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR,
685                 (void *)&i_dummy, sizeof( i_dummy ) ) == -1 )
686     {
687         msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
688     }
689
690     /* Build remote address */
691     if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
692     {
693         msg_Dbg( p_this, "could not build local address" );
694         return VLC_EGENERIC;
695     }
696     
697     /* Bind the socket */
698     if( bind( i_handle, (struct sockaddr *) &sock, sizeof( sock )) == -1 )
699     {
700 #if defined(WIN32) || defined(UNDER_CE)
701         msg_Err( p_this, "cannot bind socket (%i)", WSAGetLastError());
702 #else
703         msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );
704 #endif
705         goto error;
706     }
707  
708     /* Listen */
709     if( listen( i_handle, 100 ) == -1 )
710     {
711 #if defined(WIN32) || defined(UNDER_CE)
712         msg_Err( p_this, "cannot bring socket in listening mode (%i)",
713                  WSAGetLastError());
714 #else
715         msg_Err( p_this, "cannot bring the socket in listening mode (%s)",
716                  strerror(errno) );
717 #endif
718         goto error;
719     }
720
721     p_socket->i_handle = i_handle;
722     p_socket->i_mtu = 0; /* There is no MTU notion in TCP */
723     return VLC_SUCCESS;
724
725 error:
726     close( i_handle );
727     return VLC_EGENERIC;
728 }
729
730 /*****************************************************************************
731  * NetOpen: wrapper around OpenUDP, ListenTCP and OpenTCP
732  *****************************************************************************/
733 static int NetOpen( vlc_object_t * p_this )
734 {
735     network_socket_t * p_socket = p_this->p_private;
736
737     if( p_socket->i_type == NETWORK_UDP )
738     {
739         return OpenUDP( p_this, p_socket );
740     }
741     else if( p_socket->i_type == NETWORK_TCP_PASSIVE )
742     {
743         return ListenTCP( p_this, p_socket );
744     }
745     else
746     {
747         return OpenTCP( p_this, p_socket );
748     }
749 }