Commit 8ef9d98e6d

lithdew <kenta@lithdew.net>
2021-04-30 14:59:02
x/os: fix compile errors on mac and linux
Use i32 instead of isize for os.timeval's for socket read/write timeouts. Add a comptime check to resolveScopeID to see if `IFNAMESIZE` is available on the host. If it is not available, return an error indicating that resolving the scope ID of a IPv6 address is not yet supported on the host platform.
1 parent 76304a3
Changed files (2)
lib
lib/std/x/os/net.zig
@@ -17,18 +17,21 @@ const testing = std.testing;
 /// an error if either resolution fails, or if the interface name is
 /// too long.
 pub fn resolveScopeID(name: []const u8) !u32 {
-    if (name.len >= os.IFNAMESIZE - 1) return error.NameTooLong;
+    if (comptime @hasDecl(os, "IFNAMESIZE")) {
+        if (name.len >= os.IFNAMESIZE - 1) return error.NameTooLong;
 
-    const fd = try os.socket(os.AF_UNIX, os.SOCK_DGRAM, 0);
-    defer os.closeSocket(fd);
+        const fd = try os.socket(os.AF_UNIX, os.SOCK_DGRAM, 0);
+        defer os.closeSocket(fd);
 
-    var f: os.ifreq = undefined;
-    mem.copy(u8, &f.ifrn.name, name);
-    f.ifrn.name[name.len] = 0;
+        var f: os.ifreq = undefined;
+        mem.copy(u8, &f.ifrn.name, name);
+        f.ifrn.name[name.len] = 0;
 
-    try os.ioctl_SIOCGIFINDEX(fd, &f);
+        try os.ioctl_SIOCGIFINDEX(fd, &f);
 
-    return @bitCast(u32, f.ifru.ivalue);
+        return @bitCast(u32, f.ifru.ivalue);
+    }
+    return error.Unsupported;
 }
 
 /// An IPv4 address comprised of 4 bytes.
lib/std/x/os/Socket.zig
@@ -273,8 +273,8 @@ pub fn setReadBufferSize(self: Socket, size: u32) !void {
 /// to the socket will thereafter return `error.WouldBlock` should the timeout be exceeded.
 pub fn setWriteTimeout(self: Socket, milliseconds: usize) !void {
     const timeout = os.timeval{
-        .tv_sec = @intCast(isize, milliseconds / time.ms_per_s),
-        .tv_usec = @intCast(isize, (milliseconds % time.ms_per_s) * time.us_per_ms),
+        .tv_sec = @intCast(i32, milliseconds / time.ms_per_s),
+        .tv_usec = @intCast(i32, (milliseconds % time.ms_per_s) * time.us_per_ms),
     };
 
     return os.setsockopt(self.fd, os.SOL_SOCKET, os.SO_SNDTIMEO, mem.asBytes(&timeout));
@@ -286,8 +286,8 @@ pub fn setWriteTimeout(self: Socket, milliseconds: usize) !void {
 /// exceeded.
 pub fn setReadTimeout(self: Socket, milliseconds: usize) !void {
     const timeout = os.timeval{
-        .tv_sec = @intCast(isize, milliseconds / time.ms_per_s),
-        .tv_usec = @intCast(isize, (milliseconds % time.ms_per_s) * time.us_per_ms),
+        .tv_sec = @intCast(i32, milliseconds / time.ms_per_s),
+        .tv_usec = @intCast(i32, (milliseconds % time.ms_per_s) * time.us_per_ms),
     };
 
     return os.setsockopt(self.fd, os.SOL_SOCKET, os.SO_RCVTIMEO, mem.asBytes(&timeout));