12.1 Linux Network Programming Basics
Linux supports TCP/IP as its native network transport as most other Unix-based operating systems.
 |
Background |
|
The TCP/IP suite of protocols allows two applications, running on either the same or separate computers connected by a network, to communicate. It was specifically designed to tolerate an unreliable network. TCP/IP allows two basic modes of operation-connection-oriented, reliable transmission and connectionless, unreliable transmission (TCP and UDP respectively). Figure 1 illustrates the distinct protocol layers in the TCP/IP suite stack (Griffin and Nelson 1998). |
12.1 Linux Network Programming Basics
Figure 1: TCP/IP protocol layers
12.1 Linux Network Programming Basics
FTP: File Transfer Protocol
SMTP: Simple Mail Transfer Protocol
TCP: Transmission Control Protocol
UDP: User Datagram Protocol
ICMP: Internet Control Message Protocol
IP: Internet Protocol
ARP: Address Resolution Protocol
TCP/IP addresses consist of two parts: an IP address to identify the machine and a port number to identify particular applications running on that machine. The addresses are normally given in either the "dotted-quad" notation (i.e., 127.0.0.1) or as a host name (abc.xyz.org). The system can use either the /etc/hosts file or the Domain Name Service (DNS) to translate host names to host addresses (Griffin and Nelson 1998).
12.1 Linux Network Programming Basics
Let's look at an example using a computer running Linux (Davis, Turner and Yocom 2004).
12.1 Linux Network Programming Basics
Using the /sbin/ifconfig command, we can get a listing of the configuration of the eth0 interface on your Linux machine. The network interface might have a different name than eth0. You can use the appropriate value, or use the -a option to ifconfig to get a listing of all of the configured interfaces if you don't know the name of yours (Davis, Turner and Yocom 2004).
 |
The Domain Name System |
|
Domain Name System (DNS) is a system that lets us assign easily remembered names to their corresponding IP addresses, without having to remember the addresses themselves. The hierarchical nature of DNS can be seen in the names commonly used on the Internet. You often hear the phrase dot-com and routinely use domain names when making connections (Davis, Turner and Yocom 2004). The first level of our hierarchy contains what are known as the top-level domains (TLDs) as .com, .org, .net, etc.
The domain name system is transparent and public. You can make DNS queries in a number of different ways, from using the host and whois commands on your Linux system to using any of the web-based query sites.
Example: Let's walk through the query process for apress.com, using the command line. To query the root name servers for information, you use whois:
|
12.1 Linux Network Programming Basics
[user@host user]$ whois apress.com
After the whois command executes, you'll see output that looks like this:
Domain Name: APRESS.COM
Registrar: NETWORK SOLUTIONS, INC.
Whois Server: whois.networksolutions.com
Referral URL: http://www.networksolutions.com
Name Server: AUTH111.NS.UU.NET
Name Server: AUTH120.NS.UU.NET
Status: ACTIVE
........
If you want the IP address of the machine with the name www.apress.com:
[user@host user]$ host www.apress.com auth111.ns.uu.net
Using domain server:
Name: auth111.ns.uu.net
Address: 198.6.1.115#53
Aliases:
www.apress.com has address 65.215.221.149
12.1 Linux Network Programming Basics
The host command takes two parameters: the name you want to translate into an IP address and the name (or address) of the server you want to use to make the translation. Using one of the name servers returned by the whois query, you ask for the IP address of the web server, and the name server responds with the IP address 65.215.221.149. At this point, your web browser would have the four pieces of information it needs to make a successful TCP/IP connection (Davis, Turner and Yocom 2004).
 |
Socket Functions |
|
A socket is an abstraction for network communication, just as a file is an abstraction for file system communication. Let's consider the basic network I/O functions for a Linux system. In general, an application that performs network input and output needs to perform the five basic functions described in Table 1: open, close, read, write, and control (Davis, Turner and Yocom 2004).
|
| Operation |
Explanation |
| Open |
Prepare for input or output operations. |
| Close |
Stop previous operations and return resources. |
| Read |
Get data and place in application memory. |
| Write |
Put data from application memory and send control. |
| Control (ioctl) |
Set options such as buffer sizes and connection behavior. |
12.1 Linux Network Programming Basics
Like any other application programming interface (API), the socket interface uses a set of predefined symbolic constants and data structure declarations. These constants and structures help applications specify function parameters and declare data structures in a consistent way. This increases portability and eases application maintenance by not requiring a developer to do things a different way for each environment. There are two primary sets of constants used in the Berkeley socket interface: protocol type constants and address family constants. In addition, a number of standard data structure declarations are available, as are the function prototypes themselves. These constants, function prototypes, and data structures are typically available in header files that can be incorporated into a program using the #include C preprocessor directive. The two files that should be included are types.h and socket.h, which are found in the /usr/include directory (Davis, Turner and Yocom 2004). The "include" statements in your application's code would look like this:
#include sys/types.h>
#include sys/socket.h>
Most interprocess communication uses the client/server model. The client connects to the the server to make a request for information.