Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / stream / stream_ftp.c
blob55dc519618d2be3a3075353d50541da73eb51a8e
2 #include "config.h"
4 #include <stdlib.h>
5 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #ifndef HAVE_WINSOCK2
13 #include <sys/socket.h>
14 #define closesocket close
15 #else
16 #include <winsock2.h>
17 #endif
19 #include "mp_msg.h"
20 #include "stream.h"
21 #include "help_mp.h"
22 #include "m_option.h"
23 #include "m_struct.h"
24 #include "tcp.h"
26 static struct stream_priv_s {
27 char* user;
28 char* pass;
29 char* host;
30 int port;
31 char* filename;
33 char *cput,*cget;
34 int handle;
35 int cavail,cleft;
36 char *buf;
37 } stream_priv_dflts = {
38 "anonymous","no@spam",
39 NULL,
40 21,
41 NULL,
42 NULL,
43 NULL,
46 0,0,
47 NULL
50 #define BUFSIZE 2048
52 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
53 /// URL definition
54 static const m_option_t stream_opts_fields[] = {
55 {"username", ST_OFF(user), CONF_TYPE_STRING, 0, 0 ,0, NULL},
56 {"password", ST_OFF(pass), CONF_TYPE_STRING, 0, 0 ,0, NULL},
57 {"hostname", ST_OFF(host), CONF_TYPE_STRING, 0, 0 ,0, NULL},
58 {"port", ST_OFF(port), CONF_TYPE_INT, 0, 0 ,65635, NULL},
59 {"filename", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
60 { NULL, NULL, 0, 0, 0, 0, NULL }
62 static struct m_struct_st stream_opts = {
63 "ftp",
64 sizeof(struct stream_priv_s),
65 &stream_priv_dflts,
66 stream_opts_fields
69 #define TELNET_IAC 255 /* interpret as command: */
70 #define TELNET_IP 244 /* interrupt process--permanently */
71 #define TELNET_SYNCH 242 /* for telfunc calls */
73 // Check if there is something to read on a fd. This avoid hanging
74 // forever if the network stop responding.
75 static int fd_can_read(int fd,int timeout) {
76 fd_set fds;
77 struct timeval tv;
79 FD_ZERO(&fds);
80 FD_SET(fd,&fds);
81 tv.tv_sec = timeout;
82 tv.tv_usec = 0;
84 return (select(fd+1, &fds, NULL, NULL, &tv) > 0);
88 * read a line of text
90 * return -1 on error or bytecount
92 static int readline(char *buf,int max,struct stream_priv_s *ctl)
94 int x,retval = 0;
95 char *end,*bp=buf;
96 int eof = 0;
98 do {
99 if (ctl->cavail > 0) {
100 x = (max >= ctl->cavail) ? ctl->cavail : max-1;
101 end = memccpy(bp,ctl->cget,'\n',x);
102 if (end != NULL)
103 x = end - bp;
104 retval += x;
105 bp += x;
106 *bp = '\0';
107 max -= x;
108 ctl->cget += x;
109 ctl->cavail -= x;
110 if (end != NULL) {
111 bp -= 2;
112 if (strcmp(bp,"\r\n") == 0) {
113 *bp++ = '\n';
114 *bp++ = '\0';
115 --retval;
117 break;
120 if (max == 1) {
121 *buf = '\0';
122 break;
124 if (ctl->cput == ctl->cget) {
125 ctl->cput = ctl->cget = ctl->buf;
126 ctl->cavail = 0;
127 ctl->cleft = BUFSIZE;
129 if(eof) {
130 if (retval == 0)
131 retval = -1;
132 break;
135 if(!fd_can_read(ctl->handle, 15)) {
136 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] read timed out\n");
137 retval = -1;
138 break;
141 if ((x = recv(ctl->handle,ctl->cput,ctl->cleft,0)) == -1) {
142 mp_msg(MSGT_STREAM,MSGL_ERR, "[ftp] read error: %s\n",strerror(errno));
143 retval = -1;
144 break;
146 if (x == 0)
147 eof = 1;
148 ctl->cleft -= x;
149 ctl->cavail += x;
150 ctl->cput += x;
151 } while (1);
153 return retval;
157 * read a response from the server
159 * return 0 if first char doesn't match
160 * return 1 if first char matches
162 static int readresp(struct stream_priv_s* ctl,char* rsp)
164 static char response[256];
165 char match[5];
166 int r;
168 if (readline(response,256,ctl) == -1)
169 return 0;
171 r = atoi(response)/100;
172 if(rsp) strcpy(rsp,response);
174 mp_msg(MSGT_STREAM,MSGL_V, "[ftp] < %s",response);
176 if (response[3] == '-') {
177 strncpy(match,response,3);
178 match[3] = ' ';
179 match[4] = '\0';
180 do {
181 if (readline(response,256,ctl) == -1) {
182 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Control socket read failed\n");
183 return 0;
185 mp_msg(MSGT_OPEN,MSGL_V, "[ftp] < %s",response);
186 } while (strncmp(response,match,4));
188 return r;
192 static int FtpSendCmd(const char *cmd, struct stream_priv_s *nControl,char* rsp)
194 int l = strlen(cmd);
195 int hascrlf = cmd[l - 2] == '\r' && cmd[l - 1] == '\n';
197 if(hascrlf && l == 2) mp_msg(MSGT_STREAM,MSGL_V, "\n");
198 else mp_msg(MSGT_STREAM,MSGL_V, "[ftp] > %s",cmd);
199 while(l > 0) {
200 int s = send(nControl->handle,cmd,l,0);
202 if(s <= 0) {
203 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] write error: %s\n",strerror(errno));
204 return 0;
207 cmd += s;
208 l -= s;
211 if (hascrlf)
212 return readresp(nControl,rsp);
213 else
214 return FtpSendCmd("\r\n", nControl, rsp);
217 static int FtpOpenPort(struct stream_priv_s* p) {
218 int resp,fd;
219 char rsp_txt[256];
220 char* par,str[128];
221 int num[6];
223 resp = FtpSendCmd("PASV",p,rsp_txt);
224 if(resp != 2) {
225 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'PASV' failed: %s\n",rsp_txt);
226 return 0;
229 par = strchr(rsp_txt,'(');
231 if(!par || !par[0] || !par[1]) {
232 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] invalid server response: %s ??\n",rsp_txt);
233 return 0;
236 sscanf(par+1,"%u,%u,%u,%u,%u,%u",&num[0],&num[1],&num[2],
237 &num[3],&num[4],&num[5]);
238 snprintf(str,127,"%d.%d.%d.%d",num[0],num[1],num[2],num[3]);
239 fd = connect2Server(str,(num[4]<<8)+num[5],0);
241 if(fd < 0)
242 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] failed to create data connection\n");
244 return fd;
247 static int FtpOpenData(stream_t* s,size_t newpos) {
248 struct stream_priv_s* p = s->priv;
249 int resp;
250 char str[256],rsp_txt[256];
252 // Open a new connection
253 s->fd = FtpOpenPort(p);
255 if(s->fd < 0) return 0;
257 if(newpos > 0) {
258 snprintf(str,255,"REST %"PRId64, (int64_t)newpos);
260 resp = FtpSendCmd(str,p,rsp_txt);
261 if(resp != 3) {
262 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
263 newpos = 0;
267 // Get the file
268 snprintf(str,255,"RETR %s",p->filename);
269 resp = FtpSendCmd(str,p,rsp_txt);
271 if(resp != 1) {
272 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
273 return 0;
276 s->pos = newpos;
277 return 1;
280 static int fill_buffer(stream_t *s, char* buffer, int max_len){
281 int r;
283 if(s->fd < 0 && !FtpOpenData(s,s->pos))
284 return -1;
286 if(!fd_can_read(s->fd, 15)) {
287 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] read timed out\n");
288 return -1;
291 r = recv(s->fd,buffer,max_len,0);
292 return (r <= 0) ? -1 : r;
295 static int seek(stream_t *s,off_t newpos) {
296 struct stream_priv_s* p = s->priv;
297 int resp;
298 char rsp_txt[256];
300 if(s->pos > s->end_pos) {
301 s->eof=1;
302 return 0;
305 // Check to see if the server did not already terminate the transfer
306 if(fd_can_read(p->handle, 0)) {
307 if(readresp(p,rsp_txt) != 2)
308 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] Warning the server didn't finished the transfer correctly: %s\n",rsp_txt);
309 closesocket(s->fd);
310 s->fd = -1;
313 // Close current download
314 if(s->fd >= 0) {
315 static const char pre_cmd[]={TELNET_IAC,TELNET_IP,TELNET_IAC,TELNET_SYNCH};
316 //int fl;
318 // First close the fd
319 closesocket(s->fd);
320 s->fd = 0;
322 // Send send the telnet sequence needed to make the server react
324 // Dunno if this is really needed, lftp have it. I let
325 // it here in case it turn out to be needed on some other OS
326 //fl=fcntl(p->handle,F_GETFL);
327 //fcntl(p->handle,F_SETFL,fl&~O_NONBLOCK);
329 // send only first byte as OOB due to OOB braindamage in many unices
330 send(p->handle,pre_cmd,1,MSG_OOB);
331 send(p->handle,pre_cmd+1,sizeof(pre_cmd)-1,0);
333 //fcntl(p->handle,F_SETFL,fl);
335 // Get the 426 Transfer aborted
336 // Or the 226 Transfer complete
337 resp = readresp(p,rsp_txt);
338 if(resp != 4 && resp != 2) {
339 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Server didn't abort correctly: %s\n",rsp_txt);
340 s->eof = 1;
341 return 0;
343 // Send the ABOR command
344 // Ignore the return code as sometimes it fail with "nothing to abort"
345 FtpSendCmd("ABOR",p,rsp_txt);
347 return FtpOpenData(s,newpos);
351 static void close_f(stream_t *s) {
352 struct stream_priv_s* p = s->priv;
354 if(!p) return;
356 if(s->fd > 0) {
357 closesocket(s->fd);
358 s->fd = 0;
361 FtpSendCmd("QUIT",p,NULL);
363 if(p->handle) closesocket(p->handle);
364 if(p->buf) free(p->buf);
366 m_struct_free(&stream_opts,p);
371 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
372 int len = 0,resp;
373 struct stream_priv_s* p = (struct stream_priv_s*)opts;
374 char str[256],rsp_txt[256];
376 if(mode != STREAM_READ) {
377 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Unknown open mode %d\n",mode);
378 m_struct_free(&stream_opts,opts);
379 return STREAM_UNSUPPORTED;
382 if(!p->filename || !p->host) {
383 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Bad url\n");
384 m_struct_free(&stream_opts,opts);
385 return STREAM_ERROR;
388 // Open the control connection
389 p->handle = connect2Server(p->host,p->port,1);
391 if(p->handle < 0) {
392 m_struct_free(&stream_opts,opts);
393 return STREAM_ERROR;
396 // We got a connection, let's start serious things
397 stream->fd = -1;
398 stream->priv = p;
399 p->buf = malloc(BUFSIZE);
401 if (readresp(p, NULL) == 0) {
402 close_f(stream);
403 m_struct_free(&stream_opts,opts);
404 return STREAM_ERROR;
407 // Login
408 snprintf(str,255,"USER %s",p->user);
409 resp = FtpSendCmd(str,p,rsp_txt);
411 // password needed
412 if(resp == 3) {
413 snprintf(str,255,"PASS %s",p->pass);
414 resp = FtpSendCmd(str,p,rsp_txt);
415 if(resp != 2) {
416 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
417 close_f(stream);
418 return STREAM_ERROR;
420 } else if(resp != 2) {
421 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
422 close_f(stream);
423 return STREAM_ERROR;
426 // Set the transfer type
427 resp = FtpSendCmd("TYPE I",p,rsp_txt);
428 if(resp != 2) {
429 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'TYPE I' failed: %s\n",rsp_txt);
430 close_f(stream);
431 return STREAM_ERROR;
434 // Get the filesize
435 snprintf(str,255,"SIZE %s",p->filename);
436 resp = FtpSendCmd(str,p,rsp_txt);
437 if(resp != 2) {
438 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
439 } else {
440 int dummy;
441 sscanf(rsp_txt,"%d %d",&dummy,&len);
444 if(len > 0) {
445 stream->seek = seek;
446 stream->end_pos = len;
449 // The data connection is really opened only at the first
450 // read/seek. This must be done when the cache is used
451 // because the connection would stay open in the main process,
452 // preventing correct abort with many servers.
453 stream->fd = -1;
454 stream->priv = p;
455 stream->fill_buffer = fill_buffer;
456 stream->close = close_f;
458 return STREAM_OK;
461 const stream_info_t stream_info_ftp = {
462 "File Transfer Protocol",
463 "ftp",
464 "Albeu",
465 "reuse a bit of code from ftplib written by Thomas Pfau",
466 open_f,
467 { "ftp", NULL },
468 &stream_opts,
469 1 // Urls are an option string