]> git.sesse.net Git - ffmpeg/blob - libavformat/ftp.c
Merge commit 'e69222c50755030eef921e899e38b71a111093d9'
[ffmpeg] / libavformat / ftp.c
1 /*
2  * Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <stdlib.h>
22 #include "libavutil/avstring.h"
23 #include "libavutil/avassert.h"
24 #include "avformat.h"
25 #include "internal.h"
26 #include "network.h"
27 #include "os_support.h"
28 #include "url.h"
29 #include "libavutil/opt.h"
30
31 #define CONTROL_BUFFER_SIZE 1024
32
33 typedef enum {
34     UNKNOWN,
35     READY,
36     DOWNLOADING,
37     UPLOADING,
38     DISCONNECTED
39 } FTPState;
40
41
42 typedef struct {
43     const AVClass *class;
44     URLContext *conn_control;        /**< Control connection */
45     int conn_control_block_flag;     /**< Controls block/unblock mode of data connection */
46     AVIOInterruptCB conn_control_interrupt_cb; /**< Controls block/unblock mode of data connection */
47     URLContext *conn_data;           /**< Data connection, NULL when not connected */
48     uint8_t control_buffer[CONTROL_BUFFER_SIZE], *control_buf_ptr, *control_buf_end; /**< Control connection buffer */
49     int server_data_port;            /**< Data connection port opened by server, -1 on error. */
50     char hostname[512];              /**< Server address. */
51     char path[MAX_URL_SIZE];         /**< Path to resource on server. */
52     int64_t filesize;                /**< Size of file on server, -1 on error. */
53     int64_t position;                /**< Current position, calculated. */
54     int rw_timeout;                  /**< Network timeout. */
55     const char *anonymous_password;  /**< Password to be used for anonymous user. An email should be used. */
56     int write_seekable;              /**< Control seekability, 0 = disable, 1 = enable. */
57     FTPState state;                  /**< State of data connection */
58 } FTPContext;
59
60 #define OFFSET(x) offsetof(FTPContext, x)
61 #define D AV_OPT_FLAG_DECODING_PARAM
62 #define E AV_OPT_FLAG_ENCODING_PARAM
63 static const AVOption options[] = {
64     {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
65     {"ftp-write-seekable", "control seekability of connection during encoding", OFFSET(write_seekable), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, E },
66     {"ftp-anonymous-password", "password for anonynous login. E-mail address should be used.", OFFSET(anonymous_password), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
67     {NULL}
68 };
69
70 static const AVClass ftp_context_class = {
71     .class_name     = "ftp",
72     .item_name      = av_default_item_name,
73     .option         = options,
74     .version        = LIBAVUTIL_VERSION_INT,
75 };
76
77 static int ftp_conn_control_block_control(void *data)
78 {
79     FTPContext *s = data;
80     return s->conn_control_block_flag;
81 }
82
83 static int ftp_getc(FTPContext *s)
84 {
85     int len;
86     if (s->control_buf_ptr >= s->control_buf_end) {
87         if (s->conn_control_block_flag)
88             return AVERROR_EXIT;
89         len = ffurl_read(s->conn_control, s->control_buffer, CONTROL_BUFFER_SIZE);
90         if (len < 0) {
91             return len;
92         } else if (!len) {
93             return -1;
94         } else {
95             s->control_buf_ptr = s->control_buffer;
96             s->control_buf_end = s->control_buffer + len;
97         }
98     }
99     return *s->control_buf_ptr++;
100 }
101
102 static int ftp_get_line(FTPContext *s, char *line, int line_size)
103 {
104     int ch;
105     char *q = line;
106     int ori_block_flag = s->conn_control_block_flag;
107
108     for (;;) {
109         ch = ftp_getc(s);
110         if (ch < 0) {
111             s->conn_control_block_flag = ori_block_flag;
112             return ch;
113         }
114         if (ch == '\n') {
115             /* process line */
116             if (q > line && q[-1] == '\r')
117                 q--;
118             *q = '\0';
119
120             s->conn_control_block_flag = ori_block_flag;
121             return 0;
122         } else {
123             s->conn_control_block_flag = 0; /* line need to be finished */
124             if ((q - line) < line_size - 1)
125                 *q++ = ch;
126         }
127     }
128 }
129
130 /*
131  * This routine returns ftp server response code.
132  * Server may send more than one response for a certain command, following priorites are used:
133  *   - 5xx code is returned if occurred. (means error)
134  *   - When pref_code is set then pref_code is return if occurred. (expected result)
135  *   - The lowest code is returned. (means success)
136  */
137 static int ftp_status(FTPContext *s, int *major, int *minor, int *extra, char **line, int pref_code)
138 {
139     int err, result = -1, pref_code_found = 0;
140     char buf[CONTROL_BUFFER_SIZE];
141     unsigned char d_major, d_minor, d_extra;
142
143     /* Set blocking mode */
144     s->conn_control_block_flag = 0;
145     for (;;) {
146         if ((err = ftp_get_line(s, buf, CONTROL_BUFFER_SIZE)) < 0) {
147             if (err == AVERROR_EXIT)
148                 return result;
149             return err;
150         }
151
152         if (strlen(buf) < 3)
153             continue;
154         d_major = buf[0];
155         if (d_major < '1' || d_major > '6' || d_major == '4')
156             continue;
157         d_minor = buf[1];
158         if (d_minor < '0' || d_minor > '9')
159             continue;
160         d_extra = buf[2];
161         if (d_extra < '0' || d_extra > '9')
162             continue;
163
164         av_log(s, AV_LOG_DEBUG, "%s\n", buf);
165
166         err = d_major * 100 + d_minor * 10 + d_extra - 111 * '0';
167
168         if ((result < 0 || err < result || pref_code == err) && !pref_code_found || d_major == '5') {
169             if (pref_code == err || d_major == '5')
170                 pref_code_found = 1;
171             result = err;
172             if (major)
173                 *major = d_major - '0';
174             if (minor)
175                 *minor = d_minor - '0';
176             if (extra)
177                 *extra = d_extra - '0';
178             if (line)
179                 *line = av_strdup(buf);
180         }
181
182         /* first code received. Now get all lines in non blocking mode */
183         if (pref_code < 0 || pref_code_found)
184             s->conn_control_block_flag = 1;
185     }
186     return result;
187 }
188
189 static int ftp_auth(FTPContext *s, char *auth)
190 {
191     const char *user = NULL, *pass = NULL;
192     char *end = NULL, buf[CONTROL_BUFFER_SIZE];
193     int err;
194     av_assert2(auth);
195
196     user = av_strtok(auth, ":", &end);
197     pass = av_strtok(end, ":", &end);
198
199     if (user) {
200         snprintf(buf, sizeof(buf), "USER %s\r\n", user);
201         if ((err = ffurl_write(s->conn_control, buf, strlen(buf))) < 0)
202             return err;
203         ftp_status(s, &err, NULL, NULL, NULL, -1);
204         if (err == 3) {
205             if (pass) {
206                 snprintf(buf, sizeof(buf), "PASS %s\r\n", pass);
207                 if ((err = ffurl_write(s->conn_control, buf, strlen(buf))) < 0)
208                     return err;
209                 ftp_status(s, &err, NULL, NULL, NULL, -1);
210             } else
211                 return AVERROR(EACCES);
212         }
213         if (err != 2) {
214             return AVERROR(EACCES);
215         }
216     } else {
217         const char* command = "USER anonymous\r\n";
218         if ((err = ffurl_write(s->conn_control, command, strlen(command))) < 0)
219             return err;
220         ftp_status(s, &err, NULL, NULL, NULL, -1);
221         if (err == 3) {
222             if (s->anonymous_password) {
223                 snprintf(buf, sizeof(buf), "PASS %s\r\n", s->anonymous_password);
224             } else
225                 snprintf(buf, sizeof(buf), "PASS nopassword\r\n");
226             if ((err = ffurl_write(s->conn_control, buf, strlen(buf))) < 0)
227                 return err;
228             ftp_status(s, &err, NULL, NULL, NULL, -1);
229         }
230         if (err != 2) {
231             return AVERROR(EACCES);
232         }
233     }
234
235     return 0;
236 }
237
238 static int ftp_passive_mode(FTPContext *s)
239 {
240     char *res = NULL, *start, *end;
241     int err, i;
242     const char *command = "PASV\r\n";
243
244     if ((err = ffurl_write(s->conn_control, command, strlen(command))) < 0)
245         return err;
246     if (ftp_status(s, NULL, NULL, NULL, &res, 227) != 227)
247         goto fail;
248
249     start = NULL;
250     for (i = 0; i < strlen(res); ++i) {
251         if (res[i] == '(') {
252             start = res + i + 1;
253         } else if (res[i] == ')') {
254             end = res + i;
255             break;
256         }
257     }
258     if (!start || !end)
259         goto fail;
260
261     *end  = '\0';
262     /* skip ip */
263     if (!av_strtok(start, ",", &end)) goto fail;
264     if (!av_strtok(end, ",", &end)) goto fail;
265     if (!av_strtok(end, ",", &end)) goto fail;
266     if (!av_strtok(end, ",", &end)) goto fail;
267
268     /* parse port number */
269     start = av_strtok(end, ",", &end);
270     if (!start) goto fail;
271     s->server_data_port = atoi(start) * 256;
272     start = av_strtok(end, ",", &end);
273     if (!start) goto fail;
274     s->server_data_port += atoi(start);
275     av_dlog(s, "Server data port: %d\n", s->server_data_port);
276
277     av_free(res);
278     return 0;
279
280   fail:
281     av_free(res);
282     s->server_data_port = -1;
283     return AVERROR(EIO);
284 }
285
286 static int ftp_current_dir(FTPContext *s)
287 {
288     char *res = NULL, *start = NULL, *end = NULL;
289     int err, i;
290     const char *command = "PWD\r\n";
291
292     if ((err = ffurl_write(s->conn_control, command, strlen(command))) < 0)
293         return err;
294     if (ftp_status(s, NULL, NULL, NULL, &res, 257) != 257)
295         goto fail;
296
297     for (i = 0; res[i]; ++i) {
298         if (res[i] == '"') {
299             if (!start) {
300                 start = res + i + 1;
301                 continue;
302             }
303             end = res + i;
304             break;
305         }
306     }
307
308     if (!end)
309         goto fail;
310
311     if (end > res && end[-1] == '/') {
312         end[-1] = '\0';
313     } else
314         *end = '\0';
315     av_strlcpy(s->path, start, sizeof(s->path));
316
317     av_free(res);
318     return 0;
319
320   fail:
321     av_free(res);
322     return AVERROR(EIO);
323 }
324
325 static int ftp_file_size(FTPContext *s)
326 {
327     char buf[CONTROL_BUFFER_SIZE];
328     int err;
329     char *res = NULL;
330
331     snprintf(buf, sizeof(buf), "SIZE %s\r\n", s->path);
332     if ((err = ffurl_write(s->conn_control, buf, strlen(buf))) < 0)
333         return err;
334     if (ftp_status(s, NULL, NULL, NULL, &res, 213) == 213) {
335         s->filesize = strtoll(&res[4], NULL, 10);
336     } else {
337         s->filesize = -1;
338         av_free(res);
339         return AVERROR(EIO);
340     }
341
342     av_free(res);
343     return 0;
344 }
345
346 static int ftp_retrieve(FTPContext *s)
347 {
348     char buf[CONTROL_BUFFER_SIZE];
349     int err;
350
351     snprintf(buf, sizeof(buf), "RETR %s\r\n", s->path);
352     if ((err = ffurl_write(s->conn_control, buf, strlen(buf))) < 0)
353         return err;
354     if (ftp_status(s, NULL, NULL, NULL, NULL, 150) != 150)
355         return AVERROR(EIO);
356
357     s->state = DOWNLOADING;
358
359     return 0;
360 }
361
362 static int ftp_store(FTPContext *s)
363 {
364     char buf[CONTROL_BUFFER_SIZE];
365     int err;
366
367     snprintf(buf, sizeof(buf), "STOR %s\r\n", s->path);
368     if ((err = ffurl_write(s->conn_control, buf, strlen(buf))) < 0)
369         return err;
370     if (ftp_status(s, NULL, NULL, NULL, NULL, 150) != 150)
371         return AVERROR(EIO);
372
373     s->state = UPLOADING;
374
375     return 0;
376 }
377
378 static int ftp_send_command(FTPContext *s, const char* command)
379 {
380     int err;
381
382     if ((err = ffurl_write(s->conn_control, command, strlen(command))) < 0)
383         return err;
384     ftp_status(s, &err, NULL, NULL, NULL, -1);
385     if (err != 2)
386         return AVERROR(EIO);
387
388     return 0;
389 }
390
391 static int ftp_reconnect_data_connection(URLContext *h)
392 {
393     int err;
394     char buf[CONTROL_BUFFER_SIZE], opts_format[20];
395     AVDictionary *opts = NULL;
396     FTPContext *s = h->priv_data;
397
398     if (!s->conn_data) {
399         ff_url_join(buf, sizeof(buf), "tcp", NULL, s->hostname, s->server_data_port, NULL);
400         if (s->rw_timeout != -1) {
401             snprintf(opts_format, sizeof(opts_format), "%d", s->rw_timeout);
402             av_dict_set(&opts, "timeout", opts_format, 0);
403         } /* if option is not given, don't pass it and let tcp use its own default */
404         err = ffurl_open(&s->conn_data, buf, AVIO_FLAG_READ_WRITE,
405                          &h->interrupt_callback, &opts);
406         av_dict_free(&opts);
407         if (err < 0)
408             return err;
409     }
410     s->state = READY;
411     s->position = 0;
412     return 0;
413 }
414
415 static int ftp_open(URLContext *h, const char *url, int flags)
416 {
417     char proto[10], auth[1024], path[MAX_URL_SIZE], buf[CONTROL_BUFFER_SIZE], opts_format[20];
418     AVDictionary *opts = NULL;
419     int port, err;
420     FTPContext *s = h->priv_data;
421
422     av_dlog(h, "ftp protocol open\n");
423
424     s->state = DISCONNECTED;
425     s->filesize = -1;
426     s->conn_control_interrupt_cb.opaque = s;
427     s->conn_control_interrupt_cb.callback = ftp_conn_control_block_control;
428
429     av_url_split(proto, sizeof(proto),
430                  auth, sizeof(auth),
431                  s->hostname, sizeof(s->hostname),
432                  &port,
433                  path, sizeof(path),
434                  url);
435
436     if (port < 0)
437         port = 21;
438
439     if (!s->conn_control) {
440         ff_url_join(buf, sizeof(buf), "tcp", NULL, s->hostname, port, NULL);
441         if (s->rw_timeout != -1) {
442             snprintf(opts_format, sizeof(opts_format), "%d", s->rw_timeout);
443             av_dict_set(&opts, "timeout", opts_format, 0);
444         } /* if option is not given, don't pass it and let tcp use its own default */
445         err = ffurl_open(&s->conn_control, buf, AVIO_FLAG_READ_WRITE,
446                          &s->conn_control_interrupt_cb, &opts);
447         av_dict_free(&opts);
448         if (err < 0)
449             goto fail;
450
451         /* consume all messages from server */
452         if (ftp_status(s, NULL, NULL, NULL, NULL, 220) != 220) {
453             av_log(h, AV_LOG_ERROR, "Server not ready for new users\n");
454             err = AVERROR(EACCES);
455             goto fail;
456         }
457
458         if ((err = ftp_auth(s, auth)) < 0)
459             goto fail;
460
461         if ((err = ftp_send_command(s, "TYPE I\r\n")) < 0)
462             goto fail;
463
464         if ((err = ftp_current_dir(s)) < 0)
465             goto fail;
466         av_strlcat(s->path, path, sizeof(s->path));
467
468         if ((err = ftp_passive_mode(s)) < 0)
469             goto fail;
470
471         if (ftp_file_size(s) < 0 && flags & AVIO_FLAG_READ)
472             h->is_streamed = 1;
473         if (s->write_seekable != 1 && flags & AVIO_FLAG_WRITE)
474             h->is_streamed = 1;
475     }
476
477     if ((err = ftp_reconnect_data_connection(h)) < 0)
478         goto fail;
479
480     return 0;
481   fail:
482     av_log(h, AV_LOG_ERROR, "FTP open failed\n");
483     ffurl_closep(&s->conn_control);
484     ffurl_closep(&s->conn_data);
485     return err;
486 }
487
488 static int ftp_read(URLContext *h, unsigned char *buf, int size)
489 {
490     FTPContext *s = h->priv_data;
491     int read;
492
493     av_dlog(h, "ftp protocol read %d bytes\n", size);
494
495     if (s->state == READY) {
496         ftp_retrieve(s);
497     }
498     if (s->conn_data && s->state == DOWNLOADING) {
499         read = ffurl_read(s->conn_data, buf, size);
500         if (read >= 0) {
501             s->position += read;
502             if (s->position >= s->filesize) {
503                 ffurl_closep(&s->conn_data);
504                 s->state = DISCONNECTED;
505                 if (ftp_status(s, NULL, NULL, NULL,NULL, 226) != 226)
506                     return AVERROR(EIO);
507             }
508         }
509         return read;
510     }
511
512     av_log(h, AV_LOG_DEBUG, "FTP read failed\n");
513     return AVERROR(EIO);
514 }
515
516 static int ftp_write(URLContext *h, const unsigned char *buf, int size)
517 {
518     FTPContext *s = h->priv_data;
519     int written;
520
521     av_dlog(h, "ftp protocol write %d bytes\n", size);
522
523     if (s->state == READY) {
524         ftp_store(s);
525     }
526     if (s->conn_data && s->state == UPLOADING) {
527         written = ffurl_write(s->conn_data, buf, size);
528         if (written > 0) {
529             s->position += written;
530             s->filesize = FFMAX(s->filesize, s->position);
531         }
532         return written;
533     }
534
535     av_log(h, AV_LOG_ERROR, "FTP write failed\n");
536     return AVERROR(EIO);
537 }
538
539 static int64_t ftp_seek(URLContext *h, int64_t pos, int whence)
540 {
541     FTPContext *s = h->priv_data;
542     char buf[CONTROL_BUFFER_SIZE];
543     int err;
544     int64_t new_pos;
545
546     av_dlog(h, "ftp protocol seek %"PRId64" %d\n", pos, whence);
547
548     switch(whence) {
549     case AVSEEK_SIZE:
550         return s->filesize;
551     case SEEK_SET:
552         new_pos = pos;
553         break;
554     case SEEK_CUR:
555         new_pos = s->position + pos;
556         break;
557     case SEEK_END:
558         if (s->filesize < 0)
559             return AVERROR(EIO);
560         new_pos = s->filesize + pos;
561         break;
562     default:
563         return AVERROR(EINVAL);
564     }
565
566     if  (h->is_streamed)
567         return AVERROR(EIO);
568
569     if (new_pos < 0 || (s->filesize >= 0 && new_pos > s->filesize))
570         return AVERROR(EINVAL);
571
572     if (new_pos != s->position) {
573         /* close existing data connection */
574         if (s->state != READY) {
575             if (s->conn_data) {
576                 /* abort existing transfer */
577                 if (s->state == DOWNLOADING) {
578                     snprintf(buf, sizeof(buf), "ABOR\r\n");
579                     if ((err = ffurl_write(s->conn_control, buf, strlen(buf))) < 0)
580                         return err;
581                 }
582                 ffurl_closep(&s->conn_data);
583                 s->state = DISCONNECTED;
584                 /* Servers return 225 or 226 */
585                 ftp_status(s, &err, NULL, NULL, NULL, -1);
586                 if (err != 2)
587                     return AVERROR(EIO);
588             }
589
590             /* set passive */
591             if ((err = ftp_passive_mode(s)) < 0)
592                 return err;
593
594             /* open new data connection */
595             if ((err = ftp_reconnect_data_connection(h)) < 0)
596                 return err;
597         }
598
599         /* resume from pos position */
600         snprintf(buf, sizeof(buf), "REST %"PRId64"\r\n", pos);
601         if ((err = ffurl_write(s->conn_control, buf, strlen(buf))) < 0)
602             return err;
603         if (ftp_status(s, NULL, NULL, NULL, NULL, 350) != 350)
604             return AVERROR(EIO);
605
606         s->position = pos;
607     }
608     return new_pos;
609 }
610
611 static int ftp_close(URLContext *h)
612 {
613     FTPContext *s = h->priv_data;
614
615     av_dlog(h, "ftp protocol close\n");
616
617     ffurl_closep(&s->conn_control);
618     ffurl_closep(&s->conn_data);
619
620     return 0;
621 }
622
623 static int ftp_get_file_handle(URLContext *h)
624 {
625     FTPContext *s = h->priv_data;
626
627     av_dlog(h, "ftp protocol get_file_handle\n");
628
629     if (s->conn_data)
630         return ffurl_get_file_handle(s->conn_data);
631
632     return AVERROR(EIO);
633 }
634
635 static int ftp_shutdown(URLContext *h, int flags)
636 {
637     FTPContext *s = h->priv_data;
638
639     av_dlog(h, "ftp protocol shutdown\n");
640
641     if (s->conn_data)
642         return ffurl_shutdown(s->conn_data, flags);
643
644     return AVERROR(EIO);
645 }
646
647 URLProtocol ff_ftp_protocol = {
648     .name                = "ftp",
649     .url_open            = ftp_open,
650     .url_read            = ftp_read,
651     .url_write           = ftp_write,
652     .url_seek            = ftp_seek,
653     .url_close           = ftp_close,
654     .url_get_file_handle = ftp_get_file_handle,
655     .url_shutdown        = ftp_shutdown,
656     .priv_data_size      = sizeof(FTPContext),
657     .priv_data_class     = &ftp_context_class,
658     .flags               = URL_PROTOCOL_FLAG_NETWORK,
659 };