mirror of
https://github.com/ziglang/zig.git
synced 2026-02-20 08:14:48 +00:00
Merge pull request #5475 from marler8997/windowsDns
support name resolution on windows
This commit is contained in:
commit
c6764fd254
@ -194,6 +194,20 @@ pub usingnamespace switch (builtin.os.tag) {
|
||||
pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
|
||||
pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;
|
||||
},
|
||||
.windows => struct {
|
||||
// TODO: copied the else case and removed the socket function (because its in ws2_32)
|
||||
// need to verify which of these is actually supported on windows
|
||||
pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int;
|
||||
pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int;
|
||||
pub extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int;
|
||||
pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int;
|
||||
pub extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
|
||||
pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
|
||||
pub extern "c" fn sched_yield() c_int;
|
||||
pub extern "c" fn sigaction(sig: c_int, noalias act: *const Sigaction, noalias oact: ?*Sigaction) c_int;
|
||||
pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
|
||||
pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;
|
||||
},
|
||||
else => struct {
|
||||
pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int;
|
||||
pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int;
|
||||
|
||||
@ -406,7 +406,8 @@ pub fn tcpConnectToHost(allocator: *mem.Allocator, name: []const u8, port: u16)
|
||||
|
||||
pub fn tcpConnectToAddress(address: Address) !fs.File {
|
||||
const nonblock = if (std.io.is_async) os.SOCK_NONBLOCK else 0;
|
||||
const sock_flags = os.SOCK_STREAM | os.SOCK_CLOEXEC | nonblock;
|
||||
const sock_flags = os.SOCK_STREAM | nonblock |
|
||||
(if (builtin.os.tag == .windows) 0 else os.SOCK_CLOEXEC);
|
||||
const sockfd = try os.socket(address.any.family, sock_flags, os.IPPROTO_TCP);
|
||||
errdefer os.close(sockfd);
|
||||
try os.connect(sockfd, &address.any, address.getOsSockLen());
|
||||
@ -431,16 +432,16 @@ pub fn getAddressList(allocator: *mem.Allocator, name: []const u8, port: u16) !*
|
||||
const arena = &result.arena.allocator;
|
||||
errdefer result.arena.deinit();
|
||||
|
||||
if (builtin.link_libc) {
|
||||
const c = std.c;
|
||||
if (builtin.os.tag == .windows or builtin.link_libc) {
|
||||
const name_c = try std.cstr.addNullByte(allocator, name);
|
||||
defer allocator.free(name_c);
|
||||
|
||||
const port_c = try std.fmt.allocPrint(allocator, "{}\x00", .{port});
|
||||
defer allocator.free(port_c);
|
||||
|
||||
const sys = if (builtin.os.tag == .windows) os.windows.ws2_32 else os.system;
|
||||
const hints = os.addrinfo{
|
||||
.flags = c.AI_NUMERICSERV,
|
||||
.flags = sys.AI_NUMERICSERV,
|
||||
.family = os.AF_UNSPEC,
|
||||
.socktype = os.SOCK_STREAM,
|
||||
.protocol = os.IPPROTO_TCP,
|
||||
@ -450,8 +451,20 @@ pub fn getAddressList(allocator: *mem.Allocator, name: []const u8, port: u16) !*
|
||||
.next = null,
|
||||
};
|
||||
var res: *os.addrinfo = undefined;
|
||||
switch (os.system.getaddrinfo(name_c.ptr, @ptrCast([*:0]const u8, port_c.ptr), &hints, &res)) {
|
||||
@intToEnum(os.system.EAI, 0) => {},
|
||||
const rc = sys.getaddrinfo(name_c.ptr, @ptrCast([*:0]const u8, port_c.ptr), &hints, &res);
|
||||
if (builtin.os.tag == .windows) switch (@intToEnum(os.windows.ws2_32.WinsockError, @intCast(u16, rc))) {
|
||||
@intToEnum(os.windows.ws2_32.WinsockError, 0) => {},
|
||||
.WSATRY_AGAIN => return error.TemporaryNameServerFailure,
|
||||
.WSANO_RECOVERY => return error.NameServerFailure,
|
||||
.WSAEAFNOSUPPORT => return error.AddressFamilyNotSupported,
|
||||
.WSA_NOT_ENOUGH_MEMORY => return error.OutOfMemory,
|
||||
.WSAHOST_NOT_FOUND => return error.UnknownHostName,
|
||||
.WSATYPE_NOT_FOUND => return error.ServiceUnavailable,
|
||||
.WSAEINVAL => unreachable,
|
||||
.WSAESOCKTNOSUPPORT => unreachable,
|
||||
else => |err| return os.windows.unexpectedWSAError(err),
|
||||
} else switch (rc) {
|
||||
@intToEnum(sys.EAI, 0) => {},
|
||||
.ADDRFAMILY => return error.HostLacksNetworkAddresses,
|
||||
.AGAIN => return error.TemporaryNameServerFailure,
|
||||
.BADFLAGS => unreachable, // Invalid hints
|
||||
@ -467,7 +480,7 @@ pub fn getAddressList(allocator: *mem.Allocator, name: []const u8, port: u16) !*
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
defer os.system.freeaddrinfo(res);
|
||||
defer sys.freeaddrinfo(res);
|
||||
|
||||
const addr_count = blk: {
|
||||
var count: usize = 0;
|
||||
|
||||
@ -68,7 +68,10 @@ test "parse and render IPv4 addresses" {
|
||||
}
|
||||
|
||||
test "resolve DNS" {
|
||||
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) {
|
||||
if (std.builtin.os.tag == .windows) {
|
||||
_ = try std.os.windows.WSAStartup(2, 2);
|
||||
}
|
||||
if (builtin.os.tag == .wasi) {
|
||||
// DNS resolution not implemented on Windows yet.
|
||||
return error.SkipZigTest;
|
||||
}
|
||||
|
||||
@ -69,6 +69,8 @@ else switch (builtin.os.tag) {
|
||||
|
||||
pub usingnamespace @import("os/bits.zig");
|
||||
|
||||
pub const socket_t = if (builtin.os.tag == .windows) windows.ws2_32.SOCKET else fd_t;
|
||||
|
||||
/// See also `getenv`. Populated by startup code before main().
|
||||
/// TODO this is a footgun because the value will be undefined when using `zig build-lib`.
|
||||
/// https://github.com/ziglang/zig/issues/4524
|
||||
@ -2445,7 +2447,33 @@ pub const SocketError = error{
|
||||
SocketTypeNotSupported,
|
||||
} || UnexpectedError;
|
||||
|
||||
pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!fd_t {
|
||||
pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t {
|
||||
if (builtin.os.tag == .windows) {
|
||||
// NOTE: windows translates the SOCK_NONBLOCK/SOCK_CLOEXEC flags into windows-analagous operations
|
||||
const filtered_sock_type = socket_type & ~@as(u32, SOCK_NONBLOCK | SOCK_CLOEXEC);
|
||||
const flags : u32 = if ((socket_type & SOCK_CLOEXEC) != 0) windows.ws2_32.WSA_FLAG_NO_HANDLE_INHERIT else 0;
|
||||
const rc = windows.ws2_32.WSASocketW(@intCast(c_int, domain), @intCast(c_int, filtered_sock_type),
|
||||
@intCast(c_int, protocol), null, 0, flags);
|
||||
if (rc == windows.ws2_32.INVALID_SOCKET) switch (windows.ws2_32.WSAGetLastError()) {
|
||||
.WSAEMFILE => return error.ProcessFdQuotaExceeded,
|
||||
.WSAENOBUFS => return error.SystemResources,
|
||||
.WSAEAFNOSUPPORT => return error.AddressFamilyNotSupported,
|
||||
.WSAEPROTONOSUPPORT => return error.ProtocolNotSupported,
|
||||
else => |err| return windows.unexpectedWSAError(err),
|
||||
};
|
||||
errdefer windows.closesocket(rc) catch unreachable;
|
||||
if ((socket_type & SOCK_NONBLOCK) != 0) {
|
||||
var mode : c_ulong = 1; // nonblocking
|
||||
if (windows.ws2_32.SOCKET_ERROR == windows.ws2_32.ioctlsocket(rc, windows.ws2_32.FIONBIO, &mode)) {
|
||||
switch (windows.ws2_32.WSAGetLastError()) {
|
||||
// have not identified any error codes that should be handled yet
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
const have_sock_flags = comptime !std.Target.current.isDarwin();
|
||||
const filtered_sock_type = if (!have_sock_flags)
|
||||
socket_type & ~@as(u32, SOCK_NONBLOCK | SOCK_CLOEXEC)
|
||||
@ -2820,7 +2848,30 @@ pub const ConnectError = error{
|
||||
} || UnexpectedError;
|
||||
|
||||
/// Initiate a connection on a socket.
|
||||
pub fn connect(sockfd: fd_t, sock_addr: *const sockaddr, len: socklen_t) ConnectError!void {
|
||||
pub fn connect(sockfd: socket_t, sock_addr: *const sockaddr, len: socklen_t) ConnectError!void {
|
||||
if (builtin.os.tag == .windows) {
|
||||
const rc = windows.ws2_32.connect(sockfd, sock_addr, len);
|
||||
if (rc == 0) return;
|
||||
switch (windows.ws2_32.WSAGetLastError()) {
|
||||
.WSAEADDRINUSE => return error.AddressInUse,
|
||||
.WSAEADDRNOTAVAIL => return error.AddressNotAvailable,
|
||||
.WSAECONNREFUSED => return error.ConnectionRefused,
|
||||
.WSAETIMEDOUT => return error.ConnectionTimedOut,
|
||||
.WSAEHOSTUNREACH // TODO: should we return NetworkUnreachable in this case as well?
|
||||
,.WSAENETUNREACH => return error.NetworkUnreachable,
|
||||
.WSAEFAULT => unreachable,
|
||||
.WSAEINVAL => unreachable,
|
||||
.WSAEISCONN => unreachable,
|
||||
.WSAENOTSOCK => unreachable,
|
||||
.WSAEWOULDBLOCK => unreachable,
|
||||
.WSAEACCES => unreachable,
|
||||
.WSAENOBUFS => return error.SystemResources,
|
||||
.WSAEAFNOSUPPORT => return error.AddressFamilyNotSupported,
|
||||
else => |err| return windows.unexpectedWSAError(err),
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
switch (errno(system.connect(sockfd, sock_addr, len))) {
|
||||
0 => return,
|
||||
|
||||
@ -177,6 +177,8 @@ pub const sockaddr_un = ws2_32.sockaddr_un;
|
||||
pub const in6_addr = [16]u8;
|
||||
pub const in_addr = u32;
|
||||
|
||||
pub const addrinfo = ws2_32.addrinfo;
|
||||
|
||||
pub const AF_UNSPEC = ws2_32.AF_UNSPEC;
|
||||
pub const AF_UNIX = ws2_32.AF_UNIX;
|
||||
pub const AF_INET = ws2_32.AF_INET;
|
||||
@ -219,6 +221,15 @@ pub const SOCK_RAW = ws2_32.SOCK_RAW;
|
||||
pub const SOCK_RDM = ws2_32.SOCK_RDM;
|
||||
pub const SOCK_SEQPACKET = ws2_32.SOCK_SEQPACKET;
|
||||
|
||||
/// WARNING: this flag is not supported by windows socket functions directly,
|
||||
/// it is only supported by std.os.socket. Be sure that this value does
|
||||
/// not share any bits with any of the SOCK_* values.
|
||||
pub const SOCK_CLOEXEC = 0x10000;
|
||||
/// WARNING: this flag is not supported by windows socket functions directly,
|
||||
/// it is only supported by std.os.socket. Be sure that this value does
|
||||
/// not share any bits with any of the SOCK_* values.
|
||||
pub const SOCK_NONBLOCK = 0x20000;
|
||||
|
||||
pub const IPPROTO_ICMP = ws2_32.IPPROTO_ICMP;
|
||||
pub const IPPROTO_IGMP = ws2_32.IPPROTO_IGMP;
|
||||
pub const BTHPROTO_RFCOMM = ws2_32.BTHPROTO_RFCOMM;
|
||||
|
||||
@ -163,11 +163,35 @@ pub const IPPROTO_UDP = 17;
|
||||
pub const IPPROTO_ICMPV6 = 58;
|
||||
pub const IPPROTO_RM = 113;
|
||||
|
||||
pub const AI_PASSIVE = 0x00001;
|
||||
pub const AI_CANONNAME = 0x00002;
|
||||
pub const AI_NUMERICHOST = 0x00004;
|
||||
pub const AI_NUMERICSERV = 0x00008;
|
||||
pub const AI_ADDRCONFIG = 0x00400;
|
||||
pub const AI_V4MAPPED = 0x00800;
|
||||
pub const AI_NON_AUTHORITATIVE = 0x04000;
|
||||
pub const AI_SECURE = 0x08000;
|
||||
pub const AI_RETURN_PREFERRED_NAMES = 0x10000;
|
||||
pub const AI_DISABLE_IDN_ENCODING = 0x80000;
|
||||
|
||||
pub const FIONBIO = -2147195266;
|
||||
|
||||
pub const sockaddr = extern struct {
|
||||
family: ADDRESS_FAMILY,
|
||||
data: [14]u8,
|
||||
};
|
||||
|
||||
pub const addrinfo = extern struct {
|
||||
flags: i32,
|
||||
family: i32,
|
||||
socktype: i32,
|
||||
protocol: i32,
|
||||
addrlen: usize,
|
||||
canonname: ?[*:0]u8,
|
||||
addr: ?*sockaddr,
|
||||
next: ?*addrinfo,
|
||||
};
|
||||
|
||||
/// IPv4 socket address
|
||||
pub const sockaddr_in = extern struct {
|
||||
family: ADDRESS_FAMILY = AF_INET,
|
||||
@ -752,3 +776,17 @@ pub extern "ws2_32" fn WSASendTo(
|
||||
lpOverlapped: ?*WSAOVERLAPPED,
|
||||
lpCompletionRoutine: ?WSAOVERLAPPED_COMPLETION_ROUTINE,
|
||||
) callconv(.Stdcall) c_int;
|
||||
pub extern "ws2_32" fn getaddrinfo(
|
||||
pNodeName: [*:0]const u8,
|
||||
pServiceName: [*:0]const u8,
|
||||
pHints: *const addrinfo,
|
||||
ppResult: **addrinfo,
|
||||
) callconv(.Stdcall) i32;
|
||||
pub extern "ws2_32" fn freeaddrinfo(
|
||||
pAddrInfo: *addrinfo,
|
||||
) callconv(.Stdcall) void;
|
||||
pub extern "ws2_32" fn ioctlsocket(
|
||||
s: SOCKET,
|
||||
cmd: c_long,
|
||||
argp: *c_ulong,
|
||||
) callconv(.Stdcall) c_int;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user