Skip to content

Commit 40d3381

Browse files
authored
fix: don't panic on IP address resolution failure (#42)
* don't panic on failure to resolve IP address * cover additional Error variant in Clone impl * fmt
1 parent 253aefc commit 40d3381

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/client.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,24 @@ impl Client {
202202
.to_socket_addrs()
203203
.ctx("converting address to SocketAddr")?
204204
.next()
205-
.expect("at least one SocketAddr");
205+
.ok_or(Error::Other("no destination address could be resolved"))?;
206+
206207
let mut socket = if let Some(timeout) = timeouts.connect {
207-
TcpStream::connect_timeout(&addr, timeout).ctx("connecting TCP socket with timeout")?
208+
TcpStream::connect_timeout(&addr, timeout)
209+
.ctx("establishing connetion to remote ADS router (with timeout)")?
208210
} else {
209-
TcpStream::connect(addr).ctx("connecting TCP socket")?
211+
TcpStream::connect(addr).ctx("establishing connection to remote ADS router")?
210212
};
211213

212214
// Disable Nagle to ensure small requests are sent promptly; we're
213215
// playing ping-pong with request reply, so no pipelining.
214-
socket.set_nodelay(true).ctx("setting NODELAY")?;
215-
socket.set_write_timeout(timeouts.write).ctx("setting write timeout")?;
216+
socket.set_nodelay(true).ctx("setting client socket NODELAY")?;
217+
socket
218+
.set_write_timeout(timeouts.write)
219+
.ctx("setting client socket write timeout")?;
220+
socket
221+
.set_read_timeout(timeouts.read)
222+
.ctx("setting client socket read timeout")?;
216223

217224
// Determine our source AMS address. If it's not specified, try to use
218225
// the socket's local IPv4 address, if it's IPv6 (not sure if Beckhoff

src/errors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ pub enum Error {
2525
/// Error occurred during IO synchronization
2626
#[error("failed during synchronization of an Ads request/response: {0} ({1})")]
2727
IoSync(&'static str, &'static str, u32),
28+
29+
/// An unspecified catch-all error
30+
#[error("an error occured: {0}")]
31+
Other(&'static str),
2832
}
2933

3034
impl Clone for Error {
@@ -36,6 +40,7 @@ impl Clone for Error {
3640
Reply(ctx, e, i) => Reply(ctx, e, *i),
3741
Overflow(e) => Overflow(*e),
3842
IoSync(ctx, e, i) => IoSync(ctx, e, *i),
43+
Other(ctx) => Other(ctx),
3944
}
4045
}
4146
}

0 commit comments

Comments
 (0)