authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-04 18:09:33 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-04 18:09:33 -08:00
log864fd0043032051f066882ee7252a0fb60e04219
tree209dd9ed3d039fb1c0a24579a0d2b2e38004fec6
parent83bec24047559a94257c661b5a77c171a7f78813

add Ip6Address


1 files changed, 21 insertions(+), 0 deletions(-)

net.zig+21
......@@ -13,11 +13,16 @@ const sys = switch (os) {
1313pub const Address = extern union {
1414 any: sys.struct_sockaddr,
1515 in: Ip4Address,
16 in6: Ip6Address,
1617
1718 pub fn initIp4(addr: [4]u8, hport: u16) Address {
1819 return .{ .in = Ip4Address.init(addr, hport) };
1920 }
2021
22 pub fn initIp6(addr: [8]u16, hport: u16) Address {
23 return .{ .in6 = Ip6Address.init(addr, hport) };
24 }
25
2126 pub fn size(adr: Address) sys.socklen_t {
2227 return switch (adr.any.family) {
2328 .INET => @sizeOf(sys.struct_sockaddr_in),
......@@ -30,6 +35,7 @@ pub const Address = extern union {
3035 pub fn port(adr: Address) u16 {
3136 return std.mem.bigToNative(u16, switch (adr.any.family) {
3237 .INET => adr.in.sa.port,
38 .INET6 => adr.in6.sa.port,
3339 else => unreachable,
3440 });
3541 }
......@@ -91,6 +97,21 @@ pub const Ip4Address = extern struct {
9197 }
9298};
9399
100pub const Ip6Address = extern struct {
101 sa: sys.struct_sockaddr_in6,
102
103 pub fn init(addr: [8]u16, port: u16) Ip6Address {
104 return Ip6Address{
105 .sa = .{
106 .port = std.mem.nativeToBig(u16, port),
107 .addr = .{ .addr = @bitCast(addr) },
108 .flowinfo = 0,
109 .scope_id = 0,
110 },
111 };
112 }
113};
114
94115pub const Socket = switch (os) {
95116 .linux => enum(c_uint) { _ },
96117 else => @compileError("TODO"),