# `./fio-stl/004 sock.h`

35 public symbols.

### Macros

#### `FIO_SOCKET_INVALID`

```c
#define FIO_SOCKET_INVALID INVALID_SOCKET
```

Sentinel value for an invalid socket handle.

_Symbol type:_ `macro`

#### `FIO_SOCK_FD_ISVALID`

```c
#define FIO_SOCK_FD_ISVALID(fd) ((fio_socket_i)(fd) != FIO_SOCKET_INVALID)
```



_Symbol type:_ `macro`

#### `FIO_SOCK_DEFAULT_MAXIMIZE_LIMIT`

```c
#define FIO_SOCK_DEFAULT_MAXIMIZE_LIMIT (1ULL << 24)
```



_Symbol type:_ `macro`

#### `FIO_SOCK_UNIX`

```c
#define FIO_SOCK_UNIX         0
```



_Symbol type:_ `macro`

#### `FIO_SOCK_UNIX_PRIVATE`

```c
#define FIO_SOCK_UNIX_PRIVATE 0
```



_Symbol type:_ `macro`

#### `FIO_SOCK_WAIT_RW`

```c
#define FIO_SOCK_WAIT_RW(fd, timeout_)   \
  fio_sock_wait_io(fd, POLLIN | POLLOUT, timeout_)
```

A helper macro that waits on a single IO with no callbacks (0 = no event)

_Symbol type:_ `macro`

#### `FIO_SOCK_WAIT_R`

```c
#define FIO_SOCK_WAIT_R(fd, timeout_) fio_sock_wait_io(fd, POLLIN, timeout_)
```

A helper macro that waits on a single IO with no callbacks (0 = no event)

_Symbol type:_ `macro`

#### `FIO_SOCK_WAIT_W`

```c
#define FIO_SOCK_WAIT_W(fd, timeout_) fio_sock_wait_io(fd, POLLOUT, timeout_)
```

A helper macro that waits on a single IO with no callbacks (0 = no event)

_Symbol type:_ `macro`

#### `FIO_SOCK_IS_OPEN`

```c
#define FIO_SOCK_IS_OPEN(fd)   \
  (!(fio_sock_wait_io(fd, (POLLOUT | POLLRDHUP), 0) &   \
     (POLLRDHUP | POLLHUP | POLLNVAL)))
```

A helper macro that tests if a socket was closed.

_Symbol type:_ `macro`

### Types

#### `fio_socket_i`

```c
typedef int fio_socket_i
```

Native socket handle type: int on POSIX.

_Symbol type:_ `type`

#### `fio_sock_open_flags_e`

```c
typedef enum {
FIO_SOCK_SERVER = 0,
FIO_SOCK_CLIENT = 1,
FIO_SOCK_NONBLOCK = 2,
FIO_SOCK_TCP = 4,
FIO_SOCK_UDP = 8,
#ifdef AF_UNIX
FIO_SOCK_UNIX = 16,
FIO_SOCK_UNIX_PRIVATE = (16 | 32),
#else
#define FIO_SOCK_UNIX 0
#define FIO_SOCK_UNIX_PRIVATE 0
#endif
} fio_sock_open_flags_e
```

Socket type flags

_Symbol type:_ `type`

### Functions

#### `fio_sock_write`

```c
inline ssize_t fio_sock_write(fio_socket_i fd, const void *buf, size_t len)
```

Acts as POSIX write. Use this function for portability with WinSock2.

_Symbol type:_ `function`

#### `fio_sock_read`

```c
inline ssize_t fio_sock_read(fio_socket_i fd, void *buf, size_t len)
```

Acts as POSIX read. Use this function for portability with WinSock2.

_Symbol type:_ `function`

#### `fio_sock_sendto`

```c
inline ssize_t fio_sock_sendto(fio_socket_i fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen)
```

Acts as POSIX sendto. Use this function for portability with WinSock2.

_Symbol type:_ `function`

#### `fio_sock_recvfrom`

```c
inline ssize_t fio_sock_recvfrom(fio_socket_i fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen)
```

Acts as POSIX recvfrom. Use this function for portability with WinSock2.

_Symbol type:_ `function`

#### `fio_sock_dup`

```c
inline fio_socket_i fio_sock_dup(fio_socket_i fd)
```

Acts as POSIX dup. Sets O_CLOEXEC on the new fd.

_Symbol type:_ `function`

#### `fio_sock_close`

```c
inline int fio_sock_close(fio_socket_i fd)
```

Acts as POSIX close. Use this function for portability with WinSock2.

_Symbol type:_ `function`

#### `fio_sock_accept`

```c
#define fio_sock_accept(fd, addr, addrlen) accept(fd, addr, addrlen)
```

Acts as POSIX accept. Use this macro for portability with WinSock2.

_Note:_ this may be a macro only / macro wrapper for a function.

_Symbol type:_ `macro`

#### `fio_sock_bind`

```c
inline int fio_sock_bind(fio_socket_i fd, const struct sockaddr *addr, socklen_t addrlen)
```

Portable bind. POSIX version.

_Symbol type:_ `function`

#### `fio_sock_connect`

```c
inline int fio_sock_connect(fio_socket_i fd, const struct sockaddr *addr, socklen_t addrlen)
```

Portable connect. POSIX version.

_Symbol type:_ `function`

#### `fio_sock_listen`

```c
inline int fio_sock_listen(fio_socket_i fd, int backlog)
```

Portable listen. POSIX version.

_Symbol type:_ `function`

#### `fio_sock_setsockopt`

```c
inline int fio_sock_setsockopt(fio_socket_i fd, int level, int optname, const void *optval, socklen_t optlen)
```

Portable setsockopt. POSIX version.

_Symbol type:_ `function`

#### `fio_sock_socketpair`

```c
inline int fio_sock_socketpair(fio_socket_i fds[2])
```

Creates a connected socket pair using POSIX socketpair().

_Symbol type:_ `function`

#### `fio_sock_pipe`

```c
inline int fio_sock_pipe(fio_socket_i fds[2])
```

Creates a POSIX pipe. fds[0] = read end, fds[1] = write end.
On Windows, fio_sock_pipe() is defined in the FIO_OS_WIN block above and
delegates to fio_sock_socketpair() (loopback TCP).

_Symbol type:_ `function`

#### `fio_sock_open`

```c
inline fio_socket_i fio_sock_open(const char *restrict address, const char *restrict port, uint16_t flags)
```

Creates a new socket according to the provided flags.

The `port` string will be ignored when `FIO_SOCK_UNIX` is set.

_Symbol type:_ `function`

#### `fio_sock_open2`

```c
fio_socket_i fio_sock_open2(const char *url, uint16_t flags)
```

Creates a new socket, according to the provided flags.

_Symbol type:_ `function`

#### `fio_sock_address_new`

```c
inline struct addrinfo *fio_sock_address_new(const char *restrict address, const char *restrict port, int sock_type)
```

Attempts to resolve an address to a valid IP6 / IP4 address pointer.

The `sock_type` element should be a socket type, such as `SOCK_DGRAM` (UDP)
or `SOCK_STREAM` (TCP/IP).

The address should be freed using `fio_sock_address_free`.

_Symbol type:_ `function`

#### `fio_sock_address_free`

```c
inline void fio_sock_address_free(struct addrinfo *a)
```

Frees the pointer returned by `fio_sock_address_new`.

_Symbol type:_ `function`

#### `fio_sock_peer_addr`

```c
fio_buf_info_s fio_sock_peer_addr(fio_socket_i s)
```

Returns a human readable address representation of the socket's peer address.

On error, returns a NULL buffer with zero length.

Buffer lengths are limited to 63 bytes.

This function is limited in its thread safety to 128 threads / calls.

_Symbol type:_ `function`

#### `fio_sock_open_local`

```c
fio_socket_i fio_sock_open_local(struct addrinfo *addr, int nonblock)
```

Creates a new network socket and binds it to a local address.

_Symbol type:_ `function`

#### `fio_sock_open_remote`

```c
fio_socket_i fio_sock_open_remote(struct addrinfo *addr, int nonblock)
```

Creates a new network socket and connects it to a remote address.

_Symbol type:_ `function`

#### `fio_sock_open_unix`

```c
fio_socket_i fio_sock_open_unix(const char *address, uint16_t flags)
```

Creates a new Unix socket and binds it to a local address.

_Symbol type:_ `function`

#### `fio_sock_set_non_block`

```c
int fio_sock_set_non_block(fio_socket_i fd)
```

Sets a file descriptor / socket to non blocking state.

_Symbol type:_ `function`

#### `fio_sock_maximize_limits`

```c
size_t fio_sock_maximize_limits(size_t maximum_limit)
```

Attempts to maximize the allowed open file limits. returns known limit

_Symbol type:_ `function`

#### `fio_sock_wait_io`

```c
short fio_sock_wait_io(fio_socket_i fd, short events, int timeout)
```

Returns 0 on timeout, -1 on error or the events that are valid.

A zero timeout returns immediately.

Possible events include POLLIN | POLLOUT

Possible return values include POLLIN | POLLOUT | POLLHUP | POLLNVAL

_Symbol type:_ `function`

-----------------------------------------------------
