Refactoring
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
diff --git a/src/transports/ssh.c b/src/transports/ssh.c
index 252a802..e2f03fd 100644
--- a/src/transports/ssh.c
+++ b/src/transports/ssh.c
@@ -254,13 +254,50 @@ static int _git_ssh_authenticate_session(
break;
}
default:
- rc = -1;
+ rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED;
}
} while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
return rc;
}
+static int _git_ssh_session_create
+(
+ LIBSSH2_SESSION** session,
+ gitno_socket socket
+)
+{
+ if (!session) {
+ return -1;
+ }
+
+ LIBSSH2_SESSION* s = libssh2_session_init();
+ if (!s)
+ return -1;
+
+ int rc = 0;
+ do {
+ rc = libssh2_session_startup(s, socket.socket);
+ } while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
+
+ if (0 != rc) {
+ goto on_error;
+ }
+
+ libssh2_session_set_blocking(s, 1);
+
+ *session = s;
+
+ return 0;
+
+on_error:
+ if (s) {
+ libssh2_session_free(s), s = NULL;
+ }
+
+ return -1;
+}
+
static int _git_ssh_setup_conn(
ssh_subtransport *t,
const char *url,
@@ -268,9 +305,11 @@ static int _git_ssh_setup_conn(
git_smart_subtransport_stream **stream
)
{
- char *host, *port, *user=NULL, *pass=NULL;
- const char *default_port = "22";
+ char *host, *port=NULL, *user=NULL, *pass=NULL;
+ const char *default_port="22";
ssh_stream *s;
+ LIBSSH2_SESSION* session=NULL;
+ LIBSSH2_CHANNEL* channel=NULL;
*stream = NULL;
if (ssh_stream_alloc(t, url, cmd, stream) < 0)
@@ -307,34 +346,15 @@ static int _git_ssh_setup_conn(
user = git__strdup(default_user);
}
- LIBSSH2_SESSION* session = libssh2_session_init();
- if (!session)
- goto on_error;
-
- int rc = 0;
- do {
- rc = libssh2_session_startup(session, s->socket.socket);
- } while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
-
- if (0 != rc) {
- goto on_error;
- }
-
- libssh2_trace(session, 0x1FF);
- libssh2_session_set_blocking(session, 1);
-
- if (_git_ssh_authenticate_session(session, user, t->cred) < 0) {
+ if (_git_ssh_session_create(&session, s->socket) < 0)
goto on_error;
- }
- LIBSSH2_CHANNEL* channel = NULL;
- do {
- channel = libssh2_channel_open_session(session);
- } while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
+ if (_git_ssh_authenticate_session(session, user, t->cred) < 0)
+ goto on_error;
- if (!channel) {
+ channel = libssh2_channel_open_session(session);
+ if (!channel)
goto on_error;
- }
libssh2_channel_set_blocking(channel, 1);
@@ -343,6 +363,14 @@ static int _git_ssh_setup_conn(
t->current_stream = s;
git__free(host);
+ git__free(port);
+ if (user) {
+ git__free(user);
+ }
+ if (pass) {
+ git__free(pass);
+ }
+
return 0;
on_error:
@@ -350,6 +378,17 @@ on_error:
ssh_stream_free(*stream);
git__free(host);
+ git__free(port);
+ if (user) {
+ git__free(user);
+ }
+ if (pass) {
+ git__free(pass);
+ }
+ if (session) {
+ libssh2_session_free(session), session = NULL;
+ }
+
return -1;
}