Posix_socketPOSIX 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 codes returned by address resolution functions like getaddrinfo and getnameinfo.
type error = [ | `ADDRFAMILYAddress family not supported
*)| `AGAINTemporary failure in name resolution
*)| `BADFLAGSInvalid flags value
*)| `BADHINTSInvalid hints value
*)| `FAILNon-recoverable failure in name resolution
*)| `FAMILYAddress family not supported
*)| `MEMORYMemory allocation failure
*)| `NODATANo address associated with hostname
*)| `NONAMEName or service not known
*)| `PROTOCOLResolved protocol is unknown
*)| `SOCKTYPESocket type not supported
*)| `OVERFLOWArgument buffer overflow
*)| `SERVICEService not supported for socket type
*)| `SYSTEMSystem error, check errno
*)| `UNKNOWN of intUnknown error code
*) ]Address resolution error codes (EAI_* constants).
val int_of_error : error -> intConvert an error to its integer representation.
val error_of_int : int -> errorConvert an integer to an error code.
val is_native : error -> boolReturns true if this error code is natively defined on this platform.
val strerror : error -> stringReturn a human-readable error message. See gai_strerror(3).
exception Error of errorException raised by address resolution functions on error.
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 type constants used when creating sockets. See socket(2).
val socket_type_t : socket_type Ctypes.typCtypes representation of socket_type.
val sock_dgram : socket_typeDatagram socket (connectionless, unreliable). Used with UDP.
val sock_stream : socket_typeStream socket (connection-oriented, reliable). Used with TCP.
val sock_seqpacket : socket_typeSequenced packet socket (connection-oriented, reliable, message boundaries preserved).
Socket address family constants. See sys/socket.h.
module Sa_family = Posix_socket_types.Sa_familyModule for the sa_family field type.
val sa_family_t : sa_family_t Ctypes.typCtypes representation of sa_family_t.
val af_inet : sa_family_tIPv4 address family (AF_INET).
val af_inet6 : sa_family_tIPv6 address family (AF_INET6).
val af_unspec : sa_family_tUnspecified address family (AF_UNSPEC). Used to request any address family.
Constants for getnameinfo(3).
IP protocol numbers for use with sockets. See netinet/in.h.
The socklen_t type used for socket address lengths. See sys/socket.h.
val socklen_t : socklen_t Ctypes.typCtypes representation of socklen_t.
Generic socket address storage large enough to hold any socket address type. See sockaddr_storage.
val sockaddr_storage : unit -> sockaddr_storage Ctypes.ptrAllocate a new socket address storage structure.
The generic sockaddr structure. See sockaddr.
module Sockaddr : sig ... endGeneric socket address structure.
type sockaddr = Sockaddr.t Ctypes.structureType alias for sockaddr structure.
val sockaddr_t : sockaddr Ctypes.typCtypes representation of sockaddr.
val sockaddr_len : sockaddr Ctypes.ptr -> intReturn the actual length of a socket address based on its family.
The addrinfo structure used by getaddrinfo. See getaddrinfo(3).
module Addrinfo : sig ... endAddress information structure returned by getaddrinfo.
The sockaddr_in structure for IPv4 addresses. See netinet/in.h.
module SockaddrInet : sig ... endIPv4 socket address structure.
type sockaddr_in = SockaddrInet.t Ctypes.structureType alias for IPv4 socket address.
val sockaddr_in_t : sockaddr_in Ctypes.typCtypes representation of sockaddr_in.
The sockaddr_in6 structure for IPv6 addresses. See netinet/in.h.
module SockaddrInet6 : sig ... endIPv6 socket address structure.
type sockaddr_in6 = SockaddrInet6.t Ctypes.structureType alias for IPv6 socket address.
val sockaddr_in6_t : sockaddr_in6 Ctypes.typCtypes representation of sockaddr_in6.
val getnameinfo : sockaddr Ctypes.ptr -> string * intConvert 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)
addressesCalculate the length of a null-terminated string up to a maximum. See strnlen(3).
Functions for converting between Unix.sockaddr and POSIX socket addresses.
val from_unix_sockaddr : Unix.sockaddr -> sockaddr Ctypes.ptrConvert a Unix.sockaddr to a POSIX socket address.
val to_unix_sockaddr : sockaddr Ctypes.ptr -> Unix.sockaddrConvert a POSIX socket address to Unix.sockaddr.