Networks consist of a number of interconnected machines called hosts. The system administrator assigns the hostname when a new machine is added to the network. Each host on a TCP/IP/Ethernet network has a name and three types of addresses: an Ethernet address, an IP address, and a TCP service port number. When information is passed from one layer to another in a network, the packet contains header information, including the addresses needed to send the packet to its next destination.
Often, the addresses returned by the networking functions are in a binary format. In order to convert those addresses into an ASCII format, Perl's pack and unpack function can be used.
Since the Ethernet address is usually burned into the PROM when the machine is manufactured, it is not a number that is assigned by a system administrator. It is simply used to identify that particular piece of hardware. The /etc/ethers (UCB) file contains the Ethernet addresses and hostnames for a particular network.
The IP address is a 32-bit number assigned by the system administrator to a particular host on the network. If a host is connected to more than one network, it must have an IP address for each of the networks. It consists of a set of four decimal numbers (often called a four-octet address) separated by dots (e.g., 129.150.28.56). The first part of the address identifies the network to which the host is connected, and the rest of the address represents the host. The addresses are divided into classes (A through C). The classes determine exactly what part of the address belongs to the network and what part belongs to the host. The /etc/hosts file contains the address of your host machine, the host's name, and any aliases associated with it. (See the gethostent and related functions on page 792.)
When serving a number of user processes, a server may have a number of clients requesting a particular service that use either the TCP or UDP protocol. When delivering information to a particular application layer, these protocols use a 16-bit integer port number to identify a particular process on a given host. TCP and UDP port numbers between 0 and 255, called well-known ports, are reserved for common services. (Some operating systems reserve additional ports for privileged programs.)[2] The most common services are Telnet and FTP with TCP port numbers 23 and 21, respectively. If you write a server application that will use either the TCP or UDP protocols, the application must be assigned a unique port number. This port number should be some number outside the range of the special reserved port numbers. The /etc/services file contains a list of the well-known port numbers.
[2] Stevens, W. R., and Wright, G. R., TCP/IP Illustrated, Volume 1: The Protocols, Addison Wesley Longman, 1993, p. 13.
The following Perl functions allow you to retrieve information from the /etc/protocols file. The functions are named after the system calls and library functions found in Sections 2 and 3 of the UNIX manual pages.
The getprotoent function reads the next line from the network protocols database, /etc/protocol, and returns a list. The entries are the official names of the protocols; a list of aliases, or alternative names, for the protocol; and the protocol number. The setprotoent function opens and rewinds the /etc/protocols file. If STAYOPEN is nonzero, the database will not be closed after successive calls to the getprotoent. The endprotoent function closes the database.
The getprotobyname function is similar to getprotoent in that it gets an entry from the /etc/protocols file. getprotobyname takes the protocol name as an argument and returns its name, any aliases, and its protocol number.
Formatgetprotobyname(NAME); Example 20.2.
|
The getprotobynumber function is similar to the getprotoent function in that it gets an entry from the /etc/protocols file. The getprotobynumber takes the protocol number as an argument and returns the name of the protocol, any aliases, and its protocol number.
Formatgetprotobynumber(NUMBER); Example 20.3.
|
These functions let you look up information in the network services file, /etc/services.
The getservent function reads the next line from the /etc/services file. If STAYOPEN is nonzero, the /etc/services file will not be closed after each call.
Formatgetservent; setservent (STAYOPEN); endservent; Example 20.4.
|
The getservbyname function translates the service port name to its corresponding port number.
Formatgetservbyname(NAME, PROTOCOL); Example 20.5.
|
The getservbyport function retrieves information from the /etc/services file.
Formatgetservbyport(PORT, PROTOCOL); Example 20.6.
|
These functions allow you to retrieve information from the /etc/hosts file. They are named after the system library routines found in Section 3 of the UNIX man pages.
The gethostent function returns a list consisting of the next line from the /etc/hosts file. The entries are the official name of the host machine; a list of aliases, or alternative names for the host; the type of address being returned; the length, in bytes, of the address; and a list of network addresses, in byte order, for the named host.
Formatgethostent; sethostent(STAYOPEN); endhostent; Example 20.7.
|
The gethostbyaddr function translates a network address to its corresponding names. It retrieves the information from the /etc/hosts file for a host by passing a raw address as an argument. The entry consists of the official name of the host machine; a list of aliases, or alternative names for the host; the type of address being returned; the length, in bytes, of the address; and a list of network addresses, in byte order, for the named host.
Formatgethostbyaddr(ADDRESS, DOMAIN_NUMBER); Example 20.8.
|
The gethostbyname function returns an entry from the /etc/hosts file for the name of a specific host passed as an argument. The entry consists of the official name of the host machine; a list of aliases, or alternative names for the host; the type of address being returned; the length, in bytes, of the address; and a list of network addresses, in byte order, for the named host.
Formatgethostbyname(NAME); Example 20.9.
|