mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-21 09:14:34 +00:00
add formatter for net crate ip address
This commit is contained in:
parent
643e9a7d62
commit
1a77f0afae
1 changed files with 32 additions and 23 deletions
|
|
@ -5,6 +5,9 @@ use rkyv::{Archive, Deserialize, Serialize};
|
||||||
use std::net::{SocketAddr, IpAddr};
|
use std::net::{SocketAddr, IpAddr};
|
||||||
use smoltcp::wire::IpAddress;
|
use smoltcp::wire::IpAddress;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::Debug;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
pub(crate) const SERVER_NAME_NET: &str = "_Middleware Network Server_";
|
pub(crate) const SERVER_NAME_NET: &str = "_Middleware Network Server_";
|
||||||
|
|
||||||
|
|
@ -88,7 +91,7 @@ impl From<SocketAddr> for NetSocketAddr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)]
|
#[derive(Archive, Serialize, Deserialize, Copy, Clone)]
|
||||||
pub enum NetIpAddr {
|
pub enum NetIpAddr {
|
||||||
Ipv4([u8; 4]),
|
Ipv4([u8; 4]),
|
||||||
Ipv6([u8; 16]),
|
Ipv6([u8; 16]),
|
||||||
|
|
@ -154,32 +157,38 @@ impl From<NetIpAddr> for IpAddress {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* // can't quite figure this one out. oh well.
|
|
||||||
impl TryFrom<dyn ToSocketAddrs> for IpAddress {
|
impl fmt::Display for NetIpAddr {
|
||||||
fn try_from(socket: dyn ToSocketAddrs) -> Result<IpAddress, xous::Error> {
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match socket.to_socket_addrs() {
|
match self {
|
||||||
Ok(socks) => {
|
NetIpAddr::Ipv4(octets) => {
|
||||||
match socks.into_iter().next() {
|
// Fast Path: if there's no alignment stuff, write directly to the buffer
|
||||||
Some(socket_addr) => {
|
if fmt.precision().is_none() && fmt.width().is_none() {
|
||||||
Ok(
|
write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
|
||||||
match socket_addr {
|
} else {
|
||||||
std::net::SocketAddr::V4(v4addr) => {
|
const IPV4_BUF_LEN: usize = 15; // Long enough for the longest possible IPv4 address
|
||||||
IpAddress::Ipv4(v4addr.ip())
|
let mut buf = [0u8; IPV4_BUF_LEN];
|
||||||
|
let mut buf_slice = &mut buf[..];
|
||||||
|
|
||||||
|
// Note: The call to write should never fail, hence the unwrap
|
||||||
|
write!(buf_slice, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();
|
||||||
|
let len = IPV4_BUF_LEN - buf_slice.len();
|
||||||
|
|
||||||
|
// This unsafe is OK because we know what is being written to the buffer
|
||||||
|
let buf = unsafe { std::str::from_utf8_unchecked(&buf[..len]) };
|
||||||
|
fmt.pad(buf)
|
||||||
}
|
}
|
||||||
std::net::SocketAddr::V6(v6addr) => {
|
},
|
||||||
IpAddress::Ipv6(v6addr.ip())
|
NetIpAddr::Ipv6(ip) => ip.fmt(fmt),
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
_ => Err(xous::Error::InvalidString)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => Err(xous::Error::InvalidString)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
impl fmt::Debug for NetIpAddr {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
fmt::Display::fmt(self, fmt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// This defines a Xous Scalar message endpoint. Used for defining
|
/// This defines a Xous Scalar message endpoint. Used for defining
|
||||||
/// notification messages for incoming packets, for main loops that want to be
|
/// notification messages for incoming packets, for main loops that want to be
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue