21 #include "libavutil/avstring.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/bprint.h"
28 #define CONTROL_BUFFER_SIZE 1024
29 #define CREDENTIALS_BUFFER_SIZE 128
58 #define OFFSET(x) offsetof(FTPContext, x)
59 #define D AV_OPT_FLAG_DECODING_PARAM
60 #define E AV_OPT_FLAG_ENCODING_PARAM
62 {
"timeout",
"set timeout of socket I/O operations",
OFFSET(rw_timeout),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D|
E },
63 {
"ftp-write-seekable",
"control seekability of connection during encoding",
OFFSET(write_seekable),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
E },
64 {
"ftp-anonymous-password",
"password for anonymous login. E-mail address should be used.",
OFFSET(anonymous_password),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
104 if (q > line && q[-1] ==
'\r')
109 if ((q - line) < line_size - 1)
122 int err, i, dash = 0, result = 0, code_found = 0;
129 while (!code_found || dash) {
142 for (i = 0; i < 3; ++i) {
143 if (buf[i] <
'0' || buf[i] >
'9')
148 dash = !!(buf[3] ==
'-');
150 for (i = 0; response_codes[i]; ++i) {
151 if (err == response_codes[i]) {
167 const int response_codes[],
char **response)
177 if (response_codes) {
178 return ftp_status(s, response, response_codes);
198 const char *user = NULL, *
pass = NULL;
201 static const int user_codes[] = {331, 230, 500, 530, 0};
202 static const int pass_codes[] = {230, 503, 530, 0};
207 user =
av_strtok(credencials,
":", &end);
215 snprintf(buf,
sizeof(buf),
"USER %s\r\n", user);
219 snprintf(buf,
sizeof(buf),
"PASS %s\r\n", pass);
234 static const char *
command =
"PASV\r\n";
235 static const int pasv_codes[] = {227, 501, 0};
240 for (i = 0; res[i]; ++i) {
243 }
else if (res[i] ==
')') {
260 if (!start)
goto fail;
263 if (!start)
goto fail;
274 "Your FTP server may use IPv6 which is not supported yet.\n");
282 static const char *
command =
"PWD\r\n";
283 static const int pwd_codes[] = {257, 0};
288 for (i = 0; res[i]; ++i) {
302 if (end > res && end[-1] ==
'/') {
320 static const int size_codes[] = {213, 501, 550, 0};
322 snprintf(command,
sizeof(command),
"SIZE %s\r\n", s->
path);
324 s->
filesize = strtoll(&res[4], NULL, 10);
338 static const int retr_codes[] = {150, 550, 554, 0};
340 snprintf(command,
sizeof(command),
"RETR %s\r\n", s->
path);
352 static const int stor_codes[] = {150, 0};
354 snprintf(command,
sizeof(command),
"STOR %s\r\n", s->
path);
365 static const char *
command =
"TYPE I\r\n";
366 static const int type_codes[] = {200, 500, 504, 0};
377 static const int rest_codes[] = {350, 500, 501, 0};
379 snprintf(command,
sizeof(command),
"REST %"PRId64
"\r\n", pos);
392 static const int connect_codes[] = {220, 0};
416 av_log(h,
AV_LOG_WARNING,
"Pure-FTPd server is used as an output protocol. It is known issue this implementation may produce incorrect content and it cannot be fixed at this moment.");
466 static const char *
command =
"ABOR\r\n";
468 static const int abor_codes[] = {225, 226, 0};
508 av_dlog(h,
"ftp protocol open\n");
553 int64_t new_pos, fake_pos;
555 av_dlog(h,
"ftp protocol seek %"PRId64
" %d\n", pos, whence);
579 new_pos =
FFMAX(0, new_pos);
593 int read, err, retry_done = 0;
595 av_dlog(h,
"ftp protocol read %d bytes\n", size);
625 if (read <= 0 && s->position < s->filesize && !h->
is_streamed) {
631 if ((err =
ftp_seek(h, pos, SEEK_SET)) < 0) {
653 av_dlog(h,
"ftp protocol write %d bytes\n", size);
678 av_dlog(h,
"ftp protocol close\n");
689 av_dlog(h,
"ftp protocol get_file_handle\n");
701 av_dlog(h,
"ftp protocol shutdown\n");
719 .priv_data_class = &ftp_context_class,
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
#define URL_PROTOCOL_FLAG_NETWORK
static int ftp_get_line(FTPContext *s, char *line, int line_size)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
static int ftp_file_size(FTPContext *s)
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
int is_streamed
true if streamed (no seek possible), default = false
AVIOInterruptCB interrupt_callback
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
static int ftp_open(URLContext *h, const char *url, int flags)
uint8_t * control_buf_end
static int ftp_status(FTPContext *s, char **line, const int response_codes[])
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
int write_seekable
Control seekability, 0 = disable, 1 = enable.
static int ftp_retrieve(FTPContext *s)
int ffurl_shutdown(URLContext *h, int flags)
Signal the URLContext that we are done reading or writing the stream.
URLContext * conn_control
Control connection.
static int ftp_connect_data_connection(URLContext *h)
const char * av_default_item_name(void *ctx)
Return the context name.
static int ftp_shutdown(URLContext *h, int flags)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static av_cold int end(AVCodecContext *avctx)
int64_t filesize
Size of file on server, -1 on error.
static int ftp_connect_control_connection(URLContext *h)
uint8_t control_buffer[CONTROL_BUFFER_SIZE]
Control connection buffer.
static int ftp_current_dir(FTPContext *s)
const OptionDef options[]
uint8_t * control_buf_ptr
char path[MAX_URL_SIZE]
Path to resource on server.
static int ftp_passive_mode(FTPContext *s)
int server_data_port
Data connection port opened by server, -1 on error.
static int ftp_getc(FTPContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
#define CREDENTIALS_BUFFER_SIZE
const char * anonymous_password
Password to be used for anonymous user.
char * av_stristr(const char *haystack, const char *needle)
Locate the first case-independent occurrence in the string haystack of the string needle...
static int ftp_abort(URLContext *h)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
void av_dict_free(AVDictionary **m)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
static int ftp_send_command(FTPContext *s, const char *command, const int response_codes[], char **response)
static void ftp_close_both_connections(FTPContext *s)
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
char hostname[512]
Server address.
int rw_timeout
Network timeout.
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Init a print buffer.
static int ftp_read(URLContext *h, unsigned char *buf, int size)
static int ftp_close(URLContext *h)
URLContext * conn_data
Data connection, NULL when not connected.
Buffer to print data progressively.
static int ftp_auth(FTPContext *s)
int64_t position
Current position, calculated.
static int ftp_restart(FTPContext *s, int64_t pos)
#define AV_BPRINT_SIZE_AUTOMATIC
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
static int64_t ftp_seek(URLContext *h, int64_t pos, int whence)
#define AV_LOG_INFO
Standard information.
static int ftp_get_file_handle(URLContext *h)
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
FTPState state
State of data connection.
Describe the class of an AVClass context structure.
static void ftp_close_data_connection(FTPContext *s)
char credencials[CREDENTIALS_BUFFER_SIZE]
Authentication data.
#define CONTROL_BUFFER_SIZE
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
static int ftp_write(URLContext *h, const unsigned char *buf, int size)
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
URLProtocol ff_ftp_protocol
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
static int ftp_store(FTPContext *s)
void av_bprintf(AVBPrint *buf, const char *fmt,...) av_printf_format(2
Append a formatted string to a print buffer.
static int ftp_type(FTPContext *s)
int server_control_port
Control connection port, default is 21.
static const AVClass ftp_context_class
unbuffered private I/O API
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...