Skip to content

Commit 9389db6

Browse files
syscall::net: add connect syscall
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent ebb42be commit 9389db6

File tree

8 files changed

+110
-66
lines changed

8 files changed

+110
-66
lines changed

patches/mlibc/mlibc.patch

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From fb279173f0a97399a112bd8a5c2d3ec96f450374 Mon Sep 17 00:00:00 2001
1+
From f76727cfa3db5f15b030a3cdc4535ea1eea31ac4 Mon Sep 17 00:00:00 2001
22
From: Andy-Python-Programmer <[email protected]>
33
Date: Thu, 10 Feb 2022 19:12:25 +1100
44
Subject: [PATCH] yes
@@ -10,9 +10,9 @@ Signed-off-by: Andy-Python-Programmer <[email protected]>
1010
sysdeps/aero/generic/aero.cpp | 12 ++++-
1111
sysdeps/aero/generic/filesystem.cpp | 79 ++++++++++++++++++++++++++---
1212
sysdeps/aero/generic/signals.cpp | 8 ++-
13-
sysdeps/aero/generic/sockets.cpp | 28 ++++++++++
13+
sysdeps/aero/generic/sockets.cpp | 38 ++++++++++++++
1414
sysdeps/aero/include/aero/syscall.h | 12 +++++
15-
7 files changed, 131 insertions(+), 13 deletions(-)
15+
7 files changed, 141 insertions(+), 13 deletions(-)
1616

1717
diff --git a/.gitignore b/.gitignore
1818
index dbb35e8b..20c8d4c3 100644
@@ -210,10 +210,10 @@ index 3527370c..a6f69fff 100644
210210
} // namespace mlibc
211211
\ No newline at end of file
212212
diff --git a/sysdeps/aero/generic/sockets.cpp b/sysdeps/aero/generic/sockets.cpp
213-
index e69de29b..e4040629 100644
213+
index e69de29b..c1375859 100644
214214
--- a/sysdeps/aero/generic/sockets.cpp
215215
+++ b/sysdeps/aero/generic/sockets.cpp
216-
@@ -0,0 +1,28 @@
216+
@@ -0,0 +1,38 @@
217217
+#include <mlibc/all-sysdeps.hpp>
218218
+#include <mlibc/thread-entry.hpp>
219219
+
@@ -241,6 +241,16 @@ index e69de29b..e4040629 100644
241241
+
242242
+ return 0;
243243
+}
244+
+
245+
+int sys_connect(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) {
246+
+ auto result = syscall(SYS_CONNECT, fd, addr_ptr, (sc_word_t)addr_length);
247+
+
248+
+ if (result < 0) {
249+
+ return -result;
250+
+ }
251+
+
252+
+ return 0;
253+
+}
244254
+} // namespace mlibc
245255
diff --git a/sysdeps/aero/include/aero/syscall.h b/sysdeps/aero/include/aero/syscall.h
246256
index 07b1b51b..ef797e40 100644

src/aero_kernel/src/fs/inode.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
use core::sync::atomic::{AtomicUsize, Ordering};
2121

22-
use aero_syscall::MMapFlags;
23-
use aero_syscall::{OpenFlags, SocketAddr};
22+
use aero_syscall::{MMapFlags, OpenFlags};
2423

2524
use alloc::string::String;
2625
use alloc::sync::Arc;
@@ -30,6 +29,7 @@ use alloc::vec::Vec;
3029
use spin::Once;
3130

3231
use crate::mem::paging::PhysFrame;
32+
use crate::socket::SocketAddr;
3333
use crate::utils::sync::Mutex;
3434
use crate::utils::Downcastable;
3535

@@ -147,7 +147,11 @@ pub trait INodeInterface: Send + Sync + Downcastable {
147147

148148
// Socket operations
149149
fn bind(&self, _address: SocketAddr, _length: usize) -> Result<()> {
150-
Err(FileSystemError::NotSupported)
150+
Err(FileSystemError::NotSocket)
151+
}
152+
153+
fn connect(&self, _address: SocketAddr, _length: usize) -> Result<()> {
154+
Err(FileSystemError::NotSocket)
151155
}
152156
}
153157

src/aero_kernel/src/fs/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,22 @@ pub enum FileSystemError {
132132
Interrupted,
133133
TooSmall,
134134
InvalidPath,
135+
NotSocket,
135136
}
136137

137138
impl From<FileSystemError> for AeroSyscallError {
138139
fn from(error: FileSystemError) -> Self {
139140
match error {
140-
FileSystemError::NotSupported => AeroSyscallError::EACCES,
141-
FileSystemError::EntryExists => AeroSyscallError::EEXIST,
142-
FileSystemError::EntryNotFound => AeroSyscallError::ENOENT,
143-
FileSystemError::Busy => AeroSyscallError::EBUSY,
144-
FileSystemError::NotDirectory => AeroSyscallError::ENOTDIR,
145-
FileSystemError::IsPipe => AeroSyscallError::ESPIPE,
146-
FileSystemError::Interrupted => AeroSyscallError::EINTR,
147-
FileSystemError::TooSmall => AeroSyscallError::E2BIG,
148-
FileSystemError::InvalidPath => AeroSyscallError::EINVAL,
141+
FileSystemError::NotSupported => Self::EACCES,
142+
FileSystemError::EntryExists => Self::EEXIST,
143+
FileSystemError::EntryNotFound => Self::ENOENT,
144+
FileSystemError::Busy => Self::EBUSY,
145+
FileSystemError::NotDirectory => Self::ENOTDIR,
146+
FileSystemError::IsPipe => Self::ESPIPE,
147+
FileSystemError::Interrupted => Self::EINTR,
148+
FileSystemError::TooSmall => Self::E2BIG,
149+
FileSystemError::InvalidPath => Self::EINVAL,
150+
FileSystemError::NotSocket => Self::ENOTSOCK,
149151
}
150152
}
151153
}

src/aero_kernel/src/socket/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,25 @@
1717
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20+
use aero_syscall::*;
21+
22+
use crate::mem::paging::VirtAddr;
23+
24+
#[derive(Debug)]
25+
pub enum SocketAddr<'a> {
26+
Unix(&'a SocketAddrUnix),
27+
INet(&'a SocketAddrInet),
28+
}
29+
30+
impl<'a> SocketAddr<'a> {
31+
pub fn from_family(address: VirtAddr, family: u32) -> Option<Self> {
32+
match family {
33+
AF_UNIX => Some(SocketAddr::Unix(address.read_mut::<SocketAddrUnix>()?)),
34+
AF_INET => Some(SocketAddr::INet(address.read_mut::<SocketAddrInet>()?)),
35+
36+
_ => None,
37+
}
38+
}
39+
}
40+
2041
pub mod unix;

src/aero_kernel/src/socket/unix.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
use aero_syscall::SocketAddr;
2120
use alloc::{
2221
string::String,
2322
sync::{Arc, Weak},
@@ -29,6 +28,8 @@ use crate::fs::{
2928
FileSystemError, Path, Result,
3029
};
3130

31+
use super::SocketAddr;
32+
3233
pub struct UnixSocket {
3334
weak: Weak<UnixSocket>,
3435
}
@@ -86,4 +87,8 @@ impl INodeInterface for UnixSocket {
8687
Err(FileSystemError::NotSupported)
8788
}
8889
}
90+
91+
fn connect(&self, _address: SocketAddr, _length: usize) -> Result<()> {
92+
todo!("{_address:?}")
93+
}
8994
}

src/aero_kernel/src/syscall/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ pub fn generic_do_syscall(
210210

211211
SYS_SOCKET => net::socket(b, c, d),
212212
SYS_BIND => net::bind(b, c, d),
213+
SYS_CONNECT => net::connect(b, c, d),
213214

214215
SYS_GETTIME => time::gettime(b, c),
215216
SYS_SLEEP => time::sleep(b),

src/aero_kernel/src/syscall/net.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
11
use aero_syscall::*;
22

3-
use crate::{fs::inode::DirEntry, socket::unix::*, userland::scheduler};
3+
use crate::fs::inode::DirEntry;
4+
use crate::mem::paging::VirtAddr;
5+
6+
use crate::socket::unix::*;
7+
use crate::socket::SocketAddr;
8+
9+
use crate::userland::scheduler;
10+
11+
/// Creates a [`SocketAddr`] from the provided userland socket structure address. This
12+
/// is done by looking at the family field present in every socket address structure.
13+
fn socket_addr_from_addr<'sys>(address: VirtAddr) -> Result<SocketAddr<'sys>, AeroSyscallError> {
14+
let family = address
15+
.read_mut::<u32>()
16+
.ok_or(AeroSyscallError::EINVAL)?
17+
.clone();
18+
19+
Ok(SocketAddr::from_family(address, family).ok_or(AeroSyscallError::EINVAL)?)
20+
}
21+
22+
/// Connects the socket referred to by the file descriptor, to the specified address.
23+
#[aero_proc::syscall]
24+
pub fn connect(fd: usize, address: usize, length: usize) -> Result<usize, AeroSyscallError> {
25+
let address = socket_addr_from_addr(VirtAddr::new(address as u64))?;
26+
let file = scheduler::get_scheduler()
27+
.current_task()
28+
.file_table
29+
.get_handle(fd)
30+
.ok_or(AeroSyscallError::EINVAL)?;
31+
32+
file.inode().connect(address, length)?;
33+
Ok(0)
34+
}
435

536
#[aero_proc::syscall]
637
pub fn socket(
738
domain: usize,
839
socket_type: usize,
940
protocol: usize,
1041
) -> Result<usize, AeroSyscallError> {
11-
let socket = match (domain, socket_type, protocol) {
42+
let socket = match (domain as u32, socket_type, protocol) {
1243
(AF_UNIX, SOCK_STREAM, 0) => UnixSocket::new(),
1344
(_, _, _) => {
1445
log::warn!(
@@ -28,20 +59,10 @@ pub fn socket(
2859
Ok(fd)
2960
}
3061

31-
// TODO: Figure out how to handle this.
3262
#[aero_proc::syscall]
3363
pub fn bind(fd: usize, address: usize, length: usize) -> Result<usize, AeroSyscallError> {
34-
let family = unsafe { *(address as *const i32) };
35-
let address = match family as usize {
36-
AF_UNIX => SocketAddr::Unix(unsafe { &*(address as *const SocketAddrUnix) }),
37-
38-
_ => {
39-
log::warn!("unsupported address family: {}", family);
40-
return Err(AeroSyscallError::EINVAL);
41-
}
42-
};
64+
let address = socket_addr_from_addr(VirtAddr::new(address as u64))?;
4365

44-
// let address = unsafe { &*(address as *const SocketAddr) };
4566
let current_task = scheduler::get_scheduler().current_task();
4667
let file = current_task.file_table.get_handle(fd);
4768

src/aero_syscall/src/lib.rs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -674,11 +674,6 @@ pub struct SocketAddrInet {
674674
pub padding: [u8; 8],
675675
}
676676

677-
pub enum SocketAddr<'a> {
678-
Unix(&'a SocketAddrUnix),
679-
Inet(&'a SocketAddrInet),
680-
}
681-
682677
// constants for the socket types:
683678
//
684679
// mlibc/sysdeps/aero/include/abi-bits/socket.h
@@ -689,21 +684,21 @@ pub const SOCK_STREAM: usize = 4;
689684
pub const SOCK_NONBLOCK: usize = 0x10000;
690685
pub const SOCK_CLOEXEC: usize = 0x20000;
691686

692-
pub const PF_INET: usize = 1;
693-
pub const PF_INET6: usize = 2;
694-
pub const PF_UNIX: usize = 3;
695-
pub const PF_LOCAL: usize = 3;
696-
pub const PF_UNSPEC: usize = 4;
697-
pub const PF_NETLINK: usize = 5;
698-
pub const PF_BRIDGE: usize = 6;
699-
700-
pub const AF_INET: usize = PF_INET;
701-
pub const AF_INET6: usize = PF_INET6;
702-
pub const AF_UNIX: usize = PF_UNIX;
703-
pub const AF_LOCAL: usize = PF_LOCAL;
704-
pub const AF_UNSPEC: usize = PF_UNSPEC;
705-
pub const AF_NETLINK: usize = PF_NETLINK;
706-
pub const AF_BRIDGE: usize = PF_BRIDGE;
687+
pub const PF_INET: u32 = 1;
688+
pub const PF_INET6: u32 = 2;
689+
pub const PF_UNIX: u32 = 3;
690+
pub const PF_LOCAL: u32 = 3;
691+
pub const PF_UNSPEC: u32 = 4;
692+
pub const PF_NETLINK: u32 = 5;
693+
pub const PF_BRIDGE: u32 = 6;
694+
695+
pub const AF_INET: u32 = PF_INET;
696+
pub const AF_INET6: u32 = PF_INET6;
697+
pub const AF_UNIX: u32 = PF_UNIX;
698+
pub const AF_LOCAL: u32 = PF_LOCAL;
699+
pub const AF_UNSPEC: u32 = PF_UNSPEC;
700+
pub const AF_NETLINK: u32 = PF_NETLINK;
701+
pub const AF_BRIDGE: u32 = PF_BRIDGE;
707702

708703
pub fn sys_socket(
709704
domain: usize,
@@ -719,21 +714,6 @@ pub fn sys_listen(fd: usize, backlog: usize) -> Result<usize, AeroSyscallError>
719714
isize_as_syscall_result(value as _)
720715
}
721716

722-
pub fn sys_accept(
723-
fd: usize,
724-
address: &mut SocketAddr,
725-
length: &mut u32,
726-
) -> Result<usize, AeroSyscallError> {
727-
let value = syscall3(
728-
prelude::SYS_ACCEPT,
729-
fd,
730-
address as *const SocketAddr as usize,
731-
length as *mut u32 as usize,
732-
);
733-
734-
isize_as_syscall_result(value as _)
735-
}
736-
737717
pub fn sys_unlink(fd: usize, path: &str, flags: OpenFlags) -> Result<usize, AeroSyscallError> {
738718
let value = syscall4(
739719
prelude::SYS_UNLINK,

0 commit comments

Comments
 (0)