diff --git a/Cargo.lock b/Cargo.lock index 680fa39..0b03b58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,49 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "ahash" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" +dependencies = [ + "cfg-if", + "getrandom 0.3.4", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "anyhow" version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "arrayvec" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" + +[[package]] +name = "async-channel" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "924ed96dd52d1b75e9c1a3e6275715fd320f5f9439fb5a4a11fa51f4221158d2" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-task" +version = "4.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" + [[package]] name = "async-trait" version = "0.1.92" @@ -16,15 +53,46 @@ checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 3.0.10", ] +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + [[package]] name = "bitflags" version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" +[[package]] +name = "blocking" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e83f8d02be6967315521be875afa792a316e28d57b5a2d401897e2a7921b7f21" +dependencies = [ + "async-channel", + "async-task", + "futures-io", + "futures-lite", + "piper", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + [[package]] name = "bytes" version = "1.12.1" @@ -37,6 +105,63 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" +[[package]] +name = "cfg_aliases" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" + +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "core-foundation" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "core_detect" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "encoding_rs" +version = "0.8.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89253175e704ab869b04f9db224d38630e0211a53841a79181deae0889e169f5" +dependencies = [ + "cfg-if", + "core_detect", + "multiversion_no_op", + "rustversion", + "scopeguard", + "simdutf8", +] + [[package]] name = "errno" version = "0.3.14" @@ -47,12 +172,153 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "etherparse" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17304d06addb3283cdc4bd528e42dd95e73c8ee2d6492ffce415e93660885449" +dependencies = [ + "arrayvec", +] + +[[package]] +name = "event-listener" +version = "5.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13b66accf52311f30a0db42147dadea9850cb48cd070028831ae5f5d4b856ab" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93" +dependencies = [ + "event-listener", + "pin-project-lite", +] + +[[package]] +name = "fastrand" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" +dependencies = [ + "getrandom 0.4.3", +] + +[[package]] +name = "flume" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e139bc46ca777eb5efaf62df0ab8cc5fd400866427e56c68b22e414e53bd3be" +dependencies = [ + "fastrand", + "futures-core", + "futures-sink", + "spin", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-io" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718" + +[[package]] +name = "futures-lite" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad" +dependencies = [ + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "futures-sink" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" + +[[package]] +name = "getifaddrs" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a542e1b7ac1f3d62c5777d430d66eca9cb59e813c46b86e29fa9ce94ff9a4810" +dependencies = [ + "bitflags", + "libc", + "windows-sys", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi 5.3.0", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi 6.0.0", + "wasm-bindgen", +] + +[[package]] +name = "ipnet" +version = "2.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" + +[[package]] +name = "js-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + [[package]] name = "libc" version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" +[[package]] +name = "libloading" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "754ca22de805bb5744484a5b151a9e1a8e837d5dc232c2d7d8c2e3492edc8b60" +dependencies = [ + "cfg-if", + "windows-link 0.2.1", +] + [[package]] name = "lock_api" version = "0.4.14" @@ -62,6 +328,21 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "log" +version = "0.4.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" + +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + [[package]] name = "mio" version = "1.2.4" @@ -73,6 +354,133 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "multiversion_no_op" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d" + +[[package]] +name = "netconfig-rs" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "733ad7a1352f0748a620368f2ea5cd94f0e449c5108fbac86ace31df1601bf6f" +dependencies = [ + "cfg-if", + "core-foundation", + "ipnet", + "libc", + "netlink-packet-core 0.8.2", + "netlink-packet-route 0.25.1", + "netlink-sys 0.8.8", + "nix 0.30.1", + "scopeguard", + "system-configuration-sys", + "thiserror", + "widestring", + "windows", +] + +[[package]] +name = "netlink-packet-core" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b897d7bd4f0af82e68d40d0344cf37e97f9c97ddf74a098de3e4da05e96ca395" +dependencies = [ + "paste", +] + +[[package]] +name = "netlink-packet-core" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d6e2daf9a8c2e21302714706860a0e6be02e03d17294ecc66b354ea7d4059dd" + +[[package]] +name = "netlink-packet-route" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ec2f5b6839be2a19d7fa5aab5bc444380f6311c2b693551cb80f45caaa7b5ef" +dependencies = [ + "bitflags", + "libc", + "log", + "netlink-packet-core 0.8.2", +] + +[[package]] +name = "netlink-packet-route" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59d48ffc8b73a506e1ff0878528c72668f2bfbc3d875b6be890a96be5d0d5576" +dependencies = [ + "bitflags", + "libc", + "log", + "netlink-packet-core 0.9.0", + "zerocopy", +] + +[[package]] +name = "netlink-sys" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6c30ed10fa69cc491d491b85cc971f6bdeb8e7367b7cde2ee6cc878d583fae" +dependencies = [ + "bytes", + "libc", + "log", +] + +[[package]] +name = "netlink-sys" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c99d38e00b420df49e940fe0826e667c3dad82ac68eb4c2bbf754c1c14a83a10" +dependencies = [ + "bytes", + "libc", + "log", +] + +[[package]] +name = "nix" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", + "memoffset", +] + +[[package]] +name = "nix" +version = "0.31.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", + "memoffset", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + [[package]] name = "parking_lot" version = "0.12.5" @@ -93,15 +501,32 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-link", + "windows-link 0.2.1", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pin-project-lite" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" +[[package]] +name = "piper" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c8c490f422ef9a4efd2cb5b42b76c8613d7e7dfc1caf667b8a3350a5acc066" +dependencies = [ + "atomic-waker", + "fastrand", + "futures-io", +] + [[package]] name = "proc-macro2" version = "1.0.106" @@ -120,6 +545,18 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + [[package]] name = "redox_syscall" version = "0.5.18" @@ -129,6 +566,26 @@ dependencies = [ "bitflags", ] +[[package]] +name = "route_manager" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "694798681cd69a10928204225a680ad9416b081d9e9f8718d7a61e2d134da323" +dependencies = [ + "flume", + "libc", + "netlink-packet-core 0.9.0", + "netlink-packet-route 0.33.0", + "netlink-sys 0.9.0", + "windows-sys", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + [[package]] name = "scopeguard" version = "1.2.0" @@ -145,6 +602,12 @@ dependencies = [ "libc", ] +[[package]] +name = "simdutf8" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" + [[package]] name = "smallvec" version = "1.16.5" @@ -161,6 +624,26 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "spin" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e" +dependencies = [ + "lock_api", +] + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "3.0.10" @@ -172,6 +655,36 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + [[package]] name = "tokio" version = "1.54.1" @@ -197,7 +710,32 @@ checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 3.0.10", +] + +[[package]] +name = "tun-rs" +version = "2.8.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7300c968315399fcef3b386a0690e9d772cc4005e92d3d6be1dec6774fb98fd" +dependencies = [ + "ahash", + "blocking", + "bytes", + "encoding_rs", + "getifaddrs", + "ipnet", + "libc", + "libloading", + "log", + "netconfig-rs", + "nix 0.31.3", + "route_manager", + "scopeguard", + "tokio", + "widestring", + "windows-sys", + "winreg", ] [[package]] @@ -206,6 +744,12 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "virtual_network_cli" version = "0.1.0" @@ -216,6 +760,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", + "etherparse", "tokio", ] @@ -236,6 +781,7 @@ dependencies = [ "anyhow", "async-trait", "tokio", + "tun-rs", "virtual_network_core", ] @@ -262,17 +808,223 @@ version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" +dependencies = [ + "cfg-if", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.10", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "widestring" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72069c3113ab32ab29e5584db3c6ec55d416895e60715417b5b883a357c3e471" + +[[package]] +name = "windows" +version = "0.61.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893" +dependencies = [ + "windows-collections", + "windows-core", + "windows-future", + "windows-link 0.1.3", + "windows-numerics", +] + +[[package]] +name = "windows-collections" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8" +dependencies = [ + "windows-core", +] + +[[package]] +name = "windows-core" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link 0.1.3", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-future" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e" +dependencies = [ + "windows-core", + "windows-link 0.1.3", + "windows-threading", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-link" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-numerics" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1" +dependencies = [ + "windows-core", + "windows-link 0.1.3", +] + +[[package]] +name = "windows-result" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6" +dependencies = [ + "windows-link 0.1.3", +] + +[[package]] +name = "windows-strings" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57" +dependencies = [ + "windows-link 0.1.3", +] + [[package]] name = "windows-sys" version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ - "windows-link", + "windows-link 0.2.1", +] + +[[package]] +name = "windows-threading" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6" +dependencies = [ + "windows-link 0.1.3", +] + +[[package]] +name = "winreg" +version = "0.56.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d6f32a0ff4a9f6f01231eb2059cc85479330739333e0e58cadf03b6af2cca10" +dependencies = [ + "cfg-if", + "windows-sys", +] + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "zerocopy" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", ] diff --git a/virtual_network_core/Cargo.toml b/virtual_network_core/Cargo.toml index 11502fd..f2dbd43 100644 --- a/virtual_network_core/Cargo.toml +++ b/virtual_network_core/Cargo.toml @@ -7,3 +7,4 @@ edition = "2024" anyhow = "1.0.104" async-trait = "0.1.92" tokio = { version = "1.54.1", features = ["sync"] } +etherparse = "0.21.0" diff --git a/virtual_network_core/src/lib.rs b/virtual_network_core/src/lib.rs index 7f17b2c..7a2a38f 100644 --- a/virtual_network_core/src/lib.rs +++ b/virtual_network_core/src/lib.rs @@ -1,5 +1,7 @@ pub mod mesh; pub mod driver; pub mod router; +pub mod packet; +pub mod network; pub const RECV_BUFFER_SIZE: usize = 65535; diff --git a/virtual_network_core/src/network.rs b/virtual_network_core/src/network.rs new file mode 100644 index 0000000..4fb616c --- /dev/null +++ b/virtual_network_core/src/network.rs @@ -0,0 +1,133 @@ +use std::fmt; +use std::net::Ipv4Addr; + +use anyhow::ensure; + +/// The virtual IPv4 subnet shared by all peers of a network. +/// +/// Every node's virtual address must be a host address inside it, and the OS +/// routes every destination inside it into the virtual interface. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct VirtualNetwork { + network: Ipv4Addr, + prefix_len: u8, +} + +impl VirtualNetwork { + /// Creates the network `network/prefix_len`. + /// + /// `prefix_len` must be between 1 and 30 (/31 and /32 leave no room for + /// peers), and `network` must have no host bits set. + pub fn new(network: Ipv4Addr, prefix_len: u8) -> anyhow::Result { + ensure!( + (1..=30).contains(&prefix_len), + "prefix length {prefix_len} must be between 1 and 30" + ); + + let result = Self { network, prefix_len }; + ensure!( + (u32::from(network) & !result.mask()) == 0, + "{network}/{prefix_len} has host bits set" + ); + Ok(result) + } + + /// The network address, e.g. `100.64.0.0` for `100.64.0.0/24`. + pub fn network(&self) -> Ipv4Addr { + self.network + } + + /// The prefix length, e.g. `24` for `100.64.0.0/24`. + pub fn prefix_len(&self) -> u8 { + self.prefix_len + } + + /// The subnet mask, e.g. `255.255.255.0` for a `/24`. + pub fn netmask(&self) -> Ipv4Addr { + Ipv4Addr::from(self.mask()) + } + + /// The subnet broadcast address, e.g. `100.64.0.255` for `100.64.0.0/24`. + pub fn broadcast(&self) -> Ipv4Addr { + Ipv4Addr::from(u32::from(self.network) | !self.mask()) + } + + /// Whether `ip` is inside the network, including the network and broadcast + /// addresses. + pub fn contains(&self, ip: Ipv4Addr) -> bool { + u32::from(ip) & self.mask() == u32::from(self.network) + } + + /// Whether `ip` can be assigned to a node: inside the network, and neither + /// the network nor the broadcast address. + pub fn is_host(&self, ip: Ipv4Addr) -> bool { + self.contains(ip) && ip != self.network && ip != self.broadcast() + } + + fn mask(&self) -> u32 { + u32::MAX << (32 - self.prefix_len) + } +} + +impl fmt::Display for VirtualNetwork { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}/{}", self.network, self.prefix_len) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn net() -> VirtualNetwork { + VirtualNetwork::new(Ipv4Addr::new(100, 64, 0, 0), 24).unwrap() + } + + #[test] + fn derived_addresses() { + let net = net(); + assert_eq!(net.network(), Ipv4Addr::new(100, 64, 0, 0)); + assert_eq!(net.prefix_len(), 24); + assert_eq!(net.to_string(), "100.64.0.0/24"); + assert_eq!(net.netmask(), Ipv4Addr::new(255, 255, 255, 0)); + assert_eq!(net.broadcast(), Ipv4Addr::new(100, 64, 0, 255)); + } + + /// The mask math must also work when the prefix doesn't end on an octet. + #[test] + fn unaligned_prefix() { + let net = VirtualNetwork::new(Ipv4Addr::new(100, 64, 0, 0), 10).unwrap(); + assert_eq!(net.netmask(), Ipv4Addr::new(255, 192, 0, 0)); + assert_eq!(net.broadcast(), Ipv4Addr::new(100, 127, 255, 255)); + assert!(net.is_host(Ipv4Addr::new(100, 100, 0, 1))); + assert!(!net.contains(Ipv4Addr::new(100, 128, 0, 0))); + } + + #[test] + fn rejects_invalid_prefix_len() { + for prefix_len in [0, 31, 32, 33] { + assert!(VirtualNetwork::new(Ipv4Addr::new(10, 0, 0, 0), prefix_len).is_err()); + } + } + + #[test] + fn rejects_host_bits() { + assert!(VirtualNetwork::new(Ipv4Addr::new(100, 64, 0, 1), 24).is_err()); + assert!(VirtualNetwork::new(Ipv4Addr::new(100, 64, 0, 128), 24).is_err()); + } + + #[test] + fn membership() { + let net = net(); + + assert!(net.is_host(Ipv4Addr::new(100, 64, 0, 1))); + assert!(net.is_host(Ipv4Addr::new(100, 64, 0, 254))); + + assert!(net.contains(net.network()) && !net.is_host(net.network())); + assert!(net.contains(net.broadcast()) && !net.is_host(net.broadcast())); + + assert!(!net.contains(Ipv4Addr::new(100, 64, 1, 1))); + assert!(!net.contains(Ipv4Addr::new(100, 63, 255, 255))); + assert!(!net.is_host(Ipv4Addr::new(10, 0, 0, 1))); + } +} diff --git a/virtual_network_core/src/packet.rs b/virtual_network_core/src/packet.rs new file mode 100644 index 0000000..7a3ab78 --- /dev/null +++ b/virtual_network_core/src/packet.rs @@ -0,0 +1,84 @@ +use std::net::Ipv4Addr; + +use etherparse::Ipv4Slice; + +/// The addresses of a validated IPv4 packet. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Ipv4Header { + pub source: Ipv4Addr, + pub destination: Ipv4Addr, +} + +/// Validates that `packet` is exactly one well-formed IPv4 packet and returns +/// its addresses. +/// +/// Checks the version, header length (IHL) and total length against the +/// buffer. Only the IP header is inspected, never the payload. +pub fn parse_ipv4(packet: &[u8]) -> Option { + let slice = Ipv4Slice::from_slice(packet).ok()?; + let header = slice.header(); + + // `from_slice` tolerates trailing bytes after the packet; a packet + // buffer must not have any. + if usize::from(header.total_len()) != packet.len() { + return None; + } + + Some(Ipv4Header { + source: header.source_addr(), + destination: header.destination_addr(), + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use etherparse::PacketBuilder; + + fn udp_packet(source: [u8; 4], destination: [u8; 4]) -> Vec { + let builder = PacketBuilder::ipv4(source, destination, 64).udp(1000, 2000); + let payload = b"hello"; + let mut packet = Vec::with_capacity(builder.size(payload.len())); + builder.write(&mut packet, payload).unwrap(); + packet + } + + #[test] + fn parses_valid_packet() { + let packet = udp_packet([100, 64, 0, 1], [100, 64, 0, 2]); + assert_eq!( + parse_ipv4(&packet), + Some(Ipv4Header { + source: Ipv4Addr::new(100, 64, 0, 1), + destination: Ipv4Addr::new(100, 64, 0, 2), + }) + ); + } + + #[test] + fn rejects_ipv6() { + let builder = PacketBuilder::ipv6([0; 16], [0; 16], 64).udp(1000, 2000); + let mut packet = Vec::new(); + builder.write(&mut packet, b"hello").unwrap(); + assert_eq!(parse_ipv4(&packet), None); + } + + #[test] + fn rejects_truncated_and_padded() { + let packet = udp_packet([100, 64, 0, 1], [100, 64, 0, 2]); + assert_eq!(parse_ipv4(&packet[..packet.len() - 1]), None); + assert_eq!(parse_ipv4(&packet[..10]), None); + assert_eq!(parse_ipv4(&[]), None); + + let mut padded = packet.clone(); + padded.push(0); + assert_eq!(parse_ipv4(&padded), None); + } + + #[test] + fn rejects_bad_ihl() { + let mut packet = udp_packet([100, 64, 0, 1], [100, 64, 0, 2]); + packet[0] = 0x44; // IHL 4 (16 bytes) is below the minimum of 5 + assert_eq!(parse_ipv4(&packet), None); + } +} diff --git a/virtual_network_driver/Cargo.toml b/virtual_network_driver/Cargo.toml index b53737e..1d67467 100644 --- a/virtual_network_driver/Cargo.toml +++ b/virtual_network_driver/Cargo.toml @@ -8,4 +8,4 @@ virtual_network_core = { path = "../virtual_network_core" } anyhow = "1.0.104" async-trait = "0.1.92" tokio = { version = "1.54.1", features = ["full"] } - +tun-rs = { version = "2.8.18", features = ["async_tokio"] } diff --git a/virtual_network_driver/src/driver.rs b/virtual_network_driver/src/driver.rs deleted file mode 100644 index babfeac..0000000 --- a/virtual_network_driver/src/driver.rs +++ /dev/null @@ -1,27 +0,0 @@ -use std::net::Ipv4Addr; - -use async_trait::async_trait; -use virtual_network_core::RECV_BUFFER_SIZE; - -struct Driver {} - -impl Driver { - fn new() -> Self { - Driver {} - } -} - -#[async_trait] -impl virtual_network_core::driver::DeviceDriver for Driver { - async fn recv(&self, buf: &mut [u8; RECV_BUFFER_SIZE]) -> anyhow::Result { - todo!() - } - - async fn send(&self, packet: &[u8]) -> anyhow::Result<()> { - todo!() - } - - fn virtual_ipv4(&self) -> Ipv4Addr { - todo!() - } -} diff --git a/virtual_network_driver/src/lib.rs b/virtual_network_driver/src/lib.rs index 26f5116..061ef37 100644 --- a/virtual_network_driver/src/lib.rs +++ b/virtual_network_driver/src/lib.rs @@ -1,2 +1,2 @@ -pub mod driver; +pub mod tun; pub mod mock; diff --git a/virtual_network_driver/src/tun.rs b/virtual_network_driver/src/tun.rs new file mode 100644 index 0000000..f84af9f --- /dev/null +++ b/virtual_network_driver/src/tun.rs @@ -0,0 +1,107 @@ +use std::io; +use std::net::Ipv4Addr; + +use anyhow::{Context, ensure}; +use async_trait::async_trait; +use tun_rs::{AsyncDevice, DeviceBuilder}; +use virtual_network_core::RECV_BUFFER_SIZE; +use virtual_network_core::driver::DeviceDriver; +use virtual_network_core::network::VirtualNetwork; +use virtual_network_core::packet::parse_ipv4; + +pub const TUN_MTU: u16 = 1280; + +/// Linux limits interface names to IFNAMSIZ - 1 bytes. +const MAX_IFNAME_LEN: usize = 15; + +/// A Linux TUN interface carrying the virtual network's IPv4 traffic. +/// +/// The interface is created and brought up in [`TunDriver::new`] and removed +/// when the driver is dropped. Creating it requires root or `CAP_NET_ADMIN`. +pub struct TunDriver { + device: AsyncDevice, + ipv4_address: Ipv4Addr, +} + +impl TunDriver { + /// Creates the TUN interface `name` with address `ipv4_address` inside `network`. + /// + /// The kernel routes every destination inside `network` into this + /// interface. + pub fn new(name: &str, ipv4_address: Ipv4Addr, network: &VirtualNetwork) -> anyhow::Result { + ensure!( + !name.is_empty() && name.len() <= MAX_IFNAME_LEN, + "interface name {name:?} must be 1..={MAX_IFNAME_LEN} bytes" + ); + ensure!( + network.is_host(ipv4_address), + "{ipv4_address} is not a host address in the virtual network {network}" + ); + + let device = DeviceBuilder::new() + .name(name) + .ipv4(ipv4_address, network.prefix_len(), None) + .mtu(TUN_MTU) + .build_async() + .map_err(|err| match err.kind() { + io::ErrorKind::PermissionDenied => anyhow::Error::new(err).context(format!( + "creating TUN interface {name} requires root or CAP_NET_ADMIN" + )), + _ => anyhow::Error::new(err) + .context(format!("failed to create TUN interface {name}")), + })?; + + Ok(TunDriver { device, ipv4_address }) + } + + /// Returns the interface name as reported by the kernel. + pub fn name(&self) -> anyhow::Result { + self.device.name().context("failed to read TUN interface name") + } +} + +#[async_trait] +impl DeviceDriver for TunDriver { + async fn recv(&self, buf: &mut [u8; RECV_BUFFER_SIZE]) -> anyhow::Result { + // The kernel also emits IPv6 traffic (router solicitations, MLD, ...) + // on the interface; the trait only promises IPv4, so skip anything + // that isn't a valid IPv4 packet. + loop { + let len = self + .device + .recv(buf) + .await + .context("failed to read from TUN interface")?; + + if parse_ipv4(&buf[..len]).is_some() { + return Ok(len); + } + } + } + + async fn send(&self, packet: &[u8]) -> anyhow::Result<()> { + // Without packet info, Linux picks the protocol from the version + // nibble, so this is what stops a peer from injecting IPv6 or + // malformed packets. + ensure!( + parse_ipv4(packet).is_some(), + "refusing to write an invalid IPv4 packet to TUN" + ); + + let written = self + .device + .send(packet) + .await + .context("failed to write to TUN interface")?; + ensure!( + written == packet.len(), + "short write to TUN: {written}/{} bytes", + packet.len() + ); + Ok(()) + } + + fn virtual_ipv4(&self) -> Ipv4Addr { + self.ipv4_address + } +}