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

Duplicate sockaddr structs. #19972

Closed
vamuscari opened this issue May 14, 2024 · 5 comments
Closed

Duplicate sockaddr structs. #19972

vamuscari opened this issue May 14, 2024 · 5 comments
Labels

Comments

@vamuscari
Copy link

Zig Version

0.12.0

Steps to Reproduce and Observed Behavior

Hello! I have been having some issues the os.linux.socket to work.

Problem:
The sockaddr struct is declared in std.os and std.posix. This causes a type conflict when binding to a port because the compiler thinks they are different structs.

(main.zig:14:30: error: expected type 'os.linux.sockaddr', found 'c.darwin.sockaddr')

I was uncertain if this was a skill issue so I did a diff to make sure that they are the same struct.

Here is the code that produces the issue:

const std = @import("std");
const net = std.net;
const os = std.os.linux;
const print = std.debug.print;

pub fn main() !void {
    const ip: []const u8 = "127.0.0.1";
    const port: u16 = 3000;
    const addr = try net.Address.parseIp4(ip, port);
    const sock: i32 = @intCast(os.socket(os.AF.INET, os.SOCK.DGRAM, 0));
    try os.bind(sock, addr.any, addr.getOsSockLen());
}

Expected Behavior

I would expect to not get this issue since they are exactly the same. I would imagine std.os should reference the std.posix?

@vamuscari vamuscari added the bug Observed behavior contradicts documented or intended behavior label May 14, 2024
@nektro
Copy link
Contributor

nektro commented May 15, 2024

how did u call zig on the command line? what arch/os is your host?

@vamuscari
Copy link
Author

@nektro
CLI Call: zig
Arch: apple M3

@xdBronch
Copy link
Contributor

xdBronch commented May 15, 2024

std.net.Address uses std.posix.sockaddr which is the correct type as it will change depending on the platform but youre unconditionally using std.os.linux. heres the version that compiles

const std = @import("std");
const net = std.net;
const posix = std.posix;
const print = std.debug.print;

pub fn main() !void {
    const ip: []const u8 = "127.0.0.1";
    const port: u16 = 3000;
    const addr = try net.Address.parseIp4(ip, port);
    const sock = try posix.socket(posix.AF.INET, posix.SOCK.DGRAM, 0);
    try posix.bind(sock, &addr.any, addr.getOsSockLen());
}

I would imagine std.os should reference the std.posix?

its the exact opposite, std.posix is an abstraction over different systems that implement posix so std.posix.foo will be std.os.linux.foo when targeting linux but std.c.darwin.foo when targeting macos

@vamuscari
Copy link
Author

@xdBronch Thank for the clarification!

There is one thing Im still unsure about though. I ended up in using std.net because I wanted to parse an address into a sockaddr struct. Is there a way to do this for the std.os.linux? or is it supposed to be used in a different way? I will just use the posix lib for simplicity, but what I intend on making will end on a linux server.

@Vexu
Copy link
Member

Vexu commented May 19, 2024

If you write a program for Linux and want to compile it on MacOS you need to specify -target aarch64-linux.

@Vexu Vexu closed this as not planned Won't fix, can't repro, duplicate, stale May 19, 2024
@Vexu Vexu added question and removed bug Observed behavior contradicts documented or intended behavior labels May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants