Skip to content

Commit 165fa8e

Browse files
Samuka007fslongjin
andauthored
fix(net): udp getsockname/getpeername (#1460)
* fix(net): udp getsockname/getpeername * feat(ci): add test whitelist for new available inet syscall --------- Co-authored-by: longjin <[email protected]>
1 parent 1245799 commit 165fa8e

File tree

5 files changed

+116
-11
lines changed

5 files changed

+116
-11
lines changed

kernel/src/net/socket/inet/datagram/inner.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ impl UnboundUdp {
6969
Ok(BoundUdp {
7070
inner,
7171
remote: SpinLock::new(None),
72-
local_endpoint: smoltcp::wire::IpEndpoint::new(bind_addr, bind_port),
7372
})
7473
}
7574

@@ -85,7 +84,6 @@ impl UnboundUdp {
8584
Ok(BoundUdp {
8685
inner,
8786
remote: SpinLock::new(Some(endpoint)),
88-
local_endpoint: endpoint,
8987
})
9088
}
9189
}
@@ -94,7 +92,6 @@ impl UnboundUdp {
9492
pub struct BoundUdp {
9593
inner: BoundInner,
9694
remote: SpinLock<Option<smoltcp::wire::IpEndpoint>>,
97-
local_endpoint: smoltcp::wire::IpEndpoint,
9895
}
9996

10097
impl BoundUdp {
@@ -117,6 +114,14 @@ impl BoundUdp {
117114
.with::<SmolUdpSocket, _, _>(|socket| socket.endpoint())
118115
}
119116

117+
pub fn remote_endpoint(&self) -> Result<smoltcp::wire::IpEndpoint, SystemError> {
118+
self.remote
119+
.lock()
120+
.as_ref()
121+
.cloned()
122+
.ok_or(SystemError::ENOTCONN)
123+
}
124+
120125
pub fn connect(&self, remote: smoltcp::wire::IpEndpoint) {
121126
self.remote.lock().replace(remote);
122127
}
@@ -165,10 +170,6 @@ impl BoundUdp {
165170
socket.close();
166171
});
167172
}
168-
169-
pub fn local_endpoint(&self) -> smoltcp::wire::IpEndpoint {
170-
self.local_endpoint
171-
}
172173
}
173174

174175
// Udp Inner 负责其内部资源管理

kernel/src/net/socket/inet/datagram/mod.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ impl UdpSocket {
128128
{
129129
let mut inner_guard = self.inner.write();
130130
let inner = match inner_guard.take().expect("Udp Inner is None") {
131+
// TODO: 此处会为空,需要DEBUG
131132
UdpInner::Bound(bound) => bound,
132133
UdpInner::Unbound(unbound) => unbound
133-
.bind_ephemeral(to.ok_or(SystemError::EADDRNOTAVAIL)?.addr, self.netns())?,
134+
.bind_ephemeral(to.ok_or(SystemError::EDESTADDRREQ)?.addr, self.netns())?,
134135
};
135136
// size = inner.try_send(buf, to)?;
136137
inner_guard.replace(UdpInner::Bound(inner));
@@ -263,13 +264,25 @@ impl Socket for UdpSocket {
263264
}
264265

265266
fn remote_endpoint(&self) -> Result<Endpoint, SystemError> {
266-
todo!()
267+
match self.inner.read().as_ref().unwrap() {
268+
UdpInner::Bound(bound) => Ok(Endpoint::Ip(bound.remote_endpoint()?)),
269+
// TODO: IPv6 support
270+
_ => Err(SystemError::ENOTCONN),
271+
}
267272
}
268273

269274
fn local_endpoint(&self) -> Result<Endpoint, SystemError> {
275+
use smoltcp::wire::{IpAddress::*, IpEndpoint, IpListenEndpoint};
270276
match self.inner.read().as_ref().unwrap() {
271-
UdpInner::Bound(bound) => Ok(Endpoint::Ip(bound.local_endpoint())),
272-
_ => Err(SystemError::ENOTCONN),
277+
UdpInner::Bound(bound) => {
278+
let IpListenEndpoint { addr, port } = bound.endpoint();
279+
Ok(Endpoint::Ip(IpEndpoint::new(
280+
addr.unwrap_or(Ipv4([0, 0, 0, 0].into())),
281+
port,
282+
)))
283+
}
284+
// TODO: IPv6 support
285+
_ => Ok(Endpoint::Ip(IpEndpoint::new(Ipv4([0, 0, 0, 0].into()), 0))),
273286
}
274287
}
275288

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SocketTest.UnixSocketPairProtocol
2+
# SocketTest.ProtocolInet # Only Inet sockets are enabled
3+
SocketTest.ProtocolUnix
4+
SocketTest.UnixSocketStat
5+
SocketTest.UnixSocketStatFS
6+
SocketTest.UnixSCMRightsOnlyPassedOnce
7+
SocketTest.Permission
8+
OpenModes/*.Unix/*
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
**/1
2+
**/2
3+
UdpInet6SocketTest.ConnectInet4Sockaddr
4+
# **.Creation
5+
# **.Getsockname
6+
# **.Getpeername
7+
**.SendNotConnected/**
8+
**.ConnectBinds/**
9+
**.ReceiveNotBound/**
10+
**.Bind/**
11+
**.BindInUse/**
12+
**.ConnectWriteToInvalidPort/**
13+
**.ConnectSimultaneousWriteToInvalidPort/**
14+
**.ReceiveAfterConnect/**
15+
**.ReceiveAfterDisconnect/**
16+
**.Connect/**
17+
**.ConnectAnyZero/**
18+
**.ConnectAnyWithPort/**
19+
**.DisconnectAfterConnectAny/**
20+
**.DisconnectAfterConnectAnyWithPort/**
21+
**.DisconnectAfterBind/**
22+
**.DisconnectAfterBindToUnspecAndConnect/**
23+
**.DisconnectAfterConnectWithoutBind/**
24+
**.BindToAnyConnnectToLocalhost/**
25+
**.DisconnectAfterBindToAny/**
26+
**.Disconnect/**
27+
**.ConnectBadAddress/**
28+
**.SendToAddressOtherThanConnected/**
29+
**.ConnectAndSendNoReceiver/**
30+
**.RecvErrorConnRefusedOtherAFSockOpt/**
31+
**.RecvErrorConnRefused/**
32+
**.ZerolengthWriteAllowed/**
33+
**.ZerolengthWriteAllowedNonBlockRead/**
34+
**.SendAndReceiveNotConnected/**
35+
**.SendAndReceiveConnected/**
36+
**.ReceiveFromNotConnected/**
37+
**.ReceiveBeforeConnect/**
38+
**.ReceiveFrom/**
39+
**.Listen/**
40+
**.Accept/**
41+
**.ReadShutdownNonblockPendingData/**
42+
**.ReadShutdownSameSocketResetsShutdownState/**
43+
**.ReadShutdown/**
44+
**.ReadShutdownDifferentThread/**
45+
**.WriteShutdown/**
46+
**.SynchronousReceive/**
47+
**.BoundaryPreserved_SendRecv/**
48+
**.BoundaryPreserved_WritevReadv/**
49+
**.BoundaryPreserved_SendMsgRecvMsg/**
50+
**.FIONREADShutdown/**
51+
**.FIONREADWriteShutdown/**
52+
**.Fionread/**
53+
**.FIONREADZeroLengthPacket/**
54+
**.FIONREADZeroLengthWriteShutdown/**
55+
**.SoNoCheckOffByDefault/**
56+
**.SoNoCheck/**
57+
**.ErrorQueue/**
58+
**.SoTimestampOffByDefault/**
59+
**.SoTimestamp/**
60+
**.WriteShutdownNotConnected/**
61+
**.TimestampIoctl/**
62+
**.TimestampIoctlNothingRead/**
63+
**.TimestampIoctlPersistence/**
64+
**.RecvBufLimitsEmptyRcvBuf/**
65+
**.RecvBufLimits/**
66+
**.SetSocketDetachFilter/**
67+
**.SetSocketDetachFilterNoInstalledFilter/**
68+
**.GetSocketDetachFilter/**
69+
**.SendToZeroPort/**
70+
**.ConnectToZeroPortUnbound/**
71+
**.ConnectToZeroPortBound/**
72+
**.ConnectToZeroPortConnected/**
73+
**.SetAndReceiveTOSOrTClass/**
74+
**.SendAndReceiveTOSorTClass/**
75+
**.SetAndReceiveTTLOrHopLimit/**
76+
**.SendAndReceiveTTLOrHopLimit/**
77+
**.SetAndReceivePktInfo/**
78+
**.SendPacketLargerThanSendBufOnNonBlockingSocket/**
79+
**.ReadShutdownOnBoundSocket/**
80+
**.ReconnectDoesNotClearReadShutdown/**
81+
**.ReconnectDoesNotClearWriteShutdown/**

user/apps/tests/syscall/gvisor/whitelist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ madvise_test
6262
#bind_test
6363
#connect_test
6464
#listen_test
65+
socket_test
66+
udp_socket_test
6567

6668
# 信号处理测试
6769
sigaction_test

0 commit comments

Comments
 (0)