Skip to content

Commit 6ff420f

Browse files
committed
syscalls - Deduplication for UDP/raw... at least the start
1 parent 4a4ed0e commit 6ff420f

File tree

4 files changed

+120
-49
lines changed

4 files changed

+120
-49
lines changed

Kernel/Modules/syscalls/network_calls.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ unsafe impl crate::args::Pod for crate::values::NetworkInterface {}
1111
unsafe impl crate::args::Pod for crate::values::NetworkRoute {}
1212
unsafe impl crate::args::Pod for crate::values::NetworkAddress {}
1313

14+
mod traits;
1415
mod raw;
1516
mod tcp;
17+
//mod udp;
1618

1719
pub fn init_handles() {
1820
crate::objects::push_as_unclaimed("NetMgmt", crate::objects::new_object(InterfaceManagement));
@@ -30,6 +32,15 @@ fn from_ipv4(a: ::network::ipv4::Address) -> [u8; 16] {
3032
]
3133
}
3234

35+
fn addr_from_socket(sa: &SocketAddress) -> Result<::network::Address,super::Error> {
36+
match SocketAddressType::try_from(sa.addr_ty)
37+
{
38+
Err(_) => return Err(super::Error::BadValue),
39+
Ok(SocketAddressType::Ipv4) => Ok(::network::Address::Ipv4(make_ipv4(&sa.addr))),
40+
_ => todo!("Other address socket types - #{}", sa.addr_ty),
41+
}
42+
}
43+
3344
/// Open a connection-based listen server
3445
pub fn new_server(local_address: SocketAddress) -> Result<u64, super::Error>
3546
{
@@ -40,12 +51,7 @@ pub fn new_server(local_address: SocketAddress) -> Result<u64, super::Error>
4051
t => todo!("Socket type: {:?}", t),
4152
}
4253
}
43-
let addr = match SocketAddressType::try_from(local_address.addr_ty)
44-
{
45-
Err(_) => return Err(super::Error::BadValue),
46-
Ok(SocketAddressType::Ipv4) => ::network::Address::Ipv4(make_ipv4(&local_address.addr)),
47-
_ => todo!(""),
48-
};
54+
let addr = addr_from_socket(&local_address)?;
4955
let port_ty = crate::values::SocketPortType::try_from(local_address.port_ty).map_err(|_| super::Error::BadValue)?;
5056
let o = inner(addr, port_ty, local_address.port);
5157
Ok(super::from_result::<_,::syscall_values::SocketError>(o))
@@ -96,13 +102,19 @@ pub fn new_free_socket(local_address: SocketAddress, remote_mask: crate::values:
96102
Err(crate::values::SocketError::InvalidValue)
97103
}
98104
else {
99-
Ok(crate::objects::new_object(raw::RawIpv4::new(source, local_address.port as u8)?))
105+
Ok(crate::objects::new_object(traits::FreeSocketWrapper(
106+
raw::RawIpv4::new(source, local_address.port as u8)?
107+
)))
100108
}
101109
},
102110
_ => todo!("Handle other address types"),
103111
},
104112
SocketPortType::Tcp => Err(crate::values::SocketError::InvalidValue),
105-
SocketPortType::Udp => todo!("Handle other socket types"),
113+
//SocketPortType::Udp => Ok(crate::objects::new_object(traits::FreeSocketWrapper({
114+
// let source = addr_from_socket(&remote_mask);
115+
// udp::Udp::new(source, local_address.port)
116+
//}))),
117+
SocketPortType::Udp => todo!("UDP sockets"),
106118
_ => todo!("Handle other socket types"),
107119
}
108120
}

Kernel/Modules/syscalls/network_calls/raw.rs

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
//
44
// Core/syscalls/network_calls/raw.rs
55
//! Userland interface to the network stack - Raw Sockets
6-
use crate::args::Args;
7-
use kernel::memory::freeze::{Freeze,FreezeMut};
86
use ::syscall_values::{SocketAddress,SocketAddressType};
97

108
pub struct RawIpv4
@@ -23,47 +21,21 @@ impl RawIpv4
2321
})
2422
}
2523
}
26-
impl crate::objects::Object for RawIpv4
24+
impl super::traits::FreeSocket for RawIpv4
2725
{
28-
fn class(&self) -> u16 { crate::values::CLASS_FREESOCKET }
29-
fn as_any(&self) -> &dyn core::any::Any { self }
30-
fn try_clone(&self) -> Option<u32> {
31-
None
32-
//Some( ::objects::new_object(self.clone()) )
33-
}
34-
fn handle_syscall_ref(&self, call: u16, args: &mut Args) -> Result<u64, crate::Error> {
35-
match call
36-
{
37-
crate::values::NET_FREESOCK_SENDTO => {
38-
let data: Freeze<[u8]> = args.get()?;
39-
let addr: Freeze<SocketAddress> = args.get()?;
40-
if addr.addr_ty != SocketAddressType::Ipv4 as u8 {
41-
return Err(crate::Error::BadValue);
42-
}
43-
let dest = super::make_ipv4(&addr.addr);
44-
::kernel::futures::block_on(
45-
::network::ipv4::send_packet(self.source, dest, self.proto, ::network::nic::SparsePacket::new_root(&data))
46-
);
47-
Ok(0)
48-
},
49-
crate::values::NET_FREESOCK_RECVFROM => {
50-
let data: FreezeMut<[u8]> = args.get()?;
51-
let mut addr: FreezeMut<SocketAddress> = args.get()?;
52-
addr.addr_ty = SocketAddressType::Ipv4 as u8;
53-
todo!("NET_FREESOCK_RECV({:p}, {:p})", &*data, &*addr);
54-
},
55-
_ => crate::objects::object_has_no_such_method_ref("network_calls::FreeSocket", call),
26+
fn send_to(&self, data: &[u8], addr: &SocketAddress) -> Result<u64, crate::Error> {
27+
if addr.addr_ty != SocketAddressType::Ipv4 as u8 {
28+
return Err(crate::Error::BadValue);
5629
}
30+
let dest = super::make_ipv4(&addr.addr);
31+
::kernel::futures::block_on(
32+
::network::ipv4::send_packet(self.source, dest, self.proto, ::network::nic::SparsePacket::new_root(&data))
33+
);
34+
Ok(0)
5735
}
58-
fn handle_syscall_val(&mut self, call: u16, _args: &mut Args) -> Result<u64, crate::Error> {
59-
// SAFE: Valid pointer which is forgotten after call
60-
let _ = unsafe { ::core::ptr::read(self) };
61-
crate::objects::object_has_no_such_method_val("network_calls::FreeSocket", call)
62-
}
63-
fn bind_wait(&self, _flags: u32, _obj: &mut ::kernel::threads::SleepObject) -> u32 {
64-
0
65-
}
66-
fn clear_wait(&self, _flags: u32, _obj: &mut ::kernel::threads::SleepObject) -> u32 {
67-
0
36+
37+
fn recv_from(&self, data: &mut [u8], addr: &mut SocketAddress) -> Result<u64, crate::Error> {
38+
addr.addr_ty = SocketAddressType::Ipv4 as u8;
39+
todo!("NET_FREESOCK_RECV({:p}, {:p})", &*data, &*addr);
6840
}
6941
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
use crate::args::Args;
3+
use crate::values::SocketAddress;
4+
use kernel::memory::freeze::{Freeze,FreezeMut};
5+
6+
pub trait FreeSocket: 'static + Sized + Send + Sync
7+
{
8+
fn try_clone(&self) -> Option<Self> {
9+
None
10+
}
11+
fn send_to(&self, data: &[u8], addr: &SocketAddress) -> Result<u64, crate::Error>;
12+
fn recv_from(&self, data: &mut [u8], addr: &mut SocketAddress) -> Result<u64, crate::Error>;
13+
}
14+
pub struct FreeSocketWrapper<T>(pub T);
15+
impl<T> crate::objects::Object for FreeSocketWrapper<T>
16+
where
17+
T: FreeSocket
18+
{
19+
fn class(&self) -> u16 { crate::values::CLASS_FREESOCKET }
20+
fn as_any(&self) -> &dyn core::any::Any { self }
21+
fn try_clone(&self) -> Option<u32> {
22+
match self.0.try_clone() {
23+
Some(v) => Some(crate::objects::new_object(FreeSocketWrapper(v))),
24+
None => None,
25+
}
26+
}
27+
fn handle_syscall_ref(&self, call: u16, args: &mut Args) -> Result<u64, crate::Error> {
28+
match call
29+
{
30+
crate::values::NET_FREESOCK_SENDTO => {
31+
let data: Freeze<[u8]> = args.get()?;
32+
let addr: Freeze<SocketAddress> = args.get()?;
33+
self.0.send_to(&data, &addr)
34+
},
35+
crate::values::NET_FREESOCK_RECVFROM => {
36+
let mut data: FreezeMut<[u8]> = args.get()?;
37+
let mut addr: FreezeMut<SocketAddress> = args.get()?;
38+
self.0.recv_from(&mut data, &mut addr)
39+
},
40+
_ => crate::objects::object_has_no_such_method_ref("network_calls::FreeSocket", call),
41+
}
42+
}
43+
fn handle_syscall_val(&mut self, call: u16, _args: &mut Args) -> Result<u64, crate::Error> {
44+
// SAFE: Valid pointer which is forgotten after call
45+
let _ = unsafe { ::core::ptr::read(self) };
46+
crate::objects::object_has_no_such_method_val("network_calls::FreeSocket", call)
47+
}
48+
fn bind_wait(&self, _flags: u32, _obj: &mut ::kernel::threads::SleepObject) -> u32 {
49+
0
50+
}
51+
fn clear_wait(&self, _flags: u32, _obj: &mut ::kernel::threads::SleepObject) -> u32 {
52+
0
53+
}
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// "Tifflin" Kernel
2+
// - By John Hodge (thePowersGang)
3+
//
4+
// Core/syscalls/network_calls/udp.rs
5+
//! Userland interface to the network stack - UDP Sockets
6+
use crate::args::Args;
7+
use kernel::memory::freeze::{Freeze,FreezeMut};
8+
use ::syscall_values::{SocketAddress,SocketAddressType};
9+
10+
pub struct Udp
11+
{
12+
inner: ::network::udp::Socket,
13+
}
14+
impl Udp
15+
{
16+
pub(crate) fn new(source: ::network::ipv4::Address, port: u16) -> Result<Self, crate::values::SocketError>
17+
{
18+
Ok( RawIpv4 {
19+
source,
20+
proto,
21+
//handle: ::network::ipv4::listen_raw(source, make_ipv4(&remote_mask.addr), remote_mask.mask),
22+
})
23+
}
24+
}
25+
impl super::traits::FreeSocket for Udp {
26+
fn send_to(&self, data: &[u8], addr: &SocketAddress) -> Result<u64, crate::Error> {
27+
todo!()
28+
}
29+
30+
fn recv_from(&self, data: &mut [u8], addr: &mut SocketAddress) -> Result<u64, crate::Error> {
31+
todo!()
32+
}
33+
}

0 commit comments

Comments
 (0)