Skip to content

Commit 20ad092

Browse files
sockets::inet:: SIOCGIFINDEX stub
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 7bfbb0f commit 20ad092

File tree

3 files changed

+98
-22
lines changed

3 files changed

+98
-22
lines changed

patches/mlibc/mlibc.patch

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
From fb1766aa2abfb310b6c1b0904f849f5d6c8fb62b Mon Sep 17 00:00:00 2001
1+
From 7ffa4a93ab4c1a5a5dd129dce1d03f784ace2e06 Mon Sep 17 00:00:00 2001
22
From: Andy-Python-Programmer <[email protected]>
3-
Date: Sat, 11 Mar 2023 16:55:47 +1100
3+
Date: Mon, 13 Mar 2023 16:14:34 +1100
44
Subject: [PATCH] <xxx>
55

66
---
7-
.gitignore | 2 +
8-
options/ansi/generic/stdlib-stubs.cpp | 6 +-
9-
options/glibc/generic/execinfo.cpp | 5 +-
7+
.gitignore | 2 ++
8+
options/ansi/generic/stdlib-stubs.cpp | 6 ++++--
9+
options/glibc/generic/execinfo.cpp | 5 +++--
1010
options/rtdl/generic/linker.cpp | 2 +-
11-
sysdeps/aero/generic/aero.cpp | 77 +++++++++++++--------
12-
sysdeps/aero/generic/filesystem.cpp | 51 ++++++++++++--
13-
sysdeps/aero/generic/sockets.cpp | 96 ++++++++++++++++++++++++++-
14-
sysdeps/aero/generic/time.cpp | 24 +++++++
15-
sysdeps/aero/include/aero/syscall.h | 11 +++
16-
sysdeps/aero/meson.build | 1 +
17-
10 files changed, 234 insertions(+), 41 deletions(-)
18-
create mode 100644 sysdeps/aero/generic/time.cpp
11+
sysdeps/aero/generic/sockets.cpp | 25 +++++++++++++++++++++++++
12+
5 files changed, 35 insertions(+), 5 deletions(-)
1913

2014
diff --git a/.gitignore b/.gitignore
2115
index fdd60a0..9f811f4 100644
@@ -73,7 +67,49 @@ index 6eb9d09..2ab5bc9 100644
7367
+constexpr bool logBaseAddresses = true;
7468
constexpr bool logRpath = false;
7569
constexpr bool eagerBinding = true;
76-
70+
71+
diff --git a/sysdeps/aero/generic/sockets.cpp b/sysdeps/aero/generic/sockets.cpp
72+
index c730bff..39cacb4 100644
73+
--- a/sysdeps/aero/generic/sockets.cpp
74+
+++ b/sysdeps/aero/generic/sockets.cpp
75+
@@ -4,7 +4,11 @@
76+
#include <abi-bits/in.h>
77+
78+
#include <aero/syscall.h>
79+
+
80+
+#include <unistd.h>
81+
#include <stdint.h>
82+
+#include <net/if.h>
83+
+#include <sys/ioctl.h>
84+
85+
namespace mlibc {
86+
int sys_socket(int family, int type, int protocol, int *fd) {
87+
@@ -187,4 +191,25 @@ int sys_setsockopt(int fd, int layer, int number, const void *buffer,
88+
__builtin_unreachable();
89+
}
90+
}
91+
+
92+
+int sys_if_nametoindex(const char *name, unsigned int *ret) {
93+
+ int fd = 0;
94+
+
95+
+ // TODO(andypython): is the SOCK_CLOEXEC flag required in this case?
96+
+ int r = sys_socket(AF_INET, SOCK_DGRAM, AF_UNSPEC, &fd);
97+
+ if (r)
98+
+ return r;
99+
+
100+
+ struct ifreq ifr;
101+
+ strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
102+
+
103+
+ r = sys_ioctl(fd, SIOCGIFINDEX, &ifr, NULL);
104+
+ close(fd);
105+
+
106+
+ if (r)
107+
+ return r;
108+
+
109+
+ *ret = ifr.ifr_ifindex;
110+
+ return 0;
111+
+}
112+
} // namespace mlibc
77113
--
78-
2.39.1
114+
2.39.2
79115

src/aero_kernel/src/socket/inet.rs

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

20-
use aero_syscall::prelude::{IfReq, SIOCGIFHWADDR, SIOCSIFADDR};
20+
use aero_syscall::prelude::{IfReq, SIOCGIFHWADDR, SIOCGIFINDEX, SIOCSIFADDR};
2121
use aero_syscall::socket::MessageHeader;
2222
use aero_syscall::{IpProtocol, OpenFlags, SocketAddrInet, SocketType, SyscallError};
2323
use alloc::sync::{Arc, Weak};
@@ -64,7 +64,9 @@ pub struct InetSocket {
6464

6565
impl InetSocket {
6666
pub fn new(typ: SocketType, protocol: IpProtocol) -> Result<Arc<Self>, SyscallError> {
67-
if typ != SocketType::Dgram && protocol != IpProtocol::Udp {
67+
if typ != SocketType::Dgram
68+
&& (protocol != IpProtocol::Udp || protocol != IpProtocol::Default)
69+
{
6870
return Err(SyscallError::EINVAL);
6971
}
7072

@@ -228,6 +230,18 @@ impl INodeInterface for InetSocket {
228230

229231
fn ioctl(&self, command: usize, arg: usize) -> fs::Result<usize> {
230232
match command {
233+
SIOCGIFINDEX => {
234+
let ifreq = VirtAddr::new(arg as _)
235+
.read_mut::<IfReq>()
236+
.ok_or(FileSystemError::NotSupported)?;
237+
238+
let name = ifreq.name().unwrap();
239+
assert!(name == "eth0");
240+
241+
ifreq.data.ifindex = 1; // FIXME: Fill the actual interface index
242+
Ok(0)
243+
}
244+
231245
SIOCGIFHWADDR => {
232246
let ifreq = VirtAddr::new(arg as _)
233247
.read_mut::<IfReq>()
@@ -237,7 +251,10 @@ impl INodeInterface for InetSocket {
237251
assert!(name == "eth0");
238252

239253
let hwaddr = unsafe {
240-
core::slice::from_raw_parts_mut(ifreq.sa_data.as_mut_ptr(), MacAddr::ADDR_SIZE)
254+
core::slice::from_raw_parts_mut(
255+
ifreq.data.addr.sa_data.as_mut_ptr(),
256+
MacAddr::ADDR_SIZE,
257+
)
241258
};
242259

243260
let mac_addr = net::default_device().mac();
@@ -250,10 +267,12 @@ impl INodeInterface for InetSocket {
250267
.read_mut::<IfReq>()
251268
.ok_or(FileSystemError::NotSupported)?;
252269

270+
let family = unsafe { ifreq.data.addr.sa_family };
271+
253272
let name = ifreq.name().ok_or(FileSystemError::InvalidPath)?;
254273
let socket = SocketAddr::from_family(
255-
VirtAddr::new(&ifreq.sa_family as *const _ as _),
256-
ifreq.sa_family,
274+
VirtAddr::new(&unsafe { ifreq.data.addr } as *const _ as _),
275+
family,
257276
)
258277
.ok_or(FileSystemError::NotSupported)?
259278
.as_inet()

src/aero_syscall/src/consts.rs

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

20+
use core::ffi;
21+
2022
use crate::OpenFlags;
2123

2224
// syscall number constants:
@@ -348,17 +350,36 @@ pub struct FramebufferFScreenInfo {
348350
}
349351

350352
// networking ioctls:
353+
pub const SIOCGIFINDEX: usize = 0x8933;
351354
pub const SIOCGIFHWADDR: usize = 0x8927;
352355
pub const SIOCSIFADDR: usize = 0x8916; // set PA address
353356

354357
const IF_NAME_SIZE: usize = 16;
355358

359+
#[derive(Clone, Copy)]
360+
#[repr(C)]
361+
pub struct SockAddrStorage {
362+
pub sa_family: u32,
363+
pub sa_data: [u8; 14],
364+
}
365+
366+
#[repr(C)]
367+
pub union IfrIfru {
368+
pub addr: SockAddrStorage,
369+
pub flags: ffi::c_short,
370+
pub ifindex: ffi::c_int,
371+
pub metric: ffi::c_int,
372+
pub mtu: ffi::c_int,
373+
pub slave: [u8; IF_NAME_SIZE],
374+
pub newname: [u8; IF_NAME_SIZE],
375+
pub data: *mut u8,
376+
}
377+
356378
#[repr(C)]
357379
pub struct IfReq {
358380
/// interface name, e.g. "en0"
359381
pub name: [u8; IF_NAME_SIZE],
360-
pub sa_family: u32,
361-
pub sa_data: [u8; 14],
382+
pub data: IfrIfru,
362383
}
363384

364385
impl IfReq {

0 commit comments

Comments
 (0)