Skip to content

Commit 0757bb0

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

File tree

4 files changed

+142
-2
lines changed

4 files changed

+142
-2
lines changed

src/backend/libc/net/sockopt.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,54 @@ 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(
877+
apple,
878+
linux_like,
879+
target_os = "cygwin",
880+
target_os = "freebsd",
881+
target_os = "fuchsia",
882+
))]
883+
#[inline]
884+
pub(crate) fn set_ip_checksum(fd: BorrowedFd<'_>, value: u32) -> io::Result<()> {
885+
setsockopt(fd, c::IPPROTO_IP, c::IP_CHECKSUM, value)
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 ip_checksum(fd: BorrowedFd<'_>) -> io::Result<u32> {
897+
getsockopt(fd, c::IPPROTO_IP, c::IP_CHECKSUM)
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 set_ipv6_checksum(fd: BorrowedFd<'_>, value: u32) -> io::Result<()> {
909+
setsockopt(fd, c::IPPROTO_IPV6, c::IPV6_CHECKSUM, value)
910+
}
911+
912+
#[cfg(any(
913+
apple,
914+
linux_like,
915+
target_os = "cygwin",
916+
target_os = "freebsd",
917+
target_os = "fuchsia",
918+
))]
919+
#[inline]
920+
pub(crate) fn ipv6_checksum(fd: BorrowedFd<'_>) -> io::Result<u32> {
921+
getsockopt(fd, c::IPPROTO_IPV6, c::IPV6_CHECKSUM)
922+
}
923+
876924
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
877925
#[inline]
878926
pub(crate) fn ip_original_dst(fd: BorrowedFd<'_>) -> io::Result<SocketAddrV4> {

src/backend/linux_raw/c.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ 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,
93+
IP_ADD_SOURCE_MEMBERSHIP, IP_CHECKSUM, IP_DROP_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP, IP_FREEBIND,
9494
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,

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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,78 @@ 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(
1368+
apple,
1369+
linux_like,
1370+
target_os = "cygwin",
1371+
target_os = "freebsd",
1372+
target_os = "fuchsia",
1373+
))]
1374+
#[inline]
1375+
#[doc(alias = "IP_CHECKSUM")]
1376+
pub fn set_ip_checksum<Fd: AsFd>(fd: Fd, value: u32) -> io::Result<()> {
1377+
backend::net::sockopt::set_ip_checksum(fd.as_fd(), value)
1378+
}
1379+
1380+
/// `getsockopt(fd, IPPROTO_IP, IP_CHECKSUM)`
1381+
///
1382+
/// See the [module-level documentation] for more.
1383+
///
1384+
/// [module-level documentation]: self#references-for-get_ipv6_-and-set_ipv6_-functions
1385+
#[cfg(any(
1386+
apple,
1387+
linux_like,
1388+
target_os = "cygwin",
1389+
target_os = "freebsd",
1390+
target_os = "fuchsia",
1391+
))]
1392+
#[inline]
1393+
#[doc(alias = "IP_CHECKSUM")]
1394+
pub fn ip_checksum<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
1395+
backend::net::sockopt::ip_checksum(fd.as_fd())
1396+
}
1397+
1398+
/// `setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, value)`
1399+
///
1400+
/// See the [module-level documentation] for more.
1401+
///
1402+
/// [module-level documentation]: self#references-for-get_ipv6_-and-set_ipv6_-functions
1403+
#[cfg(any(
1404+
apple,
1405+
linux_like,
1406+
target_os = "cygwin",
1407+
target_os = "freebsd",
1408+
target_os = "fuchsia",
1409+
))]
1410+
#[inline]
1411+
#[doc(alias = "IPV6_CHECKSUM")]
1412+
pub fn set_ipv6_checksum<Fd: AsFd>(fd: Fd, value: u32) -> io::Result<()> {
1413+
backend::net::sockopt::set_ipv6_checksum(fd.as_fd(), value)
1414+
}
1415+
1416+
/// `getsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM)`
1417+
///
1418+
/// See the [module-level documentation] for more.
1419+
///
1420+
/// [module-level documentation]: self#references-for-get_ipv6_-and-set_ipv6_-functions
1421+
#[cfg(any(
1422+
apple,
1423+
linux_like,
1424+
target_os = "cygwin",
1425+
target_os = "freebsd",
1426+
target_os = "fuchsia",
1427+
))]
1428+
#[inline]
1429+
#[doc(alias = "IPV6_CHECKSUM")]
1430+
pub fn ipv6_checksum<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
1431+
backend::net::sockopt::ipv6_checksum(fd.as_fd())
1432+
}
1433+
13621434
/// `getsockopt(fd, IPPROTO_IP, SO_ORIGINAL_DST)`
13631435
///
13641436
/// Even though this corresponds to a `SO_*` constant, it is an `IPPROTO_IP`

0 commit comments

Comments
 (0)