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