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