Skip to content

Commit

Permalink
Use trait for socket as suggested by @martinthomson
Browse files Browse the repository at this point in the history
  • Loading branch information
larseggert committed Dec 18, 2023
1 parent 669dd13 commit fb5434d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
10 changes: 4 additions & 6 deletions neqo-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use std::{
fs::{create_dir_all, File, OpenOptions},
io::{self, ErrorKind, Write},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs, UdpSocket},
os::fd::AsRawFd,
path::PathBuf,
process::exit,
rc::Rc,
Expand Down Expand Up @@ -393,7 +392,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, &dgram) {
eprintln!("UDP write error: {}", e);
client.close(Instant::now(), 0, e.to_string());
exiting = true;
Expand All @@ -419,7 +418,7 @@ fn process_loop(

let mut tos = 0;
let mut ttl = 0;
match recv_datagram(socket.as_raw_fd(), &mut buf[..], &mut tos, &mut ttl) {
match recv_datagram(socket, &mut buf[..], &mut tos, &mut ttl) {
Err(ref err)
if err.kind() == ErrorKind::WouldBlock || err.kind() == ErrorKind::Interrupted => {}
Err(err) => {
Expand Down Expand Up @@ -869,7 +868,6 @@ mod old {
fs::File,
io::{ErrorKind, Write},
net::{SocketAddr, UdpSocket},
os::fd::AsRawFd,
path::PathBuf,
process::exit,
rc::Rc,
Expand Down Expand Up @@ -1088,7 +1086,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, &dgram) {
eprintln!("UDP write error: {}", e);
client.close(Instant::now(), 0, e.to_string());
exiting = true;
Expand All @@ -1114,7 +1112,7 @@ mod old {

let mut tos = 0;
let mut ttl = 0;
match recv_datagram(socket.as_raw_fd(), &mut buf[..], &mut tos, &mut ttl) {
match recv_datagram(socket, &mut buf[..], &mut tos, &mut ttl) {
Err(err) => {
if err.kind() != ErrorKind::WouldBlock && err.kind() != ErrorKind::Interrupted {
eprintln!("UDP error: {}", err);
Expand Down
3 changes: 2 additions & 1 deletion neqo-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ lazy_static = "1.3.0"
qlog = "0.10.0"
time = {version = "=0.3.23", features = ["formatting"]}
nix = { git = "https://github.com/larseggert/nix.git", branch = "feat-tos", features = ["socket", "net", "uio"], optional = true }
mio = {version = "0.6.17", optional = true}

[features]
deny-warnings = []
socket = ["dep:nix"]
socket = ["dep:nix", "dep:mio"]

[target."cfg(windows)".dependencies.winapi]
version = "0.3"
Expand Down
35 changes: 25 additions & 10 deletions neqo-common/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use std::{
io::{self, Error, ErrorKind, IoSlice, IoSliceMut},
net::{SocketAddr, UdpSocket},
os::fd::AsRawFd,
};

use nix::{
Expand All @@ -25,6 +26,24 @@ use nix::{

use crate::Datagram;

#[allow(clippy::module_name_repetitions)]
pub trait SocketLike {
// don't use my name
fn raw_fd(&self) -> i32;
}

impl SocketLike for std::net::UdpSocket {
fn raw_fd(&self) -> i32 {
self.as_raw_fd()
}
}

impl SocketLike for mio::net::UdpSocket {
fn raw_fd(&self) -> i32 {
self.as_raw_fd()
}
}

/// Binds a UDP socket to the specified local address.
///
/// # Arguments
Expand Down Expand Up @@ -79,10 +98,6 @@ pub fn bind(local_addr: SocketAddr) -> io::Result<UdpSocket> {
Ok(socket)
}

fn to_sockaddr(addr: SocketAddr) -> SockaddrStorage {
SockaddrStorage::from(addr)
}

/// Send the UDP datagram on the specified socket.
///
/// # Arguments
Expand All @@ -98,7 +113,7 @@ fn to_sockaddr(addr: SocketAddr) -> SockaddrStorage {
///
/// Panics if the `sendmsg` call fails.
#[allow(clippy::missing_errors_doc)]
pub fn emit_datagram(fd: i32, d: &Datagram) -> io::Result<()> {
pub fn emit_datagram<S: SocketLike>(socket: &S, d: &Datagram) -> io::Result<()> {
let iov = [IoSlice::new(&d[..])];
let tos = i32::from(d.tos());
let ttl = i32::from(d.ttl());
Expand All @@ -107,11 +122,11 @@ pub fn emit_datagram(fd: i32, d: &Datagram) -> io::Result<()> {
SocketAddr::V6(..) => [Ipv6TClass(&tos), Ipv6HopLimit(&ttl)],
};
let sent = sendmsg(
fd,
socket.raw_fd(),
&iov,
&cmsgs,
MsgFlags::empty(),
Some(&to_sockaddr(d.destination())),
Some(&SockaddrStorage::from(d.destination())),
)
.unwrap();
if sent != d.len() {
Expand Down Expand Up @@ -154,8 +169,8 @@ fn to_socket_addr(addr: &SockaddrStorage) -> SocketAddr {
/// # Panics
///
/// Panics if the `recvmsg` call results in any result other than success, EAGAIN, or EINTR.
pub fn recv_datagram(
fd: i32,
pub fn recv_datagram<S: SocketLike>(
socket: &S,
buf: &mut [u8],
tos: &mut u8,
ttl: &mut u8,
Expand All @@ -164,7 +179,7 @@ pub fn recv_datagram(
let mut cmsg = cmsg_space!(u8, u8);
let flags = MsgFlags::empty();

match recvmsg::<SockaddrStorage>(fd, &mut iov, Some(&mut cmsg), flags) {
match recvmsg::<SockaddrStorage>(socket.raw_fd(), &mut iov, Some(&mut cmsg), flags) {
Err(e) if e == EAGAIN => Err(Error::new(ErrorKind::WouldBlock, e)),
Err(e) if e == EINTR => Err(Error::new(ErrorKind::Interrupted, e)),
Err(e) => {
Expand Down
9 changes: 4 additions & 5 deletions neqo-interop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use std::{
collections::HashSet,
mem,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs, UdpSocket},
os::fd::AsRawFd,
rc::Rc,
};
// use std::path::PathBuf;
Expand Down Expand Up @@ -110,7 +109,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, &dgram) {
eprintln!("UDP write error: {}", e);
continue;
}
Expand All @@ -132,7 +131,7 @@ fn process_loop(

let mut tos = 0;
let mut ttl = 0;
let (sz, _) = match recv_datagram(nctx.socket.as_raw_fd(), &mut buf[..], &mut tos, &mut ttl)
let (sz, _) = match recv_datagram(&nctx.socket, &mut buf[..], &mut tos, &mut ttl)
{
Ok(sz) => sz,
Err(e) => {
Expand Down Expand Up @@ -285,7 +284,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, &dgram).unwrap(),
Output::Callback(duration) => {
let delay = min(timer.check()?, duration);
nctx.socket.set_read_timeout(Some(delay)).unwrap();
Expand All @@ -302,7 +301,7 @@ fn process_loop_h3(

let mut tos = 0;
let mut ttl = 0;
let (sz, _) = match recv_datagram(nctx.socket.as_raw_fd(), &mut buf[..], &mut tos, &mut ttl)
let (sz, _) = match recv_datagram(&nctx.socket, &mut buf[..], &mut tos, &mut ttl)
{
Ok(sz) => sz,
Err(e) => {
Expand Down
5 changes: 2 additions & 3 deletions neqo-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use std::{
io::Read,
mem,
net::{SocketAddr, ToSocketAddrs},
os::fd::AsRawFd,
path::PathBuf,
process::exit,
rc::Rc,
Expand Down Expand Up @@ -555,7 +554,7 @@ fn read_dgram(
let mut tos = 0;
let mut ttl = 0;
let (sz, remote_addr) =
match recv_datagram(socket.as_raw_fd(), &mut buf[..], &mut tos, &mut ttl) {
match recv_datagram(socket, &mut buf[..], &mut tos, &mut ttl) {
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => return Ok(None),
Err(err) => {
eprintln!("UDP recv error: {:?}", err);
Expand Down Expand Up @@ -713,7 +712,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, &dgram) {
eprintln!("UDP write error: {}", e);
}
true
Expand Down

0 comments on commit fb5434d

Please sign in to comment.