Browse Source

Merge pull request #1041 from smallcjy:fix-udp

fix: fix udp and run dog in udp
Samuel Dai 4 months ago
parent
commit
7411864225

+ 26 - 7
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<BoundUdp, SystemError> {
         // 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::<smoltcp::socket::udp::Socket,_,_>(|socket| {
+                socket.bind(bind_port)
+            }).is_err() {
+                return Err(SystemError::EINVAL);
+            }
+        } else {
+            if inner.with_mut::<smoltcp::socket::udp::Socket,_,_>(|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<smoltcp::wire::IpEndpoint>,
     ) -> Result<usize, SystemError> {
         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());

+ 5 - 3
kernel/src/net/socket/inet/syscall.rs

@@ -12,13 +12,14 @@ fn create_inet_socket(
     socket_type: PSOCK,
     protocol: smoltcp::wire::IpProtocol,
 ) -> Result<Arc<dyn Socket>, 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));
             }
             _ => {

+ 1 - 1
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} "