-1

I'm trying to call setsockopt but can't figure out the cast to c_void. IP_HDRINCL isn't in nix or socket crates, so I have to use libc. I'm following the example of How to set the socket option SO_REUSEPORT in Rust?

let trueval: c_int = 1;
let ret = setsockopt(mysocket, IPPROTO_IP, IP_HDRINCL, &trueval as *const _ as *const c_void, mem::size_of_val(&trueval) as socklen_t);
error: expected expression, found keyword `const`
  --> src/igmp.rs:30:97
   |
30 |         let ret = setsockopt(mysocket, IPPROTO_IP, IP_HDRINCL, &trueval as *const _ *const c_void, mem::size_of_val(&trueval) as socklen_t);
   |                                                                                      ^^^^^ expected expression
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ruckc
  • 505
  • 1
  • 6
  • 23
  • *cast to c_void* — this doesn't make any sense. `void` is a type that cannot exist; you cannot cast to it. – Shepmaster Nov 25 '18 at 17:45
  • You didn't read the linked SO question/answer then. You can, and once my trueval type changes to a `u8` it works. – ruckc Nov 25 '18 at 17:46
  • *You didn't read the linked SO question/answer then* — I **wrote** the linked answer. You cannot cast a value to a `void`. – Shepmaster Nov 25 '18 at 17:50
  • Then why does changing the c_int type to u8 make it work, with the cast from the linked answer? – ruckc Nov 25 '18 at 17:51
  • Please review how to create a [MCVE] and then [edit] your question to include it. We cannot tell what crates, types, traits, fields, etc. are present in the code. [Running your code](https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=0fedb7d7e7c9b7447f483ee62f651a78) produces errors that are different from what you report. Try to produce something that reproduces your error on the [Rust Playground](https://play.rust-lang.org) or you can reproduce it in a brand new Cargo project. There are [Rust-specific MCVE tips](//stackoverflow.com/tags/rust/info) as well. – Shepmaster Nov 25 '18 at 17:54
  • 1
    Your problem is simply a typo. The code you provided says `&trueval as *const _ as *const c_void` but the error message says `&trueval as *const _ *const c_void`. You forgot the second **`as`** in whatever code you attempted first. The cast works just fine. – Shepmaster Nov 25 '18 at 18:01
  • 1
    My point about `void` is that you aren't casting a value to void, you are casting a value to a pointer to void (`*const c_void` (Rust) / `void *` (C)). These have very different meanings. – Shepmaster Nov 25 '18 at 18:06

1 Answers1

-2

Changing trueval to type u8 allows the code to work.

ruckc
  • 505
  • 1
  • 6
  • 23