Module Os.Socket

Socket operations.

Endpoint

TODO. We'd like to have this in B0_std.Net.Endpoint but we need to restructure the sources.

val endpoint_wait_connectable : ?socket_type:Unix.socket_type -> timeout:Mtime.Span.t -> Net.Endpoint.t -> ([ `Ready | `Timeout ], string) Stdlib.result

endpoint_wait_connectable ~timeout ep st blocks until ep becomes connectable or duration timeout elapses.

socket_type defines the kind of socket connection, it defaults to Unix.SOCK_STREAM.

val endpoint_wait_connectable' : ?socket_type:Unix.socket_type -> timeout:Mtime.Span.t -> Net.Endpoint.t -> (unit, string) Stdlib.result

wait_connectable' is like wait_connectable but errors with a message on timeout.

Sockets

val for_endpoint : ?nonblock:bool -> Net.Endpoint.t -> Unix.socket_type -> (Unix.file_descr * bool * Unix.sockaddr option, string) Stdlib.result

for_endpoint ?nonblock e st is Ok (fd, close, addr) with:

  • fd, a file descriptor for the socket. If c is `Fd fd this is fd untouched except for setting or clearing nonblock. Otherwise fd is a new file descriptor set according to nonblock and has close on exec set to true. The socket is not connected, use either connect or bind on it. Alternatively directly use connect_endpoint.
  • close is true if the caller is in charge of closing it. This is false iff c is `Fd _.
  • addr, the socket peer address for the endpoint, if any.

nonblock defaults to false. See also connect_endpoint.

Connecting

val connect_endpoint : ?nonblock:bool -> Net.Endpoint.t -> Unix.socket_type -> (Unix.file_descr * bool * Unix.sockaddr, string) Stdlib.result

connect_endpoint ep st is Ok (fd, close, addr) with:

  • fd, a file descriptor for the socket connected to the endpoint. If c is `Fd fd this is fd untouched except for setting or clearing nonblock, it also checks that the fd is connected and errors otherwise.
  • close is true if the caller is in charge of closing it. This is false iff ep is `Fd _.
  • addr the socket peer address for the endpoint.

See also with_connected_endpoint.

val with_connected_endpoint : ?nonblock:bool -> Net.Endpoint.t -> Unix.socket_type -> (Unix.file_descr -> Unix.sockaddr -> 'a) -> ('a, string) Stdlib.result

with_connected_endpoint ep st f uses connect_endpoint and calls f with the resulting file descriptor and peer address ensuring, even if f raises, that:

  • If needed (see connect_endpoint) the file descriptor ressource is closed after f returns.
  • The Sys.sigpipe signal is ignored during the call to f.
val connect : Unix.file_descr -> Unix.sockaddr -> (unit, string) Stdlib.result

connect fd addr associates the peer addr to the file descriptor fd. Writes on fd send data to addr and reads on fd receive data from addr. See also connect_endpoint.

Listening

val listen_endpoint : ?nonblock:bool -> ?backlog:int -> Net.Endpoint.t -> Unix.socket_type -> (Unix.file_descr * bool * Unix.sockaddr, string) Stdlib.result

listen_endpoint ep st is Ok (fd, close, addr) with:

  • fd, a file descriptor for the socket listening on the endpoint. If ep is `Fd fd this is fd untouched except for setting or clearing nonblock, it also checks that the fd is bound and errors otherwise.
  • close is true if the caller is in charge of closing it. This is false iff ep is `Fd _.
  • addr the listening address for the endpoint.

If st is SOCK_STREAM listen is called on the resulting fd with backlog (see listen for default.

See also with_listening_endpoint.

val with_listening_endpoint : ?nonblock:bool -> ?backlog:int -> Net.Endpoint.t -> Unix.socket_type -> (Unix.file_descr -> Unix.sockaddr -> 'a) -> ('a, string) Stdlib.result

with_listening_endpoint ep st f uses listen_endpoint and calls f with the resulting file descriptor and listening address ensuring, even if f raises that:

  • If needed (see listen_endpoint) the file descriptor ressource is closed after f returns.
  • The Sys.sigpipe signal is ignored during the call to f.
val accept : cloexec:bool -> Unix.file_descr -> (Unix.file_descr * Unix.sockaddr, string) Stdlib.result

accept ~cloexec fd calls Unix.accept.

val bind : Unix.file_descr -> Unix.sockaddr -> (unit, string) Stdlib.result

bind fd addr binds the fd to the address addr.

val listen : ?backlog:int -> Unix.file_descr -> (unit, string) Stdlib.result

listen ~backlog fd indicates that fd can be used to accept incoming connections on the address it is bound to.

backlog is the maximum length for the queue of pending incoming connections before they start to be rejected if they are not accepted. It defaults to 128 (FIXME get a hand on SOMAXCONN).