Skip to content

Commit

Permalink
utils: refactor socket extension traits
Browse files Browse the repository at this point in the history
This commit creates the following hierarchy of socket extensions
traits:

SocketExt -> RawSocketExt
          -> UdpSocketExt
          -> TcpSocketExt -> TcpStreamExt

SocketExt, for instance, is implemented by all socket types used
in Holo. TcpSocketExt is implemented by TcpSocket, TcpStream and
TcpListener.

This restructuring promotes code reuse by consolidating extension
methods in a unified location (e.g. six `set_ipv4_tos` definitions
now unified).

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
  • Loading branch information
rwestphal committed Dec 16, 2023
1 parent 2748ba8 commit 4536a0b
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 339 deletions.
8 changes: 4 additions & 4 deletions holo-bfd/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::sync::Arc;

use holo_utils::bfd::PathType;
use holo_utils::ip::{AddressFamily, IpAddrExt};
use holo_utils::socket::{UdpSocket, UdpSocketExt, TTL_MAX};
use holo_utils::socket::{SocketExt, UdpSocket, UdpSocketExt, TTL_MAX};
use holo_utils::{capabilities, Sender};
use nix::sys::socket::{self, ControlMessageOwned};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -63,7 +63,7 @@ pub(crate) fn socket_rx(
}
AddressFamily::Ipv6 => {
socket.set_ipv6_pktinfo(true)?;
socket.set_min_hopcount_v6(TTL_MAX)?;
socket.set_ipv6_min_hopcount(TTL_MAX)?;
}
},
PathType::IpMultihop => {
Expand Down Expand Up @@ -121,11 +121,11 @@ pub(crate) fn socket_tx(
match af {
AddressFamily::Ipv4 => {
socket.set_ipv4_tos(libc::IPTOS_PREC_INTERNETCONTROL)?;
socket.set_ttl(ttl as u32)?;
socket.set_ipv4_ttl(ttl)?;
}
AddressFamily::Ipv6 => {
socket.set_ipv6_tclass(libc::IPTOS_PREC_INTERNETCONTROL)?;
socket.set_unicast_hops_v6(ttl as u32)?;
socket.set_ipv6_unicast_hops(ttl)?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion holo-ldp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::net::{IpAddr, Ipv4Addr};
use chrono::Utc;
use holo_utils::ip::IpNetworkKind;
use holo_utils::mpls::Label;
use holo_utils::socket::{TcpConnInfo, TcpStream, TcpStreamExt, TTL_MAX};
use holo_utils::socket::{SocketExt, TcpConnInfo, TcpStream, TTL_MAX};
use tracing::{debug_span, Span};

use crate::collections::{AdjacencyId, NeighborId, NeighborIndex};
Expand Down
2 changes: 1 addition & 1 deletion holo-ldp/src/neighbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use bitflags::bitflags;
use chrono::{DateTime, Utc};
use holo_utils::ip::AddressFamily;
use holo_utils::mpls::Label;
use holo_utils::socket::{TcpConnInfo, TcpListener, TcpListenerExt, TcpStream};
use holo_utils::socket::{TcpConnInfo, TcpListener, TcpSocketExt, TcpStream};
use holo_utils::task::{IntervalTask, Task, TimeoutTask};
use holo_utils::{Sender, UnboundedSender};
use ipnetwork::IpNetwork;
Expand Down
2 changes: 1 addition & 1 deletion holo-ldp/src/network/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::Arc;
use std::time::Duration;

use holo_utils::socket::{
OwnedReadHalf, OwnedWriteHalf, TcpConnInfo, TcpListener, TcpListenerExt,
OwnedReadHalf, OwnedWriteHalf, SocketExt, TcpConnInfo, TcpListener,
TcpSocket, TcpSocketExt, TcpStream, TcpStreamExt, TTL_MAX,
};
use holo_utils::task::TimeoutTask;
Expand Down
2 changes: 1 addition & 1 deletion holo-ldp/src/network/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::str::FromStr;
use std::sync::{Arc, LazyLock as Lazy};

use holo_utils::ip::{AddressFamily, IpAddrExt};
use holo_utils::socket::{UdpSocket, UdpSocketExt};
use holo_utils::socket::{SocketExt, UdpSocket, UdpSocketExt};
use holo_utils::{capabilities, Sender};
use tokio::sync::mpsc::error::SendError;

Expand Down
2 changes: 1 addition & 1 deletion holo-ospf/src/ospfv2/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::LazyLock as Lazy;

use bytes::{Buf, Bytes};
use holo_utils::capabilities;
use holo_utils::socket::{Socket, SocketExt};
use holo_utils::socket::{RawSocketExt, Socket};
use ipnetwork::Ipv4Network;
use nix::sys::socket::{self, SockaddrIn};
use socket2::InterfaceIndexOrAddress;
Expand Down
2 changes: 1 addition & 1 deletion holo-ospf/src/ospfv3/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::LazyLock as Lazy;

use bytes::Bytes;
use holo_utils::capabilities;
use holo_utils::socket::{Socket, SocketExt};
use holo_utils::socket::{RawSocketExt, Socket, SocketExt};
use ipnetwork::Ipv6Network;
use nix::sys::socket::{self, SockaddrIn6};

Expand Down
6 changes: 3 additions & 3 deletions holo-rip/src/ripng/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::str::FromStr;
use std::sync::LazyLock as Lazy;

use holo_utils::capabilities;
use holo_utils::socket::{UdpSocket, UdpSocketExt, TTL_MAX};
use holo_utils::socket::{SocketExt, UdpSocket, UdpSocketExt, TTL_MAX};

use crate::network::NetworkVersion;
use crate::version::Ripng;
Expand Down Expand Up @@ -44,8 +44,8 @@ impl NetworkVersion for Ripng {
// sent from the RIPng port (i.e. periodic advertisement
// or triggered update packets) must be examined to
// ensure that the hop count is 255".
socket.set_multicast_hopcount_v6(TTL_MAX)?;
socket.set_min_hopcount_v6(TTL_MAX)?;
socket.set_ipv6_multicast_hopcount(TTL_MAX)?;
socket.set_ipv6_min_hopcount(TTL_MAX)?;

Ok(socket)
}
Expand Down
2 changes: 1 addition & 1 deletion holo-rip/src/ripv2/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::str::FromStr;
use std::sync::LazyLock as Lazy;

use holo_utils::capabilities;
use holo_utils::socket::{UdpSocket, UdpSocketExt};
use holo_utils::socket::{SocketExt, UdpSocket, UdpSocketExt};

use crate::network::NetworkVersion;
use crate::version::Ripv2;
Expand Down
Loading

0 comments on commit 4536a0b

Please sign in to comment.