Skip to content

Feature Request: Add support for IP_HDRINCL socket option #1581

@NiiightmareXD

Description

@NiiightmareXD

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:

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::<i32>(&one).cast(),
        std::mem::size_of::<i32>() as u32,
    );
}

Proposed Solution

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

Desired API:

// In rustix::net::sockopt

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

pub fn get_ip_hdrincl<Fd: AsFd>(fd: &Fd) -> rustix::io::Result<bool> {
    // ... 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions