bytecodealliance / bytecodealliance/rustix

Feature Request: Add support for `IP_HDRINCL` socket option

Open
#1,581 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
2.1k
Forks
294
Avg merge
4h 7m
Merged PRs (30d)
2

Description

### Problem
I am currently working with raw sockets and need to set the `IP_HDRINCL` option to manually construct IP headers. Currently, `rustix::net::sockopt` does not appear to expose a wrapper for this option.

As a result, I have to fall back to `unsafe` code and `libc` to achieve this, which breaks the flow of using `rustix` for safe system calls.

### Current Workaround
Currently, I have to implement it like this:

```rust
use rustix::fd::AsRawFd;

let one: i32 = 1;
unsafe {
libc::setsockopt(
fd.as_raw_fd(),
libc::IPPROTO_IP,
libc::IP_HDRINCL,
std::ptr::from_ref::(&one).cast(),
std::mem::size_of::() as u32,
);
}
```

### Proposed Solution
It would be great to add `set_ip_hdrincl` (and potentially `get_ip_hdrincl`) to `rustix::net::sockopt`.

**Desired API:**
```rust
// In rustix::net::sockopt

pub fn set_ip_hdrincl(fd: &Fd, value: bool) -> rustix::io::Result<()> {
// ... implementation wrapping setsockopt with IPPROTO_IP / IP_HDRINCL
}

pub fn get_ip_hdrincl(fd: &Fd) -> rustix::io::Result {
// ... implementation wrapping getsockopt
}
```

### Context
This is standard functionality for raw socket programming (e.g., when building custom network tools) and seems to fit well within the scope of `rustix`'s networking primitives.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.