Skip to content

Commit

Permalink
Make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
larseggert committed Dec 18, 2023
1 parent becbe41 commit 669dd13
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 16 deletions.
4 changes: 2 additions & 2 deletions neqo-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ fn process_loop(
loop {
match client.process_output(Instant::now()) {
Output::Datagram(dgram) => {
if let Err(e) = emit_datagram(socket.as_raw_fd(), dgram) {
if let Err(e) = emit_datagram(socket.as_raw_fd(), &dgram) {
eprintln!("UDP write error: {}", e);
client.close(Instant::now(), 0, e.to_string());
exiting = true;
Expand Down Expand Up @@ -1088,7 +1088,7 @@ mod old {
loop {
match client.process_output(Instant::now()) {
Output::Datagram(dgram) => {
if let Err(e) = emit_datagram(socket.as_raw_fd(), dgram) {
if let Err(e) = emit_datagram(socket.as_raw_fd(), &dgram) {
eprintln!("UDP write error: {}", e);
client.close(Instant::now(), 0, e.to_string());
exiting = true;
Expand Down
77 changes: 66 additions & 11 deletions neqo-common/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use std::{
io::{self, Error, ErrorKind, IoSlice, IoSliceMut},
net::{SocketAddr, UdpSocket},
process::exit,
};

use nix::{
Expand All @@ -26,12 +25,31 @@ use nix::{

use crate::Datagram;

// Bind a UDP socket and set some default socket options.
/// Binds a UDP socket to the specified local address.
///
/// # Arguments
///
/// * `local_addr` - The local address to bind the socket to.
///
/// # Returns
///
/// The bound UDP socket.
///
/// # Panics
///
/// Panics if the UDP socket fails to bind to the specified local address or if various
/// socket options cannot be set.
///
/// # Notes
///
/// This function binds the UDP socket to the specified local address and performs additional
/// configuration on the socket, such as setting socket options to request TOS and TTL
/// information for incoming packets.
#[allow(clippy::missing_errors_doc)]
pub fn bind(local_addr: SocketAddr) -> io::Result<UdpSocket> {
let socket = match UdpSocket::bind(local_addr) {
Err(e) => {
eprintln!("Unable to bind UDP socket: {e}");
exit(1)
panic!("Unable to bind UDP socket: {}", e);
}
Ok(s) => {
// Don't let the host stack or network path fragment our IP packets
Expand Down Expand Up @@ -65,7 +83,22 @@ fn to_sockaddr(addr: SocketAddr) -> SockaddrStorage {
SockaddrStorage::from(addr)
}

pub fn emit_datagram(fd: i32, d: Datagram) -> io::Result<()> {
/// Send the UDP datagram on the specified socket.
///
/// # Arguments
///
/// * `fd` - The UDP socket to send the datagram on.
/// * `d` - The datagram to send.
///
/// # Returns
///
/// An `io::Result` indicating whether the datagram was sent successfully.
///
/// # Panics
///
/// Panics if the `sendmsg` call fails.
#[allow(clippy::missing_errors_doc)]
pub fn emit_datagram(fd: i32, d: &Datagram) -> io::Result<()> {
let iov = [IoSlice::new(&d[..])];
let tos = i32::from(d.tos());
let ttl = i32::from(d.ttl());
Expand Down Expand Up @@ -101,6 +134,26 @@ fn to_socket_addr(addr: &SockaddrStorage) -> SocketAddr {
}
}

/// Receive a UDP datagram on the specified socket.
///
/// # Arguments
///
/// * `fd` - The UDP socket to receive the datagram on.
/// * `buf` - The buffer to receive the datagram into.
/// * `tos` - The type-of-service (TOS) or traffic class (TC) value of the received datagram.
/// * `ttl` - The time-to-live (TTL) or hop limit (HL) value of the received datagram.
///
/// # Returns
///
/// An `io::Result` indicating the size of the received datagram.
///
/// # Errors
///
/// Returns an `io::ErrorKind::WouldBlock` error if the `recvmsg` call would block.
///
/// # Panics
///
/// Panics if the `recvmsg` call results in any result other than success, EAGAIN, or EINTR.
pub fn recv_datagram(
fd: i32,
buf: &mut [u8],
Expand All @@ -115,16 +168,18 @@ pub fn recv_datagram(
Err(e) if e == EAGAIN => Err(Error::new(ErrorKind::WouldBlock, e)),
Err(e) if e == EINTR => Err(Error::new(ErrorKind::Interrupted, e)),
Err(e) => {
eprintln!("UDP error: {}", e);
exit(1)
panic!("UDP error: {}", e);
}
Ok(res) => {
for cmsg in res.cmsgs() {
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
match cmsg {
ControlMessageOwned::IpTos(t) => *tos = t as u8,
ControlMessageOwned::Ipv6TClass(t) => *tos = t as u8,
ControlMessageOwned::IpTtl(t) => *ttl = t as u8,
ControlMessageOwned::Ipv6HopLimit(t) => *ttl = t as u8,
ControlMessageOwned::IpTos(t) | ControlMessageOwned::Ipv6TClass(t) => {
*tos = t as u8;
}
ControlMessageOwned::IpTtl(t) | ControlMessageOwned::Ipv6HopLimit(t) => {
*ttl = t as u8;
}
_ => unreachable!(),
};
}
Expand Down
4 changes: 2 additions & 2 deletions neqo-interop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn process_loop(
match output {
Output::Datagram(dgram) => {
let dgram = handler.rewrite_out(&dgram).unwrap_or(dgram);
if let Err(e) = emit_datagram(nctx.socket.as_raw_fd(), dgram) {
if let Err(e) = emit_datagram(nctx.socket.as_raw_fd(), &dgram) {
eprintln!("UDP write error: {}", e);
continue;
}
Expand Down Expand Up @@ -285,7 +285,7 @@ fn process_loop_h3(
loop {
let output = handler.h3.conn().process_output(Instant::now());
match output {
Output::Datagram(dgram) => emit_datagram(nctx.socket.as_raw_fd(), dgram).unwrap(),
Output::Datagram(dgram) => emit_datagram(nctx.socket.as_raw_fd(), &dgram).unwrap(),
Output::Callback(duration) => {
let delay = min(timer.check()?, duration);
nctx.socket.set_read_timeout(Some(delay)).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion neqo-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ impl ServersRunner {
match self.server.process(dgram, self.args.now()) {
Output::Datagram(dgram) => {
let socket = self.find_socket(dgram.source());
if let Err(e) = emit_datagram(socket.as_raw_fd(), dgram) {
if let Err(e) = emit_datagram(socket.as_raw_fd(), &dgram) {
eprintln!("UDP write error: {}", e);
}
true
Expand Down

0 comments on commit 669dd13

Please sign in to comment.