Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send TCP protocol header to ignore non-rerun clients #6253

Merged
merged 6 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/re_sdk_comms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ mod server;
#[cfg(feature = "server")]
pub use server::{serve, ServerError, ServerOptions};

pub const PROTOCOL_HEADER: &str = "rerun";

pub const PROTOCOL_VERSION: u16 = 0;

pub const DEFAULT_SERVER_PORT: u16 = 9876;
Expand Down
11 changes: 11 additions & 0 deletions crates/re_sdk_comms/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
io::{ErrorKind, Read as _},
net::{TcpListener, TcpStream},
str,
time::Instant,
};

Expand Down Expand Up @@ -38,6 +39,9 @@ enum VersionError {

#[derive(thiserror::Error, Debug)]
enum ConnectionError {
#[error("An unknown client tried to connect")]
UnknownClient,

#[error(transparent)]
VersionError(#[from] VersionError),

Expand Down Expand Up @@ -203,6 +207,13 @@ fn run_client(
) -> Result<(), ConnectionError> {
#![allow(clippy::read_zero_byte_vec)] // false positive: https://github.com/rust-lang/rust-clippy/issues/9274

let mut protocol_header = [0_u8; 5];
stream.read_exact(&mut protocol_header)?;

if !str::from_utf8(&protocol_header).is_ok_and(|header| header == crate::PROTOCOL_HEADER) {
return Err(ConnectionError::UnknownClient);
}

let mut client_version = [0_u8; 2];
stream.read_exact(&mut client_version)?;
let client_version = u16::from_le_bytes(client_version);
Expand Down
6 changes: 5 additions & 1 deletion crates/re_sdk_comms/src/tcp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ impl TcpClient {
match TcpStream::connect_timeout(&self.addr, timeout) {
Ok(mut stream) => {
re_log::debug!("Connected to {:?}.", self.addr);
if let Err(err) = stream.write(&crate::PROTOCOL_VERSION.to_le_bytes()) {

if let Err(err) = stream
.write(crate::PROTOCOL_HEADER.as_bytes())
.and_then(|_| stream.write(&crate::PROTOCOL_VERSION.to_le_bytes()))
{
self.stream_state = TcpStreamState::Pending {
start_time,
num_attempts: num_attempts + 1,
Expand Down