CSCI.4220 Network Programming
Class 8, Monday Feb 14, 2005
DNS

Here is a good description of DNS records

Here is code to display the complete hostent record. It takes a host name as an argument

#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
    struct hostent *h;
    char **p;
    struct in_addr t;

    if (argc <2) {
       fprintf(stderr, "usage %s hostname\n",argv[0]);
       exit(0);
    }

    h = gethostbyname(argv[1]);
    if (h == NULL) {
      fprintf(stderr, "error, no such host %s\n",argv[1]);
      exit(0);
    }
    printf("h_name is %s\n",h->h_name);
    p = h->h_aliases;
    while (*p != NULL) {
	 printf("Alias: %s\n",*p);
         p++;
    }
    p = h->h_addr_list;
    while (*p != NULL) {
      memcpy((char *)&t,*p,4);     
      printf("IPv4 addr %s\n",inet_ntoa(t));
      p++;
    }
    printf("Good bye\n");
    return 0;
}
a.out www.yahoo.com gets
h_name is www.yahoo.akadns.net
Alias: www.yahoo.com
IPv4 addr 216.109.118.76
IPv4 addr 216.109.118.79
IPv4 addr 216.109.117.204
IPv4 addr 216.109.117.205
IPv4 addr 216.109.118.65
IPv4 addr 216.109.118.70
IPv4 addr 216.109.118.74
IPv4 addr 216.109.118.75

Hierarchical name space

A name like mary-kate.cs.rpi.edu has four fields. The leftmost field is the name of the actual machine, the rest are domains. The domains are in a hierarchy. There can be up to 127 levels, but you don't usually see more than four.

There are stored in a distributed, hierarchical database.

There are 13 Root DNS servers, named a to m (each is actually a cluster of replicated servers)

Here are some of the top level domains.

com
edu
gov
mil
net
org
country codes (cn = china, fr=france, uk, nl, ca=canada, lv=latvia
Each of these has a set of domain name servers (top level domain servers)

Every org with publicly accessible hosts must provide publicly addressible DNS records that map the names of their hosts to IP addresses. There are generally redundant name servers for reliability.

A name server does a lot of caching. If it does not have a name in its cache, it sends a query to a top level name server, which replies with the IP address of the name server at the next level.

It then contacts the next level, etc.

Two ways to do this, iterative (as described above) or recursive. Top level contacts the next level etc and sends target IP address back to requester.

Resource Records

DNS servers have resource records (RRs) with four fields (a four-tuple) Name, Value, Type, TTL

The meaning of name and value depends on type

If type=A, name is a hostname and value is an IP address

If type=NS, name is a domain, and value is the name of the authoritative name server.

If type=CNAME Value is a canonical hostname for the alias hostname

If Type=MX value is the canonical name of a mail server

If type=AAAA value is a 128 bit IPv6 address

If type=PTR this maps IP addresses to host names - the reverse of an A record. This is sometimes used for security.

DNS Messages

There are only two types of DNS messages, and queries and replies. They ave the same format

Here is a link to a web site which describes the DNS message format