From 4e8c71b9eb3f29b4488a0a3353c18103d0427b7a Mon Sep 17 00:00:00 2001 From: smallcjy <2628035541@qq.com> Date: Mon, 11 Nov 2024 16:24:37 +0800 Subject: [PATCH] fix: fix udp and run dog in udp --- kernel/src/net/socket/inet/datagram/inner.rs | 33 +++++++++++++++----- kernel/src/net/socket/inet/syscall.rs | 8 +++-- tools/run-qemu.sh | 2 +- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/kernel/src/net/socket/inet/datagram/inner.rs b/kernel/src/net/socket/inet/datagram/inner.rs index 2cc335e5d..e27e0276c 100644 --- a/kernel/src/net/socket/inet/datagram/inner.rs +++ b/kernel/src/net/socket/inet/datagram/inner.rs @@ -33,17 +33,37 @@ impl UnboundUdp { } pub fn bind( - mut self, + self, local_endpoint: smoltcp::wire::IpEndpoint, ) -> Result { // let (addr, port) = (local_endpoint.addr, local_endpoint.port); - if self.socket.bind(local_endpoint).is_err() { - return Err(EINVAL); - } + // if self.socket.bind(local_endpoint).is_err() { + // log::debug!("bind failed!"); + // return Err(EINVAL); + // } let inner = BoundInner::bind(self.socket, &local_endpoint.addr)?; - inner + let bind_addr = local_endpoint.addr; + let bind_port = if local_endpoint.port == 0 { + inner .port_manager() - .bind_port(InetTypes::Udp, local_endpoint.port)?; + .bind_ephemeral_port(InetTypes::Udp)? + } else { + local_endpoint.port + }; + + if bind_addr.is_unspecified() { + if inner.with_mut::(|socket| { + socket.bind(bind_port) + }).is_err() { + return Err(SystemError::EINVAL); + } + } else { + if inner.with_mut::(|socket| { + socket.bind(smoltcp::wire::IpEndpoint::new(bind_addr, bind_port)) + }).is_err() { + return Err(SystemError::EINVAL); + } + } Ok(BoundUdp { inner, remote: SpinLock::new(None), @@ -122,7 +142,6 @@ impl BoundUdp { to: Option, ) -> Result { let remote = to.or(*self.remote.lock()).ok_or(ENOTCONN)?; - let result = self.with_mut_socket(|socket| { if socket.can_send() && socket.send_slice(buf, remote).is_ok() { log::debug!("send {} bytes", buf.len()); diff --git a/kernel/src/net/socket/inet/syscall.rs b/kernel/src/net/socket/inet/syscall.rs index 6049c990a..e7a8d58bf 100644 --- a/kernel/src/net/socket/inet/syscall.rs +++ b/kernel/src/net/socket/inet/syscall.rs @@ -12,13 +12,14 @@ fn create_inet_socket( socket_type: PSOCK, protocol: smoltcp::wire::IpProtocol, ) -> Result, SystemError> { - // log::debug!("type: {:?}, protocol: {:?}", socket_type, protocol); + log::debug!("type: {:?}, protocol: {:?}", socket_type, protocol); use smoltcp::wire::IpProtocol::*; match socket_type { PSOCK::Datagram => match protocol { HopByHop | Udp => { - return Err(EPROTONOSUPPORT); - // return Ok(UdpSocket::new(false)); + log::debug!("create udp socket"); + // return Err(EPROTONOSUPPORT); + return Ok(UdpSocket::new(false)); } _ => { return Err(EPROTONOSUPPORT); @@ -26,6 +27,7 @@ fn create_inet_socket( }, PSOCK::Stream => match protocol { HopByHop | Tcp => { + log::debug!("create tcp socket"); return Ok(TcpSocket::new(false, version)); } _ => { diff --git a/tools/run-qemu.sh b/tools/run-qemu.sh index 8430e20ab..8f201c8de 100644 --- a/tools/run-qemu.sh +++ b/tools/run-qemu.sh @@ -143,7 +143,7 @@ while true;do # ps: 下面这条使用tap的方式,无法dhcp获取到ip,暂时不知道为什么 # QEMU_DEVICES="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -net nic,netdev=nic0 -netdev tap,id=nic0,model=virtio-net-pci,script=qemu/ifup-nat,downscript=qemu/ifdown-nat -usb -device qemu-xhci,id=xhci,p2=8,p3=4 " QEMU_DEVICES+="${QEMU_DEVICES_DISK} " -QEMU_DEVICES+=" -netdev user,id=hostnet0,hostfwd=tcp::12580-:12580,hostfwd=udp::12549-:12549 -device virtio-net-pci,vectors=5,netdev=hostnet0,id=net0 -usb -device qemu-xhci,id=xhci,p2=8,p3=4 " +QEMU_DEVICES+=" -netdev user,id=hostnet0,hostfwd=tcp::12580-:12580 -device virtio-net-pci,vectors=5,netdev=hostnet0,id=net0 -usb -device qemu-xhci,id=xhci,p2=8,p3=4 " # E1000E # QEMU_DEVICES="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -netdev user,id=hostnet0,hostfwd=tcp::12580-:12580 -net nic,model=e1000e,netdev=hostnet0,id=net0 -netdev user,id=hostnet1,hostfwd=tcp::12581-:12581 -device virtio-net-pci,vectors=5,netdev=hostnet1,id=net1 -usb -device qemu-xhci,id=xhci,p2=8,p3=4 " QEMU_ARGUMENT+="-d ${QEMU_DISK_IMAGE} -m ${QEMU_MEMORY} -smp ${QEMU_SMP} -boot order=d ${QEMU_MONITOR} -d ${qemu_trace_std} "