Previous: Socket Options, Up: Sockets


16.13 Networks Database

Many systems come with a database that records a list of networks known to the system developer. This is usually kept either in the file /etc/networks or in an equivalent from a name server. This data base is useful for routing programs such as route, but it is not useful for programs that simply communicate over the network. We provide functions to access this database, which are declared in netdb.h.

— Data Type: struct netent

This data type is used to represent information about entries in the networks database. It has the following members:

char *n_name
This is the “official” name of the network.
char **n_aliases
These are alternative names for the network, represented as a vector of strings. A null pointer terminates the array.
int n_addrtype
This is the type of the network number; this is always equal to AF_INET for Internet networks.
unsigned long int n_net
This is the network number. Network numbers are returned in host byte order; see Byte Order.

Use the getnetbyname or getnetbyaddr functions to search the networks database for information about a specific network. The information is returned in a statically-allocated structure; you must copy the information if you need to save it.

— Function: struct netent * getnetbyname (const char *name)

The getnetbyname function returns information about the network named name. It returns a null pointer if there is no such network.

— Function: struct netent * getnetbyaddr (unsigned long int net, int type)

The getnetbyaddr function returns information about the network of type type with number net. You should specify a value of AF_INET for the type argument for Internet networks.

getnetbyaddr returns a null pointer if there is no such network.

You can also scan the networks database using setnetent, getnetent and endnetent. Be careful when using these functions because they are not reentrant.

— Function: void setnetent (int stayopen)

This function opens and rewinds the networks database.

If the stayopen argument is nonzero, this sets a flag so that subsequent calls to getnetbyname or getnetbyaddr will not close the database (as they usually would). This makes for more efficiency if you call those functions several times, by avoiding reopening the database for each call.

— Function: struct netent * getnetent (void)

This function returns the next entry in the networks database. It returns a null pointer if there are no more entries.

— Function: void endnetent (void)

This function closes the networks database.