DNS
DNS
Hostnames
IP Addresses are great for computers
IP address includes information used for routing.
IP addresses are tough for humans to remember. IP addresses are impossible to guess.
ever guessed at the name of a WWW site?
Netprog: DNS and name lookups 2
DNS Hierarchy
edu rpi albany com org jp
Examples:
whitehouse.gov barney.the.purple.dinosaur.com monica.cs.rpi.edu
Netprog: DNS and name lookups 5
Domain Name
The domain name for a host is the sequence of labels that lead from the host (leaf node in the naming tree) to the top of the worldwide naming tree. A domain is a subtree of the worldwide naming tree.
Netprog: DNS and name lookups 6
Countries each have a top level domain (2 letter domain name). New top level domains include:
.aero .biz .coop .info .name .pro
DNS Organization
Distributed Database
The organization that owns a domain name is responsible for running a DNS server that can provide the mapping between hostnames within the domain to IP addresses. So - some machine run by RPI is responsible for everything within the rpi.edu domain.
DNS Clients
A DNS client is called a resolver. A call to gethostbyname()is handled by a resolver (typically part of the client). Most Unix workstations have the file /etc/resolv.conf that contains the local domain and the addresses of DNS servers for that domain.
Netprog: DNS and name lookups 10
/etc/resolv.conf
domain rpi.edu 128.113.1.5 128.113.1.3
11
nslookup
nslookup is an interactive resolver that allows the user to communicate directly with a DNS server. nslookup is usually available on Unix workstations. (dig and host are also DNS clients).
Netprog: DNS and name lookups 12
DNS Servers
Servers handle requests for their domain directly. Servers handle requests for other domains by contacting remote DNS server(s). Servers cache external mappings.
13
DNS Data
DNS databases contain more than just hostname-to-address records:
Name server records Hostname aliases Mail Exchangers Host Information NS CNAME MX HINFO
15
com
org
jp
Server Operation
If a server has no clue about where to find the address for a hostname, ask the root server. The root server will tell you what nameserver to contact. A request may get forwarded a few times.
Netprog: DNS and name lookups 17
18
16 bit fields
Response
19
Message Flags
QR: Query=0, Response=1 AA: Authoritative Answer TC: response truncated (> 512 bytes) RD: recursion desired RA: recursion available rcode: return code
Netprog: DNS and name lookups 20
Recursion
A request can indicate that recursion is desired - this tells the server to find out the answer (possibly by contacting other servers). If recursion is not requested - the response may be a list of other name servers to contact.
Netprog: DNS and name lookups 21
Question Format
Name: domain name (or IP address) Query type (A, NS, MX, ) Query class (1 for IP)
22
24
Lots more
This is not a complete description ! If interested - look at:
RFC 1034: DNS concepts and facilities. RFC 1035: DNS implementation and protocol specification. play with nslookup. Look at code for BIND (DNS server code).
Netprog: DNS and name lookups 25
With some OSs you need to explicitly link with the DNS resolver library:
-lnsl (nsl is Name Server Library)
Netprog: DNS and name lookups 26 Suns (Solaris) need this!
27
gethostbyname
struct hostent *gethostbyname( const char *hostname); struct hostent is defined in netdb.h: #include <netdb.h>
28
struct hostent
struct hostent { official name (canonical) char *h_name; char **h_aliases; other names AF_INET or AF_INET6 int h_addrtype; address length (4 or 16) int h_length; char **h_addr_list; array of ptrs to addresses };
Netprog: DNS and name lookups 29
hostent picture
h_name h_aliases h_addrtype h_length h_addr_list Official Name alias 1 alias 2
null
IP address 1 IP address 2
null
30
Which Address?
On success, gethostbyname returns the address of a hostent that has been created.
has an array of ptrs to IP addresses Usually use the first one:
32
Using memcpy
You can copy the 4 bytes (IPv4) directly: h = gethostbyname("joe.com"); memcpy(&sockaddr.sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
Netprog: DNS and name lookups 34
35
gethostbyaddr
struct hostent *gethostbyaddr( const char *addr size_t len, int family);
36