Skip to content

Commit d067bc7

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

File tree

4 files changed

+134
-2
lines changed

4 files changed

+134
-2
lines changed

src/backend/libc/net/sockopt.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,50 @@ 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+
linux_like,
878+
target_os = "cygwin",
879+
target_os = "fuchsia",
880+
))]
881+
#[inline]
882+
pub(crate) fn set_ip_checksum(fd: BorrowedFd<'_>, value: u32) -> io::Result<()> {
883+
setsockopt(fd, c::IPPROTO_IP, c::IP_CHECKSUM, value)
884+
}
885+
886+
#[cfg(any(
887+
linux_like,
888+
target_os = "cygwin",
889+
target_os = "fuchsia",
890+
))]
891+
#[inline]
892+
pub(crate) fn ip_checksum(fd: BorrowedFd<'_>) -> io::Result<u32> {
893+
getsockopt(fd, c::IPPROTO_IP, c::IP_CHECKSUM)
894+
}
895+
896+
#[cfg(any(
897+
apple,
898+
linux_like,
899+
target_os = "cygwin",
900+
target_os = "freebsd",
901+
target_os = "fuchsia",
902+
))]
903+
#[inline]
904+
pub(crate) fn set_ipv6_checksum(fd: BorrowedFd<'_>, value: u32) -> io::Result<()> {
905+
setsockopt(fd, c::IPPROTO_IPV6, c::IPV6_CHECKSUM, value)
906+
}
907+
908+
#[cfg(any(
909+
apple,
910+
linux_like,
911+
target_os = "cygwin",
912+
target_os = "freebsd",
913+
target_os = "fuchsia",
914+
))]
915+
#[inline]
916+
pub(crate) fn ipv6_checksum(fd: BorrowedFd<'_>) -> io::Result<u32> {
917+
getsockopt(fd, c::IPPROTO_IPV6, c::IPV6_CHECKSUM)
918+
}
919+
876920
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
877921
#[inline]
878922
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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,74 @@ 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+
linux_like,
1369+
target_os = "cygwin",
1370+
target_os = "fuchsia",
1371+
))]
1372+
#[inline]
1373+
#[doc(alias = "IP_CHECKSUM")]
1374+
pub fn set_ip_checksum<Fd: AsFd>(fd: Fd, value: u32) -> io::Result<()> {
1375+
backend::net::sockopt::set_ip_checksum(fd.as_fd(), value)
1376+
}
1377+
1378+
/// `getsockopt(fd, IPPROTO_IP, IP_CHECKSUM)`
1379+
///
1380+
/// See the [module-level documentation] for more.
1381+
///
1382+
/// [module-level documentation]: self#references-for-get_ipv6_-and-set_ipv6_-functions
1383+
#[cfg(any(
1384+
linux_like,
1385+
target_os = "cygwin",
1386+
target_os = "fuchsia",
1387+
))]
1388+
#[inline]
1389+
#[doc(alias = "IP_CHECKSUM")]
1390+
pub fn ip_checksum<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
1391+
backend::net::sockopt::ip_checksum(fd.as_fd())
1392+
}
1393+
1394+
/// `setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, value)`
1395+
///
1396+
/// See the [module-level documentation] for more.
1397+
///
1398+
/// [module-level documentation]: self#references-for-get_ipv6_-and-set_ipv6_-functions
1399+
#[cfg(any(
1400+
apple,
1401+
linux_like,
1402+
target_os = "cygwin",
1403+
target_os = "freebsd",
1404+
target_os = "fuchsia",
1405+
))]
1406+
#[inline]
1407+
#[doc(alias = "IPV6_CHECKSUM")]
1408+
pub fn set_ipv6_checksum<Fd: AsFd>(fd: Fd, value: u32) -> io::Result<()> {
1409+
backend::net::sockopt::set_ipv6_checksum(fd.as_fd(), value)
1410+
}
1411+
1412+
/// `getsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM)`
1413+
///
1414+
/// See the [module-level documentation] for more.
1415+
///
1416+
/// [module-level documentation]: self#references-for-get_ipv6_-and-set_ipv6_-functions
1417+
#[cfg(any(
1418+
apple,
1419+
linux_like,
1420+
target_os = "cygwin",
1421+
target_os = "freebsd",
1422+
target_os = "fuchsia",
1423+
))]
1424+
#[inline]
1425+
#[doc(alias = "IPV6_CHECKSUM")]
1426+
pub fn ipv6_checksum<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
1427+
backend::net::sockopt::ipv6_checksum(fd.as_fd())
1428+
}
1429+
13621430
/// `getsockopt(fd, IPPROTO_IP, SO_ORIGINAL_DST)`
13631431
///
13641432
/// Even though this corresponds to a `SO_*` constant, it is an `IPPROTO_IP`

0 commit comments

Comments
 (0)