zig/std/net.zig

219 lines
6.1 KiB
Zig
Raw Normal View History

2019-03-03 05:46:04 +08:00
const std = @import("std.zig");
2018-04-09 12:52:45 +08:00
const builtin = @import("builtin");
const assert = std.debug.assert;
const net = @This();
2018-03-07 16:55:52 +08:00
const mem = std.mem;
2019-05-26 01:07:44 +08:00
const os = std.os;
2018-03-07 16:55:52 +08:00
pub const TmpWinAddr = struct {
2018-04-09 12:52:45 +08:00
family: u8,
data: [14]u8,
};
pub const OsAddress = switch (builtin.os) {
builtin.Os.windows => TmpWinAddr,
2019-05-26 01:07:44 +08:00
else => os.sockaddr,
2018-04-09 12:52:45 +08:00
};
pub const Address = struct {
2018-04-09 12:52:45 +08:00
os_addr: OsAddress,
2018-03-07 16:55:52 +08:00
2018-11-08 09:53:23 +08:00
pub fn initIp4(ip4: u32, _port: u16) Address {
return Address{
2019-05-26 01:07:44 +08:00
.os_addr = os.sockaddr{
.in = os.sockaddr_in{
.family = os.AF_INET,
breaking API changes to all readInt/writeInt functions & more * add `@bswap` builtin function. See #767 * comptime evaluation facilities are improved to be able to handle a `@ptrCast` with a backing array. * `@truncate` allows "truncating" a u0 value to any integer type, and the result is always comptime known to be `0`. * when specifying pointer alignment in a type expression, the alignment value of pointers which do not have addresses at runtime is ignored, and always has the default/ABI alignment * threw in a fix to freebsd/x86_64.zig to update syntax from language changes * some improvements are pending #863 closes #638 closes #1733 std lib API changes * io.InStream().readIntNe renamed to readIntNative * io.InStream().readIntLe renamed to readIntLittle * io.InStream().readIntBe renamed to readIntBig * introduced io.InStream().readIntForeign * io.InStream().readInt has parameter order changed * io.InStream().readVarInt has parameter order changed * io.InStream().writeIntNe renamed to writeIntNative * introduced io.InStream().writeIntForeign * io.InStream().writeIntLe renamed to writeIntLittle * io.InStream().writeIntBe renamed to writeIntBig * io.InStream().writeInt has parameter order changed * mem.readInt has different parameters and semantics * introduced mem.readIntNative * introduced mem.readIntForeign * mem.readIntBE renamed to mem.readIntBig and different API * mem.readIntLE renamed to mem.readIntLittle and different API * introduced mem.readIntSliceNative * introduced mem.readIntSliceForeign * introduced mem.readIntSliceLittle * introduced mem.readIntSliceBig * introduced mem.readIntSlice * mem.writeInt has different parameters and semantics * introduced mem.writeIntNative * introduced mem.writeIntForeign * mem.writeIntBE renamed to mem.readIntBig and different semantics * mem.writeIntLE renamed to mem.readIntLittle and different semantics * introduced mem.writeIntSliceForeign * introduced mem.writeIntSliceNative * introduced mem.writeIntSliceBig * introduced mem.writeIntSliceLittle * introduced mem.writeIntSlice * removed mem.endianSwapIfLe * removed mem.endianSwapIfBe * removed mem.endianSwapIf * added mem.littleToNative * added mem.bigToNative * added mem.toNative * added mem.nativeTo * added mem.nativeToLittle * added mem.nativeToBig
2018-12-13 09:19:46 +08:00
.port = mem.nativeToBig(u16, _port),
2018-05-29 08:23:55 +08:00
.addr = ip4,
.zero = []u8{0} ** 8,
2018-05-29 08:23:55 +08:00
},
},
};
}
2018-11-08 09:53:23 +08:00
pub fn initIp6(ip6: *const Ip6Addr, _port: u16) Address {
return Address{
2019-05-26 01:07:44 +08:00
.family = os.AF_INET6,
.os_addr = os.sockaddr{
.in6 = os.sockaddr_in6{
.family = os.AF_INET6,
breaking API changes to all readInt/writeInt functions & more * add `@bswap` builtin function. See #767 * comptime evaluation facilities are improved to be able to handle a `@ptrCast` with a backing array. * `@truncate` allows "truncating" a u0 value to any integer type, and the result is always comptime known to be `0`. * when specifying pointer alignment in a type expression, the alignment value of pointers which do not have addresses at runtime is ignored, and always has the default/ABI alignment * threw in a fix to freebsd/x86_64.zig to update syntax from language changes * some improvements are pending #863 closes #638 closes #1733 std lib API changes * io.InStream().readIntNe renamed to readIntNative * io.InStream().readIntLe renamed to readIntLittle * io.InStream().readIntBe renamed to readIntBig * introduced io.InStream().readIntForeign * io.InStream().readInt has parameter order changed * io.InStream().readVarInt has parameter order changed * io.InStream().writeIntNe renamed to writeIntNative * introduced io.InStream().writeIntForeign * io.InStream().writeIntLe renamed to writeIntLittle * io.InStream().writeIntBe renamed to writeIntBig * io.InStream().writeInt has parameter order changed * mem.readInt has different parameters and semantics * introduced mem.readIntNative * introduced mem.readIntForeign * mem.readIntBE renamed to mem.readIntBig and different API * mem.readIntLE renamed to mem.readIntLittle and different API * introduced mem.readIntSliceNative * introduced mem.readIntSliceForeign * introduced mem.readIntSliceLittle * introduced mem.readIntSliceBig * introduced mem.readIntSlice * mem.writeInt has different parameters and semantics * introduced mem.writeIntNative * introduced mem.writeIntForeign * mem.writeIntBE renamed to mem.readIntBig and different semantics * mem.writeIntLE renamed to mem.readIntLittle and different semantics * introduced mem.writeIntSliceForeign * introduced mem.writeIntSliceNative * introduced mem.writeIntSliceBig * introduced mem.writeIntSliceLittle * introduced mem.writeIntSlice * removed mem.endianSwapIfLe * removed mem.endianSwapIfBe * removed mem.endianSwapIf * added mem.littleToNative * added mem.bigToNative * added mem.toNative * added mem.nativeTo * added mem.nativeToLittle * added mem.nativeToBig
2018-12-13 09:19:46 +08:00
.port = mem.nativeToBig(u16, _port),
2018-05-29 08:23:55 +08:00
.flowinfo = 0,
.addr = ip6.addr,
.scope_id = ip6.scope_id,
},
},
2018-03-07 16:55:52 +08:00
};
}
2018-11-08 09:53:23 +08:00
pub fn port(self: Address) u16 {
breaking API changes to all readInt/writeInt functions & more * add `@bswap` builtin function. See #767 * comptime evaluation facilities are improved to be able to handle a `@ptrCast` with a backing array. * `@truncate` allows "truncating" a u0 value to any integer type, and the result is always comptime known to be `0`. * when specifying pointer alignment in a type expression, the alignment value of pointers which do not have addresses at runtime is ignored, and always has the default/ABI alignment * threw in a fix to freebsd/x86_64.zig to update syntax from language changes * some improvements are pending #863 closes #638 closes #1733 std lib API changes * io.InStream().readIntNe renamed to readIntNative * io.InStream().readIntLe renamed to readIntLittle * io.InStream().readIntBe renamed to readIntBig * introduced io.InStream().readIntForeign * io.InStream().readInt has parameter order changed * io.InStream().readVarInt has parameter order changed * io.InStream().writeIntNe renamed to writeIntNative * introduced io.InStream().writeIntForeign * io.InStream().writeIntLe renamed to writeIntLittle * io.InStream().writeIntBe renamed to writeIntBig * io.InStream().writeInt has parameter order changed * mem.readInt has different parameters and semantics * introduced mem.readIntNative * introduced mem.readIntForeign * mem.readIntBE renamed to mem.readIntBig and different API * mem.readIntLE renamed to mem.readIntLittle and different API * introduced mem.readIntSliceNative * introduced mem.readIntSliceForeign * introduced mem.readIntSliceLittle * introduced mem.readIntSliceBig * introduced mem.readIntSlice * mem.writeInt has different parameters and semantics * introduced mem.writeIntNative * introduced mem.writeIntForeign * mem.writeIntBE renamed to mem.readIntBig and different semantics * mem.writeIntLE renamed to mem.readIntLittle and different semantics * introduced mem.writeIntSliceForeign * introduced mem.writeIntSliceNative * introduced mem.writeIntSliceBig * introduced mem.writeIntSliceLittle * introduced mem.writeIntSlice * removed mem.endianSwapIfLe * removed mem.endianSwapIfBe * removed mem.endianSwapIf * added mem.littleToNative * added mem.bigToNative * added mem.toNative * added mem.nativeTo * added mem.nativeToLittle * added mem.nativeToBig
2018-12-13 09:19:46 +08:00
return mem.bigToNative(u16, self.os_addr.in.port);
2018-11-08 09:53:23 +08:00
}
2019-05-26 01:07:44 +08:00
pub fn initPosix(addr: os.sockaddr) Address {
return Address{ .os_addr = addr };
}
pub fn format(self: *const Address, out_stream: var) !void {
2018-04-09 12:52:45 +08:00
switch (self.os_addr.in.family) {
2019-05-26 01:07:44 +08:00
os.AF_INET => {
breaking API changes to all readInt/writeInt functions & more * add `@bswap` builtin function. See #767 * comptime evaluation facilities are improved to be able to handle a `@ptrCast` with a backing array. * `@truncate` allows "truncating" a u0 value to any integer type, and the result is always comptime known to be `0`. * when specifying pointer alignment in a type expression, the alignment value of pointers which do not have addresses at runtime is ignored, and always has the default/ABI alignment * threw in a fix to freebsd/x86_64.zig to update syntax from language changes * some improvements are pending #863 closes #638 closes #1733 std lib API changes * io.InStream().readIntNe renamed to readIntNative * io.InStream().readIntLe renamed to readIntLittle * io.InStream().readIntBe renamed to readIntBig * introduced io.InStream().readIntForeign * io.InStream().readInt has parameter order changed * io.InStream().readVarInt has parameter order changed * io.InStream().writeIntNe renamed to writeIntNative * introduced io.InStream().writeIntForeign * io.InStream().writeIntLe renamed to writeIntLittle * io.InStream().writeIntBe renamed to writeIntBig * io.InStream().writeInt has parameter order changed * mem.readInt has different parameters and semantics * introduced mem.readIntNative * introduced mem.readIntForeign * mem.readIntBE renamed to mem.readIntBig and different API * mem.readIntLE renamed to mem.readIntLittle and different API * introduced mem.readIntSliceNative * introduced mem.readIntSliceForeign * introduced mem.readIntSliceLittle * introduced mem.readIntSliceBig * introduced mem.readIntSlice * mem.writeInt has different parameters and semantics * introduced mem.writeIntNative * introduced mem.writeIntForeign * mem.writeIntBE renamed to mem.readIntBig and different semantics * mem.writeIntLE renamed to mem.readIntLittle and different semantics * introduced mem.writeIntSliceForeign * introduced mem.writeIntSliceNative * introduced mem.writeIntSliceBig * introduced mem.writeIntSliceLittle * introduced mem.writeIntSlice * removed mem.endianSwapIfLe * removed mem.endianSwapIfBe * removed mem.endianSwapIf * added mem.littleToNative * added mem.bigToNative * added mem.toNative * added mem.nativeTo * added mem.nativeToLittle * added mem.nativeToBig
2018-12-13 09:19:46 +08:00
const native_endian_port = mem.bigToNative(u16, self.os_addr.in.port);
const bytes = ([]const u8)((*self.os_addr.in.addr)[0..1]);
2018-03-07 16:55:52 +08:00
try out_stream.print("{}.{}.{}.{}:{}", bytes[0], bytes[1], bytes[2], bytes[3], native_endian_port);
},
2019-05-26 01:07:44 +08:00
os.AF_INET6 => {
breaking API changes to all readInt/writeInt functions & more * add `@bswap` builtin function. See #767 * comptime evaluation facilities are improved to be able to handle a `@ptrCast` with a backing array. * `@truncate` allows "truncating" a u0 value to any integer type, and the result is always comptime known to be `0`. * when specifying pointer alignment in a type expression, the alignment value of pointers which do not have addresses at runtime is ignored, and always has the default/ABI alignment * threw in a fix to freebsd/x86_64.zig to update syntax from language changes * some improvements are pending #863 closes #638 closes #1733 std lib API changes * io.InStream().readIntNe renamed to readIntNative * io.InStream().readIntLe renamed to readIntLittle * io.InStream().readIntBe renamed to readIntBig * introduced io.InStream().readIntForeign * io.InStream().readInt has parameter order changed * io.InStream().readVarInt has parameter order changed * io.InStream().writeIntNe renamed to writeIntNative * introduced io.InStream().writeIntForeign * io.InStream().writeIntLe renamed to writeIntLittle * io.InStream().writeIntBe renamed to writeIntBig * io.InStream().writeInt has parameter order changed * mem.readInt has different parameters and semantics * introduced mem.readIntNative * introduced mem.readIntForeign * mem.readIntBE renamed to mem.readIntBig and different API * mem.readIntLE renamed to mem.readIntLittle and different API * introduced mem.readIntSliceNative * introduced mem.readIntSliceForeign * introduced mem.readIntSliceLittle * introduced mem.readIntSliceBig * introduced mem.readIntSlice * mem.writeInt has different parameters and semantics * introduced mem.writeIntNative * introduced mem.writeIntForeign * mem.writeIntBE renamed to mem.readIntBig and different semantics * mem.writeIntLE renamed to mem.readIntLittle and different semantics * introduced mem.writeIntSliceForeign * introduced mem.writeIntSliceNative * introduced mem.writeIntSliceBig * introduced mem.writeIntSliceLittle * introduced mem.writeIntSlice * removed mem.endianSwapIfLe * removed mem.endianSwapIfBe * removed mem.endianSwapIf * added mem.littleToNative * added mem.bigToNative * added mem.toNative * added mem.nativeTo * added mem.nativeToLittle * added mem.nativeToBig
2018-12-13 09:19:46 +08:00
const native_endian_port = mem.bigToNative(u16, self.os_addr.in6.port);
2018-03-07 16:55:52 +08:00
try out_stream.print("[TODO render ip6 address]:{}", native_endian_port);
},
else => try out_stream.write("(unrecognized address family)"),
2016-05-05 05:52:15 +08:00
}
}
2016-12-19 08:40:26 +08:00
};
2016-05-05 05:52:15 +08:00
2018-03-07 16:55:52 +08:00
pub fn parseIp4(buf: []const u8) !u32 {
var result: u32 = undefined;
const out_ptr = @sliceToBytes((*[1]u32)(&result)[0..]);
2016-05-05 05:52:15 +08:00
2018-03-07 16:55:52 +08:00
var x: u8 = 0;
var index: u8 = 0;
var saw_any_digits = false;
for (buf) |c| {
if (c == '.') {
if (!saw_any_digits) {
return error.InvalidCharacter;
}
if (index == 3) {
return error.InvalidEnd;
}
out_ptr[index] = x;
index += 1;
x = 0;
saw_any_digits = false;
} else if (c >= '0' and c <= '9') {
saw_any_digits = true;
const digit = c - '0';
if (@mulWithOverflow(u8, x, 10, &x)) {
return error.Overflow;
}
if (@addWithOverflow(u8, x, digit, &x)) {
return error.Overflow;
}
} else {
return error.InvalidCharacter;
}
2016-05-05 05:52:15 +08:00
}
2018-03-07 16:55:52 +08:00
if (index == 3 and saw_any_digits) {
out_ptr[index] = x;
return result;
2016-05-05 05:52:15 +08:00
}
2018-03-07 16:55:52 +08:00
return error.Incomplete;
}
pub const Ip6Addr = struct {
2018-03-07 16:55:52 +08:00
scope_id: u32,
addr: [16]u8,
};
2018-03-07 16:55:52 +08:00
pub fn parseIp6(buf: []const u8) !Ip6Addr {
var result: Ip6Addr = undefined;
result.scope_id = 0;
const ip_slice = result.addr[0..];
var x: u16 = 0;
var saw_any_digits = false;
var index: u8 = 0;
var scope_id = false;
for (buf) |c| {
if (scope_id) {
if (c >= '0' and c <= '9') {
const digit = c - '0';
2016-08-17 13:42:50 +08:00
if (@mulWithOverflow(u32, result.scope_id, 10, &result.scope_id)) {
return error.Overflow;
}
2016-08-17 13:42:50 +08:00
if (@addWithOverflow(u32, result.scope_id, digit, &result.scope_id)) {
return error.Overflow;
}
} else {
2018-03-07 16:55:52 +08:00
return error.InvalidCharacter;
}
} else if (c == ':') {
if (!saw_any_digits) {
2018-03-07 16:55:52 +08:00
return error.InvalidCharacter;
}
if (index == 14) {
2018-03-07 16:55:52 +08:00
return error.InvalidEnd;
}
ip_slice[index] = @truncate(u8, x >> 8);
index += 1;
ip_slice[index] = @truncate(u8, x);
index += 1;
x = 0;
saw_any_digits = false;
} else if (c == '%') {
if (!saw_any_digits) {
2018-03-07 16:55:52 +08:00
return error.InvalidCharacter;
}
if (index == 14) {
ip_slice[index] = @truncate(u8, x >> 8);
index += 1;
ip_slice[index] = @truncate(u8, x);
index += 1;
}
scope_id = true;
saw_any_digits = false;
} else {
2018-03-07 16:55:52 +08:00
const digit = try std.fmt.charToDigit(c, 16);
2016-08-17 13:42:50 +08:00
if (@mulWithOverflow(u16, x, 16, &x)) {
return error.Overflow;
}
2016-08-17 13:42:50 +08:00
if (@addWithOverflow(u16, x, digit, &x)) {
return error.Overflow;
}
saw_any_digits = true;
}
}
if (!saw_any_digits) {
return error.Incomplete;
}
if (scope_id) {
return result;
}
if (index == 14) {
ip_slice[14] = @truncate(u8, x >> 8);
ip_slice[15] = @truncate(u8, x);
return result;
}
return error.Incomplete;
}
2018-03-07 16:55:52 +08:00
test "std.net.parseIp4" {
breaking API changes to all readInt/writeInt functions & more * add `@bswap` builtin function. See #767 * comptime evaluation facilities are improved to be able to handle a `@ptrCast` with a backing array. * `@truncate` allows "truncating" a u0 value to any integer type, and the result is always comptime known to be `0`. * when specifying pointer alignment in a type expression, the alignment value of pointers which do not have addresses at runtime is ignored, and always has the default/ABI alignment * threw in a fix to freebsd/x86_64.zig to update syntax from language changes * some improvements are pending #863 closes #638 closes #1733 std lib API changes * io.InStream().readIntNe renamed to readIntNative * io.InStream().readIntLe renamed to readIntLittle * io.InStream().readIntBe renamed to readIntBig * introduced io.InStream().readIntForeign * io.InStream().readInt has parameter order changed * io.InStream().readVarInt has parameter order changed * io.InStream().writeIntNe renamed to writeIntNative * introduced io.InStream().writeIntForeign * io.InStream().writeIntLe renamed to writeIntLittle * io.InStream().writeIntBe renamed to writeIntBig * io.InStream().writeInt has parameter order changed * mem.readInt has different parameters and semantics * introduced mem.readIntNative * introduced mem.readIntForeign * mem.readIntBE renamed to mem.readIntBig and different API * mem.readIntLE renamed to mem.readIntLittle and different API * introduced mem.readIntSliceNative * introduced mem.readIntSliceForeign * introduced mem.readIntSliceLittle * introduced mem.readIntSliceBig * introduced mem.readIntSlice * mem.writeInt has different parameters and semantics * introduced mem.writeIntNative * introduced mem.writeIntForeign * mem.writeIntBE renamed to mem.readIntBig and different semantics * mem.writeIntLE renamed to mem.readIntLittle and different semantics * introduced mem.writeIntSliceForeign * introduced mem.writeIntSliceNative * introduced mem.writeIntSliceBig * introduced mem.writeIntSliceLittle * introduced mem.writeIntSlice * removed mem.endianSwapIfLe * removed mem.endianSwapIfBe * removed mem.endianSwapIf * added mem.littleToNative * added mem.bigToNative * added mem.toNative * added mem.nativeTo * added mem.nativeToLittle * added mem.nativeToBig
2018-12-13 09:19:46 +08:00
assert((try parseIp4("127.0.0.1")) == mem.bigToNative(u32, 0x7f000001));
2018-03-07 16:55:52 +08:00
testParseIp4Fail("256.0.0.1", error.Overflow);
testParseIp4Fail("x.0.0.1", error.InvalidCharacter);
testParseIp4Fail("127.0.0.1.1", error.InvalidEnd);
testParseIp4Fail("127.0.0.", error.Incomplete);
testParseIp4Fail("100..0.1", error.InvalidCharacter);
}
fn testParseIp4Fail(buf: []const u8, expected_err: anyerror) void {
2018-03-07 16:55:52 +08:00
if (parseIp4(buf)) |_| {
@panic("expected error");
} else |e| {
assert(e == expected_err);
}
2018-03-07 16:55:52 +08:00
}
2018-03-07 16:55:52 +08:00
test "std.net.parseIp6" {
const addr = try parseIp6("FF01:0:0:0:0:0:0:FB");
assert(addr.addr[0] == 0xff);
assert(addr.addr[1] == 0x01);
assert(addr.addr[2] == 0x00);
}