Skip to content

Commit 2440729

Browse files
committed
Add ip(v6)_checksum socket options
1 parent 3832726 commit 2440729

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed

src/backend/libc/net/sockopt.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,42 @@ pub(crate) fn ipv6_freebind(fd: BorrowedFd<'_>) -> io::Result<bool> {
873873
getsockopt(fd, c::IPPROTO_IPV6, c::IPV6_FREEBIND).map(to_bool)
874874
}
875875

876+
#[cfg(any(linux_like, target_os = "cygwin", target_os = "fuchsia",))]
877+
#[inline]
878+
pub(crate) fn set_ip_checksum(fd: BorrowedFd<'_>, value: u32) -> io::Result<()> {
879+
setsockopt(fd, c::IPPROTO_IP, c::IP_CHECKSUM, value)
880+
}
881+
882+
#[cfg(any(linux_like, target_os = "cygwin", target_os = "fuchsia",))]
883+
#[inline]
884+
pub(crate) fn ip_checksum(fd: BorrowedFd<'_>) -> io::Result<u32> {
885+
getsockopt(fd, c::IPPROTO_IP, c::IP_CHECKSUM)
886+
}
887+
888+
#[cfg(any(
889+
apple,
890+
linux_like,
891+
target_os = "cygwin",
892+
target_os = "freebsd",
893+
target_os = "fuchsia",
894+
))]
895+
#[inline]
896+
pub(crate) fn set_ipv6_checksum(fd: BorrowedFd<'_>, value: u32) -> io::Result<()> {
897+
setsockopt(fd, c::IPPROTO_IPV6, c::IPV6_CHECKSUM, value)
898+
}
899+
900+
#[cfg(any(
901+
apple,
902+
linux_like,
903+
target_os = "cygwin",
904+
target_os = "freebsd",
905+
target_os = "fuchsia",
906+
))]
907+
#[inline]
908+
pub(crate) fn ipv6_checksum(fd: BorrowedFd<'_>) -> io::Result<u32> {
909+
getsockopt(fd, c::IPPROTO_IPV6, c::IPV6_CHECKSUM)
910+
}
911+
876912
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
877913
#[inline]
878914
pub(crate) fn ip_original_dst(fd: BorrowedFd<'_>) -> io::Result<SocketAddrV4> {

src/backend/linux_raw/c.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ pub(crate) use linux_raw_sys::{
8686
AF_LLC, AF_NETBEUI, AF_NETLINK, AF_NETROM, AF_PACKET, AF_PHONET, AF_PPPOX, AF_RDS, AF_ROSE,
8787
AF_RXRPC, AF_SECURITY, AF_SNA, AF_TIPC, AF_UNIX, AF_UNSPEC, AF_VSOCK, AF_WANPIPE, AF_X25,
8888
AF_XDP, IP6T_SO_ORIGINAL_DST, IPPROTO_FRAGMENT, IPPROTO_ICMPV6, IPPROTO_MH,
89-
IPPROTO_ROUTING, IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_FREEBIND,
89+
IPPROTO_ROUTING, IPV6_ADD_MEMBERSHIP, IPV6_CHECKSUM, IPV6_DROP_MEMBERSHIP, IPV6_FREEBIND,
9090
IPV6_MULTICAST_HOPS, IPV6_MULTICAST_LOOP, IPV6_PMTUDISC_DO, IPV6_PMTUDISC_DONT,
9191
IPV6_PMTUDISC_INTERFACE, IPV6_PMTUDISC_OMIT, IPV6_PMTUDISC_PROBE, IPV6_PMTUDISC_WANT,
9292
IPV6_RECVTCLASS, IPV6_TCLASS, IPV6_UNICAST_HOPS, IPV6_V6ONLY, IP_ADD_MEMBERSHIP,
93-
IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP, IP_FREEBIND,
94-
IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_PMTUDISC_DO, IP_PMTUDISC_DONT,
93+
IP_ADD_SOURCE_MEMBERSHIP, IP_CHECKSUM, IP_DROP_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP,
94+
IP_FREEBIND, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_PMTUDISC_DO, IP_PMTUDISC_DONT,
9595
IP_PMTUDISC_INTERFACE, IP_PMTUDISC_OMIT, IP_PMTUDISC_PROBE, IP_PMTUDISC_WANT, IP_RECVTOS,
9696
IP_TOS, IP_TTL, MSG_CMSG_CLOEXEC, MSG_CONFIRM, MSG_CTRUNC, MSG_DONTROUTE, MSG_DONTWAIT,
9797
MSG_EOR, MSG_ERRQUEUE, MSG_MORE, MSG_NOSIGNAL, MSG_OOB, MSG_PEEK, MSG_TRUNC, MSG_WAITALL,

src/backend/linux_raw/net/sockopt.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,26 @@ pub(crate) fn ipv6_freebind(fd: BorrowedFd<'_>) -> io::Result<bool> {
724724
getsockopt(fd, c::IPPROTO_IPV6, c::IPV6_FREEBIND).map(to_bool)
725725
}
726726

727+
#[inline]
728+
pub(crate) fn set_ip_checksum(fd: BorrowedFd<'_>, value: u32) -> io::Result<()> {
729+
setsockopt(fd, c::IPPROTO_IP, c::IP_CHECKSUM, value)
730+
}
731+
732+
#[inline]
733+
pub(crate) fn ip_checksum(fd: BorrowedFd<'_>) -> io::Result<u32> {
734+
getsockopt(fd, c::IPPROTO_IP, c::IP_CHECKSUM)
735+
}
736+
737+
#[inline]
738+
pub(crate) fn set_ipv6_checksum(fd: BorrowedFd<'_>, value: u32) -> io::Result<()> {
739+
setsockopt(fd, c::IPPROTO_IPV6, c::IPV6_CHECKSUM, value)
740+
}
741+
742+
#[inline]
743+
pub(crate) fn ipv6_checksum(fd: BorrowedFd<'_>) -> io::Result<u32> {
744+
getsockopt(fd, c::IPPROTO_IPV6, c::IPV6_CHECKSUM)
745+
}
746+
727747
#[inline]
728748
pub(crate) fn ip_original_dst(fd: BorrowedFd<'_>) -> io::Result<SocketAddrV4> {
729749
let level = c::IPPROTO_IP;

src/net/sockopt.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,66 @@ pub fn ipv6_freebind<Fd: AsFd>(fd: Fd) -> io::Result<bool> {
13591359
backend::net::sockopt::ipv6_freebind(fd.as_fd())
13601360
}
13611361

1362+
/// `setsockopt(fd, IPPROTO_IP, IP_CHECKSUM, value)`
1363+
///
1364+
/// See the [module-level documentation] for more.
1365+
///
1366+
/// [module-level documentation]: self#references-for-get_ipv6_-and-set_ipv6_-functions
1367+
#[cfg(any(linux_like, target_os = "cygwin", target_os = "fuchsia",))]
1368+
#[inline]
1369+
#[doc(alias = "IP_CHECKSUM")]
1370+
pub fn set_ip_checksum<Fd: AsFd>(fd: Fd, value: u32) -> io::Result<()> {
1371+
backend::net::sockopt::set_ip_checksum(fd.as_fd(), value)
1372+
}
1373+
1374+
/// `getsockopt(fd, IPPROTO_IP, IP_CHECKSUM)`
1375+
///
1376+
/// See the [module-level documentation] for more.
1377+
///
1378+
/// [module-level documentation]: self#references-for-get_ipv6_-and-set_ipv6_-functions
1379+
#[cfg(any(linux_like, target_os = "cygwin", target_os = "fuchsia",))]
1380+
#[inline]
1381+
#[doc(alias = "IP_CHECKSUM")]
1382+
pub fn ip_checksum<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
1383+
backend::net::sockopt::ip_checksum(fd.as_fd())
1384+
}
1385+
1386+
/// `setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, value)`
1387+
///
1388+
/// See the [module-level documentation] for more.
1389+
///
1390+
/// [module-level documentation]: self#references-for-get_ipv6_-and-set_ipv6_-functions
1391+
#[cfg(any(
1392+
apple,
1393+
linux_like,
1394+
target_os = "cygwin",
1395+
target_os = "freebsd",
1396+
target_os = "fuchsia",
1397+
))]
1398+
#[inline]
1399+
#[doc(alias = "IPV6_CHECKSUM")]
1400+
pub fn set_ipv6_checksum<Fd: AsFd>(fd: Fd, value: u32) -> io::Result<()> {
1401+
backend::net::sockopt::set_ipv6_checksum(fd.as_fd(), value)
1402+
}
1403+
1404+
/// `getsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM)`
1405+
///
1406+
/// See the [module-level documentation] for more.
1407+
///
1408+
/// [module-level documentation]: self#references-for-get_ipv6_-and-set_ipv6_-functions
1409+
#[cfg(any(
1410+
apple,
1411+
linux_like,
1412+
target_os = "cygwin",
1413+
target_os = "freebsd",
1414+
target_os = "fuchsia",
1415+
))]
1416+
#[inline]
1417+
#[doc(alias = "IPV6_CHECKSUM")]
1418+
pub fn ipv6_checksum<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
1419+
backend::net::sockopt::ipv6_checksum(fd.as_fd())
1420+
}
1421+
13621422
/// `getsockopt(fd, IPPROTO_IP, SO_ORIGINAL_DST)`
13631423
///
13641424
/// Even though this corresponds to a `SO_*` constant, it is an `IPPROTO_IP`

0 commit comments

Comments
 (0)