# Networking functions

This chapter is dedicated to the element networking functions. It assumes that the reader has a basic knowledge of the Internet Protocol or IP. The implementation provides, in a single module called `afnix-net`, all classes and functions needed to perform IP operations, create server or clients programs. This module is also designed to support IP version 6 with certain platforms.

## IP address

The IP based communication uses a standard address to reference a particular peer. With IP version 4, the standard dot notation is with 4 bytes. With IP version 6, the standard semicolon notation is with 16 bytes. The current implementation supports both versions.

127.0.0.1 # ipv4 localhost 0:0:0:0:0:0:0:1 # ipv6 localhost

IP address architecture and behavior are described in various documents as listed in the bibliography.

### Domain name system

The translation between a host name and an IP address is performed by a resolver which uses the Domain Name System or DNS. Access to the DNS is automatic with the implementation. Depending on the machine resolver configuration, a particular domain name translation might result in an IP version 4 or IP version 6 address. Most of the time, an IP version 4 address is returned.

The mapping between an IP address and a host name returns the associated canonical name for that IP address. This is the reverse of the preceding operation.

## The Address class

The `Address` class allows manipulation of IP address. The constructor takes a string as its arguments. The argument string can be either an IP address or a host name which can be qualified or not. When the address is constructed with a host name, the IP address resolution is done immediately.

### Name to address translation

The most common operation is to translate a host name to its equivalent IP address. Once the `Address` object is constructed, the `get-address` method returns a string representation of the internal IP address. The following example prints the IP address of the localhost, that is 127.0.0.1 with IP version 4.

# load network module interp:library "afnix-net"

# get the localhost address const addr (afnix:net:Address "localhost")

# print the ip address println (addr:get-address)

As another example, the `get-host-name` function returns the host name of the running machine. The previous example can be used to query its IP address.

### Address to name translation

The reverse operation of name translation maps an IP address to a canonical name. It shall be noted that the reverse lookup is not done automatically, unless the reverse flag is set in the constructoor. The `get-canonical-name` method of the `Address` class returns such name. Example XNET001.als is a demonstration program which prints the address original name, the IP address and the canonical name. Fell free to use it with your favorite site to check the equivalence between the original name and the canonical name.

# print the ip address information of the arguments # usage: axi XNET001.als [hosts ...]

# get the network module interp:library "afnix-net"

# print the ip address const ip-address-info (host) { try { const addr (afnix:net:Address host true) println "host name : " (addr:get-name) println " ip address : " (addr:get-address) println " canonical name : " (addr:get-canonical-name) # get aliases const size (addr:get-alias-size) loop (trans i 0) (< i size) (i:++) { println " alias address : " (addr:get-alias-address i) println " alias name : " (addr:get-alias-name i) } } (errorln "error: " what:reason) }

# get the hosts for (s) (interp:argv) (ip-address-info s)

zsh> axi net-0001.als localhost host name : localhost ip address : 127.0.0.1 canonical name : localhost

### Address operations

The `Address` class provides several methods and operators that ease the address manipulation in a protocol indepedant way. For example, the `==` operator compares two addresses. The `++` operator can also be used to get the next IP address.

## Transport layers

The two transport layer protocols supported by the Internet protocol is the TCP, a full-duplex oriented protocol, and UDP, a datagram protocol. TCP is a reliable protocol while UDP is not. By reliable, we mean that the protocol provides automatically some mechanisms for error recovery, message delivery, acknowledgment of reception, etc... The use of TCP vs. UDP is dictated mostly by the reliability concerns, while UDP reduces the traffic congestion.

### Service port

In the client-server model, a connection is established between two hosts. The connections is made via the IP address and the port number. For a given service, a port identifies that service at a particular address. This means that multiple services can exist at the same address. More precisely, the transport layer protocol is also used to distinguish a particular service.

The network module provides a simple mechanism to retrieve the port number, given its name and protocol. The function `get-tcp-service` and `get-udp-service` returns the port number for a given service by name. For example, the daytime server is located at port number 13.

assert 13 (afnix:net:get-tcp-service "daytime") assert 13 (afnix:net:get-udp-service "daytime")

### Host and peer

With the client server model, the only information needed to identify a particular client or server is the address and the port number. When a client connects to a server, it specify the port number the server is operating. The client uses a random port number for itself. When a server is created, the port number is used to bind the server to that particular port. If the port is already in use, that binding will fail. From a reporting point of view, a connection is therefore identified by the running host address and port, and the peer address and port. For a client, the peer is the server. For a server, the peer is the client.