Module Posix_socket Source POSIX socket interface bindings.
This module provides OCaml bindings to the POSIX socket API defined in sys/socket.h and netdb.h .
It includes functions for address resolution, byte order conversion, and socket address manipulation.
Error HandlingError codes returned by address resolution functions like getaddrinfo and getnameinfo .
Source type error = [ | `ADDRFAMILY Address family not supported
| `AGAIN Temporary failure in name resolution
| `BADFLAGS | `BADHINTS | `FAIL Non-recoverable failure in name resolution
| `FAMILY Address family not supported
| `MEMORY Memory allocation failure
| `NODATA No address associated with hostname
| `NONAME Name or service not known
| `PROTOCOL Resolved protocol is unknown
| `SOCKTYPE Socket type not supported
| `OVERFLOW | `SERVICE Service not supported for socket type
| `SYSTEM System error, check errno
| `UNKNOWN of int ] Address resolution error codes (EAI_* constants).
Convert an error to its integer representation.
Convert an integer to an error code.
Returns true if this error code is natively defined on this platform.
Exception raised by address resolution functions on error.
Byte Order ConversionFunctions for converting between network byte order (big-endian) and host byte order. See byteorder(3) .
Source val ntohl : Unsigned .uint32 -> Unsigned .uint32Convert 32-bit integer from network to host byte order.
Source val ntohs : Unsigned .uint16 -> Unsigned .uint16Convert 16-bit integer from network to host byte order.
Source val htonl : Unsigned .uint32 -> Unsigned .uint32Convert 32-bit integer from host to network byte order.
Source val htons : Unsigned .uint16 -> Unsigned .uint16Convert 16-bit integer from host to network byte order.
Socket TypesSocket type constants used when creating sockets. See socket(2) .
Abstract type representing socket types.
Ctypes representation of socket_type.
Datagram socket (connectionless, unreliable). Used with UDP.
Stream socket (connection-oriented, reliable). Used with TCP.
Sequenced packet socket (connection-oriented, reliable, message boundaries preserved).
Address FamiliesSocket address family constants. See sys/socket.h .
Module for the sa_family field type.
Source type sa_family_t = Sa_family .t Type alias for address family.
Ctypes representation of sa_family_t.
IPv4 address family (AF_INET).
IPv6 address family (AF_INET6).
Unspecified address family (AF_UNSPEC). Used to request any address family.
Name Resolution ConstantsConstants for getnameinfo(3) .
Maximum length of a service name string.
Maximum length of a host name string.
Return numeric form of the host address.
Return numeric form of the service (port number).
Protocol ConstantsIP protocol numbers for use with sockets. See netinet/in.h .
Internet Protocol version 6 (IPv6).
Internet Control Message Protocol (ICMP).
Transmission Control Protocol (TCP).
User Datagram Protocol (UDP).
Socket Length TypeThe socklen_t type used for socket address lengths. See sys/socket.h .
Source module Socklen : Unsigned .S Unsigned module for socklen_t arithmetic.
Ctypes representation of socklen_t.
Socket Address StorageGeneric socket address storage large enough to hold any socket address type. See sockaddr_storage .
Abstract type for socket address storage.
Allocate a new socket address storage structure.
val sockaddr_storage_len : intSize of sockaddr_storage in bytes.
Generic Socket AddressThe generic sockaddr structure. See sockaddr .
Generic socket address structure.
Type alias for sockaddr structure.
Ctypes representation of sockaddr.
Return the actual length of a socket address based on its family.
Address InfoThe addrinfo structure used by getaddrinfo. See getaddrinfo(3) .
Address information structure returned by getaddrinfo.
Port TypeSource type in_port = Unsigned .uint16 Port number type (16-bit unsigned integer in network byte order).
Ctypes representation of in_port_t.
IPv4 Socket AddressThe sockaddr_in structure for IPv4 addresses. See netinet/in.h .
IPv4 socket address structure.
Type alias for IPv4 socket address.
Ctypes representation of sockaddr_in.
IPv6 Socket AddressThe sockaddr_in6 structure for IPv6 addresses. See netinet/in.h .
IPv6 socket address structure.
Type alias for IPv6 socket address.
Ctypes representation of sockaddr_in6.
Address Resolution FunctionsConvert a socket address to a hostname and port number.
This is a wrapper around getnameinfo(3) .
Resolve a hostname to a list of socket addresses.
This is a wrapper around getaddrinfo(3) .
Example:
let addresses = getaddrinfo ~port:(`Int 443) "example.com" in
List.iter
(fun addr ->
let host, port = getnameinfo addr in
Printf.printf "%s:%d\n" host port)
addresses Utility FunctionsCalculate the length of a null-terminated string up to a maximum. See strnlen(3) .
Unix Module InteroperabilityFunctions for converting between Unix.sockaddr and POSIX socket addresses.
Convert a Unix.sockaddr to a POSIX socket address.
Convert a POSIX socket address to Unix.sockaddr.