Linux and macOS IPv4 socket declarations for addresses, resolution records, raw options, connection lifecycle, byte order, and I/O.

Functions that report failure with -1 set errno; getaddrinfo returns an address-resolution error code instead; for its system-error code, errno holds the cause.


Constants

Address families

NameLinuxmacOSMeaning
AF_INET22IPv4 address family.

Socket types and protocols

NameLinuxmacOSMeaning
SOCK_STREAM11Stream socket type.
IPPROTO_TCP66TCP protocol selector.

Socket options

NameLinuxmacOSMeaning
SOL_SOCKET10xFFFFSocket-level option level.
SO_ERROR40x1007Pending socket-error option.
SO_REUSEADDR20x0004Address-reuse option.
SO_NOSIGPIPE—0x1022macOS socket option that suppresses SIGPIPE.
TCP_NODELAY11Disables Nagle's algorithm for a TCP socket.

Flags

NameLinuxmacOSMeaning
MSG_NOSIGNAL0x4000n32—Linux send flag that suppresses SIGPIPE.

Shutdown modes

NameLinuxmacOSMeaning
SHUT_WR11Shutdown selector for the write direction.

File-status commands

NameLinuxmacOSMeaning
F_GETFL33File-status query command for fcntl.
F_SETFL44File-status update command for fcntl.

Address and descriptor values

NameLinuxmacOSMeaning
INADDR_ANY0x00000000n32—Linux wildcard IPv4 address.
INVALID_SOCKET-1-1Invalid descriptor marker used by this binding.
SOMAXCONN128128Conventional listen-backlog value.

Address and lookup schemas

addrinfo

(-- addrinfo) Defines one address-resolution result record.

Dict schema

Linux

FieldSchemaRole
ai_flagsInt32address-resolution flags
ai_familyInt32address family
ai_socktypeInt32socket type
ai_protocolInt32protocol selector
ai_addrlenNatxaddress length in bytes
ai_addrNatxaddress of the socket-address record
ai_canonnameNatxaddress of the zero-terminated canonical name
ai_nextNatxaddress of the next result record

Known issue: on 64-bit Linux, this binding declares ai_addrlen as Natx while the native field is a 32-bit length followed by padding, so a read of ai_addrlen from a record that getaddrinfo filled includes the padding bytes. Read the length through a socklen_t view, as in info.ai_addrlen socklen_t reinterpret.

macOS

FieldSchemaRole
ai_flagsInt32address-resolution flags
ai_familyInt32address family
ai_socktypeInt32socket type
ai_protocolInt32protocol selector
ai_addrlensocklen_taddress length in bytes
ai_canonnameNatxaddress of the zero-terminated canonical name
ai_addrNatxaddress of the socket-address record
ai_nextNatxaddress of the next result record
  • Record-address fields are viewed with addressToReference, as in info.ai_addr sockaddr addressToReference and info.ai_next addrinfo addressToReference.

in_addr

(-- in_addr) Aliases Nat32 for IPv4 address storage.

Nat32 alias

  • The address value is represented in network byte order when passed to the socket API.

sockaddr

(-- sockaddr) Defines the generic socket-address record.

Dict schema

Linux

FieldSchemaRole
sa_familyNat16address family
sa_dataNat8 14 arrayraw address bytes

macOS

FieldSchemaRole
sa_lenNat8address record length in bytes
sa_familyNat8address family
sa_dataNat8 14 arrayraw address bytes

sockaddr_in

(-- sockaddr_in) Defines the IPv4 socket-address record.

Dict schema

Linux

FieldSchemaRole
sin_familyNat16address family
sin_portNat16network-order port
sin_addrin_addrnetwork-order IPv4 address
sin_zeroNat8 8 arraypadding

macOS

FieldSchemaRole
sin_lenNat8address record length in bytes
sin_familyNat8address family
sin_portNat16network-order port
sin_addrin_addrnetwork-order IPv4 address
sin_zeroNat8 8 arraypadding
  • Where a sockaddr is required, pass addr sockaddr reinterpret to bind and connect and @addr sockaddr reinterpret as the writable storage of accept; view a sockaddr peer record as peer sockaddr_in reinterpret.
  • AF_INET is Int32: cast it before storing it in sin_family, for example AF_INET addr.sin_family cast @addr.!sin_family.

socklen_t

(-- socklen_t) Aliases Nat32 for socket lengths.

Nat32 alias


Imported functions

Stack effects list a C function's parameters with the first parameter on top; see importFunction.

accept

(addrlen addr sockfd -- result) Accepts one pending connection and returns its descriptor.

Inputs

  • addrlen: socklen_t Ref caller-owned storage initialized to the capacity of addr in bytes and receiving the actual peer-address length.
  • addr: sockaddr Ref caller-owned writable peer-address storage; the call fills it.
  • sockfd: Int32 socket descriptor to accept from.

Outputs

  • result: Int32 accepted socket descriptor, or -1 on failure.

bind

(addrlen addr sockfd -- result) Associates a socket with a local address.

Inputs

Outputs

  • result: Int32 0 on success or -1 on failure.

connect

(addrlen addr sockfd -- result) Connects a socket to a peer address.

Inputs

  • addrlen: socklen_t byte size of addr.
  • addr: sockaddr Cref borrowed read-only peer socket address.
  • sockfd: Int32 socket descriptor.

Outputs

  • result: Int32 0 on success or -1 on failure.

freeaddrinfo

(res --) Releases an address-resolution result list.

Inputs

Outputs

  • None.

getaddrinfo

(res hints service node -- result) Resolves a node and service into an address-resolution result list.

Inputs

  • res: Natx address passed as an integer to writable storage that receives the address of the first result record; view that address with addrinfo addressToReference and pass the reference to freeaddrinfo.
  • hints: addrinfo Cref borrowed read-only address-resolution hints, whose ai_addrlen, ai_addr, ai_canonname, and ai_next must be zero.
  • service: Natx address of a zero-terminated byte string naming the service, or 0nx for none.
  • node: Natx address of a zero-terminated byte string naming the node, or 0nx when service is provided.

Outputs

  • result: Int32 0 on success or a nonzero address-resolution error code.

getsockopt

(option_len option_value option_name level socket -- result) Reads one socket option.

Inputs

  • option_len: socklen_t Ref caller-owned storage initialized to the capacity of the buffer at option_value in bytes and receiving the returned option-value length.
  • option_value: Natx address of caller-owned storage that receives the requested option value.
  • option_name: Int32 option identifier.
  • level: Int32 option level.
  • socket: Int32 socket descriptor.

Outputs

  • result: Int32 0 on success or -1 on failure.

htonl

(hostlong -- result) Converts a 32-bit value from host byte order to network byte order.

Inputs

  • hostlong: Nat32 host-order 32-bit value.

Outputs

  • result: Nat32 network-order 32-bit value.

htons

(hostshort -- result) Converts a 16-bit value from host byte order to network byte order.

Inputs

  • hostshort: Nat16 host-order 16-bit value, typically a port.

Outputs

  • result: Nat16 network-order 16-bit value.

listen

(backlog sockfd -- result) Marks a socket as a listener with a backlog.

Inputs

  • backlog: Int32 requested pending-connection queue length.
  • sockfd: Int32 socket descriptor.

Outputs

  • result: Int32 0 on success or -1 on failure.

ntohl

(netlong -- result) Converts a 32-bit value from network byte order to host byte order.

Inputs

  • netlong: Nat32 network-order 32-bit value.

Outputs

  • result: Nat32 host-order 32-bit value.

recv

(flags len buf sockfd -- result) Receives bytes from a socket.

Inputs

  • flags: Int32 receive flags.
  • len: Natx maximum number of bytes to receive.
  • buf: Natx address passed as an integer to caller-owned writable byte storage.
  • sockfd: Int32 socket descriptor.

Outputs

  • result: Intx number of bytes received, or -1ix on failure (0ix marks orderly peer shutdown only for stream receives with a nonzero length).

send

(flags len buf sockfd -- result) Sends bytes through a socket.

Inputs

  • flags: Int32 send flags; on Linux, pass MSG_NOSIGNAL as MSG_NOSIGNAL Int32 cast.
  • len: Natx number of bytes to send.
  • buf: Natx address passed as an integer to caller-owned bytes.
  • sockfd: Int32 socket descriptor.

Outputs

  • result: Intx number of bytes sent, or -1ix on failure.

setsockopt

(option_len option_value option_name level socket -- result) Writes one socket option.

Inputs

  • option_len: socklen_t option-value length in bytes.
  • option_value: Natx address passed as an integer to caller-owned option bytes.
  • option_name: Int32 option identifier.
  • level: Int32 option level.
  • socket: Int32 socket descriptor.

Outputs

  • result: Int32 0 on success or -1 on failure.

shutdown

(how sockfd -- result) Disables part or all of a socket connection.

Inputs

Outputs

  • result: Int32 0 on success or -1 on failure.

socket

(protocol type domain -- result) Creates a socket and returns its descriptor.

Inputs

  • protocol: Int32 protocol selector.
  • type: Int32 socket-type selector.
  • domain: Int32 address-family selector such as AF_INET.

Outputs

  • result: Int32 socket descriptor, or -1 on failure.

Examples

Constant example

"control"      use
"linux/socket" use

{} () {} [
  AF_INET printStack _:;
  SOCK_STREAM printStack _:;
  SHUT_WR printStack _:;
  INADDR_ANY printStack _:;
] "main" exportFunction

Expected Output During Compilation

2
1
1
0n32

Signature examples

"control"      use
"linux/socket" use

{} Int32 {} [
  @getaddrinfo printStack _:;
  @recv printStack _:;
  @socket printStack _:;
  0
] "main" exportFunction

Expected Output During Compilation

{node: Natx; service: Natx; hints: {ai_flags: Int32; ai_family: Int32; ai_socktype: Int32; ai_protocol: Int32; ai_addrlen: Natx; ai_addr: Natx; ai_canonname: Natx; ai_next: Natx;} Cref; res: Natx;} Int32 {convention: "";} codeRef
{sockfd: Int32; buf: Natx; len: Natx; flags: Int32;} Intx {convention: "";} codeRef
{domain: Int32; type: Int32; protocol: Int32;} Int32 {convention: "";} codeRef

Byte-order helpers

"String"  use
"control" use
"socket"  use

{} Int32 {} [
  (0x01020304n32 htonl ntohl 0x01020304n32 = toString LF) printList
  0
] "main" exportFunction

Expected Output

TRUE

Descriptor creation example

"String"       use
"control"      use
"linux/posix"  use
"linux/socket" use

{} Int32 {} [
  sock: IPPROTO_TCP SOCK_STREAM AF_INET socket;
  ("created=" sock 0 < ~ LF
    "close=" sock close 0 = LF) printList
  0
] "main" exportFunction

Expected Output

created=TRUE
close=TRUE

See also