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) {...@@ -13,11 +13,16 @@ const sys = switch (os) {
13pub const Address = extern union {13pub const Address = extern union {
14 any: sys.struct_sockaddr,14 any: sys.struct_sockaddr,
15 in: Ip4Address,15 in: Ip4Address,
16 in6: Ip6Address,
1617
17 pub fn initIp4(addr: [4]u8, hport: u16) Address {18 pub fn initIp4(addr: [4]u8, hport: u16) Address {
18 return .{ .in = Ip4Address.init(addr, hport) };19 return .{ .in = Ip4Address.init(addr, hport) };
19 }20 }
2021
22 pub fn initIp6(addr: [8]u16, hport: u16) Address {
23 return .{ .in6 = Ip6Address.init(addr, hport) };
24 }
25
21 pub fn size(adr: Address) sys.socklen_t {26 pub fn size(adr: Address) sys.socklen_t {
22 return switch (adr.any.family) {27 return switch (adr.any.family) {
23 .INET => @sizeOf(sys.struct_sockaddr_in),28 .INET => @sizeOf(sys.struct_sockaddr_in),
...@@ -30,6 +35,7 @@ pub const Address = extern union {...@@ -30,6 +35,7 @@ pub const Address = extern union {
30 pub fn port(adr: Address) u16 {35 pub fn port(adr: Address) u16 {
31 return std.mem.bigToNative(u16, switch (adr.any.family) {36 return std.mem.bigToNative(u16, switch (adr.any.family) {
32 .INET => adr.in.sa.port,37 .INET => adr.in.sa.port,
38 .INET6 => adr.in6.sa.port,
33 else => unreachable,39 else => unreachable,
34 });40 });
35 }41 }
...@@ -91,6 +97,21 @@ pub const Ip4Address = extern struct {...@@ -91,6 +97,21 @@ pub const Ip4Address = extern struct {
91 }97 }
92};98};
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
94pub const Socket = switch (os) {115pub const Socket = switch (os) {
95 .linux => enum(c_uint) { _ },116 .linux => enum(c_uint) { _ },
96 else => @compileError("TODO"),117 else => @compileError("TODO"),