Skip to content

Commit

Permalink
TCP peer connect using domain name instead of ip.
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethKnudsen97 committed Aug 14, 2023
1 parent d0e8e5f commit 68d4a12
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
19 changes: 19 additions & 0 deletions ublox-short-range/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ pub enum DNSState {
Error(PingError),
}

pub struct DNSTable{
pub table: heapless::LinearMap<IpAddr , heapless::String<256>,16>
}

impl DNSTable {
fn new() -> Self{
Self { table: heapless::LinearMap::new() }
}

Check warning on line 63 in ublox-short-range/src/client.rs

View workflow job for this annotation

GitHub Actions / clippy

this could be a `const fn`

warning: this could be a `const fn` --> ublox-short-range/src/client.rs:61:5 | 61 | / fn new() -> Self{ 62 | | Self { table: heapless::LinearMap::new() } 63 | | } | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn = note: `-W clippy::missing-const-for-fn` implied by `-W clippy::nursery`
pub fn insert(&mut self, ip: IpAddr, hostname: heapless::String<256>,) -> Result<(), ()>{
self.table.insert(ip, hostname).ok();
Ok(())
}

Check warning on line 67 in ublox-short-range/src/client.rs

View workflow job for this annotation

GitHub Actions / clippy

this function's return value is unnecessary

warning: this function's return value is unnecessary --> ublox-short-range/src/client.rs:64:5 | 64 | / pub fn insert(&mut self, ip: IpAddr, hostname: heapless::String<256>,) -> Result<(), ()>{ 65 | | self.table.insert(ip, hostname).ok(); 66 | | Ok(()) 67 | | } | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_wraps = note: `-W clippy::unnecessary-wraps` implied by `-W clippy::pedantic` help: remove the return type... | 64 | pub fn insert(&mut self, ip: IpAddr, hostname: heapless::String<256>,) -> Result<(), ()>{ | ~~~~~~~~~~~~~~ help: ...and then remove returned values | 66 - Ok(()) 66 + |
pub fn get_hostname_by_ip(&self, ip: &IpAddr) -> Option<&heapless::String<256>>{
self.table.get(ip)
}
}

#[derive(PartialEq, Clone, Default)]

Check warning on line 73 in ublox-short-range/src/client.rs

View workflow job for this annotation

GitHub Actions / clippy

you are deriving `PartialEq` and can implement `Eq`

warning: you are deriving `PartialEq` and can implement `Eq` --> ublox-short-range/src/client.rs:73:10 | 73 | #[derive(PartialEq, Clone, Default)] | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derive_partial_eq_without_eq
pub struct SecurityCredentials {
pub ca_cert_name: Option<heapless::String<16>>,
Expand Down Expand Up @@ -84,6 +101,7 @@ where
pub(crate) module_started: bool,
pub(crate) initialized: bool,
serial_mode: SerialMode,
pub dns_table: DNSTable,
pub(crate) wifi_connection: Option<WifiConnection>,
pub(crate) wifi_config_active_on_startup: Option<u8>,
pub(crate) client: C,
Expand All @@ -109,6 +127,7 @@ where
module_started: false,
initialized: false,
serial_mode: SerialMode::Cmd,
dns_table: DNSTable::new(),
wifi_connection: None,
wifi_config_active_on_startup: None,
client,
Expand Down
6 changes: 5 additions & 1 deletion ublox-short-range/src/wifi/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ where
}

match self.dns_state {
DNSState::Resolved(ip) => Ok(ip),
DNSState::Resolved(ip) => {
//Insert hostname and ip into dns table
self.dns_table.insert(ip, heapless::String::from(hostname)).ok();
Ok(ip)
},
_ => Err(nb::Error::Other(Error::Illegal)),
}
}
Expand Down
18 changes: 13 additions & 5 deletions ublox-short-range/src/wifi/tcp_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ where
if sockets.len() >= sockets.capacity() {
// Check if there are any sockets closed by remote, and close it
// if it has exceeded its timeout, in order to recycle it.
if sockets.recycle(self.timer.now()) {
if !sockets.recycle(self.timer.now()) {
return Err(Error::SocketSetFull);
}
}
Expand Down Expand Up @@ -65,11 +65,19 @@ where
defmt::debug!("[TCP] Connect socket");
self.connected_to_network().map_err(|_| Error::Illegal)?;

let url = PeerUrlBuilder::new()
.address(&remote)
.creds(self.security_credentials.clone())
let mut url = PeerUrlBuilder::new();

//Connect with hostname if seen before
if let Some(hostname) = self.dns_table.get_hostname_by_ip(&remote.ip()){
url.hostname(hostname).port(remote.port());
}else {
url.address(&remote);
}

let url = url.creds(self.security_credentials.clone())
.tcp()
.map_err(|_| Error::Unaddressable)?;
defmt::debug!("[TCP] Connecting socket: {:?} to url: {=str}", socket, url);

// If no socket is found we stop here
let mut tcp = self
Expand Down Expand Up @@ -101,7 +109,7 @@ where
}
}

defmt::trace!("[TCP] Connecting socket: {:?} to url: {=str}", socket, url);
defmt::debug!("[TCP] Connecting socket: {:?} to url: {=str}", socket, url);

// TODO: Timeout?
// TODO: Fix the fact that it doesen't wait for both connect messages
Expand Down

0 comments on commit 68d4a12

Please sign in to comment.