Skip to content

Commit 5b87170

Browse files
inet::{new, bind, connect, send}: implement
* implement inet::{new, bind, connect, send} * remove the in-kernel dhcp client * remove the dependency of libc in userland applications (ones inside `userland/`) * init: launch the DHCP client (`dhcpd`) Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent ab20d27 commit 5b87170

File tree

25 files changed

+527
-280
lines changed

25 files changed

+527
-280
lines changed

bootstrap.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
imports:
22
- file: bootstrap/xorg.yml
3+
- file: bootstrap/net.yml
34

45
general:
56
cargo:

bootstrap/net.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packages:
2+
- name: dhcpd
3+
source:
4+
subdir: 'bundled'
5+
git: 'https://github.com/Andy-Python-Programmer/dhcpd'
6+
branch: 'main'
7+
tools_required:
8+
- host-rust
9+
- host-gcc
10+
sources_required:
11+
- rust-patched-libs
12+
pkgs_required:
13+
- mlibc
14+
configure:
15+
- args: ['python3', '@SOURCE_ROOT@/tools/cargo-inject-patches.py', '@THIS_SOURCE_DIR@/Cargo.toml']
16+
build:
17+
- args:
18+
- 'cargo'
19+
- 'install'
20+
- '--locked'
21+
- '--target-dir'
22+
- '@THIS_BUILD_DIR@'
23+
- '--path'
24+
- '@THIS_SOURCE_DIR@'
25+
- '--root'
26+
- '@THIS_COLLECT_DIR@/usr'
27+
- '-j@PARALLELISM@'
28+
environ:
29+
RUSTFLAGS: '-Cforce-frame-pointers=yes -Clink-args=-no-pie'

patches/mlibc/mlibc.patch

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
From f4c7bb3f8556d4f50fe132109de2dfedfccac293 Mon Sep 17 00:00:00 2001
1+
From a5ec321f99ccfe66889e802626230a792a3d7140 Mon Sep 17 00:00:00 2001
22
From: Andy-Python-Programmer <[email protected]>
3-
Date: Mon, 20 Feb 2023 18:40:41 +1100
3+
Date: Sun, 26 Feb 2023 17:20:53 +1100
44
Subject: [PATCH] <xxx>
55

66
---
@@ -12,11 +12,11 @@ Subject: [PATCH] <xxx>
1212
options/rtdl/generic/linker.cpp | 2 +-
1313
sysdeps/aero/generic/aero.cpp | 77 +++++++----
1414
sysdeps/aero/generic/filesystem.cpp | 51 ++++++-
15-
sysdeps/aero/generic/sockets.cpp | 87 +++++++++++-
15+
sysdeps/aero/generic/sockets.cpp | 96 ++++++++++++-
1616
sysdeps/aero/generic/time.cpp | 24 ++++
17-
sysdeps/aero/include/aero/syscall.h | 10 ++
17+
sysdeps/aero/include/aero/syscall.h | 11 ++
1818
sysdeps/aero/meson.build | 1 +
19-
12 files changed, 402 insertions(+), 47 deletions(-)
19+
12 files changed, 412 insertions(+), 47 deletions(-)
2020
create mode 100644 sysdeps/aero/generic/time.cpp
2121

2222
diff --git a/.gitignore b/.gitignore
@@ -524,17 +524,18 @@ index a3e2aca..8a1e086 100644
524524
+#endif
525525
} // namespace mlibc
526526
diff --git a/sysdeps/aero/generic/sockets.cpp b/sysdeps/aero/generic/sockets.cpp
527-
index b6b18fe..b4635df 100644
527+
index b6b18fe..0ee0838 100644
528528
--- a/sysdeps/aero/generic/sockets.cpp
529529
+++ b/sysdeps/aero/generic/sockets.cpp
530-
@@ -1,5 +1,6 @@
530+
@@ -1,5 +1,7 @@
531531
#include <mlibc/all-sysdeps.hpp>
532532
#include <mlibc/thread-entry.hpp>
533533
+#include <mlibc/debug.hpp>
534+
+#include<abi-bits/in.h>
534535

535536
#include <aero/syscall.h>
536537
#include <stdint.h>
537-
@@ -46,8 +47,8 @@ int sys_listen(int fd, int backlog) {
538+
@@ -46,8 +48,8 @@ int sys_listen(int fd, int backlog) {
538539
return 0;
539540
}
540541

@@ -545,11 +546,19 @@ index b6b18fe..b4635df 100644
545546

546547
if (result < 0) {
547548
return -result;
548-
@@ -56,4 +57,86 @@ int sys_accept(int fd, int *newfd) {
549+
@@ -56,4 +58,94 @@ int sys_accept(int fd, int *newfd) {
549550
*newfd = result;
550551
return 0;
551552
}
552553
+
554+
+int sys_msg_send(int fd, const struct msghdr *hdr, int flags, ssize_t *length) {
555+
+ auto result = syscall(SYS_SOCK_SEND, fd, hdr, flags);
556+
+ if (result < 0)
557+
+ return -result;
558+
+
559+
+ *length = result;
560+
+ return 0;
561+
+}
553562
+
554563
+int sys_msg_recv(int sockfd, struct msghdr *msg_hdr, int flags, ssize_t *length) {
555564
+ auto result = syscall(SYS_SOCK_RECV, sockfd, msg_hdr, flags);
@@ -664,10 +673,10 @@ index 0000000..460412d
664673
+}
665674
\ No newline at end of file
666675
diff --git a/sysdeps/aero/include/aero/syscall.h b/sysdeps/aero/include/aero/syscall.h
667-
index 12f8dc6..78fc99d 100644
676+
index 12f8dc6..432bad2 100644
668677
--- a/sysdeps/aero/include/aero/syscall.h
669678
+++ b/sysdeps/aero/include/aero/syscall.h
670-
@@ -64,6 +64,16 @@
679+
@@ -64,6 +64,17 @@
671680
#define SYS_FUTEX_WAIT 57
672681
#define SYS_FUTEX_WAKE 58
673682
#define SYS_LINK 59
@@ -681,6 +690,7 @@ index 12f8dc6..78fc99d 100644
681690
+#define SYS_SOCKET_PAIR 67
682691
+#define SYS_RENAME 68
683692
+#define SYS_MPROTECT 69
693+
+#define SYS_SOCK_SEND 70
684694

685695
// Invalid syscall used to trigger a log error in the kernel (as a hint)
686696
// so, that we can implement the syscall in the kernel.

src/Cargo.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aero_kernel/Cargo.toml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,22 @@ static_assertions = "1.1.0"
4242
lru = "0.8.0"
4343
bytemuck = "1.12.1"
4444
# the crate is not mainted but who cares it works (tm)!
45-
simple_endian = { git = "https://github.com/michalfita/simple-endian-rs", branch = "bugfix/4/fix-no_std-support-right-way", default_features = false, features = ["bitwise", "comparisons", "format", "math_ops", "neg_ops", "shift_ops", "both_endian", "float_impls", "integer_impls", "byte_impls"] }
46-
byteorder = { version = "*", default-features=false}
45+
simple_endian = { git = "https://github.com/michalfita/simple-endian-rs", branch = "bugfix/4/fix-no_std-support-right-way", default_features = false, features = [
46+
"bitwise",
47+
"comparisons",
48+
"format",
49+
"math_ops",
50+
"neg_ops",
51+
"shift_ops",
52+
"both_endian",
53+
"float_impls",
54+
"integer_impls",
55+
"byte_impls",
56+
] }
57+
byteorder = { version = "*", default-features = false }
4758
limine = { git = "https://github.com/limine-bootloader/limine-rs" }
4859
cfg-if = "1.0"
60+
num-traits = { version = "0.2", default-features = false }
4961

5062
# X86_64 specific dependencies:
5163
[target.'cfg(target_arch = "x86_64")'.dependencies]

src/aero_kernel/src/fs/ext2/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,14 @@ impl INodeInterface for INode {
549549
return Err(FileSystemError::NotSupported);
550550
}
551551

552+
fn send(&self, message_header: &mut MessageHeader) -> super::Result<usize> {
553+
if let Some(proxy) = self.proxy.as_ref() {
554+
return proxy.send(message_header);
555+
}
556+
557+
return Err(FileSystemError::NotSupported);
558+
}
559+
552560
fn recv(&self, message_header: &mut MessageHeader, non_block: bool) -> super::Result<usize> {
553561
if let Some(proxy) = self.proxy.as_ref() {
554562
return proxy.recv(message_header, non_block);

src/aero_kernel/src/fs/inode.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ pub trait INodeInterface: Send + Sync {
244244
Err(FileSystemError::NotSocket)
245245
}
246246

247+
fn send(&self, _message_hdr: &mut MessageHeader) -> Result<usize> {
248+
Err(FileSystemError::NotSupported)
249+
}
250+
247251
fn recv(&self, _message_header: &mut MessageHeader, _non_block: bool) -> Result<usize> {
248252
Err(FileSystemError::NotSocket)
249253
}

src/aero_kernel/src/net/checksum.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use simple_endian::BigEndian;
2+
3+
use super::ip::{self, Ipv4Addr};
4+
5+
#[repr(C, packed)]
6+
pub struct PseudoHeader {
7+
src_ip: Ipv4Addr,
8+
dst_ip: Ipv4Addr,
9+
reserved: u8,
10+
ty: ip::Type,
11+
size: BigEndian<u16>,
12+
}
13+
14+
impl PseudoHeader {
15+
pub fn new(ip_hdr: &ip::Header) -> PseudoHeader {
16+
let len = ip_hdr.length;
17+
PseudoHeader {
18+
src_ip: ip_hdr.src_ip,
19+
dst_ip: ip_hdr.dest_ip,
20+
reserved: 0,
21+
ty: ip_hdr.protocol,
22+
size: BigEndian::from(len.to_native() - core::mem::size_of::<ip::Header>() as u16),
23+
}
24+
}
25+
}
26+
27+
/// Compute the 32-bit internet checksum for `data`.
28+
fn calculate_checksum(data: &[u8]) -> u32 {
29+
let bytes = unsafe {
30+
core::slice::from_raw_parts(
31+
data.as_ptr() as *const BigEndian<u16>,
32+
data.len() / core::mem::size_of::<u16>(),
33+
)
34+
};
35+
36+
let mut sum = 0;
37+
38+
for i in 0..(data.len() / 2) {
39+
sum += bytes[i].to_native() as u32
40+
}
41+
42+
// Add left-over byte, if any.
43+
if data.len() % 2 == 1 {
44+
sum += ((*data.last().unwrap()) as u32) << 8;
45+
}
46+
47+
sum
48+
}
49+
50+
/// Folds the 32-bit sum (`sum`) to 16 bits in the network byte order.
51+
pub fn make(mut sum: u32) -> BigEndian<u16> {
52+
while (sum >> 16) != 0 {
53+
sum = (sum & 0xffff) + (sum >> 16);
54+
}
55+
56+
BigEndian::from(!(sum as u16))
57+
}
58+
59+
/// Combine several RFC 1071 compliant checksums.
60+
pub fn make_combine(a: &[u32]) -> BigEndian<u16> {
61+
make(a.iter().sum())
62+
}
63+
64+
/// Compute the internet checksum for `value`.
65+
pub fn calculate<T: Sized>(value: &T) -> u32 {
66+
let bytes = unsafe {
67+
core::slice::from_raw_parts(value as *const _ as *const u8, core::mem::size_of::<T>())
68+
};
69+
calculate_checksum(bytes)
70+
}
71+
72+
/// Compute the internet checksum for `value` of `size`.
73+
pub fn calculate_with_len<T: ?Sized>(value: &T, size: usize) -> u32 {
74+
let bytes = unsafe { core::slice::from_raw_parts(value as *const _ as *const u8, size) };
75+
calculate_checksum(bytes)
76+
}

0 commit comments

Comments
 (0)