Skip to content

Commit

Permalink
Additional Linux socket options
Browse files Browse the repository at this point in the history
Added support for setting/getting value of:
* TCP Maximum Segment Size (TCP_MAXSEG)
* Socket Mark (SO_MARK)

Signed-off-by: Ammar Zuberi <ammar@cloudflare.com>
  • Loading branch information
Ammar Zuberi authored and Thomasdezeeuw committed Nov 20, 2020
1 parent 06a2510 commit 930c9a9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,49 @@ impl Socket {
self.inner.set_ttl(ttl)
}

/// Gets the value of the `TCP_MAXSEG` option on this socket.
///
/// The `TCP_MAXSEG` option denotes the TCP Maximum Segment
/// Size and is only available on TCP sockets.
#[cfg(unix)]
pub fn mss(&self) -> io::Result<u32> {
self.inner.mss()
}

/// Sets the value of the `TCP_MAXSEG` option on this socket.
///
/// The `TCP_MAXSEG` option denotes the TCP Maximum Segment
/// Size and is only available on TCP sockets.
#[cfg(unix)]
pub fn set_mss(&self, mss: u32) -> io::Result<()> {
self.inner.set_mss(mss)
}

/// Gets the value for the `SO_MARK` option on this socket.
///
/// This value gets the socket mark field for each packet sent through
/// this socket.
///
/// This function is only available on Linux and requires the
/// `CAP_NET_ADMIN` capability.
#[cfg(target_os = "linux")]
pub fn mark(&self) -> io::Result<u32> {
self.inner.mark()
}

/// Sets the value for the `SO_MARK` option on this socket.
///
/// This value sets the socket mark field for each packet sent through
/// this socket. Changing the mark can be used for mark-based routing
/// without netfilter or for packet filtering.
///
/// This function is only available on Linux and requires the
/// `CAP_NET_ADMIN` capability.
#[cfg(target_os = "linux")]
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
self.inner.set_mark(mark)
}

/// Gets the value of the `IPV6_UNICAST_HOPS` option for this socket.
///
/// Specifies the hop limit for ipv6 unicast packets
Expand Down
24 changes: 24 additions & 0 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,30 @@ impl Socket {
unsafe { self.setsockopt(libc::IPPROTO_IP, libc::IP_TTL, ttl as c_int) }
}

pub fn mss(&self) -> io::Result<u32> {
unsafe {
let raw: c_int = self.getsockopt(libc::IPPROTO_TCP, libc::TCP_MAXSEG)?;
Ok(raw as u32)
}
}

pub fn set_mss(&self, mss: u32) -> io::Result<()> {
unsafe { self.setsockopt(libc::IPPROTO_TCP, libc::TCP_MAXSEG, mss as c_int) }
}

#[cfg(target_os = "linux")]
pub fn mark(&self) -> io::Result<u32> {
unsafe {
let raw: c_int = self.getsockopt(libc::SOL_SOCKET, libc::SO_MARK)?;
Ok(raw as u32)
}
}

#[cfg(target_os = "linux")]
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
unsafe { self.setsockopt(libc::SOL_SOCKET, libc::SO_MARK, mark as c_int) }
}

pub fn unicast_hops_v6(&self) -> io::Result<u32> {
unsafe {
let raw: c_int = self.getsockopt(libc::IPPROTO_IPV6, libc::IPV6_UNICAST_HOPS)?;
Expand Down

0 comments on commit 930c9a9

Please sign in to comment.