Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions protos/rendezvous.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package hbb;
message RegisterPeer {
string id = 1;
int32 serial = 2;
string licence_key = 3;
}

enum ConnType {
Expand Down Expand Up @@ -61,6 +62,7 @@ message PunchHole {

message TestNatRequest {
int32 serial = 1;
string licence_key = 2;
}

// per my test, uint/int has no difference in encoding, int not good for negative, use sint for negative
Expand Down Expand Up @@ -91,6 +93,7 @@ message RegisterPk {
bytes pk = 3;
string old_id = 4;
bool no_register_device = 5;
string licence_key = 6;
}

message RegisterPkResponse {
Expand All @@ -102,6 +105,8 @@ message RegisterPkResponse {
INVALID_ID_FORMAT = 5;
NOT_SUPPORT = 6;
SERVER_ERROR = 7;
LICENSE_MISMATCH = 8;
PEER_LIMIT_REACHED = 9;
}
Result result = 1;
int32 keep_alive = 2;
Expand Down Expand Up @@ -196,6 +201,7 @@ message PeerDiscovery {
message OnlineRequest {
string id = 1;
repeated string peers = 2;
string licence_key = 3;
}

message OnlineResponse {
Expand Down
38 changes: 37 additions & 1 deletion src/bytes_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::io;
use tokio_util::codec::{Decoder, Encoder};

const DEFAULT_MAX_PACKET_LENGTH: usize = 64 * 1024 * 1024;

#[derive(Debug, Clone, Copy)]
pub struct BytesCodec {
state: DecodeState,
Expand All @@ -26,7 +28,7 @@ impl BytesCodec {
Self {
state: DecodeState::Head,
raw: false,
max_packet_length: usize::MAX,
max_packet_length: DEFAULT_MAX_PACKET_LENGTH,
}
}

Expand Down Expand Up @@ -137,6 +139,25 @@ impl Encoder<Bytes> for BytesCodec {
#[cfg(test)]
mod tests {
use super::*;

fn encode_head_only(len: usize) -> BytesMut {
let mut buf = BytesMut::new();
if len <= 0x3F {
buf.put_u8((len << 2) as u8);
} else if len <= 0x3FFF {
buf.put_u16_le((len << 2) as u16 | 0x1);
} else if len <= 0x3FFFFF {
let h = (len << 2) as u32 | 0x2;
buf.put_u16_le((h & 0xFFFF) as u16);
buf.put_u8((h >> 16) as u8);
} else if len <= 0x3FFFFFFF {
buf.put_u32_le((len << 2) as u32 | 0x3);
} else {
panic!("test packet length overflow");
}
buf
}

#[test]
fn test_codec1() {
let mut codec = BytesCodec::new();
Expand Down Expand Up @@ -277,4 +298,19 @@ mod tests {
panic!();
}
}

#[test]
fn test_codec_rejects_packet_above_default_limit() {
let mut codec = BytesCodec::new();
let mut buf = encode_head_only(DEFAULT_MAX_PACKET_LENGTH + 1);
assert!(codec.decode(&mut buf).is_err());
}

#[test]
fn test_codec_rejects_packet_above_custom_limit() {
let mut codec = BytesCodec::new();
codec.set_max_packet_length(16);
let mut buf = encode_head_only(17);
assert!(codec.decode(&mut buf).is_err());
}
}
Loading