aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-10-30 12:33:47 -0400
committerAndrew Kelley <andrew@ziglang.org>2019-10-30 12:33:47 -0400
commitf5ff36271bce54eaec6f20cdff4fa4efb833c0d4 (patch)
tree8773174d52c67f23b723a1f59a437fc6c6d52b58 /lib
parent0fb1388031ec98af77fe5e0ae8459c983010d1a8 (diff)
parent4e0c2ed443a0c13030decf8a9d53aff28f415005 (diff)
downloadzig-f5ff36271bce54eaec6f20cdff4fa4efb833c0d4.tar.gz
zig-f5ff36271bce54eaec6f20cdff4fa4efb833c0d4.zip
Merge branch 'testAddresses' of https://github.com/marler8997/zig into std.net
Diffstat (limited to 'lib')
-rw-r--r--lib/std/net.zig30
-rw-r--r--lib/std/net/test.zig18
2 files changed, 38 insertions, 10 deletions
diff --git a/lib/std/net.zig b/lib/std/net.zig
index 5b1037633c..c741941328 100644
--- a/lib/std/net.zig
+++ b/lib/std/net.zig
@@ -29,16 +29,30 @@ pub const Address = struct {
//pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);
pub fn initIp4(ip4: u32, _port: u16) Address {
- return Address{
- .os_addr = os.sockaddr{
- .in = os.sockaddr_in{
- .family = os.AF_INET,
- .port = mem.nativeToBig(u16, _port),
- .addr = ip4,
- .zero = [_]u8{0} ** 8,
+ switch (builtin.os) {
+ .macosx, .ios, .watchos, .tvos, .freebsd, .netbsd => return Address{
+ .os_addr = os.sockaddr{
+ .in = os.sockaddr_in{
+ .len = @sizeOf(os.sockaddr_in),
+ .family = os.AF_INET,
+ .port = mem.nativeToBig(u16, _port),
+ .addr = ip4,
+ .zero = [_]u8{0} ** 8,
+ },
},
},
- };
+ .linux => return Address{
+ .os_addr = os.sockaddr{
+ .in = os.sockaddr_in{
+ .family = os.AF_INET,
+ .port = mem.nativeToBig(u16, _port),
+ .addr = ip4,
+ .zero = [_]u8{0} ** 8,
+ },
+ },
+ },
+ else => @compileError("Address.initIp4 not implemented for this platform"),
+ }
}
pub fn initIp6(ip6: Ip6Addr, _port: u16) Address {
diff --git a/lib/std/net/test.zig b/lib/std/net/test.zig
index aced17f1b5..6322401577 100644
--- a/lib/std/net/test.zig
+++ b/lib/std/net/test.zig
@@ -3,7 +3,7 @@ const net = std.net;
const mem = std.mem;
const testing = std.testing;
-test "std.net.parseIp4" {
+test "parseIp4" {
testing.expect((try net.parseIp4("127.0.0.1")) == mem.bigToNative(u32, 0x7f000001));
testParseIp4Fail("256.0.0.1", error.Overflow);
@@ -21,7 +21,7 @@ fn testParseIp4Fail(buf: []const u8, expected_err: anyerror) void {
}
}
-test "std.net.parseIp6" {
+test "parseIp6" {
const ip6 = try net.parseIp6("FF01:0:0:0:0:0:0:FB");
const addr = net.Address.initIp6(ip6, 80);
var buf: [100]u8 = undefined;
@@ -29,6 +29,20 @@ test "std.net.parseIp6" {
std.testing.expect(mem.eql(u8, "[ff01::fb]:80", printed));
}
+test "ip4s" {
+ var buffer: [18]u8 = undefined;
+ for ([_][]const u8{
+ "0.0.0.0",
+ "255.255.255.255",
+ "1.2.3.4",
+ "123.255.0.91",
+ }) |ip| {
+ var addr = Address.initIp4(parseIp4(ip) catch unreachable, 0);
+ var newIp = std.fmt.bufPrint(buffer[0..], "{}", addr) catch unreachable;
+ std.testing.expect(std.mem.eql(u8, ip, newIp[0 .. newIp.len - 2]));
+ }
+}
+
test "resolve DNS" {
if (std.builtin.os == .windows) {
// DNS resolution not implemented on Windows yet.