feature. See also
. The project being documented here (as the example) is the Zig library itself.
Threaded.openSocketPosix
fn openSocketPosix(
family: posix.sa_family_t,
options: IpAddress.BindOptions,
) error
File
Code
fn openSocketPosix(
family: posix.sa_family_t,
options: IpAddress.BindOptions,
) error{
AddressFamilyUnsupported,
ProtocolUnsupportedBySystem,
ProcessFdQuotaExceeded,
SystemFdQuotaExceeded,
SystemResources,
ProtocolUnsupportedByAddressFamily,
SocketModeUnsupported,
OptionUnsupported,
Unexpected,
Canceled,
}!posix.socket_t {
const mode, const protocol = try posixSocketModeProtocol(family, options.mode, options.protocol);
const flags: u32 = mode | if (socket_flags_unsupported) 0 else posix.SOCK.CLOEXEC;
const syscall: Syscall = try .start();
const socket_fd = while (true) {
const rc = posix.system.socket(family, flags, protocol);
switch (posix.errno(rc)) {
.SUCCESS => {
syscall.finish();
const fd: posix.fd_t = @intCast(rc);
errdefer closeFd(fd);
if (socket_flags_unsupported) try setCloexec(fd);
break fd;
},
.INTR => {
try syscall.checkCancel();
continue;
},
.AFNOSUPPORT => return syscall.fail(error.AddressFamilyUnsupported),
.INVAL => return syscall.fail(error.ProtocolUnsupportedBySystem),
.MFILE => return syscall.fail(error.ProcessFdQuotaExceeded),
.NFILE => return syscall.fail(error.SystemFdQuotaExceeded),
.NOBUFS => return syscall.fail(error.SystemResources),
.NOMEM => return syscall.fail(error.SystemResources),
.PROTONOSUPPORT => return syscall.fail(error.ProtocolUnsupportedByAddressFamily),
.PROTOTYPE => return syscall.fail(error.SocketModeUnsupported),
else => |err| return syscall.unexpectedErrno(err),
}
};
errdefer closeFd(socket_fd);
if (options.ip6_only) |ip6_only| {
if (posix.IPV6 == void) return error.OptionUnsupported;
try setSocketOptionPosix(socket_fd, posix.IPPROTO.IPV6, posix.IPV6.V6ONLY, @intFromBool(ip6_only));
}
return socket_fd;
}