Skip to main content

strat9_kernel/shell/commands/net/
nslookup.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23use super::*;
24
25/// Minimal DNS lookup command.
26///
27/// Reads the configured DNS servers and attempts a resolution via the
28/// `/net/dns/resolve/<domain>` scheme path. If the network strate does
29/// not yet support it, shows the configured nameservers and a TODO.
30pub fn cmd_nslookup(args: &[String]) -> Result<(), ShellError> {
31    if args.is_empty() {
32        shell_println!("Usage: nslookup <domain>");
33        return Err(ShellError::InvalidArguments);
34    }
35    let domain = &args[0];
36
37    let dns = match vfs::open("/net/dns", OpenFlags::READ) {
38        Ok(fd) => {
39            let mut buf = [0u8; 128];
40            let n = vfs::read(fd, &mut buf).unwrap_or(0);
41            let _ = vfs::close(fd);
42            let s = core::str::from_utf8(&buf[..n]).unwrap_or("").trim();
43            String::from(s)
44        }
45        Err(_) => String::from("(not configured)"),
46    };
47    shell_println!("Server: {}", dns);
48
49    let resolve_path = alloc::format!("/net/dns/resolve/{}", domain);
50    match vfs::open(&resolve_path, OpenFlags::READ) {
51        Ok(fd) => {
52            let mut buf = [0u8; 256];
53            let n = vfs::read(fd, &mut buf).unwrap_or(0);
54            let _ = vfs::close(fd);
55            if n > 0 {
56                let result = core::str::from_utf8(&buf[..n]).unwrap_or("(invalid)");
57                shell_println!("Name:   {}", domain);
58                shell_println!("Address: {}", result.trim());
59            } else {
60                shell_println!("** cannot resolve {}", domain);
61            }
62        }
63        Err(_) => {
64            // TODO: optional fallback resolver if /net/resolve is unavailable
65            shell_println!("** DNS resolution unavailable in /net scheme");
66            shell_println!("   Configured nameserver(s): {}", dns);
67        }
68    }
69    Ok(())
70}