> ## 5. TCP/IP Protocols

> This chapter discusses the protocols available in the TCP/IP protocol suite. The following figure shows how they correspond to the 5-layer TCP/IP Reference Model. This is not a perfect one-to-one correspondence; for instance, Internet Protocol (IP) uses the Address Resolution Protocol (ARP), but is shown here at the same layer in the stack.

> Figure 4. TCP/IP Protocol Flow

> ### 5.1 IP

> IP provides communication between hosts on different kinds of networks (i.e., different data-link implementations such as Ethenet and Token Ring). It is a connectionless, unreliable packet delivery service. Connectionless means that there is no handshaking, each packet is independent of any other packet. It is unreliable because there is no guarantee that a packet gets delivered; higher-level protocols must deal with that.

> #### 5.1.1 IP Address

> IP defines an addressing scheme that is independent of the underlying physical address (e.g, 48-bit MAC address). IP specifies a unique 32-bit number for each host on a network. This number is known as the Internet Protocol Address, the IP Address or the Internet Address. These terms are interchangeable. Each packet sent across the internet contains the IP address of the source of the packet and the IP address of its destination.

> For routing efficiency, the IP address is considered in two parts: the prefix which identifies the physical network, and the suffix which identifies a computer on the network. A unique prefix is needed for each network in an internet. For the global Internet, network numbers are obtained from Internet Service Providers (ISPs). ISPs coordinate with a central organization called the Internet Assigned Number Authority.

> #### 5.1.2 IP Address Classes

> The first four bits of an IP address determine the class of the network. The class specifies how many of the remaining bits belong to the prefix (aka Network ID) and to the suffix (aka Host ID). The first three classes, A, B and C, are the primary network classes.

> CLASS | FIRST 4 BITS | NUMBER OF PREFIX BITS | MAX # OF NETWORKS | NUMBER OF SUFFIX BITS | MAX # OF HOSTS PER NETWORK
> A | 0xxx | 7 | 128 | 24 | 16,777,216
> B | 10xx | 14 | 16,384 | 16 | 65,536
> C | 110x | 21 | 2,097,152 | 8 | 256
> D | 1110 | Multicast
> E | 1111 | Reserved for future use.

> When interacting with mere humans, software uses dotted decimal notation; each 8 bits is treated as an unsigned binary integer separated by periods. IP reserves host address 0 to denote a network. 140.211.0.0 denotes the network that was assigned the class B prefix 140.211.

> #### 5.1.3 Netmasks

> Netmasks are used to identify which part of the address is the Network ID and which part is the Host ID. This is done by a logical bitwise-AND of the IP address and the netmask. For class A networks the netmask is always 255.0.0.0; for class B networks it is 255.255.0.0 and for class C networks the netmask is 255.255.255.0.

> #### 5.1.4 Subnet Address

> All hosts are required to support subnet addressing. While the IP address classes are the convention, IP addresses are typically subnetted to smaller address sets that do not match the class system. The suffix bits are divided into a subnet ID and a host ID. This makes sense for class A and B networks, since no one attaches as many hosts to these networks as is allowed. Whether to subnet and how many bits to use for the subnet ID is determined by the local network administrator of each network.

> If subnetting is used, then the netmask will have to reflect this fact. On a class B network with subnetting, the netmask would not be 255.255.0.0. The bits of the Host ID that were used for the subnet would need to be set in the netmask.

> #### 5.1.5 Directed Broadcast Address

> IP defines a directed broadcast address for each physical network as all ones in the host ID part of the address. The network ID and the subnet ID must be valid network and subnet values. When a packet is sent to a network's broadcast address, a single copy travels to the network, and then the packet is sent to every host on that network or subnetwork.

> #### 5.1.6 Limited Broadcast Address

> If the IP address is all ones (255.255.255.255), this is a limited broadcast address; the packet is addressed to all hosts on the current (sub)network. A router will not forward this type of broadcast to other (sub)networks.

> ### 5.2 IP Routing

> Each IP datagram travels from its source to its destination by means of routers. All hosts and routers on an internet contain IP protocol software and use a routing table to determine where to send a packet next. The destination IP address in the IP header contains the ultimate destination of the IP datagram, but it might go through several other IP addresses (routers) before reaching that destination.

> Routing table entries are created when TCP/IP initializes. The entries can be updated manually by a network administrator or automatically by employing a routing protocol such as Routing Information Protocol (RIP). Routing table entries provide needed information to each local host regarding how to communicate with remote networks and hosts.

> When IP receives a packet from a higher-level protocol, like TCP or UDP, the routing table is searched for the route that is the closest match to the destination IP address. The most specific to the least specific route is in the following order:

> - A route that matches the destination IP address (host route).
> - A route that matches the network ID of the destination IP address (network route).
> - The default route.

> If a matching route is not found, IP discards the datagram.

> IP provides several other services:

> - Fragmentation. IP packets may be divided into smaller packets. This permits a large packet to travel across a network which only accepts smaller packets. IP fragments and reassembles packets transparent to the higher layers.
> - Timeouts. Each IP packet has a Time To Live (TTL) field, that is decremented every time a packet moves through a router. If TTL reaches zero, the packet is discarded.
> - Options. IP allows a packet's sender to set requirements on the path the packet takes through the network (source route); the route taken by a packet may be traced (record route), and packets may be labeled with security features.

> ### 5.3 ARP

> The Address Resolution Protocol is used to translate virtual addresses to physical ones. The network hardware does not understand the software-maintained IP addresses. IP uses ARP to translate the 32-bit IP address to a physical address that matches the addressing scheme of the underlying hardware (for Ethernet, the 48-bit MAC address).

> There are three general addressing strategies:

> 1. Table lookup
> 2. Translation performed by a mathematical function
> 3. Message exchange

> TCP/IP can use any of the three. ARP employs the third strategy, message exchange. ARP defines a request and a response. A request message is placed in a hardware frame (e.g., an Ethernet frame), and broadcast to all computers on the network. Only the computer whose IP address matches the request sends a response.

> ### 5.4 The Transport Layer

> There are two primary transport layer protocols: Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). They provide end-to-end communication services for applications.

> #### 5.4.1 UDP

> This is a minimal service over IP, adding only optional checksumming of data and multiplexing by port number. UDP is often used by applications that need multicast or broadcast delivery, services not offered by TCP. Like IP, UDP is connectionless and works with datagrams.

> #### 5.4.2 TCP

> TCP is a connection-oriented transport service; it provides end-to-end reliability, resequencing, and flow control. TCP enables two hosts to establish a connection and exchange streams of data, which are treated in bytes. The delivery of data in the proper order is guaranteed.

> TCP can detect errors or lost data and can trigger retransmission until the data is received, complete and without errors.

> #### 5.4.2.1 TCP Connection/Socket

> A TCP connection is done with a 3-way handshake between a client and a server. The following is a simplified explanation of this process.

> - The client asks for a connection by sending a TCP segment with the SYN control bit set.
> - The server responds with its own SYN segment that includes identifying information that was sent by the client in the initial SYN segment.
> - The client acknowledges the server's SYN segment.

> The connection is then established and is uniquely identified by a 4-tuple called a socket or socket pair:

> (destination IP address, destination port number)
> (source IP address, source port number)

> During the connection setup phase, these values are entered in a table and saved for the duration of the connection.

> #### 5.4.2.2 TCP Header

> Every TCP segment has a header. The header comprises all necessary information for reliable, complete delivery of data. Among other things, such as IP addresses, the header contains the following fields:

> Sequence Number - This 32-bit number contains either the sequence number of the first byte of data in this particular segment or the Initial Sequence Number (ISN) that identifies the first byte of data that will be sent for this particular connection.

> The ISN is sent during the connection setup phase by setting the SYN control bit. An ISN is chosen by both client and server. The first byte of data sent by either side will be identified by the sequence number ISN + 1 because the SYN control bit consumes a sequence number. The following figure illustrates the three-way handshake.
> Figure 5. Synchronizing Sequence Numbers for TCP Connection

> The sequence number is used to ensure the data is reassembled in the proper order before being passed to an application protocol.

> Acknowledgement Number - This 32-bit number is the other host's sequence number + 1 of the last successfully received byte of data. It is the sequence number of the next expected byte of data. This field is only valid when the ACK control bit is set. Since sending an ACK costs nothing, (because it and the Acknowledgement Number field are part of the header) the ACK control bit is always set after a connection has been established.

> The Acknowledgement Number ensures that the TCP segment arrived at its destination.

> Control Bits - This 6-bit field comprises the following 1-bit flags (left to right):
> - URG - Makes the Urgent Pointer field significant.
> - ACK - Makes the Acknowledgement Number field significant.
> - PSH - The Push Function causes TCP to promptly deliver data.
> - RST - Reset the connection.
> - SYN - Synchronize sequence numbers.
> - FIN - No more data from sender, but can still recieve data.

> Window Size - This 16-bit number states how much data the receiving end of the TCP connection will allow. The sending end of the TCP connection must stop and wait for an acknowledgement after it has sent the amount of data allowed.

> Checksum - This 16-bit number is the one's complement of the one's complement sum of all bytes in the TCP header, any data that is in the segment and part of the IP packet. A checksum can only detect some errors, not all, and cannot correct any.

> #### 5.4.3 ICMP

> Internet Control Message Protocol is a set of messages that communicate errors and other conditions that require attention. ICMP messages, delivered in IP datagrams, are usually acted on by either IP, TCP or UDP. Some ICMP messages are returned to application protocols.

> A common use of ICMP is "pinging" a host. The Ping command (Packet INternet Groper) is a utility that determines whether a specific IP address is accessible. It sends an ICMP echo request and waits for a reply. Ping can be used to transmit a series of packets to measure average round-trip times and packet loss percentages.

> ### 5.5 The Application Layer

> There are many applications available in the TCP/IP suite of protocols. Some of the most useful ones are for sending mail (SMTP), transferring files (FTP), and displaying web pages (HTTP). These applications are discussed in detail in the TCP/IP User's Manual.

> Another important application layer protocol is the Domain Name System (DNS). Domain names are significant because they guide users to where they want to go on the Internet.

> #### 5.5.1 DNS

> The Domain Name System is a distributed database of domain name and IP address bindings. A domain name is simply an alphanumeric character string separated into segments by periods. It represents a specific and unique place in the "domain name space." DNS makes it possible for us to use identifiers such as zworld.com to refer to an IP address on the Internet. Name servers contain information on some segment of the DNS and make that information available to clients who are called resolvers.

> #### 5.5.1.1 DCRTCP.LIB Implementation of DNS

> The resolve function in DCRTCP.LIB immediately converts a dotted decimal IP address to its corresponding binary IP address and returns this value.

> If resolve is passed a domain name, a series of queries take place between the computer that called resolve and computers running name server software. For example, to resolve the domain name www.rabbitsemiconductor.com, the following code (available in SAMPLES\TCP\DNS.C) can be used.

>     #define MY_IP_ADDRESS "10.10.6.101"
>     #define MY_NETMASK "255.255.255.0"
>     #define MY_GATEWAY "10.10.6.19"
>     #define MY_NAMESERVER "209.233.102.12"

>     #memmap xmem
>     #use dcrtcp.lib

>     main() {
>         longword ip;
>         char buffer[20];

>         sock_init();

>         ip=resolve("www.rabbitsemiconductor.com");
>         if(ip==0)
>             printf("couldn't find www.rabbitsemiconductor.com\n");
>         else

>             printf("%s is www.rabbitsemiconductors address.\n", inet_ntoa(buffer,ip));

>     }

> Your local name server is specified by the configuration macro MY_NAMESERVER. Chances are that your local name server does not have the requested information, so it queries the root server. The root server will not know the IP address either, but it will know where to find the name server that contains authoritative information for the .com zone. This information is returned to your local name server, which then sends a query to the name server for the .com zone. Again, this name server does not know the requested IP address, but does know the local name server that handles rabbitsemiconductor.com. This information is sent back to your local name server, who sends a final query to the local name server of rabbitsemiconductor.com. This local name server returns the requested IP address of www.rabbitsemiconductor.com to your local name server, who then passes it to your computer.

Z-World http://www.zworld.com Voice: (530) 757-3737 FAX: (530) 753-5141 sales@zworld.com