Skip to content

Commit 5ce6e13

Browse files
committed
feat(socket): make AF_INET non-zero
1 parent 6131789 commit 5ce6e13

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

src/fd/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ pub(crate) trait ObjectInterface: Sync + Send + core::fmt::Debug {
268268
async fn isatty(&self) -> io::Result<bool> {
269269
Ok(false)
270270
}
271+
272+
// FIXME: remove once the ecosystem has migrated away from `AF_INET_OLD`
273+
#[cfg(any(feature = "tcp", feature = "udp"))]
274+
async fn inet_domain(&self) -> io::Result<i32> {
275+
Err(io::Error::EINVAL)
276+
}
271277
}
272278

273279
pub(crate) fn read(fd: FileDescriptor, buf: &mut [MaybeUninit<u8>]) -> io::Result<usize> {

src/fd/socket/tcp.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ pub struct Socket {
3737
port: u16,
3838
is_nonblocking: bool,
3939
is_listen: bool,
40+
// FIXME: remove once the ecosystem has migrated away from `AF_INET_OLD`.
41+
domain: i32,
4042
}
4143

4244
impl Socket {
43-
pub fn new(h: Handle) -> Self {
45+
pub fn new(h: Handle, domain: i32) -> Self {
4446
let mut handle = BTreeSet::new();
4547
handle.insert(h);
4648

@@ -49,6 +51,7 @@ impl Socket {
4951
port: 0,
5052
is_nonblocking: false,
5153
is_listen: false,
54+
domain,
5255
}
5356
}
5457

@@ -349,6 +352,7 @@ impl Socket {
349352
port: self.port,
350353
is_nonblocking: self.is_nonblocking,
351354
is_listen: false,
355+
domain: self.domain,
352356
};
353357

354358
Ok((socket, endpoint))
@@ -520,4 +524,9 @@ impl ObjectInterface for async_lock::RwLock<Socket> {
520524
async fn set_status_flags(&self, status_flags: fd::StatusFlags) -> io::Result<()> {
521525
self.write().await.set_status_flags(status_flags).await
522526
}
527+
528+
async fn inet_domain(&self) -> io::Result<i32> {
529+
let domain = self.read().await.domain;
530+
Ok(domain)
531+
}
523532
}

src/fd/socket/udp.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ pub struct Socket {
1818
handle: Handle,
1919
nonblocking: bool,
2020
endpoint: Option<IpEndpoint>,
21+
// FIXME: remove once the ecosystem has migrated away from `AF_INET_OLD`.
22+
domain: i32,
2123
}
2224

2325
impl Socket {
24-
pub fn new(handle: Handle) -> Self {
26+
pub fn new(handle: Handle, domain: i32) -> Self {
2527
Self {
2628
handle,
2729
nonblocking: false,
2830
endpoint: None,
31+
domain,
2932
}
3033
}
3134

@@ -274,4 +277,9 @@ impl ObjectInterface for async_lock::RwLock<Socket> {
274277
async fn set_status_flags(&self, status_flags: fd::StatusFlags) -> io::Result<()> {
275278
self.write().await.set_status_flags(status_flags).await
276279
}
280+
281+
async fn inet_domain(&self) -> io::Result<i32> {
282+
let domain = self.read().await.domain;
283+
Ok(domain)
284+
}
277285
}

src/syscalls/socket.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use crate::fd::{
2525
use crate::syscalls::block_on;
2626

2727
pub const AF_UNSPEC: i32 = 0;
28-
pub const AF_INET: i32 = 0;
28+
pub const AF_INET_OLD: i32 = 0;
29+
pub const AF_INET: i32 = 3;
2930
pub const AF_INET6: i32 = 1;
3031
pub const AF_VSOCK: i32 = 2;
3132
pub const IPPROTO_IP: i32 = 0;
@@ -437,7 +438,7 @@ pub extern "C" fn sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32
437438
}
438439

439440
#[cfg(any(feature = "tcp", feature = "udp"))]
440-
if (domain == AF_INET || domain == AF_INET6)
441+
if (domain == AF_INET_OLD || domain == AF_INET || domain == AF_INET6)
441442
&& type_.intersects(SockType::SOCK_STREAM | SockType::SOCK_DGRAM)
442443
{
443444
let mut guard = NIC.lock();
@@ -447,7 +448,7 @@ pub extern "C" fn sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32
447448
if type_.contains(SockType::SOCK_DGRAM) {
448449
let handle = nic.create_udp_handle().unwrap();
449450
drop(guard);
450-
let socket = Arc::new(async_lock::RwLock::new(udp::Socket::new(handle)));
451+
let socket = Arc::new(async_lock::RwLock::new(udp::Socket::new(handle, domain)));
451452

452453
if type_.contains(SockType::SOCK_NONBLOCK) {
453454
block_on(socket.set_status_flags(fd::StatusFlags::O_NONBLOCK), None).unwrap();
@@ -462,7 +463,7 @@ pub extern "C" fn sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32
462463
if type_.contains(SockType::SOCK_STREAM) {
463464
let handle = nic.create_tcp_handle().unwrap();
464465
drop(guard);
465-
let socket = Arc::new(async_lock::RwLock::new(tcp::Socket::new(handle)));
466+
let socket = Arc::new(async_lock::RwLock::new(tcp::Socket::new(handle, domain)));
466467

467468
if type_.contains(SockType::SOCK_NONBLOCK) {
468469
block_on(socket.set_status_flags(fd::StatusFlags::O_NONBLOCK), None).unwrap();
@@ -500,6 +501,10 @@ pub unsafe extern "C" fn sys_accept(fd: i32, addr: *mut sockaddr, addrlen: *mut
500501
if *addrlen >= size_of::<sockaddr_in>().try_into().unwrap() {
501502
let addr = unsafe { &mut *addr.cast() };
502503
*addr = sockaddr_in::from(endpoint);
504+
addr.sin_family = block_on(v.inet_domain(), None)
505+
.unwrap()
506+
.try_into()
507+
.unwrap();
503508
*addrlen = size_of::<sockaddr_in>().try_into().unwrap();
504509
}
505510
}
@@ -564,7 +569,7 @@ pub unsafe extern "C" fn sys_bind(fd: i32, name: *const sockaddr, namelen: sockl
564569
|e| -num::ToPrimitive::to_i32(&e).unwrap(),
565570
|v| match family {
566571
#[cfg(any(feature = "tcp", feature = "udp"))]
567-
AF_INET => {
572+
AF_INET_OLD | AF_INET => {
568573
if namelen < size_of::<sockaddr_in>().try_into().unwrap() {
569574
return -crate::errno::EINVAL;
570575
}
@@ -606,7 +611,7 @@ pub unsafe extern "C" fn sys_connect(fd: i32, name: *const sockaddr, namelen: so
606611

607612
let endpoint = match sa_family {
608613
#[cfg(any(feature = "tcp", feature = "udp"))]
609-
AF_INET => {
614+
AF_INET_OLD | AF_INET => {
610615
if namelen < size_of::<sockaddr_in>().try_into().unwrap() {
611616
return -crate::errno::EINVAL;
612617
}
@@ -663,6 +668,10 @@ pub unsafe extern "C" fn sys_getsockname(
663668
if *addrlen >= size_of::<sockaddr_in>().try_into().unwrap() {
664669
let addr = unsafe { &mut *addr.cast() };
665670
*addr = sockaddr_in::from(endpoint);
671+
addr.sin_family = block_on(v.inet_domain(), None)
672+
.unwrap()
673+
.try_into()
674+
.unwrap();
666675
*addrlen = size_of::<sockaddr_in>().try_into().unwrap();
667676
} else {
668677
return -crate::errno::EINVAL;
@@ -803,6 +812,10 @@ pub unsafe extern "C" fn sys_getpeername(
803812
if *addrlen >= size_of::<sockaddr_in>().try_into().unwrap() {
804813
let addr = unsafe { &mut *addr.cast() };
805814
*addr = sockaddr_in::from(endpoint);
815+
addr.sin_family = block_on(v.inet_domain(), None)
816+
.unwrap()
817+
.try_into()
818+
.unwrap();
806819
*addrlen = size_of::<sockaddr_in>().try_into().unwrap();
807820
} else {
808821
return -crate::errno::EINVAL;
@@ -915,7 +928,7 @@ pub unsafe extern "C" fn sys_sendto(
915928
if #[cfg(any(feature = "tcp", feature = "udp"))] {
916929
let sa_family = unsafe { i32::from((*addr).sa_family) };
917930

918-
if sa_family == AF_INET {
931+
if sa_family == AF_INET_OLD || sa_family == AF_INET {
919932
if addr_len < size_of::<sockaddr_in>().try_into().unwrap() {
920933
return (-crate::errno::EINVAL).try_into().unwrap();
921934
}
@@ -982,6 +995,10 @@ pub unsafe extern "C" fn sys_recvfrom(
982995
if *addrlen >= size_of::<sockaddr_in>().try_into().unwrap() {
983996
let addr = unsafe { &mut *addr.cast() };
984997
*addr = sockaddr_in::from(endpoint);
998+
addr.sin_family = block_on(v.inet_domain(), None)
999+
.unwrap()
1000+
.try_into()
1001+
.unwrap();
9851002
*addrlen = size_of::<sockaddr_in>().try_into().unwrap();
9861003
} else {
9871004
return (-crate::errno::EINVAL).try_into().unwrap();

0 commit comments

Comments
 (0)