2 * Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
4 * This file is part of FFmpeg.
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.
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.
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
22 #include <libssh/sftp.h>
23 #include "libavutil/avstring.h"
24 #include "libavutil/opt.h"
39 static int libssh_close(URLContext *h)
41 LIBSSHContext *s = h->priv_data;
47 ssh_disconnect(s->session);
53 static int libssh_open(URLContext *h, const char *url, int flags)
55 static const int verbosity = SSH_LOG_NOLOG;
56 LIBSSHContext *s = h->priv_data;
57 char proto[10], path[MAX_URL_SIZE], hostname[1024], credencials[1024];
58 int port = 22, access, ret;
59 long timeout = s->rw_timeout * 1000;
60 const char *user = NULL, *pass = NULL;
64 av_url_split(proto, sizeof(proto),
65 credencials, sizeof(credencials),
66 hostname, sizeof(hostname),
71 if (port <= 0 || port > 65535)
74 if (!(s->session = ssh_new())) {
75 ret = AVERROR(ENOMEM);
78 user = av_strtok(credencials, ":", &end);
79 pass = av_strtok(end, ":", &end);
80 ssh_options_set(s->session, SSH_OPTIONS_HOST, hostname);
81 ssh_options_set(s->session, SSH_OPTIONS_PORT, &port);
82 ssh_options_set(s->session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
84 ssh_options_set(s->session, SSH_OPTIONS_TIMEOUT_USEC, &timeout);
86 ssh_options_set(s->session, SSH_OPTIONS_USER, user);
88 if (ssh_connect(s->session) != SSH_OK) {
89 av_log(h, AV_LOG_ERROR, "Connection failed. %s\n", ssh_get_error(s->session));
94 if (ssh_userauth_autopubkey(s->session, pass) != SSH_AUTH_SUCCESS) {
95 av_log(s, AV_LOG_DEBUG, "Authentication using public key failed, trying password method.\n");
96 if (ssh_userauth_password(s->session, NULL, pass) != SSH_AUTH_SUCCESS) {
97 av_log(h, AV_LOG_ERROR, "Authentication failed.\n");
98 ret = AVERROR(EACCES);
103 if (!(s->sftp = sftp_new(s->session))) {
104 av_log(h, AV_LOG_ERROR, "SFTP session creation failed: %s\n", ssh_get_error(s->session));
105 ret = AVERROR(ENOMEM);
109 if (sftp_init(s->sftp) != SSH_OK) {
110 av_log(h, AV_LOG_ERROR, "Error initializing sftp session: %s\n", ssh_get_error(s->session));
115 if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
116 access = O_CREAT | O_RDWR;
119 } else if (flags & AVIO_FLAG_WRITE) {
120 access = O_CREAT | O_WRONLY;
127 /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
128 if (!(s->file = sftp_open(s->sftp, path, access, 0666))) {
129 av_log(h, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(s->session));
134 if (!(stat = sftp_fstat(s->file))) {
135 av_log(h, AV_LOG_WARNING, "Cannot stat remote file %s.\n", path);
138 s->filesize = stat->size;
139 sftp_attributes_free(stat);
149 static int64_t libssh_seek(URLContext *h, int64_t pos, int whence)
151 LIBSSHContext *s = h->priv_data;
154 if (s->filesize == -1 && (whence == AVSEEK_SIZE || whence == SEEK_END)) {
155 av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
166 newpos = sftp_tell64(s->file);
169 newpos = s->filesize + pos;
172 return AVERROR(EINVAL);
175 if (sftp_seek64(s->file, newpos)) {
176 av_log(h, AV_LOG_ERROR, "Error during seeking.\n");
183 static int libssh_read(URLContext *h, unsigned char *buf, int size)
185 LIBSSHContext *s = h->priv_data;
188 if ((bytes_read = sftp_read(s->file, buf, size)) < 0) {
189 av_log(h, AV_LOG_ERROR, "Read error.\n");
195 static int libssh_write(URLContext *h, const unsigned char *buf, int size)
197 LIBSSHContext *s = h->priv_data;
200 if ((bytes_written = sftp_write(s->file, buf, size)) < 0) {
201 av_log(h, AV_LOG_ERROR, "Write error.\n");
204 return bytes_written;
207 #define OFFSET(x) offsetof(LIBSSHContext, x)
208 #define D AV_OPT_FLAG_DECODING_PARAM
209 #define E AV_OPT_FLAG_ENCODING_PARAM
210 static const AVOption options[] = {
211 {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
212 {"truncate", "Truncate existing files on write", OFFSET(trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
216 static const AVClass libssh_context_class = {
217 .class_name = "libssh",
218 .item_name = av_default_item_name,
220 .version = LIBAVUTIL_VERSION_INT,
223 URLProtocol ff_libssh_protocol = {
225 .url_open = libssh_open,
226 .url_read = libssh_read,
227 .url_write = libssh_write,
228 .url_seek = libssh_seek,
229 .url_close = libssh_close,
230 .priv_data_size = sizeof(LIBSSHContext),
231 .priv_data_class = &libssh_context_class,
232 .flags = URL_PROTOCOL_FLAG_NETWORK,