Skip to content

Commit 930c9a9

Browse files
Ammar ZuberiThomasdezeeuw
Ammar Zuberi
authored andcommitted
Additional Linux socket options
Added support for setting/getting value of: * TCP Maximum Segment Size (TCP_MAXSEG) * Socket Mark (SO_MARK) Signed-off-by: Ammar Zuberi <[email protected]>
1 parent 06a2510 commit 930c9a9

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/socket.rs

+43
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,49 @@ impl Socket {
369369
self.inner.set_ttl(ttl)
370370
}
371371

372+
/// Gets the value of the `TCP_MAXSEG` option on this socket.
373+
///
374+
/// The `TCP_MAXSEG` option denotes the TCP Maximum Segment
375+
/// Size and is only available on TCP sockets.
376+
#[cfg(unix)]
377+
pub fn mss(&self) -> io::Result<u32> {
378+
self.inner.mss()
379+
}
380+
381+
/// Sets the value of the `TCP_MAXSEG` option on this socket.
382+
///
383+
/// The `TCP_MAXSEG` option denotes the TCP Maximum Segment
384+
/// Size and is only available on TCP sockets.
385+
#[cfg(unix)]
386+
pub fn set_mss(&self, mss: u32) -> io::Result<()> {
387+
self.inner.set_mss(mss)
388+
}
389+
390+
/// Gets the value for the `SO_MARK` option on this socket.
391+
///
392+
/// This value gets the socket mark field for each packet sent through
393+
/// this socket.
394+
///
395+
/// This function is only available on Linux and requires the
396+
/// `CAP_NET_ADMIN` capability.
397+
#[cfg(target_os = "linux")]
398+
pub fn mark(&self) -> io::Result<u32> {
399+
self.inner.mark()
400+
}
401+
402+
/// Sets the value for the `SO_MARK` option on this socket.
403+
///
404+
/// This value sets the socket mark field for each packet sent through
405+
/// this socket. Changing the mark can be used for mark-based routing
406+
/// without netfilter or for packet filtering.
407+
///
408+
/// This function is only available on Linux and requires the
409+
/// `CAP_NET_ADMIN` capability.
410+
#[cfg(target_os = "linux")]
411+
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
412+
self.inner.set_mark(mark)
413+
}
414+
372415
/// Gets the value of the `IPV6_UNICAST_HOPS` option for this socket.
373416
///
374417
/// Specifies the hop limit for ipv6 unicast packets

src/sys/unix.rs

+24
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,30 @@ impl Socket {
534534
unsafe { self.setsockopt(libc::IPPROTO_IP, libc::IP_TTL, ttl as c_int) }
535535
}
536536

537+
pub fn mss(&self) -> io::Result<u32> {
538+
unsafe {
539+
let raw: c_int = self.getsockopt(libc::IPPROTO_TCP, libc::TCP_MAXSEG)?;
540+
Ok(raw as u32)
541+
}
542+
}
543+
544+
pub fn set_mss(&self, mss: u32) -> io::Result<()> {
545+
unsafe { self.setsockopt(libc::IPPROTO_TCP, libc::TCP_MAXSEG, mss as c_int) }
546+
}
547+
548+
#[cfg(target_os = "linux")]
549+
pub fn mark(&self) -> io::Result<u32> {
550+
unsafe {
551+
let raw: c_int = self.getsockopt(libc::SOL_SOCKET, libc::SO_MARK)?;
552+
Ok(raw as u32)
553+
}
554+
}
555+
556+
#[cfg(target_os = "linux")]
557+
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
558+
unsafe { self.setsockopt(libc::SOL_SOCKET, libc::SO_MARK, mark as c_int) }
559+
}
560+
537561
pub fn unicast_hops_v6(&self) -> io::Result<u32> {
538562
unsafe {
539563
let raw: c_int = self.getsockopt(libc::IPPROTO_IPV6, libc::IPV6_UNICAST_HOPS)?;

0 commit comments

Comments
 (0)