23 #include <libssh/sftp.h>
46 static const int verbosity = SSH_LOG_NOLOG;
48 if (!(libssh->
session = ssh_new())) {
52 ssh_options_set(libssh->
session, SSH_OPTIONS_HOST, hostname);
53 ssh_options_set(libssh->
session, SSH_OPTIONS_PORT, &port);
54 ssh_options_set(libssh->
session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
57 ssh_options_set(libssh->
session, SSH_OPTIONS_TIMEOUT_USEC, &timeout);
60 if (ssh_options_parse_config(libssh->
session,
NULL) < 0) {
64 if (ssh_connect(libssh->
session) != SSH_OK) {
78 ssh_options_set(libssh->
session, SSH_OPTIONS_USER, user);
80 if (ssh_userauth_none(libssh->
session,
NULL) == SSH_AUTH_SUCCESS)
83 auth_methods = ssh_userauth_list(libssh->
session,
NULL);
85 if (auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
88 ssh_private_key priv_key;
90 if (!ssh_try_publickey_from_file(libssh->
session, libssh->
priv_key, &pub_key, &type)) {
91 priv_key = privatekey_from_file(libssh->
session, libssh->
priv_key, type, password);
92 if (ssh_userauth_pubkey(libssh->
session,
NULL, pub_key, priv_key) == SSH_AUTH_SUCCESS) {
93 av_log(libssh,
AV_LOG_DEBUG,
"Authentication successful with selected private key.\n");
100 }
else if (ssh_userauth_autopubkey(libssh->
session, password) == SSH_AUTH_SUCCESS) {
106 if (!authorized && (auth_methods & SSH_AUTH_METHOD_PASSWORD)) {
107 if (ssh_userauth_password(libssh->
session,
NULL, password) == SSH_AUTH_SUCCESS) {
128 if (sftp_init(libssh->
sftp) != SSH_OK) {
141 access = O_CREAT | O_RDWR;
144 }
else if (flags & AVIO_FLAG_WRITE) {
145 access = O_CREAT | O_WRONLY;
152 if (!(libssh->
file = sftp_open(libssh->
sftp, file, access, 0666))) {
162 sftp_attributes stat;
164 if (!(stat = sftp_fstat(libssh->
file))) {
169 sftp_attributes_free(stat);
177 sftp_close(libssh->
file);
181 sftp_free(libssh->
sftp);
185 ssh_disconnect(libssh->
session);
195 char proto[10], hostname[1024], credencials[1024];
201 credencials,
sizeof(credencials),
202 hostname,
sizeof(hostname),
211 if (port < 0 || port > 65535)
217 user =
av_strtok(credencials,
":", &end);
267 newpos = sftp_tell64(libssh->
file) + pos;
281 if (sftp_seek64(libssh->
file, newpos)) {
294 if ((bytes_read = sftp_read(libssh->
file, buf, size)) < 0) {
306 if ((bytes_written = sftp_write(libssh->
file, buf, size)) < 0) {
310 return bytes_written;
322 if (!(libssh->
dir = sftp_opendir(libssh->
sftp, path))) {
338 sftp_attributes attr =
NULL;
347 sftp_attributes_free(attr);
348 attr = sftp_readdir(libssh->
sftp, libssh->
dir);
351 if (sftp_dir_eof(libssh->
dir))
355 }
while (!strcmp(attr->name,
".") || !strcmp(attr->name,
".."));
360 entry->
size = attr->size;
363 entry->
filemode = attr->permissions & 0777;
365 case SSH_FILEXFER_TYPE_REGULAR:
368 case SSH_FILEXFER_TYPE_DIRECTORY:
371 case SSH_FILEXFER_TYPE_SYMLINK:
374 case SSH_FILEXFER_TYPE_SPECIAL:
377 case SSH_FILEXFER_TYPE_UNKNOWN:
381 sftp_attributes_free(attr);
389 sftp_closedir(libssh->
dir);
399 sftp_attributes attr =
NULL;
405 if (!(attr = sftp_stat(libssh->
sftp, path))) {
410 if (attr->type == SSH_FILEXFER_TYPE_DIRECTORY) {
411 if (sftp_rmdir(libssh->
sftp, path) < 0) {
416 if (sftp_unlink(libssh->
sftp, path) < 0) {
426 sftp_attributes_free(attr);
436 char hostname_src[1024], hostname_dst[1024];
437 char credentials_src[1024], credentials_dst[1024];
438 int port_src = 22, port_dst = 22;
441 credentials_src,
sizeof(credentials_src),
442 hostname_src,
sizeof(hostname_src),
444 path_src,
sizeof(path_src),
448 credentials_dst,
sizeof(credentials_dst),
449 hostname_dst,
sizeof(hostname_dst),
451 path_dst,
sizeof(path_dst),
454 if (strcmp(credentials_src, credentials_dst) ||
455 strcmp(hostname_src, hostname_dst) ||
456 port_src != port_dst) {
463 if (sftp_rename(libssh->
sftp, path_src, path_dst) < 0) {
475 #define OFFSET(x) offsetof(LIBSSHContext, x)
476 #define D AV_OPT_FLAG_DECODING_PARAM
477 #define E AV_OPT_FLAG_ENCODING_PARAM
479 {
"timeout",
"set timeout of socket I/O operations",
OFFSET(rw_timeout),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D|
E },
505 .priv_data_class = &libssh_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.
static av_cold int libssh_open(URLContext *h, const char *url, int flags)
#define URL_PROTOCOL_FLAG_NETWORK
static int libssh_write(URLContext *h, const unsigned char *buf, int size)
int64_t filemode
Unix file mode, -1 if unknown.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
Describes single entry of the directory.
#define AVIO_FLAG_READ
read-only
#define AVIO_FLAG_WRITE
write-only
Macro definitions for various function/variable attributes.
static int libssh_open_dir(URLContext *h)
int64_t modification_timestamp
Time of last modification in microseconds since unix epoch, -1 if unknown.
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)
static int libssh_read_dir(URLContext *h, AVIODirEntry **next)
static int libssh_move(URLContext *h_src, URLContext *h_dst)
static int libssh_delete(URLContext *h)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static av_cold int libssh_create_ssh_session(LIBSSHContext *libssh, const char *hostname, unsigned int port)
static av_cold int libssh_create_sftp_session(LIBSSHContext *libssh)
static av_cold int libssh_authentication(LIBSSHContext *libssh, const char *user, const char *password)
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.
int64_t access_timestamp
Time of last access in microseconds since unix epoch, -1 if unknown.
static av_always_inline av_const double trunc(double x)
static av_cold int libssh_open_file(LIBSSHContext *libssh, int flags, const char *file)
static av_cold int libssh_close(URLContext *h)
static const AVClass libssh_context_class
int64_t size
File size in bytes, -1 if unknown.
const URLProtocol ff_libssh_protocol
static int64_t libssh_seek(URLContext *h, int64_t pos, int whence)
int type
Type of the entry.
char * av_strdup(const char *s)
Duplicate the string s.
Describe the class of an AVClass context structure.
int64_t group_id
Group ID of owner, -1 if unknown.
static const AVOption options[]
static int libssh_read(URLContext *h, unsigned char *buf, int size)
static av_cold int libssh_connect(URLContext *h, const char *url, char *path, size_t path_size)
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()...
AVIODirEntry * ff_alloc_dir_entry(void)
Allocate directory entry with default values.
char * filename
specified URL
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
static int libssh_close_dir(URLContext *h)
int64_t user_id
User ID of owner, -1 if unknown.
static av_cold void libssh_stat_file(LIBSSHContext *libssh)
unbuffered private I/O API
static av_cold void cleanup(FlashSV2Context *s)