diff options
| author | Ulf Lilleengen <[email protected]> | 2023-01-31 22:06:41 +0100 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2023-02-10 17:45:03 +0100 |
| commit | 9cfea693edec5af17ba698f64b3f0a168ad92944 (patch) | |
| tree | c5c4508bba1583fcea082143e331dca2f498e32d /examples/std | |
| parent | 023b0d5b2270f31aa69e54aa3d43416e16c33966 (diff) | |
Add DNS socket to embassy-net
Diffstat (limited to 'examples/std')
| -rw-r--r-- | examples/std/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/std/src/bin/net_dns.rs | 102 |
2 files changed, 103 insertions, 1 deletions
diff --git a/examples/std/Cargo.toml b/examples/std/Cargo.toml index af1481e08..8087df09a 100644 --- a/examples/std/Cargo.toml +++ b/examples/std/Cargo.toml | |||
| @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0" | |||
| 8 | embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["log"] } | 8 | embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["log"] } |
| 9 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["log", "std", "nightly", "integrated-timers"] } | 9 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["log", "std", "nightly", "integrated-timers"] } |
| 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["log", "std", "nightly"] } | 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["log", "std", "nightly"] } |
| 11 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "nightly", "log", "medium-ethernet", "tcp", "udp", "dhcpv4"] } | 11 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "nightly", "log", "medium-ethernet", "tcp", "udp", "dns", "dhcpv4", "unstable-traits", "proto-ipv6"] } |
| 12 | embassy-net-driver = { version = "0.1.0", path = "../../embassy-net-driver" } | 12 | embassy-net-driver = { version = "0.1.0", path = "../../embassy-net-driver" } |
| 13 | embedded-io = { version = "0.4.0", features = ["async", "std", "futures"] } | 13 | embedded-io = { version = "0.4.0", features = ["async", "std", "futures"] } |
| 14 | critical-section = { version = "1.1", features = ["std"] } | 14 | critical-section = { version = "1.1", features = ["std"] } |
diff --git a/examples/std/src/bin/net_dns.rs b/examples/std/src/bin/net_dns.rs new file mode 100644 index 000000000..6203f8370 --- /dev/null +++ b/examples/std/src/bin/net_dns.rs | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | #![feature(type_alias_impl_trait)] | ||
| 2 | |||
| 3 | use std::default::Default; | ||
| 4 | |||
| 5 | use clap::Parser; | ||
| 6 | use embassy_executor::{Executor, Spawner}; | ||
| 7 | use embassy_net::dns::{DnsQueryType, DnsSocket}; | ||
| 8 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | ||
| 9 | use heapless::Vec; | ||
| 10 | use log::*; | ||
| 11 | use rand_core::{OsRng, RngCore}; | ||
| 12 | use static_cell::StaticCell; | ||
| 13 | |||
| 14 | #[path = "../tuntap.rs"] | ||
| 15 | mod tuntap; | ||
| 16 | |||
| 17 | use crate::tuntap::TunTapDevice; | ||
| 18 | |||
| 19 | macro_rules! singleton { | ||
| 20 | ($val:expr) => {{ | ||
| 21 | type T = impl Sized; | ||
| 22 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); | ||
| 23 | STATIC_CELL.init_with(move || $val) | ||
| 24 | }}; | ||
| 25 | } | ||
| 26 | |||
| 27 | #[derive(Parser)] | ||
| 28 | #[clap(version = "1.0")] | ||
| 29 | struct Opts { | ||
| 30 | /// TAP device name | ||
| 31 | #[clap(long, default_value = "tap0")] | ||
| 32 | tap: String, | ||
| 33 | /// use a static IP instead of DHCP | ||
| 34 | #[clap(long)] | ||
| 35 | static_ip: bool, | ||
| 36 | } | ||
| 37 | |||
| 38 | #[embassy_executor::task] | ||
| 39 | async fn net_task(stack: &'static Stack<TunTapDevice>) -> ! { | ||
| 40 | stack.run().await | ||
| 41 | } | ||
| 42 | |||
| 43 | #[embassy_executor::task] | ||
| 44 | async fn main_task(spawner: Spawner) { | ||
| 45 | let opts: Opts = Opts::parse(); | ||
| 46 | |||
| 47 | // Init network device | ||
| 48 | let device = TunTapDevice::new(&opts.tap).unwrap(); | ||
| 49 | |||
| 50 | // Choose between dhcp or static ip | ||
| 51 | let config = if opts.static_ip { | ||
| 52 | Config::Static(embassy_net::StaticConfig { | ||
| 53 | address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 1), 24), | ||
| 54 | dns_servers: Vec::from_slice(&[Ipv4Address::new(8, 8, 4, 4).into(), Ipv4Address::new(8, 8, 8, 8).into()]) | ||
| 55 | .unwrap(), | ||
| 56 | gateway: Some(Ipv4Address::new(192, 168, 69, 100)), | ||
| 57 | }) | ||
| 58 | } else { | ||
| 59 | Config::Dhcp(Default::default()) | ||
| 60 | }; | ||
| 61 | |||
| 62 | // Generate random seed | ||
| 63 | let mut seed = [0; 8]; | ||
| 64 | OsRng.fill_bytes(&mut seed); | ||
| 65 | let seed = u64::from_le_bytes(seed); | ||
| 66 | |||
| 67 | // Init network stack | ||
| 68 | let stack = &*singleton!(Stack::new(device, config, singleton!(StackResources::<2>::new()), seed)); | ||
| 69 | |||
| 70 | // Launch network task | ||
| 71 | spawner.spawn(net_task(stack)).unwrap(); | ||
| 72 | |||
| 73 | // Then we can use it! | ||
| 74 | |||
| 75 | let mut socket = DnsSocket::new(stack, vec![]); | ||
| 76 | |||
| 77 | let host = "example.com"; | ||
| 78 | info!("querying host {:?}...", host); | ||
| 79 | match socket.query(host, DnsQueryType::A).await { | ||
| 80 | Ok(r) => { | ||
| 81 | info!("query response: {:?}", r); | ||
| 82 | } | ||
| 83 | Err(e) => { | ||
| 84 | warn!("query error: {:?}", e); | ||
| 85 | } | ||
| 86 | }; | ||
| 87 | } | ||
| 88 | |||
| 89 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); | ||
| 90 | |||
| 91 | fn main() { | ||
| 92 | env_logger::builder() | ||
| 93 | .filter_level(log::LevelFilter::Debug) | ||
| 94 | .filter_module("async_io", log::LevelFilter::Info) | ||
| 95 | .format_timestamp_nanos() | ||
| 96 | .init(); | ||
| 97 | |||
| 98 | let executor = EXECUTOR.init(Executor::new()); | ||
| 99 | executor.run(|spawner| { | ||
| 100 | spawner.spawn(main_task(spawner)).unwrap(); | ||
| 101 | }); | ||
| 102 | } | ||
