Skip to content

Commit f494986

Browse files
Simplify socket disconnect code
1 parent 29e5734 commit f494986

4 files changed

Lines changed: 20 additions & 57 deletions

File tree

src/net/payload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub(crate) fn handle_payload_result(
195195
) {
196196
Ok(v) => v,
197197
Err(e) => {
198-
log_warn_dir!(worker_id, c2u, "udp_disconnect failed: {}", e);
198+
log_warn_dir!(worker_id, c2u, "disconnect_socket failed: {}", e);
199199
prev_ver
200200
}
201201
};

src/net/sock_mgr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cli::SupportedProtocol;
22
use crate::net::socket::{
3-
family_changed, make_socket, make_upstream_socket_for, resolve_first, udp_disconnect,
3+
disconnect_socket, family_changed, make_socket, make_upstream_socket_for, resolve_first,
44
};
55
use socket2::{SockAddr, Socket};
66

@@ -131,7 +131,7 @@ impl SocketManager {
131131
self.publish_version(true);
132132
if !connected {
133133
// Use a clone because the original may not be marked as connected
134-
udp_disconnect(&client_sock_guard.try_clone()?)?;
134+
disconnect_socket(&client_sock_guard.try_clone()?)?;
135135
}
136136
Ok(prev_ver + 1)
137137
}

src/net/socket.rs

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -147,52 +147,10 @@ pub const fn family_changed(a: SocketAddr, b: SocketAddr) -> bool {
147147
/// connecting to an invalid address (NULL or AF_UNSPEC). The error
148148
/// EAFNOSUPPORT may be harmlessly returned; consider it success.
149149
#[cfg(unix)]
150-
pub fn udp_disconnect(sock: &Socket) -> io::Result<()> {
150+
pub fn disconnect_socket(sock: &Socket) -> io::Result<()> {
151151
let fd = sock.as_raw_fd();
152152

153-
// Interpret connect() rc correctly per platform.
154-
#[inline]
155-
#[cfg(any(
156-
target_os = "macos",
157-
target_os = "ios",
158-
target_os = "freebsd",
159-
target_os = "openbsd",
160-
target_os = "netbsd",
161-
target_os = "dragonfly",
162-
))]
163-
fn ok_or_eafnosupport(rc: i32) -> io::Result<()> {
164-
if rc == 0 {
165-
Ok(())
166-
} else {
167-
let err = io::Error::last_os_error();
168-
if err.raw_os_error() == Some(libc::EAFNOSUPPORT) {
169-
// macOS/*BSD man page: harmless when disconnecting UDP
170-
Ok(())
171-
} else {
172-
Err(err)
173-
}
174-
}
175-
}
176-
177-
// On non-BSD Unix (Linux/Android), do NOT ignore EAFNOSUPPORT.
178-
#[inline]
179-
#[cfg(not(any(
180-
target_os = "macos",
181-
target_os = "ios",
182-
target_os = "freebsd",
183-
target_os = "openbsd",
184-
target_os = "netbsd",
185-
target_os = "dragonfly",
186-
)))]
187-
fn ok_or_eafnosupport(rc: i32) -> io::Result<()> {
188-
if rc == 0 {
189-
Ok(())
190-
} else {
191-
Err(io::Error::last_os_error())
192-
}
193-
}
194-
195-
// --- macOS / iOS / *BSD: try AF_UNSPEC first, then NULL ---
153+
// --- macOS / iOS / *BSD: AF_UNSPEC is sufficient. ---
196154
#[cfg(any(
197155
target_os = "macos",
198156
target_os = "ios",
@@ -215,13 +173,15 @@ pub fn udp_disconnect(sock: &Socket) -> io::Result<()> {
215173
addr.sa_len as libc::socklen_t,
216174
)
217175
};
218-
if ok_or_eafnosupport(rc).is_ok() {
176+
if rc == 0 {
219177
return Ok(());
220178
}
221-
222-
// Fallback: connect(fd, NULL, 0)
223-
let rc2 = unsafe { libc::connect(fd, std::ptr::null(), 0) };
224-
return ok_or_eafnosupport(rc2);
179+
let err = io::Error::last_os_error();
180+
if err.raw_os_error() == Some(libc::EAFNOSUPPORT) {
181+
// macOS/*BSD man page: harmless when disconnecting UDP
182+
return Ok(());
183+
}
184+
return Err(err);
225185
}
226186

227187
// --- Linux/Android: AF_UNSPEC is the standard way; no sa_len field. ---
@@ -245,13 +205,16 @@ pub fn udp_disconnect(sock: &Socket) -> io::Result<()> {
245205
std::mem::size_of::<libc::sockaddr>() as libc::socklen_t,
246206
)
247207
};
248-
return ok_or_eafnosupport(rc);
208+
if rc == 0 {
209+
return Ok(());
210+
}
211+
return Err(io::Error::last_os_error());
249212
}
250213
}
251214

252215
/// Windows: disconnect a UDP socket by connecting to INADDR_ANY/IN6ADDR_ANY and port 0.
253216
#[cfg(windows)]
254-
pub fn udp_disconnect(sock: &Socket) -> io::Result<()> {
217+
pub fn disconnect_socket(sock: &Socket) -> io::Result<()> {
255218
let local = sock.local_addr()?;
256219
let any_std = match local.as_socket() {
257220
Some(SocketAddr::V6(_)) => SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0),
@@ -264,9 +227,9 @@ pub fn udp_disconnect(sock: &Socket) -> io::Result<()> {
264227

265228
/// Fallback: not supported on this platform.
266229
#[cfg(all(not(unix), not(windows)))]
267-
pub fn udp_disconnect(_sock: &Socket) -> io::Result<()> {
230+
pub fn disconnect_socket(_sock: &Socket) -> io::Result<()> {
268231
Err(io::Error::new(
269232
io::ErrorKind::Other,
270-
"Function udp_disconnect is not supported on this OS",
233+
"Function disconnect_socket is not supported on this OS",
271234
))
272235
}

src/worker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ pub fn run_watchdog_thread(
177177
{
178178
Ok(v) => v,
179179
Err(e) => {
180-
log_error!("watchdog udp_disconnect failed: {}", e);
180+
log_error!("watchdog disconnect_socket failed: {}", e);
181181
exit_code_set.store((1 << 31) | 1, AtomOrdering::Relaxed);
182182
return;
183183
}

0 commit comments

Comments
 (0)