Skip to content

Commit

Permalink
Merge pull request #1264 from microsoft/bugfix-catnap-socket
Browse files Browse the repository at this point in the history
[catnap] Don't Fail Silently When Cannot Set Socket Options
  • Loading branch information
ppenna committed May 7, 2024
2 parents 540c178 + 6f866e1 commit fc96ac0
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/rust/catnap/linux/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,39 +280,52 @@ impl NetworkTransport for SharedCatnapTransport {
Type::STREAM => Protocol::TCP,
Type::DGRAM => Protocol::UDP,
_ => {
return Err(Fail::new(libc::ENOTSUP, "socket type not supported"));
let cause: String = format!("socket type not supported: {:?}", typ);
error!("socket(): {}", cause);
return Err(Fail::new(libc::ENOTSUP, &cause));
},
};

// Attempts to shutdown a socket. If we fail, log a warn message and do not overwrite the original error.
let attempt_shutdown = |socket: Socket| {
if let Err(e) = socket.shutdown(Shutdown::Both) {
let cause: String = format!("cannot shutdown socket: {:?}", e);
warn!("socket(): {}", cause);
}
};

// Create socket.
let socket: Socket = match socket2::Socket::new(domain, typ, Some(protocol)) {
Ok(socket) => {
// Set socket options.
if let Err(e) = socket.set_reuse_address(true) {
let cause: String = format!("cannot set REUSE_ADDRESS option: {:?}", e);
socket.shutdown(Shutdown::Both)?;
error!("new(): {}", cause);
return Err(Fail::new(get_libc_err(e), &cause));
let errno: i32 = get_libc_err(e);
error!("socket(): {}", cause);
attempt_shutdown(socket);
return Err(Fail::new(errno, &cause));
}

let socket_fd = socket.as_raw_fd();
let flags = unsafe { libc::fcntl(socket_fd, libc::F_GETFL) };
if flags & libc::O_NONBLOCK == 0 {
if let Err(e) = socket.set_nonblocking(true) {
let cause: String = format!("cannot set NONBLOCKING option: {:?}", e);
socket.shutdown(Shutdown::Both)?;
error!("new(): {}", cause);
return Err(Fail::new(get_libc_err(e), &cause));
let errno: i32 = get_libc_err(e);
error!("socket(): {}", cause);
attempt_shutdown(socket);
return Err(Fail::new(errno, &cause));
}
}

// Set TCP socket options
if typ == Type::STREAM {
if let Err(e) = socket.set_nodelay(true) {
let cause: String = format!("cannot set TCP_NODELAY option: {:?}", e);
socket.shutdown(Shutdown::Both)?;
error!("new(): {}", cause);
return Err(Fail::new(get_libc_err(e), &cause));
let errno: i32 = get_libc_err(e);
error!("socket(): {}", cause);
attempt_shutdown(socket);
return Err(Fail::new(errno, &cause));
}
}

Expand Down

0 comments on commit fc96ac0

Please sign in to comment.