Skip to content
Merged
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
26 changes: 18 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/tuic-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ wind-core = { version = "0.1.1", path = "../wind-core", default-features = false

# Wire codecs
bytes = "1"
nom = "8"
tokio-util = { version = "0.7", features = ["codec"] }
num_enum = "0.7"
snafu = "0.9"
Expand Down
83 changes: 10 additions & 73 deletions crates/tuic-core/src/proto/addr.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use std::{
net::{Ipv4Addr, Ipv6Addr},
str,
};
use std::net::{Ipv4Addr, Ipv6Addr};

use bytes::{Buf, BufMut};
use num_enum::{FromPrimitive, IntoPrimitive};
use snafu::{ResultExt, ensure};
use tokio_util::codec::{Decoder, Encoder};
use wind_core::types::TargetAddr;

use crate::proto::{BytesRemainingSnafu, DomainTooLongSnafu, FailParseDomainSnafu, ProtoError, UnknownAddressTypeSnafu};
use crate::proto::{BytesRemainingSnafu, DomainTooLongSnafu, ProtoError};

//-----------------------------------------------------------------------------
// Type Definitions
Expand Down Expand Up @@ -70,74 +66,15 @@ impl Decoder for AddressCodec {
type Item = Address;

fn decode(&mut self, src: &mut bytes::BytesMut) -> Result<Option<Self::Item>, Self::Error> {
// Return None if buffer is empty
if src.is_empty() {
return Ok(None);
}

// Parse address type from first byte
let addr_type = AddressType::from(src[0]);

ensure!(
!matches!(addr_type, AddressType::Other(_)),
UnknownAddressTypeSnafu {
value: u8::from(addr_type)
}
);

match addr_type {
AddressType::None => {
src.advance(1);
Ok(Some(Address::None))
}
AddressType::IPv4 => {
// Type (1) + IPv4 (4) + Port (2)
if src.len() < 1 + 4 + 2 {
return Ok(None);
}
src.advance(1);
let mut octets = [0; 4];
src.copy_to_slice(&mut octets);
let ip = Ipv4Addr::from(octets);
let port = src.get_u16();
Ok(Some(Address::IPv4(ip, port)))
}
AddressType::IPv6 => {
// Type (1) + IPv6 (16) + Port (2)
if src.len() < 1 + 16 + 2 {
return Ok(None);
}
src.advance(1);
let mut octets = [0; 16];
src.copy_to_slice(&mut octets);
let ip = Ipv6Addr::from(octets);
let port = src.get_u16();
Ok(Some(Address::IPv6(ip, port)))
}
AddressType::Domain => {
// Need at least type byte and length byte
if src.len() < 1 + 1 {
return Ok(None);
}
let domain_len = src[1] as usize;

// Type (1) + Length (1) + Domain + Port (2)
if src.len() < 1 + 1 + domain_len + 2 {
return Ok(None);
}
src.advance(2);

let domain = &src[..domain_len];
let domain = str::from_utf8(domain)
.context(FailParseDomainSnafu {
raw: hex::encode(domain),
})?
.to_string();
src.advance(domain_len);
let port = src.get_u16();
Ok(Some(Address::Domain(domain, port)))
let input: &[u8] = &src[..];
match super::parse_address(input) {
Ok((remaining, address)) => {
let consumed = input.len() - remaining.len();
src.advance(consumed);
Ok(Some(address))
}
_ => unreachable!(),
Err(nom::Err::Incomplete(_)) => Ok(None),
Err(_) => BytesRemainingSnafu.fail(),
}
}

Expand Down
45 changes: 9 additions & 36 deletions crates/tuic-core/src/proto/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use bytes::{Buf, BufMut as _};
use tokio_util::codec::{Decoder, Encoder};
use uuid::Uuid;

use crate::proto::{BytesRemainingSnafu, UnknownCommandTypeSnafu, header::CmdType};
use crate::proto::{BytesRemainingSnafu, header::CmdType};

#[derive(Debug, Clone, Copy)]
pub struct CmdCodec(pub CmdType);
Expand Down Expand Up @@ -34,41 +33,15 @@ impl Decoder for CmdCodec {
type Item = Command;

fn decode(&mut self, src: &mut bytes::BytesMut) -> Result<Option<Self::Item>, Self::Error> {
match self.0 {
CmdType::Auth => {
if src.len() < 16 + 32 {
return Ok(None);
}
let mut uuid = [0; 16];
src.copy_to_slice(&mut uuid);
let uuid = Uuid::from_bytes(uuid);
let mut token = [0; 32];
src.copy_to_slice(&mut token);
Ok(Some(Command::Auth { uuid, token }))
let input: &[u8] = &src[..];
match super::parse_command_body(self.0, input) {
Ok((remaining, command)) => {
let consumed = input.len() - remaining.len();
src.advance(consumed);
Ok(Some(command))
}
CmdType::Connect => Ok(Some(Command::Connect)),
CmdType::Packet => {
if src.len() < 8 {
return Ok(None);
}

Ok(Some(Command::Packet {
assoc_id: src.get_u16(),
pkt_id: src.get_u16(),
frag_total: src.get_u8(),
frag_id: src.get_u8(),
size: src.get_u16(),
}))
}
CmdType::Dissociate => {
if src.len() < 2 {
return Ok(None);
}

Ok(Some(Command::Dissociate { assoc_id: src.get_u16() }))
}
CmdType::Heartbeat => Ok(Some(Command::Heartbeat)),
CmdType::Other(value) => UnknownCommandTypeSnafu { value }.fail(),
Err(nom::Err::Incomplete(_)) => Ok(None),
Err(_) => BytesRemainingSnafu.fail(),
}
}

Expand Down
33 changes: 11 additions & 22 deletions crates/tuic-core/src/proto/header.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use bytes::{Buf, BufMut};
use num_enum::{FromPrimitive, IntoPrimitive};
use snafu::ensure;
use tokio_util::codec::{Decoder, Encoder};

use super::{Command, VER};
use crate::proto::{BytesRemainingSnafu, UnknownCommandTypeSnafu, VersionMismatchSnafu};
use super::Command;
use crate::proto::{BytesRemainingSnafu, VER};

#[derive(Debug, Clone, Copy)]
pub struct HeaderCodec;
Expand Down Expand Up @@ -39,26 +38,16 @@ impl Decoder for HeaderCodec {
type Item = Header;

fn decode(&mut self, src: &mut bytes::BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if src.len() < 2 {
return Ok(None);
}
let ver = src.get_u8();
ensure!(
ver == VER,
VersionMismatchSnafu {
expect: VER,
current: ver
let input: &[u8] = &src[..];
match super::parse_header(input) {
Ok((remaining, header)) => {
let consumed = input.len() - remaining.len();
src.advance(consumed);
Ok(Some(header))
}
);

let cmd = CmdType::from(src.get_u8());

ensure!(
!matches!(cmd, CmdType::Other(..)),
UnknownCommandTypeSnafu { value: u8::from(cmd) }
);

Ok(Some(Header::new(cmd)))
Err(nom::Err::Incomplete(_)) => Ok(None),
Err(_) => BytesRemainingSnafu.fail(),
}
}

fn decode_eof(&mut self, buf: &mut bytes::BytesMut) -> Result<Option<Self::Item>, Self::Error> {
Expand Down
Loading
Loading