Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make growing types non exhaustive #52

Merged
merged 4 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/statsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ mod real {
const PREFIX: &str = "tcp2udp";

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// Failed to create + bind the statsd UDP socket.
BindUdpSocket(std::io::Error),
Expand Down
43 changes: 41 additions & 2 deletions src/tcp2udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ use tokio::time::sleep;
#[path = "statsd.rs"]
mod statsd;

#[derive(Debug)]
/// Settings for a tcp2udp session. This is the argument to [`run`] to
/// describe how the forwarding from TCP -> UDP should be set up.
///
/// This struct is `non_exhaustive` in order to allow adding more optional fields without
/// being considered breaking changes. So you need to create an instance via [`Options::new`].
#[derive(Debug, Clone)]
#[cfg_attr(feature = "clap", derive(clap::Parser))]
#[cfg_attr(feature = "clap", group(skip))]
#[non_exhaustive]
pub struct Options {
/// The IP and TCP port(s) to listen to for incoming traffic from udp2tcp.
/// Supports binding multiple TCP sockets.
Expand All @@ -39,11 +45,44 @@ pub struct Options {
#[cfg(feature = "statsd")]
/// Host to send statsd metrics to.
#[cfg_attr(feature = "clap", clap(long))]
statsd_host: Option<SocketAddr>,
pub statsd_host: Option<SocketAddr>,
}

impl Options {
/// Creates a new [`Options`] with all mandatory fields set to the passed arguments.
/// All optional values are set to their default values. They can later be set, since
/// they are public.
///
/// # Examples
///
/// ```
/// # use std::net::{IpAddr, Ipv4Addr, SocketAddrV4, SocketAddr};
///
/// let mut options = udp_over_tcp::tcp2udp::Options::new(
/// // Listen on 127.0.0.1:1234/TCP
/// vec![SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 1234))],
/// // Forward to 192.0.2.15:5001/UDP
/// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 0, 2, 15), 5001)),
/// );
///
/// // Bind the local UDP socket (used to send to 192.0.2.15:5001/UDP) to the loopback interface
/// options.udp_bind_ip = Some(IpAddr::V4(Ipv4Addr::LOCALHOST));
/// ```
pub fn new(tcp_listen_addrs: Vec<SocketAddr>, udp_forward_addr: SocketAddr) -> Self {
Options {
tcp_listen_addrs,
udp_forward_addr,
udp_bind_ip: None,
tcp_options: Default::default(),
#[cfg(feature = "statsd")]
statsd_host: None,
}
}
}

/// Error returned from [`run`] if something goes wrong.
#[derive(Debug)]
#[non_exhaustive]
pub enum Tcp2UdpError {
/// No TCP listen addresses given in the `Options`.
NoTcpListenAddrs,
Expand Down
2 changes: 2 additions & 0 deletions src/tcp_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::net::{TcpSocket, TcpStream};
/// Options to apply to the TCP socket involved in the tunneling.
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "clap", derive(clap::Parser))]
#[non_exhaustive]
pub struct TcpOptions {
/// If given, sets the SO_RCVBUF option on the TCP socket to the given number of bytes.
/// Changes the size of the operating system's receive buffer associated with the socket.
Expand Down Expand Up @@ -35,6 +36,7 @@ pub struct TcpOptions {
}

#[derive(Debug)]
#[non_exhaustive]
pub enum ApplyTcpOptionsError {
/// Failed to get/set TCP_RCVBUF
RecvBuffer(io::Error),
Expand Down
1 change: 1 addition & 0 deletions src/udp2tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tokio::net::{TcpSocket, UdpSocket};
use std::os::unix::io::{AsRawFd, RawFd};

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// Failed to create the TCP socket.
CreateTcpSocket(io::Error),
Expand Down
Loading