| author | |
| committer | |
| log | 4a4d2c0d80443a00945beeff4e3acaa9e7ea59cb |
| tree | bcb4f1f7202f51f03d6f40a1df295e952533f71b |
| parent | 631eb6783d1dbc64f031426436ab685008c31ca8 |
| signature | Commit is signed but in an unrecognized format. |
- net: use os.setsockopt()2 files changed, 25 insertions(+), 7 deletions(-)
lib/std/net.zig+5-7| ... | @@ -1323,14 +1323,12 @@ pub const StreamServer = struct { | ... | @@ -1323,14 +1323,12 @@ pub const StreamServer = struct { |
| 1323 | self.sockfd = null; | 1323 | self.sockfd = null; |
| 1324 | } | 1324 | } |
| 1325 | 1325 | ||
| 1326 | // TODO proper interface with errors in std.os | ||
| 1327 | var optval: c_int = 1; | ||
| 1328 | |||
| 1329 | if (self.options.reuse_address) { | 1326 | if (self.options.reuse_address) { |
| 1330 | _ = os.linux.setsockopt( | 1327 | var optval: c_int = 1; |
| 1331 | server.sockfd.?, | 1328 | try os.setsockopt( |
| 1332 | os.linux.SOL_SOCKET, | 1329 | self.sockfd.?, |
| 1333 | os.linux.SO_REUSEADDR, | 1330 | os.SOL_SOCKET, |
| 1331 | os.SO_REUSEADDR, | ||
| 1334 | @ptrCast([*]const u8, &optval), | 1332 | @ptrCast([*]const u8, &optval), |
| 1335 | @sizeOf(c_int), | 1333 | @sizeOf(c_int), |
| 1336 | ); | 1334 | ); |
lib/std/os.zig+20| ... | @@ -3250,3 +3250,23 @@ pub fn sched_yield() SchedYieldError!void { | ... | @@ -3250,3 +3250,23 @@ pub fn sched_yield() SchedYieldError!void { |
| 3250 | else => return error.SystemCannotYield, | 3250 | else => return error.SystemCannotYield, |
| 3251 | } | 3251 | } |
| 3252 | } | 3252 | } |
| 3253 | |||
| 3254 | /// Set a socket's options. | ||
| 3255 | pub fn setsockopt(fd: fd_t, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) SetSockOptError!void { | ||
| 3256 | const rc = system.setsockopt(); | ||
| 3257 | |||
| 3258 | switch (errno(system.setsockopt(fd, level, optname, optval, optlen))) { | ||
| 3259 | 0 => {}, | ||
| 3260 | EBADF => unreachable, | ||
| 3261 | EINVAL => unreachable, | ||
| 3262 | EDOM => return error.TimeoutTooBig, | ||
| 3263 | EISCONN => return error.AlreadyConnected, | ||
| 3264 | ENOPROTOOOPT => return error.InvalidProtocolOption, | ||
| 3265 | ENOTSOCK => return error.NotSocket, | ||
| 3266 | |||
| 3267 | ENOMEM => return error.OutOfMemory, | ||
| 3268 | ENOBUFS => return error.SystemResources, | ||
| 3269 | |||
| 3270 | else => |err| return std.os.unexpectedErrno(err), | ||
| 3271 | } | ||
| 3272 | } |