- Error Handling
- Byte Order Conversion
- Socket Types
- Address Families
- Name Resolution Constants
- Protocol Constants
- Socket Length Type
- Socket Address Storage
- Generic Socket Address
- Address Info
- Port Type
- IPv4 Socket Address
- IPv6 Socket Address
- Address Resolution Functions
- Utility Functions
- Unix Module Interoperability
package posix-socket
Module Posix_socketSource
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 Handling
Error codes returned by address resolution functions like getaddrinfo and getnameinfo.
type error = [ | `ADDRFAMILY(*Address family not supported
*)| `AGAIN(*Temporary failure in name resolution
*)| `BADFLAGS(*Invalid flags value
*)| `BADHINTS(*Invalid hints value
*)| `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(*Argument buffer overflow
*)| `SERVICE(*Service not supported for socket type
*)| `SYSTEM(*System error, check errno
*)| `UNKNOWN of int(*Unknown error code
*)
]Address resolution error codes (EAI_* constants).
Returns true if this error code is natively defined on this platform.
Return a human-readable error message. See gai_strerror(3).
Byte Order Conversion
Functions for converting between network byte order (big-endian) and host byte order. See byteorder(3).
Convert 32-bit integer from network to host byte order.
Convert 16-bit integer from network to host byte order.
Convert 32-bit integer from host to network byte order.
Convert 16-bit integer from host to network byte order.
Socket Types
Socket 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 Families
Socket address family constants. See sys/socket.h.
Module for the sa_family field type.
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 Constants
Constants 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 Constants
IP protocol numbers for use with sockets. See netinet/in.h.
Internet Protocol (IP).
Internet Protocol version 6 (IPv6).
Internet Control Message Protocol (ICMP).
Raw IP packets.
Transmission Control Protocol (TCP).
User Datagram Protocol (UDP).
Socket Length Type
The socklen_t type used for socket address lengths. See sys/socket.h.
Unsigned module for socklen_t arithmetic.
Abstract socklen_t type.
Ctypes representation of socklen_t.
Socket Address Storage
Generic 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.
Size of sockaddr_storage in bytes.
Generic Socket Address
The generic sockaddr structure. See sockaddr.
Type alias for sockaddr structure.
Ctypes representation of sockaddr.
Return the actual length of a socket address based on its family.
Address Info
The addrinfo structure used by getaddrinfo. See getaddrinfo(3).
Port Type
Port number type (16-bit unsigned integer in network byte order).
Ctypes representation of in_port_t.
IPv4 Socket Address
The 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 Address
The 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 Functions
Convert a socket address to a hostname and port number.
This is a wrapper around getnameinfo(3).
val getaddrinfo :
?hints:Addrinfo.t Ctypes.structure Ctypes.ptr ->
?port:[ `Int of int | `String of string ] ->
string ->
sockaddr Ctypes.ptr listResolve 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)
addressesUtility Functions
Calculate the length of a null-terminated string up to a maximum. See strnlen(3).
Unix Module Interoperability
Functions 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.
- Error Handling
- Byte Order Conversion
- Socket Types
- Address Families
- Name Resolution Constants
- Protocol Constants
- Socket Length Type
- Socket Address Storage
- Generic Socket Address
- Address Info
- Port Type
- IPv4 Socket Address
- IPv6 Socket Address
- Address Resolution Functions
- Utility Functions
- Unix Module Interoperability