fix: fix udp and run dog in udp

This commit is contained in:
smallcjy
2024-11-11 16:24:37 +08:00
parent ff13f9f622
commit 4e8c71b9eb
3 changed files with 32 additions and 11 deletions

View File

@ -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());

View File

@ -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));
}
_ => {