Networking / Beginners

Hostname-to-Address Mapping

Converting between hostnames and IP addresses is the most common use for DNS. DNS can perform forward lookups or reverse lookups. A forward lookup converts a hostname to an IP address. A reverse lookup identifies the hostnames associated with an IP address.

Software Development

When programming, there are four main functions used to perform DNS resolutions. These are provided by the resolver library and perform the actual hostname lookups. The resolver library may access a local hosts file, such as C:\Windows\System32\ Drivers\Etc\Hosts or /etc/hosts, DNS, LDAP, NIS, or other name resolution system. The calling program has no option to specify the name resolution method.

sethostent: This function initializes the DNS library. In some implementations, this function takes a Boolean parameter to indicate whether TCP or UDP should be used for the connection. This is the case for Linux and BSD. Other operating systems do not require sethostent or simply ignore any passed parameters. In general, sethostent should be called before performing any hostname lookups.

gethostbyname: This function performs a forward lookup. Given a hostname, it returns the network address(es) associated with the hostname.

gethostbyaddr: This function performs a reverse lookup. Given a network address, it returns the hostname (and aliases) associated with the address.

endhostent: After performing a hostname lookup, this closes the library call. If sethostent specifies a TCP connection, then this function closes the connection. Also, if a host's file was used for the hostname lookup, then this call closes the file.

DNS (and other name resolution systems) provides a many-to-many mapping between hostnames and addresses. A single IP address may match a variety of hostnames, and a single hostname may map to a set of network addresses. The data structure returned by gethostbyname and gethostbyaddr provides a list of matches. In addition, hostname mapping may not be symmetrical. For example, the hostname chutney may resolve to 10.1.3.5, but a reverse lookup of 10.1.3.5 may not return chutney. This is common for servers that host many domain names.

[Previous] [Contents] [Next]