Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor network commands #322

Merged
merged 17 commits into from
Apr 10, 2022
59 changes: 27 additions & 32 deletions doc/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@

## NET

The `net` command allows you to configure your network interface:
Display the network configuration:

> net config debug true
> net config
mac: 52-54-00-12-34-56
ip: 10.0.2.15/24
gw: 10.0.2.2
dns: 10.0.2.3

And listen what is happening on the network:
Display one attribute of the network configuration:

> net config dns
dns: 10.0.2.3

Set one attribute of the network configuration:

> net config dns 10.0.2.3

Display network statistics:

> net stat
rx: 13 packets (4052 bytes)
tx: 15 packets (1518 bytes)

Listen for packets transmitted on the network:

> net monitor
------------------------------------------------------------------
Expand All @@ -28,38 +47,14 @@ And listen what is happening on the network:

## DHCP

The `dhcp` command configures your network automatically:
The `dhcp` command configures the network automatically:

> dhcp
> dhcp -v
DEBUG: DHCP Discover transmitted
DEBUG: DHCP Offer received
IP Address: 10.0.2.15/24
Gateway: 10.0.2.2
DNS: 10.0.2.3

## IP

The `ip` command displays information about your IP address:

> ip
Link: 52-54-00-12-34-56
Addr: 10.0.2.15/24
RX: 14 packets (4112 bytes)
TX: 16 packets (1589 bytes)

It can also be used to set your IP address:

> ip set 10.0.2.15/24

## ROUTE

The `route` command displays the IP routing table:

> route
Destination Gateway
0.0.0.0/0 10.0.2.2

NOTE: It will later allow you to manipulate it.
ip: 10.0.2.15/24
gw: 10.0.2.2
dns: 10.0.2.3

## HOST

Expand Down
66 changes: 60 additions & 6 deletions doc/syscalls.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,73 @@
# MOROS Syscalls

## Sleep
## EXIT (0x1)

```rust
pub fn sleep(seconds: f64) { ... }
pub fn exit(code: usize) -> usize
```

## Uptime
## SPAWN (0x2)

```rust
pub fn uptime() -> f64 { ... }
pub fn spawn(path: &str) -> isize
```

## Realtime
## READ (0x3)

```rust
pub fn realtime() -> f64 { ... }
pub fn read(handle: usize, buf: &mut [u8]) -> isize
```

## WRITE (0x4)

```rust
pub fn write(handle: usize, buf: &mut [u8]) -> isize
```

## OPEN (0x5)

```rust
pub fn open(path: &str, flags: usize) -> isize
```

## CLOSE (0x6)

```rust
pub fn close(handle: usize)
```

## INFO (0x7)

```rust
pub fn info(path: &str, info: &mut FileInfo) -> isize
```

## DUP (0x8)

```rust
pub fn dup(old_handle: usize, new_handle: usize) -> isize
```

## SLEEP (0x9)

```rust
pub fn sleep(seconds: f64)
```

## UPTIME (0xA)

```rust
pub fn uptime() -> f64
```

## REALTIME (0xB)

```rust
pub fn realtime() -> f64
```

## DELETE (0xC)

```rust
pub fn delete(path: &str) -> isize
```
9 changes: 9 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ macro_rules! eprintln {
});
}

#[macro_export]
macro_rules! error {
($($arg:tt)*) => ({
let csi_color = $crate::api::console::Style::color("LightRed");
let csi_reset = $crate::api::console::Style::reset();
eprintln!("{}Error:{} {}", csi_color, csi_reset, format_args!($($arg)*));
});
}

pub mod console;
pub mod font;
pub mod fs;
Expand Down
2 changes: 1 addition & 1 deletion src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ macro_rules! printk {
#[macro_export]
macro_rules! debug {
($($arg:tt)*) => ({
let csi_color = $crate::api::console::Style::color("Yellow");
let csi_color = $crate::api::console::Style::color("LightBlue");
let csi_reset = $crate::api::console::Style::reset();
$crate::sys::console::print_fmt(format_args!("{}DEBUG: ", csi_color));
$crate::sys::console::print_fmt(format_args!($($arg)*));
Expand Down
8 changes: 4 additions & 4 deletions src/usr/beep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ pub fn main(args: &[&str]) -> usr::shell::ExitCode {
if let Ok(value) = args[i + 1].parse() {
freq = value;
} else {
eprintln!("Could not parse freq");
error!("Could not parse freq");
return usr::shell::ExitCode::CommandError;
}
i += 1;
} else {
eprintln!("Missing freq");
error!("Missing freq");
return usr::shell::ExitCode::CommandError;
}
},
Expand All @@ -60,12 +60,12 @@ pub fn main(args: &[&str]) -> usr::shell::ExitCode {
if let Ok(value) = args[i + 1].parse() {
len = value;
} else {
eprintln!("Could not parse len");
error!("Could not parse len");
return usr::shell::ExitCode::CommandError;
}
i += 1;
} else {
eprintln!("Missing len");
error!("Missing len");
return usr::shell::ExitCode::CommandError;
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/usr/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub fn main(args: &[&str]) -> usr::shell::ExitCode {
usr::shell::ExitCode::CommandSuccessful
}
Err(msg) => {
eprintln!("{}", msg);
error!("{}", msg);
usr::shell::ExitCode::CommandError
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/usr/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ pub fn main(args: &[&str]) -> usr::shell::ExitCode {
if fs::write(dest, &contents).is_ok() {
usr::shell::ExitCode::CommandSuccessful
} else {
eprintln!("Could not write to '{}'", dest);
error!("Could not write to '{}'", dest);
usr::shell::ExitCode::CommandError
}
} else {
eprintln!("File not found '{}'", source);
error!("File not found '{}'", source);
usr::shell::ExitCode::CommandError
}
}
2 changes: 1 addition & 1 deletion src/usr/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn main(args: &[&str]) -> usr::shell::ExitCode {
usr::shell::ExitCode::CommandSuccessful
}
Err(e) => {
eprintln!("Error: {}", e);
error!("{}", e);
usr::shell::ExitCode::CommandError
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/usr/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ pub fn main(args: &[&str]) -> usr::shell::ExitCode {
}

if !fs::exists(pathname) {
eprintln!("File not found '{}'", pathname);
error!("File not found '{}'", pathname);
return usr::shell::ExitCode::CommandError;
}

if let Some(info) = syscall::info(pathname) {
if info.is_dir() && info.size() > 0 {
eprintln!("Directory '{}' not empty", pathname);
error!("Directory '{}' not empty", pathname);
return usr::shell::ExitCode::CommandError;
}
}

if fs::delete(pathname).is_ok() {
usr::shell::ExitCode::CommandSuccessful
} else {
eprintln!("Could not delete file '{}'", pathname);
error!("Could not delete file '{}'", pathname);
usr::shell::ExitCode::CommandError
}
}
84 changes: 44 additions & 40 deletions src/usr/dhcp.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
use crate::{sys, usr, debug};
use crate::api::console::Style;
use crate::api::syscall;
use crate::alloc::string::ToString;
use alloc::string::ToString;
use alloc::vec::Vec;
use smoltcp::socket::{Dhcpv4Event, Dhcpv4Socket};
use smoltcp::time::Instant;
use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr};

pub fn main(_args: &[&str]) -> usr::shell::ExitCode {
let csi_color = Style::color("LightCyan");
let csi_reset = Style::reset();
pub fn main(args: &[&str]) -> usr::shell::ExitCode {
let mut verbose = false;
let dhcp_config;

// TODO: Add `--verbose` option
for arg in args {
match *arg {
"-v" | "--verbose" => {
verbose = true;
}
_ => {}
}
}

if let Some(ref mut iface) = *sys::net::IFACE.lock() {
let dhcp_socket = Dhcpv4Socket::new();
let dhcp_handle = iface.add_socket(dhcp_socket);

let previous_address = match iface.ip_addrs().first() {
Some(IpCidr::Ipv4(ip_addr)) => *ip_addr,
_ => Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0),
};

debug!("DHCP Discover transmitted");
if verbose {
debug!("DHCP Discover transmitted");
}
let timeout = 30.0;
let started = syscall::realtime();
loop {
if syscall::realtime() - started > timeout {
eprintln!("Timeout reached");
error!("Timeout reached");
iface.remove_socket(dhcp_handle);
return usr::shell::ExitCode::CommandError;
}
Expand All @@ -39,39 +40,19 @@ pub fn main(_args: &[&str]) -> usr::shell::ExitCode {

let timestamp = Instant::from_micros((syscall::realtime() * 1000000.0) as i64);
if let Err(e) = iface.poll(timestamp) {
eprintln!("Network Error: {}", e);
error!("Network Error: {}", e);
}

let event = iface.get_socket::<Dhcpv4Socket>(dhcp_handle).poll();
match event {
None => {}
Some(Dhcpv4Event::Configured(config)) => {
debug!("DHCP Offer received");
if config.address != previous_address {
iface.update_ip_addrs(|addrs| {
if let Some(addr) = addrs.iter_mut().next() {
*addr = IpCidr::Ipv4(config.address);
}
});
println!("{}IP Address:{} {}", csi_color, csi_reset, config.address);
}

if let Some(router) = config.router {
println!("{}Gateway:{} {}", csi_color, csi_reset, router);
iface.routes_mut().add_default_ipv4_route(router).unwrap();
} else {
println!("{}Gateway:{} none", csi_color, csi_reset);
iface.routes_mut().remove_default_ipv4_route();
dhcp_config = Some(config);
if verbose {
debug!("DHCP Offer received");
}

// TODO: save DNS servers in `/ini/dns` and use them with `host` command
let dns_servers: Vec<_> = config.dns_servers.iter().filter_map(|s| *s).map(|s| s.to_string()).collect();
if !dns_servers.is_empty() {
println!("{}DNS:{} {}", csi_color, csi_reset, dns_servers.join(", "));
}

iface.remove_socket(dhcp_handle);
return usr::shell::ExitCode::CommandSuccessful;
break;
}
Some(Dhcpv4Event::Deconfigured) => {
}
Expand All @@ -81,6 +62,29 @@ pub fn main(_args: &[&str]) -> usr::shell::ExitCode {
syscall::sleep((wait_duration.total_micros() as f64) / 1000000.0);
}
}
} else {
error!("Network Error");
return usr::shell::ExitCode::CommandError;
}

if let Some(config) = dhcp_config {
usr::net::main(&["net", "config", "ip", &config.address.to_string()]);
usr::net::main(&["net", "config", "ip"]);

if let Some(router) = config.router {
//iface.routes_mut().add_default_ipv4_route(router).unwrap();
usr::net::main(&["net", "config", "gw", &router.to_string()]);
} else {
//iface.routes_mut().remove_default_ipv4_route();
usr::net::main(&["net", "config", "gw", ""]);
}
usr::net::main(&["net", "config", "gw"]);

let dns: Vec<_> = config.dns_servers.iter().filter_map(|s| *s).map(|s| s.to_string()).collect();
usr::net::main(&["net", "config", "dns", &dns.join(",")]);
usr::net::main(&["net", "config", "dns"]);

return usr::shell::ExitCode::CommandSuccessful;
}

usr::shell::ExitCode::CommandError
Expand Down
Loading