Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/os, x/net: layout tcp, ipv4/ipv6, and socket abstractions #8649

Merged
merged 12 commits into from
May 3, 2021
Merged
2 changes: 1 addition & 1 deletion lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub const CallingConvention = enum {
APCS,
AAPCS,
AAPCSVFP,
SysV
SysV,
};

/// This data structure is used by the Zig language code generation and
Expand Down
6 changes: 2 additions & 4 deletions lib/std/compress/deflate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -662,14 +662,12 @@ test "lengths overflow" {
// malformed final dynamic block, tries to write 321 code lengths (MAXCODES is 316)
// f dy hlit hdist hclen 16 17 18 0 (18) x138 (18) x138 (18) x39 (16) x6
// 1 10 11101 11101 0000 010 010 010 010 (11) 1111111 (11) 1111111 (11) 0011100 (01) 11
const stream = [_]u8{
0b11101101, 0b00011101, 0b00100100, 0b11101001, 0b11111111, 0b11111111, 0b00111001, 0b00001110
};
const stream = [_]u8{ 0b11101101, 0b00011101, 0b00100100, 0b11101001, 0b11111111, 0b11111111, 0b00111001, 0b00001110 };

const reader = std.io.fixedBufferStream(&stream).reader();
var window: [0x8000]u8 = undefined;
var inflate = inflateStream(reader, &window);

var buf: [1]u8 = undefined;
std.testing.expectError(error.InvalidLength, inflate.read(&buf));
std.testing.expectError(error.InvalidLength, inflate.read(&buf));
}
7 changes: 7 additions & 0 deletions lib/std/os/bits/darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,13 @@ pub const SO_RCVTIMEO = 0x1006;
pub const SO_ERROR = 0x1007;
pub const SO_TYPE = 0x1008;

pub const SO_NREAD = 0x1020;
pub const SO_NKE = 0x1021;
pub const SO_NOSIGPIPE = 0x1022;
pub const SO_NOADDRERR = 0x1023;
pub const SO_NWRITE = 0x1024;
pub const SO_REUSESHAREUID = 0x1025;

fn wstatus(x: u32) u32 {
return x & 0o177;
}
Expand Down
39 changes: 35 additions & 4 deletions lib/std/os/bits/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub usingnamespace @import("linux/prctl.zig");
pub usingnamespace @import("linux/securebits.zig");

const is_mips = builtin.arch.isMIPS();
const is_ppc = builtin.arch.isPPC();
const is_ppc64 = builtin.arch.isPPC64();
const is_sparc = builtin.arch.isSPARC();

Expand Down Expand Up @@ -458,7 +459,39 @@ pub const AF_QIPCRTR = PF_QIPCRTR;
pub const AF_SMC = PF_SMC;
pub const AF_MAX = PF_MAX;

pub usingnamespace if (!is_mips)
pub usingnamespace if (is_mips)
struct {}
else if (is_ppc or is_ppc64)
struct {
pub const SO_DEBUG = 1;
pub const SO_REUSEADDR = 2;
pub const SO_TYPE = 3;
pub const SO_ERROR = 4;
pub const SO_DONTROUTE = 5;
pub const SO_BROADCAST = 6;
pub const SO_SNDBUF = 7;
pub const SO_RCVBUF = 8;
pub const SO_KEEPALIVE = 9;
pub const SO_OOBINLINE = 10;
pub const SO_NO_CHECK = 11;
pub const SO_PRIORITY = 12;
pub const SO_LINGER = 13;
pub const SO_BSDCOMPAT = 14;
pub const SO_REUSEPORT = 15;
pub const SO_RCVLOWAT = 16;
pub const SO_SNDLOWAT = 17;
pub const SO_RCVTIMEO = 18;
pub const SO_SNDTIMEO = 19;
pub const SO_PASSCRED = 20;
pub const SO_PEERCRED = 21;
pub const SO_ACCEPTCONN = 30;
pub const SO_PEERSEC = 31;
pub const SO_SNDBUFFORCE = 32;
pub const SO_RCVBUFFORCE = 33;
pub const SO_PROTOCOL = 38;
pub const SO_DOMAIN = 39;
}
else
struct {
pub const SO_DEBUG = 1;
pub const SO_REUSEADDR = 2;
Expand Down Expand Up @@ -487,9 +520,7 @@ pub usingnamespace if (!is_mips)
pub const SO_RCVBUFFORCE = 33;
pub const SO_PROTOCOL = 38;
pub const SO_DOMAIN = 39;
}
else
struct {};
};

pub const SO_SECURITY_AUTHENTICATION = 22;
pub const SO_SECURITY_ENCRYPTION_TRANSPORT = 23;
Expand Down
1 change: 1 addition & 0 deletions lib/std/os/bits/linux/arm64.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
const std = @import("../../../std.zig");
const linux = std.os.linux;
const socklen_t = linux.socklen_t;
const sockaddr = linux.sockaddr;
const iovec = linux.iovec;
const iovec_const = linux.iovec_const;
const uid_t = linux.uid_t;
Expand Down
5 changes: 5 additions & 0 deletions lib/std/os/bits/linux/riscv64.zig
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ pub const timespec = extern struct {
tv_nsec: isize,
};

pub const timeval = extern struct {
tv_sec: time_t,
tv_usec: i64,
};

pub const Flock = extern struct {
l_type: i16,
l_whence: i16,
Expand Down
5 changes: 5 additions & 0 deletions lib/std/os/bits/windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ pub const timespec = extern struct {
tv_nsec: c_long,
};

pub const timeval = extern struct {
tv_sec: c_long,
tv_usec: c_long,
};

pub const sig_atomic_t = c_int;

/// maximum signal number + 1
Expand Down
24 changes: 23 additions & 1 deletion lib/std/x.zig
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
pub const os = @import("x/os/os.zig");
// SPDX-License-Identifier: MIT
// Copyright (c) 2015-2021 Zig Contributors
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
// The MIT license requires this copyright notice to be included in all copies
// and substantial portions of the software.

const std = @import("std.zig");

pub const os = struct {
pub const Socket = @import("x/os/Socket.zig");
pub usingnamespace @import("x/os/net.zig");
};

pub const net = struct {
pub const ip = @import("x/net/ip.zig");
pub const tcp = @import("x/net/tcp.zig");
};

test {
inline for (.{ os, net }) |module| {
std.testing.refAllDecls(module);
}
}
59 changes: 59 additions & 0 deletions lib/std/x/net/ip.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2015-2021 Zig Contributors
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
// The MIT license requires this copyright notice to be included in all copies
// and substantial portions of the software.

const std = @import("../../std.zig");

const IPv4 = std.x.os.IPv4;
const IPv6 = std.x.os.IPv6;
const Socket = std.x.os.Socket;

/// A generic IP abstraction.
const ip = @This();

/// A union of all eligible types of IP addresses.
pub const Address = union(enum) {
ipv4: IPv4.Address,
ipv6: IPv6.Address,

/// Instantiate a new address with a IPv4 host and port.
pub fn initIPv4(host: IPv4, port: u16) Address {
return .{ .ipv4 = .{ .host = host, .port = port } };
}

/// Instantiate a new address with a IPv6 host and port.
pub fn initIPv6(host: IPv6, port: u16) Address {
return .{ .ipv6 = .{ .host = host, .port = port } };
}

/// Re-interpret a generic socket address into an IP address.
pub fn from(address: Socket.Address) ip.Address {
return switch (address) {
.ipv4 => |ipv4_address| .{ .ipv4 = ipv4_address },
.ipv6 => |ipv6_address| .{ .ipv6 = ipv6_address },
};
}

/// Re-interpret an IP address into a generic socket address.
pub fn into(self: ip.Address) Socket.Address {
return switch (self) {
.ipv4 => |ipv4_address| .{ .ipv4 = ipv4_address },
.ipv6 => |ipv6_address| .{ .ipv6 = ipv6_address },
};
}

/// Implements the `std.fmt.format` API.
pub fn format(
self: ip.Address,
comptime layout: []const u8,
opts: fmt.FormatOptions,
writer: anytype,
) !void {
switch (self) {
.ipv4 => |address| try fmt.format(writer, "{}:{}", .{ address.host, address.port }),
.ipv6 => |address| try fmt.format(writer, "{}:{}", .{ address.host, address.port }),
}
}
};
Loading