[FFmpeg-cvslog] lavf/libssh: factorize create_ssh_session function

Lukasz Marek git at videolan.org
Wed Jan 22 01:35:27 CET 2014


ffmpeg | branch: master | Lukasz Marek <lukasz.m.luki at gmail.com> | Tue Jan 21 23:59:59 2014 +0100| [77fadab1beb690a20e364acd222475e4eba4862c] | committer: Lukasz Marek

lavf/libssh: factorize create_ssh_session function

Signed-off-by: Lukasz Marek <lukasz.m.luki at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=77fadab1beb690a20e364acd222475e4eba4862c
---

 libavformat/libssh.c |   42 ++++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 16 deletions(-)

diff --git a/libavformat/libssh.c b/libavformat/libssh.c
index 3bd581b..a28b2e8 100644
--- a/libavformat/libssh.c
+++ b/libavformat/libssh.c
@@ -38,6 +38,30 @@ typedef struct {
     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;
@@ -137,11 +161,9 @@ static int libssh_close(URLContext *h)
 
 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, ret;
-    long timeout = s->rw_timeout * 1000;
     const char *user = NULL, *pass = NULL;
     char *end = NULL;
 
@@ -155,23 +177,11 @@ static int libssh_open(URLContext *h, const char *url, int flags)
     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;



More information about the ffmpeg-cvslog mailing list