-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add net-gw device * Add net-ip device * Add net-mac device * Add net-usage device * Use filesystem instead of net command * Add deprecation warnings
- Loading branch information
Showing
11 changed files
with
293 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::api::fs::{FileIO, IO}; | ||
|
||
use alloc::string::{String, ToString}; | ||
use core::str::FromStr; | ||
use smoltcp::wire::Ipv4Address; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NetGw; | ||
|
||
impl NetGw { | ||
pub fn new() -> Self { | ||
Self | ||
} | ||
|
||
pub fn size() -> usize { | ||
16 | ||
} | ||
} | ||
|
||
impl FileIO for NetGw { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> { | ||
if let Some((ref mut iface, _)) = *super::NET.lock() { | ||
let mut n = 0; | ||
iface.routes_mut().update(|storage| { | ||
if let Some(route) = storage.iter().next() { | ||
let s = route.via_router.to_string(); | ||
n = s.len(); | ||
buf[0..n].copy_from_slice(s.as_bytes()); | ||
} | ||
}); | ||
if n > 0 { | ||
return Ok(n); | ||
} | ||
} | ||
Err(()) | ||
} | ||
|
||
fn write(&mut self, buf: &[u8]) -> Result<usize, ()> { | ||
if let Some((ref mut iface, _)) = *super::NET.lock() { | ||
if let Ok(s) = String::from_utf8(buf.to_vec()) { | ||
if s == "0.0.0.0" { | ||
iface.routes_mut().remove_default_ipv4_route(); | ||
return Ok(s.len()); | ||
} else if let Ok(ip) = Ipv4Address::from_str(&s) { | ||
iface.routes_mut().add_default_ipv4_route(ip).unwrap(); | ||
log!("NET GW {}", s); | ||
return Ok(s.len()); | ||
} | ||
} | ||
} | ||
Err(()) | ||
} | ||
|
||
fn close(&mut self) {} | ||
|
||
fn poll(&mut self, event: IO) -> bool { | ||
match event { | ||
IO::Read => true, | ||
IO::Write => true, | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use crate::api::fs::{FileIO, IO}; | ||
|
||
use alloc::format; | ||
use alloc::string::String; | ||
use core::str::FromStr; | ||
use smoltcp::wire::IpCidr; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NetIp; | ||
|
||
impl NetIp { | ||
pub fn new() -> Self { | ||
Self | ||
} | ||
|
||
pub fn size() -> usize { | ||
16 + 1 + 3 | ||
} | ||
} | ||
|
||
impl FileIO for NetIp { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> { | ||
if let Some((ref mut iface, _)) = *super::NET.lock() { | ||
if let Some(ip) = iface.ip_addrs().iter().next() { | ||
let s = format!("{}/{}", ip.address(), ip.prefix_len()); | ||
let n = s.len(); | ||
buf[0..n].copy_from_slice(s.as_bytes()); | ||
return Ok(n); | ||
} | ||
} | ||
Err(()) | ||
} | ||
|
||
fn write(&mut self, buf: &[u8]) -> Result<usize, ()> { | ||
if let Ok(s) = String::from_utf8(buf.to_vec()) { | ||
if let Ok(addr) = IpCidr::from_str(&s) { | ||
if let Some((ref mut iface, _)) = *super::NET.lock() { | ||
iface.update_ip_addrs(|addrs| { | ||
addrs.clear(); | ||
addrs.push(addr).unwrap(); | ||
log!("NET IP {}", s); | ||
}); | ||
return Ok(buf.len()); | ||
} | ||
} | ||
} | ||
Err(()) | ||
} | ||
|
||
fn close(&mut self) {} | ||
|
||
fn poll(&mut self, event: IO) -> bool { | ||
match event { | ||
IO::Read => true, | ||
IO::Write => true, | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::api::fs::{FileIO, IO}; | ||
|
||
use alloc::string::ToString; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NetMac; | ||
|
||
impl NetMac { | ||
pub fn new() -> Self { | ||
Self | ||
} | ||
|
||
pub fn size() -> usize { | ||
17 | ||
} | ||
} | ||
|
||
impl FileIO for NetMac { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> { | ||
if let Some((ref mut iface, _)) = *super::NET.lock() { | ||
let s = iface.hardware_addr().to_string(); | ||
let n = s.len(); | ||
buf[0..n].copy_from_slice(s.as_bytes()); | ||
return Ok(n); | ||
} | ||
Err(()) | ||
} | ||
|
||
fn write(&mut self, _buf: &[u8]) -> Result<usize, ()> { | ||
Err(()) | ||
} | ||
|
||
fn close(&mut self) {} | ||
|
||
fn poll(&mut self, event: IO) -> bool { | ||
match event { | ||
IO::Read => true, | ||
IO::Write => false, | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
mod nic; | ||
pub mod gw; | ||
pub mod ip; | ||
pub mod mac; | ||
pub mod usage; | ||
pub mod socket; | ||
|
||
use crate::{sys, usr}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::api::fs::{FileIO, IO}; | ||
use crate::sys::net::EthernetDeviceIO; | ||
|
||
use alloc::format; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NetUsage; | ||
|
||
impl NetUsage { | ||
pub fn new() -> Self { | ||
Self | ||
} | ||
|
||
pub fn size() -> usize { | ||
83 | ||
} | ||
} | ||
|
||
impl FileIO for NetUsage { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> { | ||
if let Some((_, ref mut device)) = *super::NET.lock() { | ||
let stats = device.stats(); | ||
let s = format!( | ||
"{} {} {} {}", | ||
stats.rx_packets_count(), | ||
stats.rx_bytes_count(), | ||
stats.tx_packets_count(), | ||
stats.tx_bytes_count(), | ||
); | ||
let n = s.len(); | ||
buf[0..n].copy_from_slice(s.as_bytes()); | ||
return Ok(n); | ||
} | ||
Err(()) | ||
} | ||
|
||
fn write(&mut self, _buf: &[u8]) -> Result<usize, ()> { | ||
Err(()) | ||
} | ||
|
||
fn close(&mut self) {} | ||
|
||
fn poll(&mut self, event: IO) -> bool { | ||
match event { | ||
IO::Read => true, | ||
IO::Write => false, | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.