Skip to content
Snippets Groups Projects
Commit 2ae025dc authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Move the transport state into the session struct.

This avoids another allocation/free requirement.
parent 8dc94392
Branches
Tags
No related merge requests found
Pipeline #7371 passed
...@@ -30,7 +30,12 @@ typedef int (*deuce_ssh_transport_io_cb_t)(uint8_t *buf, size_t bufsz, atomic_bo ...@@ -30,7 +30,12 @@ typedef int (*deuce_ssh_transport_io_cb_t)(uint8_t *buf, size_t bufsz, atomic_bo
typedef int (*deuce_ssh_transport_rxline_cb_t)(uint8_t *buf, size_t bufsz, size_t *bytes_received, atomic_bool *terminate, void *cbdata); typedef int (*deuce_ssh_transport_rxline_cb_t)(uint8_t *buf, size_t bufsz, size_t *bytes_received, atomic_bool *terminate, void *cbdata);
typedef int (*deuce_ssh_transport_extra_line_cb_t)(uint8_t *buf, size_t bufsz, void *cbdata); typedef int (*deuce_ssh_transport_extra_line_cb_t)(uint8_t *buf, size_t bufsz, void *cbdata);
typedef struct deuce_ssh_session { typedef struct deuce_ssh_session *deuce_ssh_session_t;
#include "ssh-arch.h"
#include "ssh-trans.h"
struct deuce_ssh_session {
/* Global */ /* Global */
mtx_t mtx; mtx_t mtx;
atomic_bool initialized; atomic_bool initialized;
...@@ -47,8 +52,8 @@ typedef struct deuce_ssh_session { ...@@ -47,8 +52,8 @@ typedef struct deuce_ssh_session {
void *rx_line_cbdata; void *rx_line_cbdata;
void *extra_line_cbdata; void *extra_line_cbdata;
deuce_ssh_transport_state_t trans; struct deuce_ssh_transport_state trans;
} *deuce_ssh_session_t; };
int deuce_ssh_session_init(deuce_ssh_session_t sess); int deuce_ssh_session_init(deuce_ssh_session_t sess);
bool deuce_ssh_session_terminate(deuce_ssh_session_t sess); bool deuce_ssh_session_terminate(deuce_ssh_session_t sess);
...@@ -56,7 +61,4 @@ void deuce_ssh_session_cleanup(deuce_ssh_session_t sess); ...@@ -56,7 +61,4 @@ void deuce_ssh_session_cleanup(deuce_ssh_session_t sess);
int deuce_ssh_transport_set_callbacks(deuce_ssh_transport_io_cb_t tx, deuce_ssh_transport_io_cb_t rx, deuce_ssh_transport_rxline_cb_t rx_line, deuce_ssh_transport_extra_line_cb_t extra_line_cb); int deuce_ssh_transport_set_callbacks(deuce_ssh_transport_io_cb_t tx, deuce_ssh_transport_io_cb_t rx, deuce_ssh_transport_rxline_cb_t rx_line, deuce_ssh_transport_extra_line_cb_t extra_line_cb);
#include "ssh-arch.h"
#include "ssh-trans.h"
#endif #endif
...@@ -156,9 +156,9 @@ tx_handshake(void *arg) ...@@ -156,9 +156,9 @@ tx_handshake(void *arg)
memcpy(&tx_packet[sz], gconf.version_comment, asz); memcpy(&tx_packet[sz], gconf.version_comment, asz);
sz += asz; sz += asz;
} }
memcpy(sess->trans->id_str, tx_packet, sz); memcpy(sess->trans.id_str, tx_packet, sz);
sess->trans->id_str_sz = sz; sess->trans.id_str_sz = sz;
sess->trans->id_str[sz] = 0; sess->trans.id_str[sz] = 0;
memcpy(&tx_packet[sz], "\r\n", 2); memcpy(&tx_packet[sz], "\r\n", 2);
sz += 2; sz += 2;
res = gconf.tx(tx_packet, sz, &sess->terminate, sess->tx_cbdata); res = gconf.tx(tx_packet, sz, &sess->terminate, sess->tx_cbdata);
...@@ -172,45 +172,45 @@ tx_handshake(void *arg) ...@@ -172,45 +172,45 @@ tx_handshake(void *arg)
void void
deuce_ssh_transport_cleanup(deuce_ssh_session_t sess) deuce_ssh_transport_cleanup(deuce_ssh_session_t sess)
{ {
if (sess->trans->kex_selected) { if (sess->trans.kex_selected) {
if (sess->trans->kex_selected->cleanup != NULL) if (sess->trans.kex_selected->cleanup != NULL)
sess->trans->kex_selected->cleanup(sess); sess->trans.kex_selected->cleanup(sess);
sess->trans->kex_selected = NULL; sess->trans.kex_selected = NULL;
} }
if (sess->trans->key_algo_selected) { if (sess->trans.key_algo_selected) {
if (sess->trans->key_algo_selected->cleanup != NULL) if (sess->trans.key_algo_selected->cleanup != NULL)
sess->trans->key_algo_selected->cleanup(sess); sess->trans.key_algo_selected->cleanup(sess);
sess->trans->key_algo_selected = NULL; sess->trans.key_algo_selected = NULL;
} }
if (sess->trans->enc_c2s_selected) { if (sess->trans.enc_c2s_selected) {
if (sess->trans->enc_c2s_selected->cleanup != NULL) if (sess->trans.enc_c2s_selected->cleanup != NULL)
sess->trans->enc_c2s_selected->cleanup(sess); sess->trans.enc_c2s_selected->cleanup(sess);
sess->trans->enc_c2s_selected = NULL; sess->trans.enc_c2s_selected = NULL;
} }
if (sess->trans->enc_s2c_selected) { if (sess->trans.enc_s2c_selected) {
if (sess->trans->enc_s2c_selected->cleanup != NULL) if (sess->trans.enc_s2c_selected->cleanup != NULL)
sess->trans->enc_s2c_selected->cleanup(sess); sess->trans.enc_s2c_selected->cleanup(sess);
sess->trans->enc_s2c_selected = NULL; sess->trans.enc_s2c_selected = NULL;
} }
if (sess->trans->mac_c2s_selected) { if (sess->trans.mac_c2s_selected) {
if (sess->trans->mac_c2s_selected->cleanup != NULL) if (sess->trans.mac_c2s_selected->cleanup != NULL)
sess->trans->mac_c2s_selected->cleanup(sess); sess->trans.mac_c2s_selected->cleanup(sess);
sess->trans->mac_c2s_selected = NULL; sess->trans.mac_c2s_selected = NULL;
} }
if (sess->trans->mac_s2c_selected) { if (sess->trans.mac_s2c_selected) {
if (sess->trans->mac_s2c_selected->cleanup != NULL) if (sess->trans.mac_s2c_selected->cleanup != NULL)
sess->trans->mac_s2c_selected->cleanup(sess); sess->trans.mac_s2c_selected->cleanup(sess);
sess->trans->mac_s2c_selected = NULL; sess->trans.mac_s2c_selected = NULL;
} }
if (sess->trans->comp_c2s_selected) { if (sess->trans.comp_c2s_selected) {
if (sess->trans->comp_c2s_selected->cleanup != NULL) if (sess->trans.comp_c2s_selected->cleanup != NULL)
sess->trans->comp_c2s_selected->cleanup(sess); sess->trans.comp_c2s_selected->cleanup(sess);
sess->trans->comp_c2s_selected = NULL; sess->trans.comp_c2s_selected = NULL;
} }
if (sess->trans->comp_s2c_selected) { if (sess->trans.comp_s2c_selected) {
if (sess->trans->comp_s2c_selected->cleanup != NULL) if (sess->trans.comp_s2c_selected->cleanup != NULL)
sess->trans->comp_s2c_selected->cleanup(sess); sess->trans.comp_s2c_selected->cleanup(sess);
sess->trans->comp_s2c_selected = NULL; sess->trans.comp_s2c_selected = NULL;
} }
sess->remote_software_id_string_sz = 0; sess->remote_software_id_string_sz = 0;
...@@ -230,9 +230,6 @@ deuce_ssh_transport_init(deuce_ssh_session_t sess) ...@@ -230,9 +230,6 @@ deuce_ssh_transport_init(deuce_ssh_session_t sess)
if (gconf.software_version == NULL) if (gconf.software_version == NULL)
gconf.software_version = sw_ver; gconf.software_version = sw_ver;
sess->trans = calloc(1, sizeof(struct deuce_ssh_transport_state));
if (sess->trans == NULL)
return DEUCE_SSH_ERROR_ALLOC;
thrd_t thrd; thrd_t thrd;
if (thrd_create(&thrd, rx_thread, sess) != thrd_success) if (thrd_create(&thrd, rx_thread, sess) != thrd_success)
return DEUCE_SSH_ERROR_INIT; return DEUCE_SSH_ERROR_INIT;
...@@ -243,15 +240,15 @@ deuce_ssh_transport_init(deuce_ssh_session_t sess) ...@@ -243,15 +240,15 @@ deuce_ssh_transport_init(deuce_ssh_session_t sess)
thrd_join(thrd, &tres); thrd_join(thrd, &tres);
return res; return res;
} }
sess->trans->kex_selected = NULL; sess->trans.kex_selected = NULL;
sess->trans->key_algo_selected = NULL; sess->trans.key_algo_selected = NULL;
sess->trans->enc_c2s_selected = NULL; sess->trans.enc_c2s_selected = NULL;
sess->trans->enc_s2c_selected = NULL; sess->trans.enc_s2c_selected = NULL;
sess->trans->mac_c2s_selected = NULL; sess->trans.mac_c2s_selected = NULL;
sess->trans->mac_s2c_selected = NULL; sess->trans.mac_s2c_selected = NULL;
sess->trans->comp_c2s_selected = NULL; sess->trans.comp_c2s_selected = NULL;
sess->trans->comp_s2c_selected = NULL; sess->trans.comp_s2c_selected = NULL;
sess->trans->transport_thread = thrd; sess->trans.transport_thread = thrd;
return 0; return 0;
} }
......
...@@ -22,7 +22,7 @@ deuce_ssh_session_terminate(deuce_ssh_session_t sess) ...@@ -22,7 +22,7 @@ deuce_ssh_session_terminate(deuce_ssh_session_t sess)
if (atomic_compare_exchange_strong(&sess->initialized, &t, false)) { if (atomic_compare_exchange_strong(&sess->initialized, &t, false)) {
sess->terminate = true; sess->terminate = true;
int tres; int tres;
thrd_join(sess->trans->transport_thread, &tres); thrd_join(sess->trans.transport_thread, &tres);
sess->terminate = false; sess->terminate = false;
return true; return true;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment