Previous Page Next Page

20.4. Network Addressing

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.

20.4.1. Ethernet Addresses

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.

20.4.2. IP Addresses

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.)

20.4.3. Port Numbers

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.

20.4.4. Perl Protocol Functions

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

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.

Format

getprotoent;
setprotoent (STAYOPEN);
endprotoent;

Example 20.1.

(The Script)
1   while (($name,  $aliases, $proto ) = getprotent){
2   printf "name=%-5s,aliases=%-6sproto=%-8s\n",
            $name, $aliases, $proto;
    }

(Output)
2   name=ip,     aliases=IP    proto=0
    name=icmp,   aliases=ICMP  proto=1
    name=igmp,   aliases=OGMP  proto=2
    name=ggp,    aliases=GGP   proto=3
    name=tcp,    aliases=TCP   proto=6
    name=pup,    aliases=PUP   proto=12
    name=udp,    aliases=IDP   proto=17

Explanation

  1. The getprotoent function gets an entry from the /etc/protocols file. The loop will read through the entire file. The name of the protocol, any aliases associated with it, and the protocol number are retrieved.

  2. Each entry and its values are printed.


The getprotobyname Function

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.

Format

getprotobyname(NAME);

Example 20.2.

(The Script)
1   ($name,  $aliases, $proto ) = getprotobyname('tcp');
    print "name=$name\taliases=$aliases\t$protocol number=$proto\n";

(Output)
name=tcp    aliases=TCP    protocol number=6

Explanation

  1. The name of the protocol, any alias name, and the protocol number are retrieved from the /etc/protocols function. The name of the protocol, tcp, is passed as the NAME argument.


The getprotobynumber Function

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.

Format

getprotobynumber(NUMBER);

Example 20.3.

(The Script)
1   ($name,  $aliases, $proto) = getprotobynumber(0);
    print "name=$name\taliases=$aliases\t$protocol number=$proto\n";

(Output)
name=ip    aliases=IP    protocol number=0

Explanation

  1. getprotobynumber retrieves an entry from the /etc/protocols file based on the protocol number passed as an argument. It returns the name of the protocol, any aliases, and the protocol number.


20.4.5. Perl's Server Functions

These functions let you look up information in the network services file, /etc/services.

The getservent Function

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.

Format

getservent;
setservent (STAYOPEN);
endservent;

Example 20.4.

(The Script)
1   setservent(1 );
2   ($name, $aliases, $port, $proto) = getservent;
3   print
"Name=$name\nAliases=$aliases\nPort=$port\nProtocol=$protocol\n";
           <  program continues here >

4   ($name, $aliases, $port, $proto) = getservent;
                   # Retrieves the next entry in /etc/services
5   print
"Name=$name\nAliases=$aliases\nPort=$port\nProtocol=$protocol\n";
6   endservent;

(Output)
3   Name=tcpmux
    Aliases=
    Port=1
    Protocol=tcp
5   Name=echo
    Aliases=
    Port=7
    Protocol=tcp

Explanation

  1. The setservent function guarantees that the /etc/services file remains open after each call if the STAYOPEN flag is nonzero.

  2. The getservent function returns the name of the service, any aliases associated with the services, the port number, and the network protocol.

  3. The retrieved values are printed.

  4. The second call to getservent retrieves the next line from the /etc/services file.

  5. The retrieved values are printed.

  6. The endservent function closes the network file.


The getservbyname Function

The getservbyname function translates the service port name to its corresponding port number.

Format

getservbyname(NAME, PROTOCOL);

Example 20.5.

($name,$aliases,$port,$protocol)=getservbyname('telnet', 'tcp');

Explanation

The name of the service is telnet and the protocol is tcp. The service name, aliases, the port number, and the protocol are returned.


The getservbyport Function

The getservbyport function retrieves information from the /etc/services file.

Format

getservbyport(PORT, PROTOCOL);

Example 20.6.

(The Script)
    #!/bin/perl
    print "What is the port number? ";
    chomp($PORT=<>);

    print "What is the protocol? ";
    chomp($PROTOCOL=<>);

1   ($name, $aliases, $port, $proto ) = getservbyport(
                                       $PORT, $PROTOCOL);
    print "The getservbyport function returns:
        name=$name
        aliases=$aliases
        port number=$port
        prototype=$protocol \n";

(Output)
What is the port number?  517
What is the protocol?  udp
The getservbyport function returns:
    name=talk
    aliases=talk
    port number=517
    prototype=udp

Explanation

  1. The well-known port number 517 and the protocol name udp are passed to the getservbyport function. The name of the service, any aliases, the port number, and the protocol name are returned.


20.4.6. Perl's Host Information Functions

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

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.

Format

gethostent;
sethostent(STAYOPEN);
endhostent;

Example 20.7.

(The Script)
    #!/bin/perl

1   while ( ($name,  $aliases, $addrtype, $length, @addrs) =
           gethostent ){
2       ($a, $b, $c, $d) = unpack ( 'C4', $addrs[0]);
3       print "The name of the host is $name.\n";
4       print "Local host address (unpacked) $a.$b.$c.$d\n";
    }

(Output)
3   The name of the host is localhost.
4   Local host address (unpacked) 127.0.0.1
    The name of the host is jody.
    Local host address (unpacked) 129.150.28.56

Explanation

  1. The gethostent function retrieves the next entry from the /etc/hosts file.

  2. The raw address returned by the gethostent function is unpacked into 4 bytes (C4) so that it can be printed.

  3. The name of the host, jody, is printed.

  4. The local host's IP address is printed.


The gethostbyaddr Function

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.

Format

gethostbyaddr(ADDRESS, DOMAIN_NUMBER);

Example 20.8.

(The Script)
    #!/bin/perl
1   $address=pack("C4", 127,0,0,1);
2   ($name, $aliases,  $addrtype, $length, @addrs) = gethostbyaddr
        ($address,2);
3   ($a, $b, $c, $d) = unpack ( 'C4', $addrs[0]);
4   print "Hostname Is $name and the Internet address Is
        $a.$b.$c.$d.\n";

(Output)
Hostname is localhost and the Internet address is 127.0.0.1.

Explanation

  1. The Internet address is 127.0.0.1. It is packed into 4 bytes, and this address is used by the gethostbyaddr function.

  2. The raw address and the value of AF_INET (found in /usr/lnclude/sys/socket.h) are passed to the gethostbyaddr function.

  3. The raw address is unpacked into 4 bytes.

  4. The name of the host and Internet address are printed.


The gethostbyname Function

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.

Format

gethostbyname(NAME);

Example 20.9.

($name, $aliases, $addtrtype, $length,
        @addrs)=gethostbyname("dolphin");

Explanation

(See the gethostent function for explanation.)


Previous Page Next Page