Skip to content

Commit

Permalink
fix: binding to 0.0.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Jan 5, 2025
1 parent 437d905 commit d139bc1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions edge-nal-embassy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.4.1] - 2024-01-05
* Fix regression: ability to UDP/TCP bind to socket 0.0.0.0

## [0.4.0] - 2024-01-02
* Proper TCP socket shutdown; Generic TCP timeout utils; built-in HTTP server timeouts; update docu (#34)
* fix a typo (#44)
Expand Down
2 changes: 1 addition & 1 deletion edge-nal-embassy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "edge-nal-embassy"
version = "0.4.0"
version = "0.4.1"
edition = "2021"
rust-version = "1.77"
description = "An implementation of edge-nal based on `embassy-net`"
Expand Down
16 changes: 15 additions & 1 deletion edge-nal-embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::mem::MaybeUninit;
use core::net::SocketAddr;
use core::ptr::NonNull;

use embassy_net::IpEndpoint;
use embassy_net::{IpEndpoint, IpListenEndpoint};

pub use dns::*;
pub use tcp::*;
Expand Down Expand Up @@ -72,3 +72,17 @@ pub(crate) fn to_net_socket(socket: IpEndpoint) -> SocketAddr {
// socket.port,
// )
// }

pub(crate) fn to_emb_socket(socket: SocketAddr) -> IpEndpoint {
IpEndpoint {
addr: socket.ip().into(),
port: socket.port(),
}
}

pub(crate) fn to_emb_bind_socket(socket: SocketAddr) -> IpListenEndpoint {
IpListenEndpoint {
addr: (!socket.ip().is_unspecified()).then(|| socket.ip().into()),
port: socket.port(),
}
}
6 changes: 3 additions & 3 deletions edge-nal-embassy/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use embassy_net::Stack;

use embedded_io_async::{ErrorKind, ErrorType, Read, Write};

use crate::{to_net_socket, Pool};
use crate::{to_emb_bind_socket, to_emb_socket, to_net_socket, Pool};

/// A struct that implements the `TcpConnect` and `TcpBind` factory traits from `edge-nal`
/// Capable of managing up to N concurrent connections with TX and RX buffers according to TX_SZ and RX_SZ.
Expand Down Expand Up @@ -44,7 +44,7 @@ impl<const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpConnect
async fn connect(&self, remote: SocketAddr) -> Result<Self::Socket<'_>, Self::Error> {
let mut socket = TcpSocket::new(self.stack, self.buffers)?;

socket.socket.connect(remote).await?;
socket.socket.connect(to_emb_socket(remote)).await?;

Ok(socket)
}
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<const N: usize, const TX_SZ: usize, const RX_SZ: usize> edge_nal::TcpAccept
async fn accept(&self) -> Result<(SocketAddr, Self::Socket<'_>), Self::Error> {
let mut socket = TcpSocket::new(self.stack.stack, self.stack.buffers)?;

socket.socket.accept(self.local).await?;
socket.socket.accept(to_emb_bind_socket(self.local)).await?;

let local_endpoint = socket.socket.local_endpoint().unwrap();

Expand Down
6 changes: 3 additions & 3 deletions edge-nal-embassy/src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use embassy_net::{MulticastError, Stack};

use embedded_io_async::{ErrorKind, ErrorType};

use crate::{to_net_socket, Pool};
use crate::{to_emb_bind_socket, to_emb_socket, to_net_socket, Pool};

/// A struct that implements the `UdpBind` factory trait from `edge-nal`
/// Capable of managing up to N concurrent connections with TX and RX buffers according to TX_SZ and RX_SZ, and packet metadata according to `M`.
Expand Down Expand Up @@ -49,7 +49,7 @@ impl<const N: usize, const TX_SZ: usize, const RX_SZ: usize, const M: usize> Udp
async fn bind(&self, local: SocketAddr) -> Result<Self::Socket<'_>, Self::Error> {
let mut socket = UdpSocket::new(self.stack, self.buffers)?;

socket.socket.bind(local)?;
socket.socket.bind(to_emb_bind_socket(local))?;

Ok(socket)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ impl<const N: usize, const TX_SZ: usize, const RX_SZ: usize, const M: usize> Udp
for UdpSocket<'_, N, TX_SZ, RX_SZ, M>
{
async fn send(&mut self, remote: SocketAddr, data: &[u8]) -> Result<(), Self::Error> {
self.socket.send_to(data, remote).await?;
self.socket.send_to(data, to_emb_socket(remote)).await?;

Ok(())
}
Expand Down

0 comments on commit d139bc1

Please sign in to comment.