CITS3002 Computer Networks  
prev
CITS3002 help3002 CITS3002 schedule  

Typical API Library Routines

Access to lists of networking structures and services is generally provided by library routines.

For example, the traditional Unix API provides routines to discover hosts, networks, protocols, application services, and the necessary data conversion operations.

Consider the following code to iterate through (many) hostnames at UWA:

#include  <stdint.h>

int main(int argc, char *argv[])
{
    struct  netent *ne;
    struct hostent *he;

    uint32_t  hostorder, netorder;

    setnetent(1);           // open the network name database

    while(ne = getnetent()) {   // foreach network entry
	printf("NETWORK %s :\n", ne->n_name);
	hostorder = (ne->n_net << 8);

	sethostent(1);      // open the host name database

	for(int i=1 ; i<=254 ; i++) {
	    netorder = htonl(hostorder);
	    if(he = gethostbyaddr((char *)&netorder, sizeof(netorder), AF_INET))
                printf("%36s : %s\n", he->h_name, inet_ntoa(he->h_addr));
                ++hostorder;
            }
        }
        endhostent();       // close the host name database
    }
    endnetent();            // close the network name database
    return 0;
}




CITS3002 Computer Networks, Lecture 9, Client/server design, p14, 1st May 2024.