Skip to content

Commit

Permalink
Reduce code duplication for client/server
Browse files Browse the repository at this point in the history
Take is_client argument for Conn::new()
  • Loading branch information
mkj committed May 30, 2024
1 parent 815d793 commit 8ee18a1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
17 changes: 7 additions & 10 deletions src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,14 @@ pub(crate) struct Dispatched {
}

impl Conn {
pub fn new_client() -> Result<Self> {
let algo_conf = AlgoConfig::new(true);
Self::new(ClientServer::Client(client::Client::new()), algo_conf)
}

pub fn new_server() -> Result<Self> {
let algo_conf = AlgoConfig::new(false);
Self::new(ClientServer::Server(server::Server::new()), algo_conf)
}
pub fn new(is_client: bool) -> Result<Self> {
let algo_conf = AlgoConfig::new(is_client);
let cliserv = if is_client {
ClientServer::Client(client::Client::new())
} else {
ClientServer::Server(server::Server::new())
};

fn new(cliserv: ClientServer, algo_conf: AlgoConfig) -> Result<Self, Error> {
Ok(Conn {
sess_id: None,
kex: Kex::new(),
Expand Down
24 changes: 11 additions & 13 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,23 @@ impl<'a> Runner<'a> {
inbuf: &'a mut [u8],
outbuf: &'a mut [u8],
) -> Result<Runner<'a>, Error> {
let conn = Conn::new_client()?;
let runner = Runner {
conn,
traf_in: TrafIn::new(inbuf),
traf_out: TrafOut::new(outbuf),
keys: KeyState::new_cleartext(),
output_waker: None,
input_waker: None,
closed: false,
};

Ok(runner)
Self::new(inbuf, outbuf, true)
}

/// `inbuf` and `outbuf` must be sized to fit the largest SSH packet allowed.
pub fn new_server(
inbuf: &'a mut [u8],
outbuf: &'a mut [u8],
) -> Result<Runner<'a>, Error> {
let conn = Conn::new_server()?;
Self::new(inbuf, outbuf, false)
}

pub fn new(
inbuf: &'a mut [u8],
outbuf: &'a mut [u8],
is_client: bool,
) -> Result<Runner<'a>, Error> {
let conn = Conn::new(is_client)?;
let runner = Runner {
conn,
traf_in: TrafIn::new(inbuf),
Expand All @@ -99,6 +96,7 @@ impl<'a> Runner<'a> {

Ok(runner)
}

pub fn is_client(&self) -> bool {
self.conn.is_client()
}
Expand Down

0 comments on commit 8ee18a1

Please sign in to comment.