Function socketpair [src]

Prototype

pub fn socketpair(domain: u32, socket_type: u32, protocol: u32) SocketError![2]socket_t

Parameters

domain: u32socket_type: u32protocol: u32

Possible Errors

AccessDenied

Permission to create a socket of the specified type and/or pro‐tocol is denied.

AddressFamilyNotSupported

The implementation does not support the specified address family.

ProcessFdQuotaExceeded

The per-process limit on the number of open file descriptors has been reached.

ProtocolFamilyNotAvailable

Unknown protocol, or protocol family not available.

ProtocolNotSupported

The protocol type or the specified protocol is not supported within this domain.

SocketTypeNotSupported

The socket type is not supported by the protocol.

SystemFdQuotaExceeded

The system-wide limit on the total number of open files has been reached.

SystemResources

Insufficient memory is available. The socket cannot be created until sufficient resources are freed.

Unexpected UnexpectedError

The Operating System returned an undocumented error code.

This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.

When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.

Source

pub fn socketpair(domain: u32, socket_type: u32, protocol: u32) SocketError![2]socket_t { // Note to the future: we could provide a shim here for e.g. windows which // creates a listening socket, then creates a second socket and connects it // to the listening socket, and then returns the two. if (@TypeOf(system.socketpair) == void) @compileError("socketpair() not supported by this OS"); // I'm not really sure if haiku supports flags here. I'm following the // existing filter here from pipe2(), because it sure seems like it // supports flags there too, but haiku can be hard to understand. const have_sock_flags = !builtin.target.os.tag.isDarwin() and native_os != .haiku; const filtered_sock_type = if (!have_sock_flags) socket_type & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC) else socket_type; var socks: [2]socket_t = undefined; const rc = system.socketpair(domain, filtered_sock_type, protocol, &socks); switch (errno(rc)) { .SUCCESS => { errdefer close(socks[0]); errdefer close(socks[1]); if (!have_sock_flags) { try setSockFlags(socks[0], socket_type); try setSockFlags(socks[1], socket_type); } return socks; }, .ACCES => return error.AccessDenied, .AFNOSUPPORT => return error.AddressFamilyNotSupported, .INVAL => return error.ProtocolFamilyNotAvailable, .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .PROTONOSUPPORT => return error.ProtocolNotSupported, .PROTOTYPE => return error.SocketTypeNotSupported, else => |err| return unexpectedErrno(err), } }