]> git.sesse.net Git - vlc/blob - modules/access/ftp.c
8d77c52b29b8c0b6e56ca1f5ab3fb1d56caa0845
[vlc] / modules / access / ftp.c
1 /*****************************************************************************
2  * ftp.c:
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: ftp.c,v 1.10 2003/03/24 17:15:29 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <fcntl.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/input.h>
37
38 #ifdef HAVE_SYS_TIME_H
39 #    include <sys/time.h>
40 #endif
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #endif
45
46 #ifdef WIN32
47 #   include <winsock2.h>
48 #   include <ws2tcpip.h>
49 #   ifndef IN_MULTICAST
50 #       define IN_MULTICAST(a) IN_CLASSD(a)
51 #   endif
52 #else
53 #   include <sys/socket.h>
54 #   include <netinet/in.h>
55 #   if HAVE_ARPA_INET_H
56 #      include <arpa/inet.h>
57 #   elif defined( SYS_BEOS )
58 #      include <net/netdb.h>
59 #   endif
60 #endif
61
62 #include "network.h"
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67 static int  Open        ( vlc_object_t * );
68 static void Close       ( vlc_object_t * );
69
70 static int  Read        ( input_thread_t * p_input, byte_t * p_buffer,
71                           size_t i_len );
72 static void Seek        ( input_thread_t *, off_t );
73 static int  SetProgram  ( input_thread_t *, pgrm_descriptor_t * );
74
75
76 static ssize_t NetRead ( input_thread_t *, input_socket_t *, byte_t *, size_t );
77 static void    NetClose( input_thread_t *, input_socket_t *);
78
79 static int  ftp_SendCommand( input_thread_t *, char *, ... );
80 static int  ftp_ReadCommand( input_thread_t *, int *, char ** );
81 static int  ftp_StartStream( input_thread_t *, off_t );
82 static int  ftp_StopStream ( input_thread_t *);
83
84 /*****************************************************************************
85  * Module descriptor
86  *****************************************************************************/
87 #define CACHING_TEXT N_("caching value in ms")
88 #define CACHING_LONGTEXT N_( \
89     "Allows you to modify the default caching value for ftp streams. This " \
90     "value should be set in miliseconds units." )
91
92 vlc_module_begin();
93     set_description( _("ftp access module") );
94     set_capability( "access", 0 );
95     add_category_hint( "stream", NULL, VLC_FALSE );
96         add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
97                      CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
98         add_string( "ftp-user", "anonymous", NULL, "ftp user name", "ftp user name", VLC_FALSE );
99         add_string( "ftp-pwd", "anonymous@dummy.org", NULL, "ftp password", "ftp password, be careful with that option...", VLC_FALSE );
100         add_string( "ftp-account", "anonymous", NULL, "ftp account", "ftp account", VLC_FALSE );
101     add_shortcut( "ftp" );
102     set_callbacks( Open, Close );
103 vlc_module_end();
104
105 /* url: [/]host[:port][/path] */
106 typedef struct url_s
107 {
108     char    *psz_server_addr;
109     int     i_server_port;
110
111     char    *psz_bind_addr;
112     int     i_bind_port;
113
114     char    *psz_path;
115
116     /* private */
117     char *psz_private;
118 } url_t;
119
120 static void ftp_ParseURL( url_t *, char * );
121
122 #define FREE( p ) if( p ) free( p )
123
124 typedef struct access_s
125 {
126     input_socket_t  socket_cmd;
127     input_socket_t  socket_data;
128
129     url_t           url;                        /* connect to this server */
130
131     off_t           i_filesize;
132
133     int             i_eos;
134
135 } access_t;
136
137
138 /****************************************************************************
139  ****************************************************************************
140  *******************                                      *******************
141  *******************       Main functions                 *******************
142  *******************                                      *******************
143  ****************************************************************************
144  ****************************************************************************/
145
146 /****************************************************************************
147  * Open: connect to ftp server and ask for file
148  ****************************************************************************/
149 static int Open( vlc_object_t *p_this )
150 {
151     input_thread_t  *p_input = (input_thread_t*)p_this;
152
153     access_t    *p_access;
154     char        *psz_network;
155
156     module_t            *p_network;
157     network_socket_t    socket_desc;
158     url_t               *p_url;
159
160     int                 i_answer;
161     char                *psz_user, *psz_pwd, *psz_account;
162
163     char                *psz_arg;
164
165     /* *** allocate p_access_data *** */
166     p_input->p_access_data =
167         (void*)p_access = malloc( sizeof( access_t ) );
168     memset( p_access, 0, sizeof( access_t ) );
169     p_url = &p_access->url;
170
171     /* *** Parse URL and get server addr/port and path *** */
172     ftp_ParseURL( p_url, p_input->psz_name );
173
174     if( p_url->psz_server_addr == NULL ||
175         !( *p_url->psz_server_addr ) )
176     {
177         FREE( p_url->psz_private );
178         msg_Err( p_input, "invalid server name" );
179         return( -1 );
180     }
181     if( p_url->i_server_port == 0 )
182     {
183         p_url->i_server_port = 21; /* default port */
184     }
185
186     /* 2: look at ip version ipv4/ipv6 */
187     psz_network = "";
188     if( config_GetInt( p_input, "ipv4" ) )
189     {
190         psz_network = "ipv4";
191     }
192     else if( config_GetInt( p_input, "ipv6" ) )
193     {
194         psz_network = "ipv6";
195     }
196
197     /* 3: Open a TCP connection with server *** */
198     msg_Dbg( p_input, "waiting for connection..." );
199     socket_desc.i_type = NETWORK_TCP;
200     socket_desc.psz_server_addr = p_url->psz_server_addr;
201     socket_desc.i_server_port   = p_url->i_server_port;
202     socket_desc.psz_bind_addr   = "";
203     socket_desc.i_bind_port     = 0;
204     p_input->p_private = (void*)&socket_desc;
205     if( !( p_network = module_Need( p_input, "network", psz_network ) ) )
206     {
207         msg_Err( p_input, "failed to connect with server" );
208         FREE( p_access->url.psz_private );
209         FREE( p_input->p_access_data );
210         return( -1 );
211     }
212     module_Unneed( p_input, p_network );
213     p_access->socket_cmd.i_handle = socket_desc.i_handle;
214     p_input->i_mtu    = socket_desc.i_mtu;
215     msg_Dbg( p_input,
216              "connection with \"%s:%d\" successful",
217              p_url->psz_server_addr,
218              p_url->i_server_port );
219
220
221     for( ;; )
222     {
223         if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
224         {
225             msg_Err( p_input, "failed to get answer" );
226             goto exit_error;
227         }
228         if( i_answer / 100 != 1 )
229         {
230             break;
231         }
232     }
233
234     if( i_answer / 100 != 2 )
235     {
236         msg_Err( p_input, "connection rejected" );
237         goto exit_error;
238     }
239     else
240     {
241         msg_Dbg( p_input, "connection accepted (%d)", i_answer );
242     }
243
244     psz_user = config_GetPsz( p_input, "ftp-user" );
245     if( ftp_SendCommand( p_input, "USER %s", psz_user ) < 0 )
246     {
247         FREE( psz_user );
248         goto exit_error;
249     }
250     FREE( psz_user );
251
252     if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
253     {
254         msg_Err( p_input, "failed to get answer" );
255         goto exit_error;
256     }
257     switch( i_answer / 100 )
258     {
259         case 2:
260             msg_Dbg( p_input, "user accepted" );
261             break;
262         case 3:
263             msg_Dbg( p_input, "password needed" );
264             psz_pwd = config_GetPsz( p_input, "ftp-pwd" );
265             if( ftp_SendCommand( p_input, "PASS %s", psz_pwd ) < 0 )
266             {
267                 FREE( psz_pwd );
268                 goto exit_error;
269             }
270             FREE( psz_pwd );
271             if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
272             {
273                 msg_Err( p_input, "failed to get answer" );
274                 goto exit_error;
275             }
276             switch( i_answer / 100 )
277             {
278                 case 2:
279                     msg_Dbg( p_input, "password accepted" );
280                     break;
281                 case 3:
282                     msg_Dbg( p_input, "account needed" );
283                     psz_account = config_GetPsz( p_input, "ftp-account" );
284                     if( ftp_SendCommand( p_input, "ACCT %s", psz_account ) < 0 )
285                     {
286                         FREE( psz_account );
287                         goto exit_error;
288                     }
289                     FREE( psz_account );
290                     if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
291                     {
292                         msg_Err( p_input, "failed to get answer" );
293                         goto exit_error;
294                     }
295                     if( i_answer / 100 != 2 )
296                     {
297                         msg_Err( p_input, "account rejected" );
298                         goto exit_error;
299                     }
300                     else
301                     {
302                         msg_Dbg( p_input, "account accepted" );
303                     }
304                     break;
305                 default:
306                     msg_Err( p_input, "password rejected" );
307                     goto exit_error;
308             }
309             break;
310         default:
311             msg_Err( p_input, "user rejected" );
312             goto exit_error;
313     }
314
315     if( ftp_SendCommand( p_input, "TYPE I" ) < 0 )
316     {
317         msg_Err( p_input, "cannot set binary transfert mode" );
318         goto exit_error;
319     }
320     if( ftp_ReadCommand( p_input, &i_answer, NULL ) != 2 )
321     {
322         msg_Err( p_input, "cannot set binary transfert mode" );
323         goto exit_error;
324     }
325
326     /* get size */
327     if( ftp_SendCommand( p_input, "SIZE %s", p_url->psz_path ) < 0 )
328     {
329         msg_Err( p_input, "cannot get file size" );
330         goto exit_error;
331     }
332     if( ftp_ReadCommand( p_input, &i_answer, &psz_arg ) != 2 )
333     {
334         msg_Err( p_input, "cannot get file size" );
335         goto exit_error;
336     }
337
338 #ifdef HAVE_ATOLL
339     p_access->i_filesize = atoll( psz_arg + 4 );
340 #else
341     {
342         int64_t i_size = 0;
343         char    *psz_parser = psz_arg + 4;
344
345         while( *psz_parser == ' ' ) psz_parser++;
346
347         while( psz_parser[0] >= '0' && psz_parser[0] <= '9' )
348         {
349             i_size *= 10;
350             i_size += psz_parser[0] - '0';
351         }
352         p_access->i_filesize = i_size;
353     }
354 #endif
355
356     msg_Dbg( p_input, "file size: "I64Fd, p_access->i_filesize );
357     FREE( psz_arg );
358
359     if( ftp_StartStream( p_input, 0 ) < 0 )
360     {
361         msg_Err( p_input, "cannot retrieve file" );
362         goto exit_error;
363     }
364     /* *** set exported functions *** */
365     p_input->pf_read = Read;
366     p_input->pf_seek = Seek;
367     p_input->pf_set_program = SetProgram;
368     p_input->pf_set_area = NULL;
369
370     p_input->p_private = NULL;
371
372     /* *** finished to set some variable *** */
373     vlc_mutex_lock( &p_input->stream.stream_lock );
374     p_input->stream.b_pace_control = 1;
375     p_input->stream.p_selected_area->i_tell = 0;
376     p_input->stream.b_seekable = 1;
377     p_input->stream.p_selected_area->i_size = p_access->i_filesize;
378     p_input->stream.i_method = INPUT_METHOD_NETWORK;
379     vlc_mutex_unlock( &p_input->stream.stream_lock );
380
381     /* Update default_pts to a suitable value for ftp access */
382     p_input->i_pts_delay = config_GetInt( p_input, "ftp-caching" ) * 1000;
383
384     return( 0 );
385
386 exit_error:
387     NetClose( p_input, &p_access->socket_cmd );
388     FREE( p_access->url.psz_private );
389     FREE( p_input->p_access_data );
390     return( -1 );
391 }
392
393 /*****************************************************************************
394  * Close: free unused data structures
395  *****************************************************************************/
396 static void Close( vlc_object_t *p_this )
397 {
398     input_thread_t  *p_input = (input_thread_t *)p_this;
399     access_t        *p_access = (access_t*)p_input->p_access_data;
400
401     msg_Dbg( p_input, "stopping stream" );
402     ftp_StopStream( p_input );
403
404     if( ftp_SendCommand( p_input, "QUIT" ) < 0 )
405     {
406         msg_Err( p_input, "cannot quit" );
407     }
408     else
409     {
410         ftp_ReadCommand( p_input, NULL, NULL );
411     }
412
413
414     NetClose( p_input, &p_access->socket_cmd );
415
416     /* free memory */
417     FREE( p_access->url.psz_private );
418 }
419
420 /*****************************************************************************
421  * SetProgram: do nothing
422  *****************************************************************************/
423 static int SetProgram( input_thread_t * p_input,
424                        pgrm_descriptor_t * p_program )
425 {
426     return( 0 );
427 }
428
429 /*****************************************************************************
430  * Seek: try to go at the right place
431  *****************************************************************************/
432 static void Seek( input_thread_t * p_input, off_t i_pos )
433 {
434     //access_t    *p_access = (access_t*)p_input->p_access_data;
435     if( i_pos < 0 )
436     {
437         return;
438     }
439     vlc_mutex_lock( &p_input->stream.stream_lock );
440
441     msg_Dbg( p_input, "seeking to "I64Fd, i_pos );
442
443     ftp_StopStream( p_input );
444     ftp_StartStream( p_input, i_pos );
445
446     p_input->stream.p_selected_area->i_tell = i_pos;
447     vlc_mutex_unlock( &p_input->stream.stream_lock );
448 }
449
450 static int  Read        ( input_thread_t * p_input, byte_t * p_buffer,
451                           size_t i_len )
452 {
453     access_t    *p_access = (access_t*)p_input->p_access_data;
454     size_t      i_data;
455
456     i_data = NetRead( p_input, &p_access->socket_data, p_buffer, i_len );
457
458     return( i_data );
459 }
460
461 static int  ftp_SendCommand( input_thread_t *p_input, char *psz_fmt, ... )
462 {
463     access_t        *p_access = (access_t*)p_input->p_access_data;
464     va_list args;
465     char    *psz_buffer;
466 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
467         size_t  i_size;
468 #endif
469
470     va_start( args, psz_fmt );
471
472 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN)
473     vasprintf( &psz_buffer, psz_fmt, args );
474 #else
475     i_size = strlen( psz_fmt ) + 2048;
476     psz_buffer = calloc( i_size, sizeof( char ) );
477     vsnprintf( psz_buffer, i_size, psz_fmt, args );
478     psz_buffer[i_size - 1] = 0;
479 #endif
480     if( !strncmp( psz_buffer, "PASS", 4 ) )
481     {
482         msg_Dbg( p_input, "ftp_SendCommand:\"PASS xxx\"" );
483     }
484     else
485     {
486         msg_Dbg( p_input, "ftp_SendCommand:\"%s\"", psz_buffer );
487     }
488     psz_buffer = realloc( psz_buffer, strlen( psz_buffer ) + 3 );
489     strcat( psz_buffer, "\r\n" );
490     if( send( p_access->socket_cmd.i_handle,
491               psz_buffer,
492               strlen( psz_buffer ),
493               0 ) == -1 )
494     {
495         FREE( psz_buffer );
496         msg_Err( p_input, "failed to send command" );
497         return( -1 );
498     }
499     FREE( psz_buffer );
500
501     va_end( args );
502
503     return( 0 );
504 }
505
506 #define BLOCK_SIZE  1024
507 /* TODO support this s**t :
508  RFC 959 allows the client to send certain TELNET strings at any moment,
509  even in the middle of a request:
510
511  * \377\377.
512  * \377\376x where x is one byte.
513  * \377\375x where x is one byte. The server is obliged to send \377\374x
514  *                                immediately after reading x.
515  * \377\374x where x is one byte.
516  * \377\373x where x is one byte. The server is obliged to send \377\376x
517  *                                immediately after reading x.
518  * \377x for any other byte x.
519
520  These strings are not part of the requests, except in the case \377\377,
521  where the request contains one \377. */
522
523 static int  ftp_ReadCommand( input_thread_t *p_input,
524                              int *pi_answer, char **ppsz_answer )
525 {
526     access_t        *p_access = (access_t*)p_input->p_access_data;
527     uint8_t *p_buffer;
528     int     i_buffer;
529     int     i_buffer_size;
530
531     int i_answer;
532
533     i_buffer      = 0;
534     i_buffer_size = BLOCK_SIZE + 1;
535     p_buffer      = malloc( BLOCK_SIZE + 1);
536
537     for( ;; )
538     {
539         ssize_t i_read;
540         i_read = NetRead( p_input, &p_access->socket_cmd,
541                           p_buffer + i_buffer, BLOCK_SIZE );
542         if( i_read <= 0 || p_input->b_die || p_input->b_error )
543         {
544             free( p_buffer );
545             if( pi_answer )   *pi_answer    = 500;
546             if( ppsz_answer ) *ppsz_answer  = NULL;
547             return( -1 );
548         }
549         if( i_read == 0 )
550         {
551 //            continue;
552         }
553         i_buffer += i_read;
554         if( i_read < BLOCK_SIZE )
555         {
556             p_buffer[i_buffer] = '\0';
557             break;
558         }
559         i_buffer_size += BLOCK_SIZE;
560         p_buffer = realloc( p_buffer, i_buffer_size );
561     }
562
563     if( i_buffer < 3 )
564     {
565         goto exit_error;
566     }
567
568     i_answer = atoi( p_buffer );
569
570     if( pi_answer ) *pi_answer = i_answer;
571     if( ppsz_answer )
572     {
573         *ppsz_answer = p_buffer;
574     }
575     else
576     {
577         free( p_buffer );
578     }
579     return( i_answer / 100 );
580
581 exit_error:
582     free( p_buffer );
583     if( pi_answer )   *pi_answer    = 500;
584     if( ppsz_answer ) *ppsz_answer  = NULL;
585     return( -1 );
586 }
587
588 static int  ftp_StartStream( input_thread_t *p_input, off_t i_start )
589 {
590     access_t        *p_access = (access_t*)p_input->p_access_data;
591
592     char psz_ip[1000];
593     int  i_answer;
594     char *psz_arg, *psz_parser;
595     int  a1,a2,a3,a4;
596     int  p1,p2;
597     int  i_port;
598     module_t            *p_network;
599     network_socket_t    socket_desc;
600
601     if( ftp_SendCommand( p_input, "PASV" ) < 0 )
602     {
603         msg_Err( p_input, "cannot set passive transfert mode" );
604         return( -1 );
605     }
606     if( ftp_ReadCommand( p_input, &i_answer, &psz_arg ) != 2 )
607     {
608         msg_Err( p_input, "cannot set passive transfert mode" );
609         return( -1 );
610     }
611     psz_parser = strchr( psz_arg, '(' );
612     if( !psz_parser || sscanf( psz_parser, "(%d,%d,%d,%d,%d,%d", &a1, &a2, &a3, &a4, &p1, &p2 ) < 6 )
613     {
614         FREE( psz_arg );
615         msg_Err( p_input, "cannot get ip/port for passive transfert mode" );
616         return( -1 );
617     }
618     FREE( psz_arg );
619
620     sprintf( psz_ip, "%d.%d.%d.%d", a1, a2, a3, a4 );
621     i_port = p1 * 256 + p2;
622     msg_Dbg( p_input, "ip:%s port:%d", psz_ip, i_port );
623
624     if( ftp_SendCommand( p_input, "TYPE I" ) < 0 )
625     {
626         msg_Err( p_input, "cannot set binary transfert mode" );
627         return( -1 );
628     }
629     if( ftp_ReadCommand( p_input, &i_answer, NULL ) != 2 )
630     {
631         msg_Err( p_input, "cannot set binary transfert mode" );
632         return( -1 );
633     }
634
635
636     if( i_start > 0 )
637     {
638         if( ftp_SendCommand( p_input, "REST "I64Fu, i_start ) < 0 )
639         {
640             msg_Err( p_input, "cannot set restart point" );
641             return( -1 );
642         }
643         if( ftp_ReadCommand( p_input, &i_answer, NULL ) > 3 )
644         {
645             msg_Err( p_input, "cannot set restart point" );
646             return( -1 );
647         }
648     }
649
650     msg_Dbg( p_input, "waiting for data connection..." );
651     socket_desc.i_type = NETWORK_TCP;
652     socket_desc.psz_server_addr = psz_ip;
653     socket_desc.i_server_port   = i_port;
654     socket_desc.psz_bind_addr   = "";
655     socket_desc.i_bind_port     = 0;
656     p_input->p_private = (void*)&socket_desc;
657     if( !( p_network = module_Need( p_input, "network", "" ) ) )
658     {
659         msg_Err( p_input, "failed to connect with server" );
660         return( -1 );
661     }
662     module_Unneed( p_input, p_network );
663     p_access->socket_data.i_handle = socket_desc.i_handle;
664     p_input->i_mtu    = socket_desc.i_mtu;
665     msg_Dbg( p_input,
666              "connection with \"%s:%d\" successful",
667              psz_ip, i_port );
668
669     if( ftp_SendCommand( p_input, "RETR %s", p_access->url.psz_path ) < 0 )
670     {
671         msg_Err( p_input, "cannot retreive file" );
672         return( -1 );
673     }
674     /* "1xx" message */
675     if( ftp_ReadCommand( p_input, &i_answer, NULL ) > 2 )
676     {
677         msg_Err( p_input, "cannot retreive file" );
678         return( -1 );
679     }
680
681     return( 0 );
682 }
683
684 static int  ftp_StopStream ( input_thread_t *p_input)
685 {
686     access_t        *p_access = (access_t*)p_input->p_access_data;
687
688     int i_answer;
689
690     NetClose( p_input, &p_access->socket_data );
691
692     if( ftp_SendCommand( p_input, "ABOR" ) < 0 )
693     {
694         msg_Err( p_input, "cannot abord file" );
695     }
696     else
697     {
698         ftp_ReadCommand( p_input, &i_answer, NULL );
699         ftp_ReadCommand( p_input, &i_answer, NULL );
700     }
701
702     return( 0 );
703 }
704
705 /****************************************************************************
706  *
707  ****************************************************************************/
708 static void ftp_ParseURL( url_t *p_url, char *psz_url )
709 {
710     char *psz_parser;
711     char *psz_server_port;
712
713     p_url->psz_private = strdup( psz_url );
714
715     psz_parser = p_url->psz_private;
716
717     while( *psz_parser == '/' )
718     {
719         psz_parser++;
720     }
721     p_url->psz_server_addr = psz_parser;
722
723     while( *psz_parser &&
724            *psz_parser != ':' &&  *psz_parser != '/' && *psz_parser != '@' )
725     {
726         psz_parser++;
727     }
728
729     if( *psz_parser == ':' )
730     {
731         *psz_parser = '\0';
732         psz_parser++;
733         psz_server_port = psz_parser;
734
735         while( *psz_parser && *psz_parser != '/' )
736         {
737             psz_parser++;
738         }
739     }
740     else
741     {
742         psz_server_port = "";
743     }
744
745     if( *psz_parser == '@' )
746     {
747         char *psz_bind_port;
748
749         *psz_parser = '\0';
750         psz_parser++;
751
752         p_url->psz_bind_addr = psz_parser;
753
754         while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
755         {
756             psz_parser++;
757         }
758
759         if( *psz_parser == ':' )
760         {
761             *psz_parser = '\0';
762             psz_parser++;
763             psz_bind_port = psz_parser;
764
765             while( *psz_parser && *psz_parser != '/' )
766             {
767                 psz_parser++;
768             }
769         }
770         else
771         {
772             psz_bind_port = "";
773         }
774         if( *psz_bind_port )
775         {
776             p_url->i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
777         }
778         else
779         {
780             p_url->i_bind_port = 0;
781         }
782     }
783     else
784     {
785         p_url->psz_bind_addr = "";
786         p_url->i_bind_port = 0;
787     }
788
789     if( *psz_parser == '/' )
790     {
791         *psz_parser = '\0';
792         psz_parser++;
793         p_url->psz_path = psz_parser;
794     }
795
796     if( *psz_server_port )
797     {
798         p_url->i_server_port = strtol( psz_server_port, &psz_parser, 10 );
799     }
800     else
801     {
802         p_url->i_server_port = 0;
803     }
804 }
805
806 /*****************************************************************************
807  * Read: read on a file descriptor, checking b_die periodically
808  *****************************************************************************/
809 static ssize_t NetRead( input_thread_t *p_input,
810                         input_socket_t *p_socket,
811                         byte_t *p_buffer, size_t i_len )
812 {
813 #ifdef UNDER_CE
814     return -1;
815
816 #else
817     struct timeval  timeout;
818     fd_set          fds;
819     ssize_t         i_recv;
820     int             i_ret;
821
822     /* Initialize file descriptor set */
823     FD_ZERO( &fds );
824     FD_SET( p_socket->i_handle, &fds );
825
826     /* We'll wait 1 second if nothing happens */
827     timeout.tv_sec  = 0;
828     timeout.tv_usec = 1000000;
829
830     /* Find if some data is available */
831     while( (i_ret = select( p_socket->i_handle + 1, &fds,
832                             NULL, NULL, &timeout )) == 0
833            || (i_ret < 0 && errno == EINTR) )
834     {
835         if( p_input->b_die || p_input->b_error )
836         {
837             return 0;
838         }
839     }
840
841     if( i_ret < 0 )
842     {
843         msg_Err( p_input, "network select error (%s)", strerror(errno) );
844         return -1;
845     }
846
847     i_recv = recv( p_socket->i_handle, p_buffer, i_len, 0 );
848
849     if( i_recv < 0 )
850     {
851         msg_Err( p_input, "recv failed (%s)", strerror(errno) );
852     }
853
854     return i_recv;
855
856 #endif
857 }
858
859 static void NetClose( input_thread_t *p_input, input_socket_t *p_socket )
860 {
861 #if defined( UNDER_CE )
862     CloseHandle( (HANDLE)p_socket->i_handle );
863 #elif defined( WIN32 )
864     closesocket( p_socket->i_handle );
865 #else
866     close( p_socket->i_handle );
867 #endif
868 }
869