char *priv_key;
} LIBSSHContext;
+static av_cold int libssh_create_ssh_session(LIBSSHContext *libssh, const char* hostname, unsigned int port)
+{
+ static const int verbosity = SSH_LOG_NOLOG;
+
+ if (!(libssh->session = ssh_new())) {
+ av_log(libssh, AV_LOG_ERROR, "SSH session creation failed: %s\n", ssh_get_error(libssh->session));
+ return AVERROR(ENOMEM);
+ }
+ ssh_options_set(libssh->session, SSH_OPTIONS_HOST, hostname);
+ ssh_options_set(libssh->session, SSH_OPTIONS_PORT, &port);
+ ssh_options_set(libssh->session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
+ if (libssh->rw_timeout > 0) {
+ long timeout = libssh->rw_timeout * 1000;
+ ssh_options_set(libssh->session, SSH_OPTIONS_TIMEOUT_USEC, &timeout);
+ }
+
+ if (ssh_connect(libssh->session) != SSH_OK) {
+ av_log(libssh, AV_LOG_ERROR, "Connection failed: %s\n", ssh_get_error(libssh->session));
+ return AVERROR(EIO);
+ }
+
+ return 0;
+}
+
static av_cold int libssh_authentication(LIBSSHContext *libssh, const char *user, const char *password)
{
int authorized = 0;
return 0;
}
+static av_cold int libssh_open_file(LIBSSHContext *libssh, int flags, const char *file)
+{
+ int access;
+
+ if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
+ access = O_CREAT | O_RDWR;
+ if (libssh->trunc)
+ access |= O_TRUNC;
+ } else if (flags & AVIO_FLAG_WRITE) {
+ access = O_CREAT | O_WRONLY;
+ if (libssh->trunc)
+ access |= O_TRUNC;
+ } else
+ access = O_RDONLY;
+
+ /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
+ if (!(libssh->file = sftp_open(libssh->sftp, file, access, 0666))) {
+ av_log(libssh, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(libssh->session));
+ return AVERROR(EIO);
+ }
+
+ return 0;
+}
+
+static av_cold void libssh_stat_file(LIBSSHContext *libssh)
+{
+ sftp_attributes stat;
+
+ if (!(stat = sftp_fstat(libssh->file))) {
+ av_log(libssh, AV_LOG_WARNING, "Cannot stat remote file.\n");
+ libssh->filesize = -1;
+ } else {
+ libssh->filesize = stat->size;
+ sftp_attributes_free(stat);
+ }
+}
+
static int libssh_close(URLContext *h)
{
LIBSSHContext *s = h->priv_data;
static int libssh_open(URLContext *h, const char *url, int flags)
{
- static const int verbosity = SSH_LOG_NOLOG;
LIBSSHContext *s = h->priv_data;
char proto[10], path[MAX_URL_SIZE], hostname[1024], credencials[1024];
- int port = 22, access, ret;
- long timeout = s->rw_timeout * 1000;
+ int port = 22, ret;
const char *user = NULL, *pass = NULL;
char *end = NULL;
- sftp_attributes stat;
av_url_split(proto, sizeof(proto),
credencials, sizeof(credencials),
if (port <= 0 || port > 65535)
port = 22;
- if (!(s->session = ssh_new())) {
- ret = AVERROR(ENOMEM);
+ if ((ret = libssh_create_ssh_session(s, hostname, port)) < 0)
goto fail;
- }
+
user = av_strtok(credencials, ":", &end);
pass = av_strtok(end, ":", &end);
- ssh_options_set(s->session, SSH_OPTIONS_HOST, hostname);
- ssh_options_set(s->session, SSH_OPTIONS_PORT, &port);
- ssh_options_set(s->session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
- if (timeout > 0)
- ssh_options_set(s->session, SSH_OPTIONS_TIMEOUT_USEC, &timeout);
-
- if (ssh_connect(s->session) != SSH_OK) {
- av_log(h, AV_LOG_ERROR, "Connection failed. %s\n", ssh_get_error(s->session));
- ret = AVERROR(EIO);
- goto fail;
- }
if ((ret = libssh_authentication(s, user, pass)) < 0)
goto fail;
goto fail;
}
- if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
- access = O_CREAT | O_RDWR;
- if (s->trunc)
- access |= O_TRUNC;
- } else if (flags & AVIO_FLAG_WRITE) {
- access = O_CREAT | O_WRONLY;
- if (s->trunc)
- access |= O_TRUNC;
- } else {
- access = O_RDONLY;
- }
-
- /* 0666 = -rw-rw-rw- = read+write for everyone, minus umask */
- if (!(s->file = sftp_open(s->sftp, path, access, 0666))) {
- av_log(h, AV_LOG_ERROR, "Error opening sftp file: %s\n", ssh_get_error(s->session));
- ret = AVERROR(EIO);
+ if ((ret = libssh_open_file(s, flags, path)) < 0)
goto fail;
- }
- if (!(stat = sftp_fstat(s->file))) {
- av_log(h, AV_LOG_WARNING, "Cannot stat remote file %s.\n", path);
- s->filesize = -1;
- } else {
- s->filesize = stat->size;
- sftp_attributes_free(stat);
- }
+ libssh_stat_file(s);
return 0;