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