Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to add custom socket options #577

Closed
code-ape opened this issue Apr 14, 2017 · 5 comments · Fixed by #2556
Closed

Ability to add custom socket options #577

code-ape opened this issue Apr 14, 2017 · 5 comments · Fixed by #2556

Comments

@code-ape
Copy link

I was looking to do the following code except using nix instead of libc:

use libc::{c_ushort, c_int, c_void, socklen_t, SOL_SOCKET, setsockopt};

const SO_ATTACH_FILTER: c_int = 26;

pub fn attach_filter(fd: RawFd, prog: Prog) -> Result<(), Error> {
    match unsafe {
              setsockopt(fd as c_int,
                         SOL_SOCKET,
                         SO_ATTACH_FILTER,
                         &prog as *const _ as *const c_void,
                         size_of_val(&prog) as socklen_t)
          } {
        0 => Ok(()),
        _ => Err(Error::last_os_error()),
    }
}

However nix currently doesn't support this, which is fine. However, nix doesn't expose an easy way to use it's internal macros to construct the SO_ATTACH_FILTER command. I wanted to file an issue to suggest exposing this. I'll also look into contributing my use case to nix so it's added t the library for future users.

Code source: https://github.com/jedisct1/rust-bpf/blob/0122d0a95e6012abab0475b14cef20dbfa481704/src/bpf_linux.rs#L58

@Susurrus
Copy link
Contributor

Susurrus commented Jun 4, 2017

I don't quite understand the issue here as I'm not familiar with setsockopt. Do you have a proposal for how we might integrate this code example that would fit in with the rest of nix? Would exposing sockopt_impl! be enough for you?

@mexus
Copy link
Contributor

mexus commented Jan 26, 2018

Sorry for bringing up an old issue, but it happened so I need the same feature :)

Basically the issue is that the macro sockopt_impl! is not exposed, which will help a lot.
But it's not the only thing.

For example, for SO_ATTACH_FILTER we need to pass an object of a special type, which obviously does not implement Set. But the Set trait is not exposed as well :)

So I guess exposing sockopt_impl! macro and Set and Get traits will help a lot.

@Susurrus
Copy link
Contributor

Is there a reason we can't provide a sockopt_impl! within nix that exposes SO_ATTACH_FILTER? This has been the need for both you and @code-ape, so I'd suggest we start going down this route. We'd welcome a PR for it.

If we were to expose sockopt_impl! and those other traits, we'd need them all documented much more extensively. If you'd like to help with this effort, we welcome all PRs!

@mexus
Copy link
Contributor

mexus commented Jan 26, 2018

That's great, thanks! 👍

I'll try to make it happen by the beginning of next week.

@mexus
Copy link
Contributor

mexus commented Jan 29, 2018

(I went for the second option)

bors bot added a commit that referenced this issue Feb 17, 2018
846: Export socket options related macros r=Susurrus a=mexus

In this PR I've added docs to that macros and exported them.
I've also made related structs/traits to be public as well.
Hope I didn't forget anything :)

Reference issue: #577.
asomers added a commit to asomers/nix that referenced this issue Dec 4, 2024
This allows users to define their own sockopts, instead of needing to
make a PR to Nix for every single one.

Fixes nix-rust#577
@asomers asomers mentioned this issue Dec 4, 2024
3 tasks
asomers added a commit to asomers/nix that referenced this issue Dec 4, 2024
This allows users to define their own sockopts, instead of needing to
make a PR to Nix for every single one.

Fixes nix-rust#577
asomers added a commit to asomers/nix that referenced this issue Dec 5, 2024
This allows users to define their own sockopts, instead of needing to
make a PR to Nix for every single one.

Fixes nix-rust#577
asomers added a commit to asomers/nix that referenced this issue Dec 5, 2024
This allows users to define their own sockopts, instead of needing to
make a PR to Nix for every single one.

Fixes nix-rust#577
asomers added a commit to asomers/nix that referenced this issue Dec 5, 2024
This allows users to define their own sockopts, instead of needing to
make a PR to Nix for every single one.

Fixes nix-rust#577
asomers added a commit to asomers/nix that referenced this issue Dec 5, 2024
This allows users to define their own sockopts, instead of needing to
make a PR to Nix for every single one.

Fixes nix-rust#577
asomers added a commit to asomers/nix that referenced this issue Dec 5, 2024
This allows users to define their own sockopts, instead of needing to
make a PR to Nix for every single one.

Fixes nix-rust#577
asomers added a commit to asomers/nix that referenced this issue Dec 5, 2024
This allows users to define their own sockopts, instead of needing to
make a PR to Nix for every single one.

Fixes nix-rust#577
asomers added a commit to asomers/nix that referenced this issue Dec 5, 2024
This allows users to define their own sockopts, instead of needing to
make a PR to Nix for every single one.

Fixes nix-rust#577
github-merge-queue bot pushed a commit that referenced this issue Dec 5, 2024
* Make sockopt_impl! public

This allows users to define their own sockopts, instead of needing to
make a PR to Nix for every single one.

Fixes #577

* Respond to @SteveLauC's comments

* No need to make GetCString pub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants