@@ -11,7 +11,7 @@ use std::{
1111 time:: Duration ,
1212} ;
1313
14- use bytes:: BytesMut ;
14+ use bytes:: Bytes ;
1515use eyre:: { Context , ContextCompat } ;
1616use quinn:: { Endpoint , EndpointConfig , IdleTimeout , ServerConfig , TokioRuntime , TransportConfig , VarInt } ;
1717use rustls:: {
@@ -383,7 +383,7 @@ async fn handle_uni_stream<C: InboundCallback>(
383383 . read_to_end ( 65536 )
384384 . await
385385 . map_err ( |e| eyre:: eyre!( "Failed to read stream: {}" , e) ) ?;
386- let mut buf = BytesMut :: from ( & data[ .. ] ) ;
386+ let mut buf = bytes :: Bytes :: from ( data) ;
387387
388388 // Decode header and command using helper functions
389389 let header = crate :: proto:: decode_header ( & mut buf, "uni stream" ) ?;
@@ -402,7 +402,7 @@ async fn handle_uni_stream<C: InboundCallback>(
402402 } => {
403403 // Decode address (may be Address::None for non-first fragments)
404404 let addr = crate :: proto:: decode_address ( & mut buf, "uni stream packet" ) ?;
405- let payload = buf. split_to ( size as usize ) . freeze ( ) ;
405+ let payload = buf. split_to ( size as usize ) ;
406406
407407 // Convert address to TargetAddr, using placeholder for non-first fragments
408408 let target_addr = match crate :: proto:: address_to_target ( addr) {
@@ -448,18 +448,18 @@ async fn handle_bi_stream<C: InboundCallback>(
448448 }
449449
450450 // Read header and command
451- let mut header_buf = vec ! [ 0u8 ; 2 ] ;
451+ let mut header_buf = [ 0u8 ; 2 ] ;
452452 recv. read_exact ( & mut header_buf)
453453 . await
454454 . map_err ( |e| eyre:: eyre!( "Failed to read header: {}" , e) ) ?;
455- let mut buf = BytesMut :: from ( & header_buf[ ..] ) ;
455+ let mut buf = & header_buf[ ..] ;
456456
457457 let header = crate :: proto:: decode_header ( & mut buf, "bi stream" ) ?;
458458
459459 match header. command {
460460 CmdType :: Connect => {
461461 // Decode command (Connect has no additional fields)
462- let _cmd = crate :: proto:: decode_command ( CmdType :: Connect , & mut BytesMut :: new ( ) , "bi stream" ) ?;
462+ let _cmd = crate :: proto:: decode_command ( CmdType :: Connect , & mut [ ] . as_ref ( ) , "bi stream" ) ?;
463463
464464 // Read exactly the address bytes, leaving the relay payload in `recv`
465465 // so that the same stream can be used for bidirectional data relay.
@@ -501,7 +501,7 @@ async fn handle_datagram<C: InboundCallback>(connection: Arc<InboundCtx>, data:
501501 }
502502 }
503503
504- let mut buf = BytesMut :: from ( data. as_ref ( ) ) ;
504+ let mut buf = data;
505505
506506 // Decode header using helper function
507507 let header = crate :: proto:: decode_header ( & mut buf, "datagram" ) ?;
@@ -519,7 +519,7 @@ async fn handle_datagram<C: InboundCallback>(connection: Arc<InboundCtx>, data:
519519 } = cmd
520520 {
521521 let addr = crate :: proto:: decode_address ( & mut buf, "datagram packet" ) ?;
522- let payload = buf. split_to ( size as usize ) . freeze ( ) ;
522+ let payload = buf. split_to ( size as usize ) ;
523523
524524 // Convert address to TargetAddr, using placeholder for non-first fragments
525525 let target_addr = match crate :: proto:: address_to_target ( addr) {
@@ -721,49 +721,57 @@ async fn read_address_exact(recv: &mut quinn::RecvStream) -> eyre::Result<crate:
721721 . await
722722 . map_err ( |e| eyre:: eyre!( "Failed to read address type: {}" , e) ) ?;
723723
724- let mut buf = BytesMut :: with_capacity ( 20 ) ;
725- buf. extend_from_slice ( & type_byte) ;
726-
727724 match type_byte[ 0 ] {
728- 0xFF => {
729- // AddressType::None — just the single type byte
730- }
725+ 0xFF => Ok ( crate :: proto:: Address :: None ) ,
731726 0x01 => {
732727 // AddressType::IPv4 — 4-byte address + 2-byte port
733728 let mut rest = [ 0u8 ; 6 ] ;
734729 recv. read_exact ( & mut rest)
735730 . await
736731 . map_err ( |e| eyre:: eyre!( "Failed to read IPv4 address: {}" , e) ) ?;
737- buf. extend_from_slice ( & rest) ;
732+ let mut ip_bytes = [ 0u8 ; 4 ] ;
733+ ip_bytes. copy_from_slice ( & rest[ 0 ..4 ] ) ;
734+ let port = u16:: from_be_bytes ( [ rest[ 4 ] , rest[ 5 ] ] ) ;
735+ Ok ( crate :: proto:: Address :: IPv4 ( std:: net:: Ipv4Addr :: from ( ip_bytes) , port) )
738736 }
739737 0x02 => {
740738 // AddressType::IPv6 — 16-byte address + 2-byte port
741739 let mut rest = [ 0u8 ; 18 ] ;
742740 recv. read_exact ( & mut rest)
743741 . await
744742 . map_err ( |e| eyre:: eyre!( "Failed to read IPv6 address: {}" , e) ) ?;
745- buf. extend_from_slice ( & rest) ;
743+ let mut ip_bytes = [ 0u8 ; 16 ] ;
744+ ip_bytes. copy_from_slice ( & rest[ 0 ..16 ] ) ;
745+ let port = u16:: from_be_bytes ( [ rest[ 16 ] , rest[ 17 ] ] ) ;
746+ Ok ( crate :: proto:: Address :: IPv6 ( std:: net:: Ipv6Addr :: from ( ip_bytes) , port) )
746747 }
747748 0x00 => {
748749 // AddressType::Domain — 1-byte length + <length> bytes + 2-byte port
749750 let mut len_byte = [ 0u8 ; 1 ] ;
750751 recv. read_exact ( & mut len_byte)
751752 . await
752753 . map_err ( |e| eyre:: eyre!( "Failed to read domain length: {}" , e) ) ?;
753- buf. extend_from_slice ( & len_byte) ;
754754 let domain_len = len_byte[ 0 ] as usize ;
755- let mut rest = vec ! [ 0u8 ; domain_len + 2 ] ;
756- recv. read_exact ( & mut rest)
755+
756+ // Max domain len is 255. Use a stack buffer.
757+ let mut domain_buf = [ 0u8 ; 255 ] ;
758+ let domain_slice = & mut domain_buf[ ..domain_len] ;
759+ recv. read_exact ( domain_slice)
757760 . await
758761 . map_err ( |e| eyre:: eyre!( "Failed to read domain address: {}" , e) ) ?;
759- buf. extend_from_slice ( & rest) ;
760- }
761- t => {
762- return Err ( eyre:: eyre!( "Unknown address type byte 0x{:02x}" , t) ) ;
762+
763+ let mut port_buf = [ 0u8 ; 2 ] ;
764+ recv. read_exact ( & mut port_buf)
765+ . await
766+ . map_err ( |e| eyre:: eyre!( "Failed to read domain port: {}" , e) ) ?;
767+ let port = u16:: from_be_bytes ( port_buf) ;
768+
769+ let domain_str = String :: from_utf8 ( domain_slice. to_vec ( ) )
770+ . map_err ( |_| eyre:: eyre!( "Invalid UTF-8 domain address" ) ) ?;
771+ Ok ( crate :: proto:: Address :: Domain ( domain_str, port) )
763772 }
773+ t => Err ( eyre:: eyre!( "Unknown address type byte 0x{:02x}" , t) ) ,
764774 }
765-
766- crate :: proto:: decode_address ( & mut buf, "bi stream connect" )
767775}
768776
769777/// Handle UDP dissociate
0 commit comments