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 <fcntl.h>
22 #include <libssh/sftp.h>
28
38
40 {
49 }
50 return 0;
51 }
52
54 {
55 static const int verbosity = SSH_LOG_NOLOG;
57 char proto[10], path[
MAX_URL_SIZE], hostname[1024], credencials[1024];
58 int port = 22, access,
ret;
60 const char *user = NULL, *
pass = NULL;
62 sftp_attributes stat;
63
65 credencials, sizeof(credencials),
66 hostname, sizeof(hostname),
67 &port,
68 path, sizeof(path),
69 url);
70
71 if (port <= 0 || port > 65535)
72 port = 22;
73
74 if (!(s->
session = ssh_new())) {
76 goto fail;
77 }
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);
83 if (timeout > 0)
84 ssh_options_set(s->
session, SSH_OPTIONS_TIMEOUT_USEC, &timeout);
85 if (user)
86 ssh_options_set(s->
session, SSH_OPTIONS_USER, user);
87
88 if (ssh_connect(s->
session) != SSH_OK) {
91 goto fail;
92 }
93
94 if (pass && ssh_userauth_password(s->
session, NULL, pass) != SSH_AUTH_SUCCESS) {
97 goto fail;
98 }
99
103 goto fail;
104 }
105
106 if (sftp_init(s->
sftp) != SSH_OK) {
109 goto fail;
110 }
111
113 access = O_CREAT | O_RDWR;
115 access |= O_TRUNC;
116 } else if (flags & AVIO_FLAG_WRITE) {
117 access = O_CREAT | O_WRONLY;
119 access |= O_TRUNC;
120 } else {
121 access = O_RDONLY;
122 }
123
124 if (!(s->
file = sftp_open(s->
sftp, path, access, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH))) {
127 goto fail;
128 }
129
130 if (!(stat = sftp_fstat(s->
file))) {
133 } else {
135 sftp_attributes_free(stat);
136 }
137
138 return 0;
139
140 fail:
143 }
144
146 {
148 int64_t newpos;
149
153 }
154
155 switch(whence) {
158 case SEEK_SET:
159 newpos = pos;
160 break;
161 case SEEK_CUR:
162 newpos = sftp_tell64(s->
file) + pos;
163 break;
164 case SEEK_END:
166 break;
167 default:
169 }
170
171 if (sftp_seek64(s->
file, newpos)) {
174 }
175
176 return newpos;
177 }
178
180 {
182 int bytes_read;
183
184 if ((bytes_read = sftp_read(s->
file, buf, size)) < 0) {
187 }
188 return bytes_read;
189 }
190
192 {
194 int bytes_written;
195
196 if ((bytes_written = sftp_write(s->
file, buf, size)) < 0) {
199 }
200 return bytes_written;
201 }
202
203 #define OFFSET(x) offsetof(LIBSSHContext, x)
204 #define D AV_OPT_FLAG_DECODING_PARAM
205 #define E AV_OPT_FLAG_ENCODING_PARAM
207 {
"timeout",
"set timeout of socket I/O operations",
OFFSET(rw_timeout),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D|
E },
209 {NULL}
210 };
211
217 };
218
227 .priv_data_class = &libssh_context_class,
229 };