Commit 7ddadca8 authored by Jeremy Soller's avatar Jeremy Soller

Merge branch 'master' into 'master'

Update smoltcp

Closes #30

See merge request !30
parents 40a8cb7d f9d6f5d8
This diff is collapsed.
......@@ -28,7 +28,7 @@ features = ["release_max_level_off"]
[dependencies.smoltcp]
git = "https://github.com/m-labs/smoltcp.git"
rev = "c418b60b5db93753999ae51d33ec4dc1d1631c69"
rev = "d23aee483f2dc78e8070d2ad53e400cd0dcb9ae1"
default-features = false
features = ["std", "socket-raw", "proto-ipv4", "socket-udp", "socket-tcp", "socket-icmp"]
......
use smoltcp::socket::{IcmpEndpoint, IcmpPacketBuffer, IcmpSocket, IcmpSocketBuffer, SocketHandle};
use smoltcp::socket::{IcmpEndpoint, IcmpPacketMetadata, IcmpSocket, IcmpSocketBuffer, SocketHandle};
use smoltcp::wire::{Icmpv4Packet, Icmpv4Repr, IpAddress, IpEndpoint};
use std::mem;
use std::str;
......@@ -86,16 +86,15 @@ impl<'a, 'b> SchemeSocket for IcmpSocket<'a, 'b> {
let ip =
IpAddress::from_str(addr).map_err(|_| syscall::Error::new(syscall::EINVAL))?;
let mut rx_packets = Vec::with_capacity(Smolnetd::SOCKET_BUFFER_SIZE);
let mut tx_packets = Vec::with_capacity(Smolnetd::SOCKET_BUFFER_SIZE);
for _ in 0..Smolnetd::SOCKET_BUFFER_SIZE {
rx_packets.push(IcmpPacketBuffer::new(vec![0; NetworkDevice::MTU]));
tx_packets.push(IcmpPacketBuffer::new(vec![0; NetworkDevice::MTU]));
}
let socket = IcmpSocket::new(
IcmpSocketBuffer::new(rx_packets),
IcmpSocketBuffer::new(tx_packets),
IcmpSocketBuffer::new(
vec![IcmpPacketMetadata::EMPTY; Smolnetd::SOCKET_BUFFER_SIZE],
vec![0; NetworkDevice::MTU * Smolnetd::SOCKET_BUFFER_SIZE]
),
IcmpSocketBuffer::new(
vec![IcmpPacketMetadata::EMPTY; Smolnetd::SOCKET_BUFFER_SIZE],
vec![0; NetworkDevice::MTU * Smolnetd::SOCKET_BUFFER_SIZE]
)
);
let handle = socket_set.add(socket);
let mut icmp_socket = socket_set.get::<IcmpSocket>(handle);
......@@ -119,15 +118,15 @@ impl<'a, 'b> SchemeSocket for IcmpSocket<'a, 'b> {
let ip =
IpAddress::from_str(addr).map_err(|_| syscall::Error::new(syscall::EINVAL))?;
let mut rx_packets = Vec::with_capacity(Smolnetd::SOCKET_BUFFER_SIZE);
let mut tx_packets = Vec::with_capacity(Smolnetd::SOCKET_BUFFER_SIZE);
for _ in 0..Smolnetd::SOCKET_BUFFER_SIZE {
rx_packets.push(IcmpPacketBuffer::new(vec![0; NetworkDevice::MTU]));
}
let socket = IcmpSocket::new(
IcmpSocketBuffer::new(rx_packets),
IcmpSocketBuffer::new(tx_packets),
IcmpSocketBuffer::new(
vec![IcmpPacketMetadata::EMPTY; Smolnetd::SOCKET_BUFFER_SIZE],
vec![0; NetworkDevice::MTU * Smolnetd::SOCKET_BUFFER_SIZE]
),
IcmpSocketBuffer::new(
vec![IcmpPacketMetadata::EMPTY; Smolnetd::SOCKET_BUFFER_SIZE],
vec![0; NetworkDevice::MTU * Smolnetd::SOCKET_BUFFER_SIZE]
)
);
let handle = socket_set.add(socket);
let mut icmp_socket = socket_set.get::<IcmpSocket>(handle);
......
use smoltcp::socket::{RawPacketBuffer, RawSocket, RawSocketBuffer, SocketHandle};
use smoltcp::socket::{RawPacketMetadata, RawSocket, RawSocketBuffer, SocketHandle};
use smoltcp::wire::{IpProtocol, IpVersion};
use std::str;
use syscall::{Error as SyscallError, Result as SyscallResult};
......@@ -61,14 +61,14 @@ impl<'a, 'b> SchemeSocket for RawSocket<'a, 'b> {
let proto =
u8::from_str_radix(path, 16).or_else(|_| Err(SyscallError::new(syscall::ENOENT)))?;
let mut rx_packets = Vec::with_capacity(Smolnetd::SOCKET_BUFFER_SIZE);
let mut tx_packets = Vec::with_capacity(Smolnetd::SOCKET_BUFFER_SIZE);
for _ in 0..Smolnetd::SOCKET_BUFFER_SIZE {
rx_packets.push(RawPacketBuffer::new(vec![0; NetworkDevice::MTU]));
tx_packets.push(RawPacketBuffer::new(vec![0; NetworkDevice::MTU]));
}
let rx_buffer = RawSocketBuffer::new(rx_packets);
let tx_buffer = RawSocketBuffer::new(tx_packets);
let rx_buffer = RawSocketBuffer::new(
vec![RawPacketMetadata::EMPTY; Smolnetd::SOCKET_BUFFER_SIZE],
vec![0; NetworkDevice::MTU * Smolnetd::SOCKET_BUFFER_SIZE]
);
let tx_buffer = RawSocketBuffer::new(
vec![RawPacketMetadata::EMPTY; Smolnetd::SOCKET_BUFFER_SIZE],
vec![0; NetworkDevice::MTU * Smolnetd::SOCKET_BUFFER_SIZE]
);
let ip_socket = RawSocket::new(
IpVersion::Ipv4,
IpProtocol::from(proto),
......
use netutils::getcfg;
use smoltcp;
use smoltcp::iface::{EthernetInterface, EthernetInterfaceBuilder, NeighborCache};
use smoltcp::iface::{EthernetInterface, EthernetInterfaceBuilder, NeighborCache, Routes};
use smoltcp::socket::SocketSet as SmoltcpSocketSet;
use smoltcp::time::{Duration, Instant};
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, IpEndpoint, Ipv4Address};
......@@ -31,7 +31,7 @@ mod icmp;
mod netcfg;
type SocketSet = SmoltcpSocketSet<'static, 'static, 'static>;
type Interface = Rc<RefCell<EthernetInterface<'static, 'static, NetworkDevice>>>;
type Interface = Rc<RefCell<EthernetInterface<'static, 'static, 'static, NetworkDevice>>>;
const MAX_DURATION: Duration = Duration { millis: ::std::u64::MAX };
const MIN_DURATION: Duration = Duration { millis: 0 };
......@@ -89,11 +89,13 @@ impl Smolnetd {
hardware_addr,
Rc::clone(&buffer_pool),
);
let mut routes = Routes::new(BTreeMap::new());
routes.add_default_ipv4_route(default_gw).expect("Failed to add default gateway");
let iface = EthernetInterfaceBuilder::new(network_device)
.neighbor_cache(NeighborCache::new(BTreeMap::new()))
.ethernet_addr(hardware_addr)
.ip_addrs(protocol_addrs)
.ipv4_gateway(default_gw)
.routes(routes)
.finalize();
let iface = Rc::new(RefCell::new(iface));
let socket_set = Rc::new(RefCell::new(SocketSet::new(vec![])));
......
......@@ -2,7 +2,7 @@
mod nodes;
mod notifier;
use smoltcp::wire::{EthernetAddress, IpCidr, Ipv4Address};
use smoltcp::wire::{IpAddress, EthernetAddress, IpCidr, Ipv4Address};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::fs::File;
......@@ -23,6 +23,11 @@ use super::{post_fevent, Interface};
const WRITE_BUFFER_MAX_SIZE: usize = 0xffff;
fn gateway_cidr() -> IpCidr {
// TODO: const fn
IpCidr::new(IpAddress::v4(0, 0, 0, 0), 0)
}
fn parse_default_gw(value: &str) -> SyscallResult<Ipv4Address> {
let mut routes = value.lines();
if let Some(route) = routes.next() {
......@@ -75,7 +80,11 @@ fn mk_root_node(iface: Interface, notifier: NotifierRef, dns_config: DNSConfigRe
"route" => {
"list" => {
ro [iface] || {
if let Some(ip) = iface.borrow().ipv4_gateway() {
let mut gateway = None;
iface.borrow_mut().routes_mut().update(|map| {
gateway = map.get(&gateway_cidr()).map(|route| route.via_router);
});
if let Some(ip) = gateway {
format!("default via {}\n", ip)
} else {
String::new()
......@@ -98,7 +107,9 @@ fn mk_root_node(iface: Interface, notifier: NotifierRef, dns_config: DNSConfigRe
}
|cur_value| {
if let Some(default_gw) = *cur_value {
iface.borrow_mut().set_ipv4_gateway(Some(default_gw));
if iface.borrow_mut().routes_mut().add_default_ipv4_route(default_gw).is_err() {
return Err(SyscallError::new(syscall::EINVAL));
}
notifier.borrow_mut().schedule_notify("route/list");
Ok(())
} else {
......@@ -123,10 +134,16 @@ fn mk_root_node(iface: Interface, notifier: NotifierRef, dns_config: DNSConfigRe
|cur_value| {
if let Some(default_gw) = *cur_value {
let mut iface = iface.borrow_mut();
if iface.ipv4_gateway() != Some(default_gw) {
let mut gateway = None;
iface.routes_mut().update(|map| {
gateway = map.get(&gateway_cidr()).map(|route| route.via_router);
});
if gateway != Some(IpAddress::Ipv4(default_gw)) {
return Err(SyscallError::new(syscall::EINVAL));
}
iface.set_ipv4_gateway(None);
iface.routes_mut().update(|map| {
map.remove(&gateway_cidr());
});
notifier.borrow_mut().schedule_notify("route/list");
Ok(())
} else {
......
use smoltcp::socket::{SocketHandle, UdpPacketBuffer, UdpSocket, UdpSocketBuffer};
use smoltcp::socket::{SocketHandle, UdpPacketMetadata, UdpSocket, UdpSocketBuffer};
use smoltcp::wire::IpEndpoint;
use std::str;
use syscall::{Error as SyscallError, Result as SyscallResult};
......@@ -66,14 +66,14 @@ impl<'a, 'b> SchemeSocket for UdpSocket<'a, 'b> {
return Err(SyscallError::new(syscall::EACCES));
}
let mut rx_packets = Vec::with_capacity(Smolnetd::SOCKET_BUFFER_SIZE);
let mut tx_packets = Vec::with_capacity(Smolnetd::SOCKET_BUFFER_SIZE);
for _ in 0..Smolnetd::SOCKET_BUFFER_SIZE {
rx_packets.push(UdpPacketBuffer::new(vec![0; NetworkDevice::MTU]));
tx_packets.push(UdpPacketBuffer::new(vec![0; NetworkDevice::MTU]));
}
let rx_buffer = UdpSocketBuffer::new(rx_packets);
let tx_buffer = UdpSocketBuffer::new(tx_packets);
let rx_buffer = UdpSocketBuffer::new(
vec![UdpPacketMetadata::EMPTY; Smolnetd::SOCKET_BUFFER_SIZE],
vec![0; NetworkDevice::MTU * Smolnetd::SOCKET_BUFFER_SIZE]
);
let tx_buffer = UdpSocketBuffer::new(
vec![UdpPacketMetadata::EMPTY; Smolnetd::SOCKET_BUFFER_SIZE],
vec![0; NetworkDevice::MTU * Smolnetd::SOCKET_BUFFER_SIZE]
);
let udp_socket = UdpSocket::new(rx_buffer, tx_buffer);
if local_endpoint.port == 0 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment