Sortix
Sortix Download Manual Development Source Code News Blog More
current nightly

Sortix nightly manual

This manual documents Sortix nightly, a development build that has not been officially released. You can instead view this document in the latest official manual.

NAME

inet — internet protocol family

SYNOPSIS

#include <sys/socket.h>
#include <netinet/in.h>

#define AF_INET 1
typedef uint16_t sa_family_t; 
typedef uint16_t in_port_t; 
typedef uint32_t in_addr_t; 
 
struct in_addr { 
        in_addr_t s_addr; 
}; 
 
struct sockaddr_in { 
        sa_family_t sin_family; 
        in_port_t sin_port; 
        struct in_addr sin_addr; 
};

DESCRIPTION

The Internet Protocol version 4 protocol family is a set of protocols using the Internet Protocol version 4 ip(4) as the network layer. SOCK_STREAM sockets are provided by the Transmission Control Protocol tcp(4). SOCK_DGRAM sockets are provided by the User Datagram Protocol udp(4).
Hosts are addressed with a four byte Internet Protocol (IP) address stored in a struct in_addr in network byte order. IP addresses are notated by the decimal byte values interspaced with periods, e.g. 192.0.2.255. Subnetworks are ranges of IP addresses given by a starting IP address along with how many leading bits (most significant bit first) of the IP address are common to the network (the prefix), the first address of the subnetwork is the prefix with the remaining bits set to zero, and the last address is the prefix with the remaining bits set to one. A subnetwork can be denoted by the starting IP address and the leading bits, e.g. 198.51.100.0/24 spans from 198.51.100.0 to 198.51.100.255. The subnet mask of a subnetwork is an IP address with the bits set that belong to the network prefix, e.g. the subnet mask of 203.0.113.0/24 is 255.255.255.0. The subnetworks 192.0.2.0/24 (TEST-NET-1), 198.51.100.0/24 (TEST-NET-2), and 203.0.113.0/24 (TEST-NET-3) are reserved for documentation. The last address in a network is normally the broadcast address for the network.
Services are addressed with a 16-bit byte port number in a in_port_t in network byte order.
Sockets are addressed with the combination of a host address and port number stored in a struct sockaddr_in where sin_family is set to AF_INET, sin_port is set to the 16-bit port number in network byte order, and sin_addr is set to the host address in network byte order.
sin_port can be set to 0 (converted to network byte order) to request bind(2) allocate a port. Port 0 is not valid as a destination port.
sin_addr.s_addr can be set to INADDR_ANY (0.0.0.0) (converted to network byte order) to mean an unspecified address. When a socket is bound to the address INADDR_ANY, messages are accepted from any address. In connect(2) and sendto(2), the destination address INADDR_ANY means the current host.
sin_addr.s_addr can be set to INADDR_BROADCAST (255.255.255.255) (converted to network byte order), the broadcast address of the local network.
sin_addr.s_addr can be set to INADDR_LOOPBACK (127.0.0.1) (converted to network byte order), the address of the loopback interface lo(4). INADDR_LOOPMASK (255.0.0.0) contains the subnet mask of the loopback interface.
Sockets of this protocol family can be created by passing AF_INET as the domain parameter of socket(2).
The network byte order is big-endian.
IP addresses in 32-bit integer format in the host endian can be converted to network byte order using htobe32(3) or htonl(3) and back using be32toh(3) or ntohl(3).
Port numbers in 16-bit integer format in the host endian can be converted to network byte order using htobe16(3) or htons(3) and back using be16toh(3) or ntohs(3).
inet_pton(3) can be used to convert an IP address from textual representation to binary representation. inet_ntop(3) can be used to convert an IP address from binary representation to textual representation.

EXAMPLES

This example manually constructs and deconstructs a struct inaddr_in.
struct sockaddr_in saddr; 
 
memset(&saddr, 0, sizeof(saddr)); 
saddr.sin_family = AF_INET; 
saddr.sin_addr.s_addr = htobe32(0xC0000201); /* 192.0.2.1 */ 
saddr.sin_port = htobe16(1234); 
 
sa_family_t family = saddr.sin_family; 
in_addr_t addr = be32toh(saddr.sin_addr.s_addr); 
in_port_t port = be16toh(saddr.sin_port);

COMPATIBILITY

On some operating systems, struct sockaddr_in may contain padding and additional members and the structure should be initialized to zero prior to initializing its members.

SEE ALSO

socket(2), arp(4), icmp(4), ip(4), ping(4), tcp(4), udp(4), kernel(7)

STANDARDS

J. Postel (ed.), Internet Protocol - DARPA Internet Program Protocol Specification, STD 5, RFC 791, USC/Information Sciences Institute, September 1981.
Internet Engineering Task Force, J. Arkko, M. Cotton, and L. Vegoda, IPv4 Address Blocks Reserved for Documentation, RFC 5737, Ericsson, ICANN, January 2010.
The protocol family programming interface conforms to IEEE Std 1003.1-2008 (“POSIX.1”).

BUGS

The network stack implementation is incomplete and has known bugs. See the protocol manual pages for more information.
The 4-byte address space allows only a maximum of 4294967296 addresses and is being exhausted. The Internet Protocol version 6 replaces version 4 and provides a 16-byte address space instead.
Copyright 2011-2025 Jonas 'Sortie' Termansen and contributors.
Sortix's source code is free software under the ISC license.
#sortix on irc.sortix.org
@sortix_org