Commit d5865f5319

Andrew Kelley <andrew@ziglang.org>
2019-10-27 22:31:49
move libc/linux bits around
1 parent 845be4e
Changed files (3)
lib
lib/std/c/linux.zig
@@ -17,6 +17,41 @@ pub const _errno = switch (builtin.abi) {
 
 pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize));
 
+pub const AI_PASSIVE = 0x01;
+pub const AI_CANONNAME = 0x02;
+pub const AI_NUMERICHOST = 0x04;
+pub const AI_V4MAPPED = 0x08;
+pub const AI_ALL = 0x10;
+pub const AI_ADDRCONFIG = 0x20;
+pub const AI_NUMERICSERV = 0x400;
+
+pub const NI_NUMERICHOST = 0x01;
+pub const NI_NUMERICSERV = 0x02;
+pub const NI_NOFQDN = 0x04;
+pub const NI_NAMEREQD = 0x08;
+pub const NI_DGRAM = 0x10;
+pub const NI_NUMERICSCOPE = 0x100;
+
+pub const EAI_BADFLAGS = -1;
+pub const EAI_NONAME = -2;
+pub const EAI_AGAIN = -3;
+pub const EAI_FAIL = -4;
+pub const EAI_FAMILY = -6;
+pub const EAI_SOCKTYPE = -7;
+pub const EAI_SERVICE = -8;
+pub const EAI_MEMORY = -10;
+pub const EAI_SYSTEM = -11;
+pub const EAI_OVERFLOW = -12;
+
+pub const EAI_NODATA = -5;
+pub const EAI_ADDRFAMILY = -9;
+pub const EAI_INPROGRESS = -100;
+pub const EAI_CANCELED = -101;
+pub const EAI_NOTCANCELED = -102;
+pub const EAI_ALLDONE = -103;
+pub const EAI_INTR = -104;
+pub const EAI_IDN_ENCODE = -105;
+
 pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
 pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;
 pub extern "c" fn eventfd(initval: c_uint, flags: c_uint) c_int;
lib/std/os/bits/linux.zig
@@ -1396,41 +1396,6 @@ pub const addrinfo = extern struct {
     next: ?*addrinfo,
 };
 
-pub const AI_PASSIVE = 0x01;
-pub const AI_CANONNAME = 0x02;
-pub const AI_NUMERICHOST = 0x04;
-pub const AI_V4MAPPED = 0x08;
-pub const AI_ALL = 0x10;
-pub const AI_ADDRCONFIG = 0x20;
-pub const AI_NUMERICSERV = 0x400;
-
-pub const NI_NUMERICHOST = 0x01;
-pub const NI_NUMERICSERV = 0x02;
-pub const NI_NOFQDN = 0x04;
-pub const NI_NAMEREQD = 0x08;
-pub const NI_DGRAM = 0x10;
-pub const NI_NUMERICSCOPE = 0x100;
-
-pub const EAI_BADFLAGS = -1;
-pub const EAI_NONAME = -2;
-pub const EAI_AGAIN = -3;
-pub const EAI_FAIL = -4;
-pub const EAI_FAMILY = -6;
-pub const EAI_SOCKTYPE = -7;
-pub const EAI_SERVICE = -8;
-pub const EAI_MEMORY = -10;
-pub const EAI_SYSTEM = -11;
-pub const EAI_OVERFLOW = -12;
-
-pub const EAI_NODATA = -5;
-pub const EAI_ADDRFAMILY = -9;
-pub const EAI_INPROGRESS = -100;
-pub const EAI_CANCELED = -101;
-pub const EAI_NOTCANCELED = -102;
-pub const EAI_ALLDONE = -103;
-pub const EAI_INTR = -104;
-pub const EAI_IDN_ENCODE = -105;
-
 pub const IPPORT_RESERVED = 1024;
 
 pub const IPPROTO_IP = 0;
lib/std/net.zig
@@ -347,6 +347,7 @@ pub fn getAddressList(allocator: *mem.Allocator, name: []const u8, port: u16) !*
     errdefer result.arena.deinit();
 
     if (builtin.link_libc) {
+        const c = std.c;
         const name_c = try std.cstr.addNullByte(allocator, name);
         defer allocator.free(name_c);
 
@@ -354,7 +355,7 @@ pub fn getAddressList(allocator: *mem.Allocator, name: []const u8, port: u16) !*
         defer allocator.free(port_c);
 
         const hints = os.addrinfo{
-            .flags = os.AI_NUMERICSERV,
+            .flags = c.AI_NUMERICSERV,
             .family = os.AF_UNSPEC,
             .socktype = os.SOCK_STREAM,
             .protocol = os.IPPROTO_TCP,
@@ -366,17 +367,17 @@ pub fn getAddressList(allocator: *mem.Allocator, name: []const u8, port: u16) !*
         var res: *os.addrinfo = undefined;
         switch (os.system.getaddrinfo(name_c.ptr, port_c.ptr, &hints, &res)) {
             0 => {},
-            os.EAI_ADDRFAMILY => return error.HostLacksNetworkAddresses,
-            os.EAI_AGAIN => return error.TemporaryNameServerFailure,
-            os.EAI_BADFLAGS => unreachable, // Invalid hints
-            os.EAI_FAIL => return error.NameServerFailure,
-            os.EAI_FAMILY => return error.AddressFamilyNotSupported,
-            os.EAI_MEMORY => return error.OutOfMemory,
-            os.EAI_NODATA => return error.HostLacksNetworkAddresses,
-            os.EAI_NONAME => return error.UnknownName,
-            os.EAI_SERVICE => return error.ServiceUnavailable,
-            os.EAI_SOCKTYPE => unreachable, // Invalid socket type requested in hints
-            os.EAI_SYSTEM => switch (os.errno(-1)) {
+            c.EAI_ADDRFAMILY => return error.HostLacksNetworkAddresses,
+            c.EAI_AGAIN => return error.TemporaryNameServerFailure,
+            c.EAI_BADFLAGS => unreachable, // Invalid hints
+            c.EAI_FAIL => return error.NameServerFailure,
+            c.EAI_FAMILY => return error.AddressFamilyNotSupported,
+            c.EAI_MEMORY => return error.OutOfMemory,
+            c.EAI_NODATA => return error.HostLacksNetworkAddresses,
+            c.EAI_NONAME => return error.UnknownName,
+            c.EAI_SERVICE => return error.ServiceUnavailable,
+            c.EAI_SOCKTYPE => unreachable, // Invalid socket type requested in hints
+            c.EAI_SYSTEM => switch (os.errno(-1)) {
                 else => |e| return os.unexpectedErrno(e),
             },
             else => unreachable,
@@ -485,6 +486,9 @@ fn linuxLookupName(
         });
         if (cnt == 0 and (flags & os.AI_NUMERICHOST) == 0) {
             cnt = try linuxLookupNameFromHosts(buf, canon_buf, canon_len, name, family);
+            if (cnt == 0) {
+                cnt = try linuxLookupNameFromDnsSearch(buf, canon_buf, canon_len, name, family);
+            }
         }
     } else {
         canon_len.* = 0;
@@ -641,3 +645,52 @@ pub fn isValidHostName(hostname: []const u8) bool {
     }
     return true;
 }
+
+fn linuxLookupNameFromDnsSearch(
+    buf: []LookupAddr,
+    canon_buf: []u8,
+    canon_len: *usize,
+    name: []const u8,
+    family: i32,
+) !usize {
+    var search: [256]u8 = undefined;
+    const resolv_conf = try getResolvConf(&search);
+
+    // Count dots, suppress search when >=ndots or name ends in
+    // a dot, which is an explicit request for global scope.
+    //var dots: usize = 0;
+    //for (name) |byte| {
+    //    if (byte == '.') dots += 1;
+    //}
+
+    //if (dots >= conf.ndots || name[l-1]=='.') *search = 0;
+
+    //// Strip final dot for canon, fail if multiple trailing dots.
+    //if (name[l-1]=='.') l--;
+    //if (!l || name[l-1]=='.') return EAI_NONAME;
+
+    //// This can never happen; the caller already checked length.
+    //if (l >= 256) return EAI_NONAME;
+
+    //// Name with search domain appended is setup in canon[]. This both
+    //// provides the desired default canonical name (if the requested
+    //// name is not a CNAME record) and serves as a buffer for passing
+    //// the full requested name to name_from_dns.
+    //memcpy(canon, name, l);
+    //canon[l] = '.';
+
+    //for (p=search; *p; p=z) {
+    //    for (; isspace(*p); p++);
+    //    for (z=p; *z && !isspace(*z); z++);
+    //    if (z==p) break;
+    //    if (z-p < 256 - l - 1) {
+    //        memcpy(canon+l+1, p, z-p);
+    //        canon[z-p+1+l] = 0;
+    //        int cnt = name_from_dns(buf, canon, canon, family, &conf);
+    //        if (cnt) return cnt;
+    //    }
+    //}
+
+    //canon[l] = 0;
+    //return name_from_dns(buf, canon, name, family, &conf);
+}