ad_faad: fix crash when used on an empty audio stream
[mplayer.git] / stream / stream_ftp.c
blob00027d5205476c54415d492d190326326f4eea5c
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #if !HAVE_WINSOCK2_H
30 #include <sys/socket.h>
31 #else
32 #include <winsock2.h>
33 #endif
35 #include "mp_msg.h"
36 #include "network.h"
37 #include "stream.h"
38 #include "m_option.h"
39 #include "m_struct.h"
40 #include "tcp.h"
42 static struct stream_priv_s {
43 char* user;
44 char* pass;
45 char* host;
46 int port;
47 char* filename;
49 char *cput,*cget;
50 int handle;
51 int cavail,cleft;
52 char *buf;
53 } stream_priv_dflts = {
54 "anonymous","no@spam",
55 NULL,
56 21,
57 NULL,
58 NULL,
59 NULL,
62 0,0,
63 NULL
66 #define BUFSIZE 2048
68 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
69 /// URL definition
70 static const m_option_t stream_opts_fields[] = {
71 {"username", ST_OFF(user), CONF_TYPE_STRING, 0, 0 ,0, NULL},
72 {"password", ST_OFF(pass), CONF_TYPE_STRING, 0, 0 ,0, NULL},
73 {"hostname", ST_OFF(host), CONF_TYPE_STRING, 0, 0 ,0, NULL},
74 {"port", ST_OFF(port), CONF_TYPE_INT, 0, 0 ,65635, NULL},
75 {"filename", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
76 { NULL, NULL, 0, 0, 0, 0, NULL }
78 static const struct m_struct_st stream_opts = {
79 "ftp",
80 sizeof(struct stream_priv_s),
81 &stream_priv_dflts,
82 stream_opts_fields
85 #define TELNET_IAC 255 /* interpret as command: */
86 #define TELNET_IP 244 /* interrupt process--permanently */
87 #define TELNET_SYNCH 242 /* for telfunc calls */
89 // Check if there is something to read on a fd. This avoid hanging
90 // forever if the network stop responding.
91 static int fd_can_read(int fd,int timeout) {
92 fd_set fds;
93 struct timeval tv;
95 FD_ZERO(&fds);
96 FD_SET(fd,&fds);
97 tv.tv_sec = timeout;
98 tv.tv_usec = 0;
100 return select(fd+1, &fds, NULL, NULL, &tv) > 0;
104 * read a line of text
106 * return -1 on error or bytecount
108 static int readline(char *buf,int max,struct stream_priv_s *ctl)
110 int x,retval = 0;
111 char *end,*bp=buf;
112 int eof = 0;
114 do {
115 if (ctl->cavail > 0) {
116 x = (max >= ctl->cavail) ? ctl->cavail : max-1;
117 end = memccpy(bp,ctl->cget,'\n',x);
118 if (end != NULL)
119 x = end - bp;
120 retval += x;
121 bp += x;
122 *bp = '\0';
123 max -= x;
124 ctl->cget += x;
125 ctl->cavail -= x;
126 if (end != NULL) {
127 bp -= 2;
128 if (strcmp(bp,"\r\n") == 0) {
129 *bp++ = '\n';
130 *bp++ = '\0';
131 --retval;
133 break;
136 if (max == 1) {
137 *buf = '\0';
138 break;
140 if (ctl->cput == ctl->cget) {
141 ctl->cput = ctl->cget = ctl->buf;
142 ctl->cavail = 0;
143 ctl->cleft = BUFSIZE;
145 if(eof) {
146 if (retval == 0)
147 retval = -1;
148 break;
151 if(!fd_can_read(ctl->handle, 15)) {
152 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] read timed out\n");
153 retval = -1;
154 break;
157 if ((x = recv(ctl->handle,ctl->cput,ctl->cleft,0)) == -1) {
158 mp_msg(MSGT_STREAM,MSGL_ERR, "[ftp] read error: %s\n",strerror(errno));
159 retval = -1;
160 break;
162 if (x == 0)
163 eof = 1;
164 ctl->cleft -= x;
165 ctl->cavail += x;
166 ctl->cput += x;
167 } while (1);
169 return retval;
173 * read a response from the server
175 * return 0 if first char doesn't match
176 * return 1 if first char matches
178 static int readresp(struct stream_priv_s* ctl,char* rsp)
180 static char response[256];
181 char match[5];
182 int r;
184 if (readline(response,256,ctl) == -1)
185 return 0;
187 r = atoi(response)/100;
188 if(rsp) strcpy(rsp,response);
190 mp_msg(MSGT_STREAM,MSGL_V, "[ftp] < %s",response);
192 if (response[3] == '-') {
193 strncpy(match,response,3);
194 match[3] = ' ';
195 match[4] = '\0';
196 do {
197 if (readline(response,256,ctl) == -1) {
198 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Control socket read failed\n");
199 return 0;
201 mp_msg(MSGT_OPEN,MSGL_V, "[ftp] < %s",response);
202 } while (strncmp(response,match,4));
204 return r;
208 static int FtpSendCmd(const char *cmd, struct stream_priv_s *nControl,char* rsp)
210 int l = strlen(cmd);
211 int hascrlf = cmd[l - 2] == '\r' && cmd[l - 1] == '\n';
213 if(hascrlf && l == 2) mp_msg(MSGT_STREAM,MSGL_V, "\n");
214 else mp_msg(MSGT_STREAM,MSGL_V, "[ftp] > %s",cmd);
215 while(l > 0) {
216 int s = send(nControl->handle,cmd,l,DEFAULT_SEND_FLAGS);
218 if(s <= 0) {
219 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] write error: %s\n",strerror(errno));
220 return 0;
223 cmd += s;
224 l -= s;
227 if (hascrlf)
228 return readresp(nControl,rsp);
229 else
230 return FtpSendCmd("\r\n", nControl, rsp);
233 static int FtpOpenPort(struct stream_priv_s* p) {
234 int resp,fd;
235 char rsp_txt[256];
236 char* par,str[128];
237 int num[6];
239 resp = FtpSendCmd("PASV",p,rsp_txt);
240 if(resp != 2) {
241 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'PASV' failed: %s\n",rsp_txt);
242 return 0;
245 par = strchr(rsp_txt,'(');
247 if(!par || !par[0] || !par[1]) {
248 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] invalid server response: %s ??\n",rsp_txt);
249 return 0;
252 sscanf(par+1,"%u,%u,%u,%u,%u,%u",&num[0],&num[1],&num[2],
253 &num[3],&num[4],&num[5]);
254 snprintf(str,127,"%d.%d.%d.%d",num[0],num[1],num[2],num[3]);
255 fd = connect2Server(str,(num[4]<<8)+num[5],0);
257 if(fd < 0)
258 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] failed to create data connection\n");
260 return fd;
263 static int FtpOpenData(stream_t* s,off_t newpos) {
264 struct stream_priv_s* p = s->priv;
265 int resp;
266 char str[256],rsp_txt[256];
268 // Open a new connection
269 s->fd = FtpOpenPort(p);
271 if(s->fd < 0) return 0;
273 if(newpos > 0) {
274 snprintf(str,255,"REST %"PRId64, (int64_t)newpos);
276 resp = FtpSendCmd(str,p,rsp_txt);
277 if(resp != 3) {
278 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
279 newpos = 0;
283 // Get the file
284 snprintf(str,255,"RETR %s",p->filename);
285 resp = FtpSendCmd(str,p,rsp_txt);
287 if(resp != 1) {
288 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
289 return 0;
292 s->pos = newpos;
293 return 1;
296 static int fill_buffer(stream_t *s, char* buffer, int max_len){
297 int r;
299 if(s->fd < 0 && !FtpOpenData(s,s->pos))
300 return -1;
302 if(!fd_can_read(s->fd, 15)) {
303 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] read timed out\n");
304 return -1;
307 r = recv(s->fd,buffer,max_len,0);
308 return (r <= 0) ? -1 : r;
311 static int seek(stream_t *s,off_t newpos) {
312 struct stream_priv_s* p = s->priv;
313 int resp;
314 char rsp_txt[256];
316 if(s->pos > s->end_pos) {
317 s->eof=1;
318 return 0;
321 // Check to see if the server did not already terminate the transfer
322 if(fd_can_read(p->handle, 0)) {
323 if(readresp(p,rsp_txt) != 2)
324 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] Warning the server didn't finished the transfer correctly: %s\n",rsp_txt);
325 closesocket(s->fd);
326 s->fd = -1;
329 // Close current download
330 if(s->fd >= 0) {
331 static const char pre_cmd[]={TELNET_IAC,TELNET_IP,TELNET_IAC,TELNET_SYNCH};
332 //int fl;
334 // First close the fd
335 closesocket(s->fd);
336 s->fd = 0;
338 // Send send the telnet sequence needed to make the server react
340 // Dunno if this is really needed, lftp have it. I let
341 // it here in case it turn out to be needed on some other OS
342 //fl=fcntl(p->handle,F_GETFL);
343 //fcntl(p->handle,F_SETFL,fl&~O_NONBLOCK);
345 // send only first byte as OOB due to OOB braindamage in many unices
346 send(p->handle,pre_cmd,1,MSG_OOB|DEFAULT_SEND_FLAGS);
347 send(p->handle,pre_cmd+1,sizeof(pre_cmd)-1,DEFAULT_SEND_FLAGS);
349 //fcntl(p->handle,F_SETFL,fl);
351 // Get the 426 Transfer aborted
352 // Or the 226 Transfer complete
353 resp = readresp(p,rsp_txt);
354 if(resp != 4 && resp != 2) {
355 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Server didn't abort correctly: %s\n",rsp_txt);
356 s->eof = 1;
357 return 0;
359 // Send the ABOR command
360 // Ignore the return code as sometimes it fail with "nothing to abort"
361 FtpSendCmd("ABOR",p,rsp_txt);
363 return FtpOpenData(s,newpos);
367 static void close_f(stream_t *s) {
368 struct stream_priv_s* p = s->priv;
370 if(!p) return;
372 if(s->fd > 0) {
373 closesocket(s->fd);
374 s->fd = 0;
377 FtpSendCmd("QUIT",p,NULL);
379 if(p->handle) closesocket(p->handle);
380 free(p->buf);
382 m_struct_free(&stream_opts,p);
387 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
388 int resp;
389 int64_t len = 0;
390 struct stream_priv_s* p = (struct stream_priv_s*)opts;
391 char str[256],rsp_txt[256];
393 if(mode != STREAM_READ) {
394 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Unknown open mode %d\n",mode);
395 m_struct_free(&stream_opts,opts);
396 return STREAM_UNSUPPORTED;
399 if(!p->filename || !p->host) {
400 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Bad url\n");
401 m_struct_free(&stream_opts,opts);
402 return STREAM_ERROR;
405 // Open the control connection
406 p->handle = connect2Server(p->host,p->port,1);
408 if(p->handle < 0) {
409 m_struct_free(&stream_opts,opts);
410 return STREAM_ERROR;
413 // We got a connection, let's start serious things
414 stream->fd = -1;
415 stream->priv = p;
416 p->buf = malloc(BUFSIZE);
418 if (readresp(p, NULL) == 0) {
419 close_f(stream);
420 m_struct_free(&stream_opts,opts);
421 return STREAM_ERROR;
424 // Login
425 snprintf(str,255,"USER %s",p->user);
426 resp = FtpSendCmd(str,p,rsp_txt);
428 // password needed
429 if(resp == 3) {
430 snprintf(str,255,"PASS %s",p->pass);
431 resp = FtpSendCmd(str,p,rsp_txt);
432 if(resp != 2) {
433 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
434 close_f(stream);
435 return STREAM_ERROR;
437 } else if(resp != 2) {
438 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
439 close_f(stream);
440 return STREAM_ERROR;
443 // Set the transfer type
444 resp = FtpSendCmd("TYPE I",p,rsp_txt);
445 if(resp != 2) {
446 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'TYPE I' failed: %s\n",rsp_txt);
447 close_f(stream);
448 return STREAM_ERROR;
451 // Get the filesize
452 snprintf(str,255,"SIZE %s",p->filename);
453 resp = FtpSendCmd(str,p,rsp_txt);
454 if(resp != 2) {
455 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
456 } else {
457 int dummy;
458 sscanf(rsp_txt,"%d %"SCNd64,&dummy,&len);
461 if(len > 0) {
462 stream->seek = seek;
463 stream->end_pos = len;
466 // The data connection is really opened only at the first
467 // read/seek. This must be done when the cache is used
468 // because the connection would stay open in the main process,
469 // preventing correct abort with many servers.
470 stream->fd = -1;
471 stream->priv = p;
472 stream->fill_buffer = fill_buffer;
473 stream->close = close_f;
475 return STREAM_OK;
478 const stream_info_t stream_info_ftp = {
479 "File Transfer Protocol",
480 "ftp",
481 "Albeu",
482 "reuse a bit of code from ftplib written by Thomas Pfau",
483 open_f,
484 { "ftp", NULL },
485 &stream_opts,
486 1 // Urls are an option string