feat: implement NTCP2 protocol, native TCP transport, and live I2P interop
This commit is contained in:
1 parent
dff69dfac6
commit
e6d1c464dd
12 files changed
+2557
-2
No files matched your search
@@ -0,0 +1,203 @@
|
||||
///|
|
||||
/// Errors raised by the NTCP2 protocol engine.
|
||||
#derive(Eq, Debug)
|
||||
pub suberror Ntcp2Error {
|
||||
Crypto(@crypto.CryptoError)
|
||||
Noise(@noise.NoiseError)
|
||||
Encoding(@encoding.EncodingError)
|
||||
Common(@common.CommonError)
|
||||
I2np(@i2np.I2npError)
|
||||
InvalidState(String)
|
||||
InvalidFormat(String)
|
||||
InvalidLength(String)
|
||||
}
|
||||
|
||||
///|
|
||||
/// NTCP2 Data Phase & SessionConfirmed Part 2 block types.
|
||||
#derive(Eq, Debug)
|
||||
pub(all) enum Ntcp2Block {
|
||||
/// Type 0: DateTime (4-byte uint32 Unix timestamp in seconds).
|
||||
DateTime(UInt32)
|
||||
/// Type 1: Options (opaque bytes).
|
||||
Options(Bytes)
|
||||
/// Type 2: RouterInfo (`flood_flag`: bit 0 requests floodfill; `router_info`: raw RouterInfo bytes).
|
||||
RouterInfo(flood_flag~ : Byte, router_info~ : Bytes)
|
||||
/// Type 3: I2NP message with 9-byte short header (`type | msg_id | expiration_sec | body`).
|
||||
I2np(Bytes)
|
||||
/// Type 4: Termination (`valid_frames_received`, `reason`, `additional_data`).
|
||||
Termination(
|
||||
valid_frames_received~ : UInt64,
|
||||
reason~ : Byte,
|
||||
additional_data~ : Bytes,
|
||||
)
|
||||
/// Type 254: Padding (must be last block in frame).
|
||||
Padding(Bytes)
|
||||
/// Unknown / future block type.
|
||||
Unknown(Byte, Bytes)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Ntcp2Block::encode(
|
||||
self : Ntcp2Block,
|
||||
w : @encoding.Writer,
|
||||
) -> Unit raise Ntcp2Error {
|
||||
match self {
|
||||
DateTime(ts) => {
|
||||
w.write_u8(b'\x00')
|
||||
w.write_u16_be(4U)
|
||||
w.write_u32_be(ts)
|
||||
}
|
||||
Options(data) => {
|
||||
if data.length() > 65535 {
|
||||
raise InvalidLength("NTCP2 Options block exceeds 65535 bytes")
|
||||
}
|
||||
w.write_u8(b'\x01')
|
||||
w.write_u16_be(data.length().reinterpret_as_uint())
|
||||
w.write_bytes(data)
|
||||
}
|
||||
RouterInfo(flood_flag~, router_info~) => {
|
||||
let sz = 1 + router_info.length()
|
||||
if sz > 65535 {
|
||||
raise InvalidLength("NTCP2 RouterInfo block exceeds 65535 bytes")
|
||||
}
|
||||
w.write_u8(b'\x02')
|
||||
w.write_u16_be(sz.reinterpret_as_uint())
|
||||
w.write_u8(flood_flag)
|
||||
w.write_bytes(router_info)
|
||||
}
|
||||
I2np(short_msg) => {
|
||||
if short_msg.length() > 65535 {
|
||||
raise InvalidLength("NTCP2 I2NP block exceeds 65535 bytes")
|
||||
}
|
||||
w.write_u8(b'\x03')
|
||||
w.write_u16_be(short_msg.length().reinterpret_as_uint())
|
||||
w.write_bytes(short_msg)
|
||||
}
|
||||
Termination(valid_frames_received~, reason~, additional_data~) => {
|
||||
let sz = 9 + additional_data.length()
|
||||
if sz > 65535 {
|
||||
raise InvalidLength("NTCP2 Termination block exceeds 65535 bytes")
|
||||
}
|
||||
w.write_u8(b'\x04')
|
||||
w.write_u16_be(sz.reinterpret_as_uint())
|
||||
w.write_u64_be(valid_frames_received)
|
||||
w.write_u8(reason)
|
||||
w.write_bytes(additional_data)
|
||||
}
|
||||
Padding(data) => {
|
||||
if data.length() > 65535 {
|
||||
raise InvalidLength("NTCP2 Padding block exceeds 65535 bytes")
|
||||
}
|
||||
w.write_u8(b'\xfe')
|
||||
w.write_u16_be(data.length().reinterpret_as_uint())
|
||||
w.write_bytes(data)
|
||||
}
|
||||
Unknown(btype, data) => {
|
||||
if data.length() > 65535 {
|
||||
raise InvalidLength("NTCP2 block exceeds 65535 bytes")
|
||||
}
|
||||
w.write_u8(btype)
|
||||
w.write_u16_be(data.length().reinterpret_as_uint())
|
||||
w.write_bytes(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Encodes a sequence of NTCP2 blocks into a frame plaintext payload.
|
||||
pub fn encode_blocks(blocks : Array[Ntcp2Block]) -> Bytes raise Ntcp2Error {
|
||||
let w = @encoding.Writer::new(capacity=256)
|
||||
for blk in blocks {
|
||||
blk.encode(w)
|
||||
}
|
||||
w.to_bytes()
|
||||
}
|
||||
|
||||
///|
|
||||
/// Decodes all NTCP2 blocks from a decrypted frame plaintext payload.
|
||||
pub fn decode_blocks(data : Bytes) -> Array[Ntcp2Block] raise Ntcp2Error {
|
||||
let r = @encoding.Reader::new(data)
|
||||
let out : Array[Ntcp2Block] = []
|
||||
while !r.is_eof() {
|
||||
let btype = r.read_u8() catch { e => raise Encoding(e) }
|
||||
let size = (r.read_u16_be() catch { e => raise Encoding(e) }).reinterpret_as_int()
|
||||
let body = r.read_bytes(size) catch { e => raise Encoding(e) }
|
||||
match btype.to_int() {
|
||||
0 => {
|
||||
if size != 4 {
|
||||
raise InvalidLength(
|
||||
"NTCP2 DateTime block must be 4 bytes, got \{size}",
|
||||
)
|
||||
}
|
||||
let br = @encoding.Reader::new(body)
|
||||
let ts = br.read_u32_be() catch { e => raise Encoding(e) }
|
||||
out.push(DateTime(ts))
|
||||
}
|
||||
1 => out.push(Options(body))
|
||||
2 => {
|
||||
if size < 1 {
|
||||
raise InvalidLength("NTCP2 RouterInfo block must be at least 1 byte")
|
||||
}
|
||||
let br = @encoding.Reader::new(body)
|
||||
let flag = br.read_u8() catch { e => raise Encoding(e) }
|
||||
let ri = br.read_remaining()
|
||||
out.push(RouterInfo(flood_flag=flag, router_info=ri))
|
||||
}
|
||||
3 => out.push(I2np(body))
|
||||
4 => {
|
||||
if size < 9 {
|
||||
raise InvalidLength(
|
||||
"NTCP2 Termination block must be at least 9 bytes, got \{size}",
|
||||
)
|
||||
}
|
||||
let br = @encoding.Reader::new(body)
|
||||
let valid_frames = br.read_u64_be() catch { e => raise Encoding(e) }
|
||||
let reason = br.read_u8() catch { e => raise Encoding(e) }
|
||||
let extra = br.read_remaining()
|
||||
out.push(
|
||||
Termination(
|
||||
valid_frames_received=valid_frames,
|
||||
reason~,
|
||||
additional_data=extra,
|
||||
),
|
||||
)
|
||||
}
|
||||
254 => out.push(Padding(body))
|
||||
_ => out.push(Unknown(btype, body))
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
///|
|
||||
/// Helper to build an NTCP2 I2NP block from a structured `I2npPayload`.
|
||||
pub fn i2np_block(
|
||||
msg_id : UInt32,
|
||||
expiration_sec : UInt32,
|
||||
payload : @i2np.I2npPayload,
|
||||
) -> Ntcp2Block raise Ntcp2Error {
|
||||
let encoded = @i2np.encode_short_message(msg_id, expiration_sec, payload) catch {
|
||||
e => raise I2np(e)
|
||||
}
|
||||
I2np(encoded)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Helper to decode an NTCP2 I2NP block payload into `(ShortHeader, I2npPayload)`.
|
||||
pub fn decode_i2np_block(
|
||||
data : Bytes,
|
||||
) -> (@i2np.ShortHeader, @i2np.I2npPayload) raise Ntcp2Error {
|
||||
@i2np.decode_short_message(@encoding.Reader::new(data)) catch {
|
||||
e => raise I2np(e)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Helper to build an NTCP2 RouterInfo block from a `@common.RouterInfo`.
|
||||
pub fn router_info_block(
|
||||
ri : @common.RouterInfo,
|
||||
flood~ : Bool = false,
|
||||
) -> Ntcp2Block raise Ntcp2Error {
|
||||
let raw = ri.to_bytes() catch { e => raise Common(e) }
|
||||
RouterInfo(flood_flag=if flood { b'\x01' } else { b'\x00' }, router_info=raw)
|
||||
}
|
||||
Reference in new issue
Block a user