Skip to content

Commit

Permalink
Version handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
ohsayan committed Mar 11, 2024
1 parent 821652f commit c115fa1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/io/aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Config {
/// Establish an async connection to the database using the current configuration
pub async fn connect_async(&self) -> ClientResult<ConnectionAsync> {
let mut tcpstream = TcpStream::connect((self.host(), self.port())).await?;
let handshake = ClientHandshake::new(self);
let handshake = ClientHandshake::new_v1(self);
tcpstream.write_all(handshake.inner()).await?;
let mut resp = [0u8; 4];
tcpstream.read_exact(&mut resp).await?;
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Config {
.await
.map_err(|e| ConnectionSetupError::Other(format!("TLS handshake failed: {e}")))?;
// handshake
let handshake = ClientHandshake::new(self);
let handshake = ClientHandshake::new_v1(self);
stream.write_all(handshake.inner()).await?;
let mut resp = [0u8; 4];
stream.read_exact(&mut resp).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/io/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Config {
/// Establish a connection to the database using the current configuration
pub fn connect(&self) -> ClientResult<Connection> {
let mut tcpstream = TcpStream::connect((self.host(), self.port()))?;
let handshake = ClientHandshake::new(self);
let handshake = ClientHandshake::new_v1(self);
tcpstream.write_all(handshake.inner())?;
let mut resp = [0u8; 4];
tcpstream.read_exact(&mut resp)?;
Expand All @@ -113,7 +113,7 @@ impl Config {
})?
.connect(self.host(), stream)
.map_err(|e| ConnectionSetupError::Other(format!("TLS handshake failed: {e}")))?;
let handshake = ClientHandshake::new(self);
let handshake = ClientHandshake::new_v1(self);
stream.write_all(handshake.inner())?;
let mut resp = [0u8; 4];
stream.read_exact(&mut resp)?;
Expand Down
8 changes: 6 additions & 2 deletions src/protocol/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ use crate::{

pub struct ClientHandshake(Box<[u8]>);
impl ClientHandshake {
pub(crate) fn new(cfg: &Config) -> Self {
const HANDSHAKE_PROTO_V1: [u8; 6] = [b'H', 0, 0, 0, 0, 0];
pub(crate) fn new_v1(cfg: &Config) -> Self {
Self::_new(Self::HANDSHAKE_PROTO_V1, cfg)
}
fn _new(hs: [u8; 6], cfg: &Config) -> Self {
let mut v = Vec::with_capacity(6 + cfg.username().len() + cfg.password().len() + 5);
v.extend(b"H\x00\x00\x00\x00\x00");
v.extend(hs);
pushlen!(v, cfg.username().len());
pushlen!(v, cfg.password().len());
v.extend(cfg.username().as_bytes());
Expand Down
1 change: 1 addition & 0 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ impl<'a> Decoder<'a> {
}
let lf = self._creq(b'\n');
self._cursor_incr_if(lf);
// FIXME(@ohsayan): the below is not exactly necessary and we can actually remove this if it complicates state management
okay &= !(lf & (self._cursor() == meta.start));
if okay & lf {
let start = meta.start;
Expand Down

0 comments on commit c115fa1

Please sign in to comment.