remove unnescesary Arc<_>

This commit is contained in:
DJMrTV 2025-01-20 18:01:54 +01:00
commit ef844183d6
6 changed files with 433 additions and 60 deletions

355
Cargo.lock generated
View file

@ -2,6 +2,15 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "android-tzdata" name = "android-tzdata"
version = "0.1.1" version = "0.1.1"
@ -29,6 +38,54 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "aws-lc-rs"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ea835662a0af02443aa1396d39be523bbf8f11ee6fad20329607c480bea48c3"
dependencies = [
"aws-lc-sys",
"paste",
"zeroize",
]
[[package]]
name = "aws-lc-sys"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71b2ddd3ada61a305e1d8bb6c005d1eaa7d14d903681edfc400406d523a9b491"
dependencies = [
"bindgen",
"cc",
"cmake",
"dunce",
"fs_extra",
"paste",
]
[[package]]
name = "bindgen"
version = "0.69.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088"
dependencies = [
"bitflags",
"cexpr",
"clang-sys",
"itertools",
"lazy_static",
"lazycell",
"log",
"prettyplease",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex",
"syn 2.0.96",
"which",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "2.8.0" version = "2.8.0"
@ -73,9 +130,20 @@ version = "1.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13208fcbb66eaeffe09b99fffbe1af420f00a7b35aa99ad683dfc1aa76145229" checksum = "13208fcbb66eaeffe09b99fffbe1af420f00a7b35aa99ad683dfc1aa76145229"
dependencies = [ dependencies = [
"jobserver",
"libc",
"shlex", "shlex",
] ]
[[package]]
name = "cexpr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [
"nom",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -106,6 +174,26 @@ dependencies = [
"inout", "inout",
] ]
[[package]]
name = "clang-sys"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
dependencies = [
"glob",
"libc",
"libloading",
]
[[package]]
name = "cmake"
version = "0.1.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c682c223677e0e5b6b7f63a64b9351844c3f1b1678a68b7ee617e30fb082620e"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "core-foundation-sys" name = "core-foundation-sys"
version = "0.8.7" version = "0.8.7"
@ -137,6 +225,18 @@ version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]]
name = "dunce"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813"
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.3.10" version = "0.3.10"
@ -144,9 +244,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys", "windows-sys 0.59.0",
] ]
[[package]]
name = "fs_extra"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
[[package]] [[package]]
name = "generic-array" name = "generic-array"
version = "0.14.7" version = "0.14.7"
@ -157,6 +263,17 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.3.0-rc.0" version = "0.3.0-rc.0"
@ -167,11 +284,26 @@ dependencies = [
"js-sys", "js-sys",
"libc", "libc",
"rustix", "rustix",
"wasi", "wasi 0.13.3+wasi-0.2.2",
"wasm-bindgen", "wasm-bindgen",
"windows-targets", "windows-targets",
] ]
[[package]]
name = "glob"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
[[package]]
name = "home"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589533453244b0995c858700322199b2becb13b627df2851f64a2775d024abcf"
dependencies = [
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.61" version = "0.1.61"
@ -204,12 +336,30 @@ dependencies = [
"generic-array", "generic-array",
] ]
[[package]]
name = "itertools"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.14" version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]]
name = "jobserver"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.77" version = "0.3.77"
@ -220,12 +370,34 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.169" version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]]
name = "libloading"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
dependencies = [
"cfg-if",
"windows-targets",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.15" version = "0.4.15"
@ -238,6 +410,28 @@ version = "0.4.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f"
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]] [[package]]
name = "num-conv" name = "num-conv"
version = "0.1.0" version = "0.1.0"
@ -268,6 +462,12 @@ version = "1.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]] [[package]]
name = "powerfmt" name = "powerfmt"
version = "0.2.0" version = "0.2.0"
@ -283,6 +483,16 @@ dependencies = [
"zerocopy 0.7.35", "zerocopy 0.7.35",
] ]
[[package]]
name = "prettyplease"
version = "0.2.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6924ced06e1f7dfe3fa48d57b9f74f55d8915f5036121bef647ef4b204895fac"
dependencies = [
"proc-macro2",
"syn 2.0.96",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.93" version = "1.0.93"
@ -328,7 +538,7 @@ version = "0.9.0-beta.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a98fa0b8309344136abe6244130311e76997e546f76fae8054422a7539b43df7" checksum = "a98fa0b8309344136abe6244130311e76997e546f76fae8054422a7539b43df7"
dependencies = [ dependencies = [
"getrandom", "getrandom 0.3.0-rc.0",
"zerocopy 0.8.14", "zerocopy 0.8.14",
] ]
@ -341,6 +551,56 @@ dependencies = [
"cipher", "cipher",
] ]
[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "ring"
version = "0.17.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d"
dependencies = [
"cc",
"cfg-if",
"getrandom 0.2.15",
"libc",
"spin",
"untrusted",
"windows-sys 0.52.0",
]
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.43" version = "0.38.43"
@ -351,7 +611,40 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys", "linux-raw-sys",
"windows-sys", "windows-sys 0.59.0",
]
[[package]]
name = "rustls"
version = "0.23.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f287924602bf649d949c63dc8ac8b235fa5387d394020705b80c4eb597ce5b8"
dependencies = [
"aws-lc-rs",
"log",
"once_cell",
"rustls-pki-types",
"rustls-webpki",
"subtle",
"zeroize",
]
[[package]]
name = "rustls-pki-types"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2bf47e6ff922db3825eb750c4e2ff784c6ff8fb9e13046ef6a1d1c5401b0b37"
[[package]]
name = "rustls-webpki"
version = "0.102.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9"
dependencies = [
"aws-lc-rs",
"ring",
"rustls-pki-types",
"untrusted",
] ]
[[package]] [[package]]
@ -397,6 +690,12 @@ dependencies = [
"time", "time",
] ]
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]] [[package]]
name = "splatoon-server-rust" name = "splatoon-server-rust"
version = "0.1.0" version = "0.1.0"
@ -409,11 +708,18 @@ dependencies = [
"once_cell", "once_cell",
"rand", "rand",
"rc4", "rc4",
"rustls",
"simplelog", "simplelog",
"thiserror", "thiserror",
"v_byte_macros", "v_byte_macros",
] ]
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.109" version = "1.0.109"
@ -510,6 +816,12 @@ version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "untrusted"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
[[package]] [[package]]
name = "v_byte_macros" name = "v_byte_macros"
version = "0.0.3" version = "0.0.3"
@ -526,6 +838,12 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.13.3+wasi-0.2.2" version = "0.13.3+wasi-0.2.2"
@ -593,13 +911,25 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "which"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7"
dependencies = [
"either",
"home",
"once_cell",
"rustix",
]
[[package]] [[package]]
name = "winapi-util" name = "winapi-util"
version = "0.1.9" version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [ dependencies = [
"windows-sys", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@ -611,6 +941,15 @@ dependencies = [
"windows-targets", "windows-targets",
] ]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.59.0" version = "0.59.0"
@ -733,3 +1072,9 @@ dependencies = [
"quote", "quote",
"syn 2.0.96", "syn 2.0.96",
] ]
[[package]]
name = "zeroize"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"

View file

@ -14,4 +14,5 @@ simplelog = "0.12.2"
chrono = "0.4.39" chrono = "0.4.39"
log = "0.4.25" log = "0.4.25"
anyhow = "1.0.95" anyhow = "1.0.95"
rand = "0.9.0-beta.3" rand = "0.9.0-beta.3"
rustls = "^0.23.21"

View file

@ -49,7 +49,7 @@ fn main() {
info!("setting up endpoints"); info!("setting up endpoints");
let auth_endpoints = vec![ let auth_endpoints = vec![
Endpoint::new(auth_server.socket.clone(), VirtualPort::new(1,10)) Endpoint::new(auth_server.socket.try_clone().unwrap(), VirtualPort::new(1,10))
]; ];
auth_server.endpoints.set(auth_endpoints) auth_server.endpoints.set(auth_endpoints)

View file

@ -9,7 +9,7 @@ use crate::prudp::sockaddr::PRUDPSockAddr;
#[derive(Debug)] #[derive(Debug)]
pub struct Endpoint{ pub struct Endpoint{
virtual_port: VirtualPort, virtual_port: VirtualPort,
socket: Arc<UdpSocket>, socket: UdpSocket,
connections: RwLock<HashMap<PRUDPSockAddr, Connection>> connections: RwLock<HashMap<PRUDPSockAddr, Connection>>
} }
@ -20,7 +20,7 @@ pub struct Connection{
} }
impl Endpoint{ impl Endpoint{
pub fn new(socket: Arc<UdpSocket>, port: VirtualPort) -> Self{ pub fn new(socket: UdpSocket, port: VirtualPort) -> Self{
Self{ Self{
socket, socket,
virtual_port: port, virtual_port: port,
@ -72,7 +72,10 @@ impl Endpoint{
match packet.header.types_and_flags.get_types() { match packet.header.types_and_flags.get_types() {
types::SYN => { types::SYN => {
// reset heartbeat?
let response_header = packet.base_response_header();
} }
_ => unimplemented!() _ => unimplemented!()
} }

View file

@ -4,13 +4,14 @@ use std::io;
use std::io::{Cursor, ErrorKind, Read, Seek}; use std::io::{Cursor, ErrorKind, Read, Seek};
use std::net::SocketAddrV4; use std::net::SocketAddrV4;
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use log::{error, warn};
use thiserror::Error; use thiserror::Error;
use v_byte_macros::{EnumTryInto, SwapEndian}; use v_byte_macros::{EnumTryInto, SwapEndian};
use crate::endianness::{IS_BIG_ENDIAN, IS_LITTLE_ENDIAN, ReadExtensions}; use crate::endianness::{IS_BIG_ENDIAN, IS_LITTLE_ENDIAN, ReadExtensions};
use crate::prudp::sockaddr::PRUDPSockAddr; use crate::prudp::sockaddr::PRUDPSockAddr;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Error{ pub enum Error {
#[error("{0}")] #[error("{0}")]
IO(#[from] io::Error), IO(#[from] io::Error),
#[error("invalid magic {0:#06x}")] #[error("invalid magic {0:#06x}")]
@ -20,10 +21,10 @@ pub enum Error{
#[error("invalid option id {0}")] #[error("invalid option id {0}")]
InvalidOptionId(u8), InvalidOptionId(u8),
#[error("option size {size} doesnt match expected option for given option id {id}")] #[error("option size {size} doesnt match expected option for given option id {id}")]
InvalidOptionSize{ InvalidOptionSize {
id: u8, id: u8,
size: u8 size: u8,
} },
} }
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
@ -32,7 +33,7 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Copy, Clone, Pod, Zeroable, SwapEndian)] #[derive(Copy, Clone, Pod, Zeroable, SwapEndian)]
pub struct TypesFlags(u16); pub struct TypesFlags(u16);
impl TypesFlags{ impl TypesFlags {
pub fn get_types(self) -> u8 { pub fn get_types(self) -> u8 {
(self.0 & 0x000F) as u8 (self.0 & 0x000F) as u8
} }
@ -46,24 +47,23 @@ impl TypesFlags{
} }
pub fn flags(self, val: u16) -> Self { pub fn flags(self, val: u16) -> Self {
Self((self.0 & 0x000F) | ((val << 4) & 0xFFF0) ) Self((self.0 & 0x000F) | ((val << 4) & 0xFFF0))
} }
} }
pub mod flags{ pub mod flags {
pub const ACK: u16 = 0x001; pub const ACK: u16 = 0x001;
pub const RELIABLE: u16 = 0x002; pub const RELIABLE: u16 = 0x002;
pub const NEED_ACK: u16 = 0x004; pub const NEED_ACK: u16 = 0x004;
pub const HAS_SIZE: u16 = 0x008; pub const HAS_SIZE: u16 = 0x008;
pub const MULTI_ACK: u16 = 0x200; pub const MULTI_ACK: u16 = 0x200;
} }
pub mod types { pub mod types {
pub const SYN: u8 = 0x0; pub const SYN: u8 = 0x0;
} }
impl Debug for TypesFlags{ impl Debug for TypesFlags {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let stream_type = self.get_types(); let stream_type = self.get_types();
let port_number = self.get_flags(); let port_number = self.get_flags();
@ -75,7 +75,7 @@ impl Debug for TypesFlags{
#[derive(PartialEq, Eq, Copy, Clone, Pod, Zeroable, SwapEndian, Hash)] #[derive(PartialEq, Eq, Copy, Clone, Pod, Zeroable, SwapEndian, Hash)]
pub struct VirtualPort(u8); pub struct VirtualPort(u8);
impl VirtualPort{ impl VirtualPort {
#[inline] #[inline]
pub const fn get_stream_type(self) -> u8 { pub const fn get_stream_type(self) -> u8 {
(self.0 & 0xF0) >> 4 (self.0 & 0xF0) >> 4
@ -103,12 +103,12 @@ impl VirtualPort{
} }
#[inline] #[inline]
pub fn new(port: u8, stream_type: u8) -> Self{ pub fn new(port: u8, stream_type: u8) -> Self {
Self(0).stream_type(stream_type).port_number(port) Self(0).stream_type(stream_type).port_number(port)
} }
} }
impl Debug for VirtualPort{ impl Debug for VirtualPort {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let stream_type = self.get_stream_type(); let stream_type = self.get_stream_type();
let port_number = self.get_port_number(); let port_number = self.get_port_number();
@ -118,7 +118,7 @@ impl Debug for VirtualPort{
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Debug, Copy, Clone, Pod, Zeroable, SwapEndian)] #[derive(Debug, Copy, Clone, Pod, Zeroable, SwapEndian)]
pub struct PRUDPHeader{ pub struct PRUDPHeader {
magic: [u8; 2], magic: [u8; 2],
version: u8, version: u8,
pub packet_specific_size: u8, pub packet_specific_size: u8,
@ -132,22 +132,23 @@ pub struct PRUDPHeader{
} }
#[repr(u16)] #[repr(u16)]
#[derive(EnumTryInto)] #[derive(EnumTryInto)]
enum PacketSpecificData{ enum PacketSpecificData {
E = 0x10 E = 0x10
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct PRUDPPacket{ pub struct PRUDPPacket {
pub header: PRUDPHeader, pub header: PRUDPHeader,
pub payload: Vec<u8> pub payload: Vec<u8>,
pub options: Vec<(u8, Vec<u8>)>,
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
// Invariant: can only contain 0, 1, 2, 3 or 4 // Invariant: can only contain 0, 1, 2, 3 or 4
struct OptionId(u8); struct OptionId(u8);
impl OptionId{ impl OptionId {
fn new(val: u8) -> Result<Self>{ fn new(val: u8) -> Result<Self> {
// Invariant is upheld because we only create the object if it doesn't violate the invariant // Invariant is upheld because we only create the object if it doesn't violate the invariant
match val { match val {
0 | 1 | 2 | 3 | 4 => Ok(Self(val)), 0 | 1 | 2 | 3 | 4 => Ok(Self(val)),
@ -155,8 +156,8 @@ impl OptionId{
} }
} }
fn option_type_size(self) -> u8{ fn option_type_size(self) -> u8 {
match self.0{ match self.0 {
0 => 4, 0 => 4,
1 => 16, 1 => 16,
2 => 1, 2 => 1,
@ -169,40 +170,41 @@ impl OptionId{
} }
} }
impl Into<u8> for OptionId{ impl Into<u8> for OptionId {
fn into(self) -> u8 { fn into(self) -> u8 {
self.0 self.0
} }
} }
impl PRUDPPacket{ impl PRUDPPacket {
pub fn new(reader: &mut (impl Read + Seek)) -> Result<Self>{ pub fn new(reader: &mut (impl Read + Seek)) -> Result<Self> {
let header: PRUDPHeader = reader.read_struct(IS_BIG_ENDIAN)?; let header: PRUDPHeader = reader.read_struct(IS_BIG_ENDIAN)?;
if header.magic[0] != 0xEA || if header.magic[0] != 0xEA ||
header.magic[1] != 0xD0{ header.magic[1] != 0xD0 {
return Err(Error::InvalidMagic(u16::from_be_bytes(header.magic))); return Err(Error::InvalidMagic(u16::from_be_bytes(header.magic)));
} }
if header.version != 1{ if header.version != 1 {
return Err(Error::InvalidVersion(header.version)) return Err(Error::InvalidVersion(header.version));
} }
//discard it for now //discard it for now
let _: [u8; 16] = reader.read_struct(IS_BIG_ENDIAN)?; let _: [u8; 16] = reader.read_struct(IS_BIG_ENDIAN)?;
assert_eq!(reader.stream_position().ok(), Some(14+16)); assert_eq!(reader.stream_position().ok(), Some(14 + 16));
let mut packet_specific_buffer = vec![0u8; header.packet_specific_size as usize]; let mut packet_specific_buffer = vec![0u8; header.packet_specific_size as usize];
reader.read_exact(&mut packet_specific_buffer)?; reader.read_exact(&mut packet_specific_buffer)?;
//no clue whats up with options but they are broken //no clue whats up with options but they are broken
let mut packet_specific_data_cursor = Cursor::new(&packet_specific_buffer); let mut packet_specific_data_cursor = Cursor::new(&packet_specific_buffer);
let mut options = Vec::new();
loop { loop {
let Ok(option_id): io::Result<u8> = packet_specific_data_cursor.read_struct(IS_BIG_ENDIAN) else { let Ok(option_id): io::Result<u8> = packet_specific_data_cursor.read_struct(IS_BIG_ENDIAN) else {
break break
@ -214,22 +216,27 @@ impl PRUDPPacket{
if value_size == 0 { if value_size == 0 {
// skip it if its 0 and dont check? // skip it if its 0 and dont check?
warn!("reading packets options might be going wrong");
continue; continue;
} }
let option_id: OptionId = OptionId::new(option_id)?; let option_id: OptionId = OptionId::new(option_id)?;
if option_id.option_type_size() != value_size{ if option_id.option_type_size() != value_size {
error!("invalid packet options");
return Err(Error::InvalidOptionSize { return Err(Error::InvalidOptionSize {
size: value_size, size: value_size,
id: option_id.0 id: option_id.0,
}) });
} }
let mut option_data = vec![0u8,value_size]; let mut option_data = vec![0u8, value_size];
if packet_specific_data_cursor.read_exact(&mut option_data).is_err(){ if packet_specific_data_cursor.read_exact(&mut option_data).is_err() {
error!("unable to read options");
break; break;
} }
options.push((option_id.into(), option_data));
} }
@ -237,25 +244,42 @@ impl PRUDPPacket{
reader.read_exact(&mut payload)?; reader.read_exact(&mut payload)?;
Ok(Self{ Ok(Self {
header, header,
payload payload,
options,
}) })
} }
pub fn source_sockaddr(&self,socket_addr_v4: SocketAddrV4) -> PRUDPSockAddr{ pub fn source_sockaddr(&self, socket_addr_v4: SocketAddrV4) -> PRUDPSockAddr {
PRUDPSockAddr{ PRUDPSockAddr {
regular_socket_addr: socket_addr_v4, regular_socket_addr: socket_addr_v4,
virtual_port: self.header.source_port virtual_port: self.header.source_port,
}
}
pub fn base_response_header(&self) -> PRUDPHeader {
PRUDPHeader {
magic: [0xEA, 0xD0],
types_and_flags: TypesFlags(0),
destination_port: self.header.source_port,
source_port: self.header.destination_port,
payload_size: 0,
version: 1,
packet_specific_size: 0,
sequence_id: 0,
session_id: 0,
substream_id: 0,
} }
} }
} }
#[cfg(test)] #[cfg(test)]
mod test{ mod test {
use super::{PRUDPHeader}; use super::{PRUDPHeader};
#[test] #[test]
fn size_test(){ fn size_test() {
assert_eq!(size_of::<PRUDPHeader>(), 14); assert_eq!(size_of::<PRUDPHeader>(), 14);
} }
} }

View file

@ -23,7 +23,7 @@ static SERVER_DATAGRAMS: Lazy<u8> = Lazy::new(||{
pub struct NexServer{ pub struct NexServer{
pub endpoints: OnceLock<Vec<Endpoint>>, pub endpoints: OnceLock<Vec<Endpoint>>,
pub socket: Arc<UdpSocket>, pub socket: UdpSocket,
pub running: AtomicBool, pub running: AtomicBool,
//pub auth_module: Arc<dyn AuthModule> //pub auth_module: Arc<dyn AuthModule>
_no_outside_construction: PhantomData<()> _no_outside_construction: PhantomData<()>
@ -68,7 +68,7 @@ impl NexServer{
} }
} }
fn server_thread_entry(self: Arc<Self>, socket: Arc<UdpSocket>){ fn server_thread_entry(self: Arc<Self>, socket: UdpSocket){
info!("starting datagram thread"); info!("starting datagram thread");
while self.running.load(Ordering::Relaxed) { while self.running.load(Ordering::Relaxed) {
@ -90,12 +90,12 @@ impl NexServer{
} }
pub fn new(addr: SocketAddrV4) -> io::Result<(Arc<Self>, JoinHandle<()>)>{ pub fn new(addr: SocketAddrV4) -> io::Result<(Arc<Self>, JoinHandle<()>)>{
let socket = Arc::new(UdpSocket::bind(addr)?); let socket = UdpSocket::bind(addr)?;
let own_impl = NexServer{ let own_impl = NexServer{
endpoints: Default::default(), endpoints: Default::default(),
running: AtomicBool::new(true), running: AtomicBool::new(true),
socket: socket.clone(), socket: socket.try_clone().unwrap(),
_no_outside_construction: Default::default() _no_outside_construction: Default::default()
}; };
@ -104,7 +104,7 @@ impl NexServer{
let mut thread = None; let mut thread = None;
for _ in 0..*SERVER_DATAGRAMS { for _ in 0..*SERVER_DATAGRAMS {
let socket = socket.clone(); let socket = socket.try_clone().unwrap();
let server= arc.clone(); let server= arc.clone();
thread = Some(thread::spawn(move || { thread = Some(thread::spawn(move || {