Skip to content

Commit a09971f

Browse files
sgasseasomers
andauthored
Fix setting sockopt::IpMulticastTtl (#2072)
* Fix the value of optlen as passed to libc::setsockopt Ever since the sockopt macro was written in 979faf0 we've been passing the wrong size for u8 and usize sockopts. * Add tests for sockopt::IpMulticastTtl #2073 On Linux and Android, sockopt::IpMulticastTtl can be set for both IPv4 and IPv6 sockets, but not on FreeBSD. --------- Co-authored-by: Alan Somers <asomers@gmail.com> Co-authored-by: Simon B. Gasse <sgasse@users.noreply.github.com>
1 parent 6961f0f commit a09971f

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

changelog/2072.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed `::sys::socket::sockopt::IpMulticastTtl` by fixing the value of optlen passed to `libc::setsockopt` and added tests.

src/sys/socket/sockopt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ impl<'a> Set<'a, bool> for SetBool {
12471247
}
12481248

12491249
fn ffi_len(&self) -> socklen_t {
1250-
mem::size_of::<c_int>() as socklen_t
1250+
mem::size_of_val(&self.val) as socklen_t
12511251
}
12521252
}
12531253

@@ -1298,7 +1298,7 @@ impl<'a> Set<'a, u8> for SetU8 {
12981298
}
12991299

13001300
fn ffi_len(&self) -> socklen_t {
1301-
mem::size_of::<c_int>() as socklen_t
1301+
mem::size_of_val(&self.val) as socklen_t
13021302
}
13031303
}
13041304

@@ -1349,7 +1349,7 @@ impl<'a> Set<'a, usize> for SetUsize {
13491349
}
13501350

13511351
fn ffi_len(&self) -> socklen_t {
1352-
mem::size_of::<c_int>() as socklen_t
1352+
mem::size_of_val(&self.val) as socklen_t
13531353
}
13541354
}
13551355

test/sys/test_sockopt.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,34 @@ fn test_ttl_opts() {
320320
.expect("setting ipv6ttl on an inet6 socket should succeed");
321321
}
322322

323+
#[test]
324+
#[cfg(any(linux_android, target_os = "freebsd"))]
325+
fn test_multicast_ttl_opts_ipv4() {
326+
let fd4 = socket(
327+
AddressFamily::Inet,
328+
SockType::Datagram,
329+
SockFlag::empty(),
330+
None,
331+
)
332+
.unwrap();
333+
setsockopt(&fd4, sockopt::IpMulticastTtl, &2)
334+
.expect("setting ipmulticastttl on an inet socket should succeed");
335+
}
336+
337+
#[test]
338+
#[cfg(linux_android)]
339+
fn test_multicast_ttl_opts_ipv6() {
340+
let fd6 = socket(
341+
AddressFamily::Inet6,
342+
SockType::Datagram,
343+
SockFlag::empty(),
344+
None,
345+
)
346+
.unwrap();
347+
setsockopt(&fd6, sockopt::IpMulticastTtl, &2)
348+
.expect("setting ipmulticastttl on an inet6 socket should succeed");
349+
}
350+
323351
#[test]
324352
fn test_ipv6_multicast_hops() {
325353
let fd6 = socket(

0 commit comments

Comments
 (0)