Skip to content

std.posix: adding getsockopt #22335

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

Merged
merged 4 commits into from
Jan 30, 2025
Merged

std.posix: adding getsockopt #22335

merged 4 commits into from
Jan 30, 2025

Conversation

mrjbq7
Copy link
Contributor

@mrjbq7 mrjbq7 commented Dec 28, 2024

I noticed that std.posix.setsockopt exists but not std.posix.getsockopt.

I tried to add it, but I'm not sure if the anyopaque / optlen approach is a good one.

https://linux.die.net/man/3/getsockopt

@mrjbq7
Copy link
Contributor Author

mrjbq7 commented Jan 1, 2025

Hi @alexrp I made the changes you suggested above.

@mrjbq7
Copy link
Contributor Author

mrjbq7 commented Jan 8, 2025

Okay, so I made a patch moving from opaque pointers:

pub fn getsockopt(fd: socket_t, level: i32, optname: u32, noalias optval: ?*anyopaque, noalias optlen: *socklen_t) GetSockOptError!void {

to slices:

pub fn getsockopt(fd: socket_t, level: i32, optname: u32, opt: []u8) GetSockOptError!void {

And to use it, you do this

var bytes = std.mem.toBytes(@as(c_int, 0));
try std.posix.getsockopt(socket, std.posix.SOL.SOCKET, std.posix.SO.SNDBUF, &bytes);
std.debug.print("send buffer size: {}\n", .{std.mem.bytesToValue(c_int, &bytes)});

I dunno if I like it, although it is a safer interface maybe.

It almost feels like it would be nicer to do something like passing the type in, but probably doesn't work for all sockopts

const size = try std.posix.getsockopt(socket, std.posix.SOL.SOCKET, std.posix.SO.SNDBUF, c_int);

@alexrp
Copy link
Member

alexrp commented Jan 26, 2025

There's definitely room for discussion around []u8 vs *anyopaque vs generics, but I think that should be a separate issue. For this initial implementation, it's best to just follow the existing precedent, I think.

@alexrp
Copy link
Member

alexrp commented Jan 26, 2025

CI is failing due to formatting issues.

@mrjbq7
Copy link
Contributor Author

mrjbq7 commented Jan 26, 2025

@alexrp ok i pushed a zig fmt patch, CI is restarting, thank you.

@alexrp alexrp enabled auto-merge (squash) January 29, 2025 11:58
@alexrp alexrp merged commit 53598e3 into ziglang:master Jan 30, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants