Introduction
DNS is a service that converts a host's name to an IP address. The Domain Name System (DNS) is a distributed database that is implemented as a hierarchy of name servers. It's an application layer protocol that allows clients and servers to send and receive messages.
Why do we need DNS?
There are two ways to identify a host. One by the hostname and another by an IP address. Routers prefer fixed-length, hierarchically structured IP addresses, while people prefer the more mnemonic hostname identification. Furthermore, because IP addresses are not static, a mapping is required to convert a domain name to an IP address. As a result, DNS is used to transform a website's domain name to a numerical IP address.
Domain
There are different categories of domains:
1. Generic domain: Examples of generic domains are ‘.com’(commercial), ‘.edu’(educational), ‘.org’(non-profit organization), ‘.mil’(military), ‘.net’(similar to commercial) etc.
2. Country domain: Examples of country domains are ‘.in’(India), ‘.us’(United States), ‘.uk’(United Kingdoms), ‘.au’(Australia), ‘.lk’(Srilanka) etc.
3. Inverse domain: Basically, it is IP to domain name mapping.
How does DNS work?
Assume that a user's host application (such as a Web browser or a mail reader) needs to convert a hostname to an IP address. The application will use DNS on the client-side to provide the hostname to be translated. (On various UNIX-based platforms, an application uses the function call gethostbyname() to perform the translation.)The DNS on the user's host then takes control, and it sends a query message into the network. All DNS query and reply messages are sent to port 53 as UDP datagrams. DNS in the user's host receives a DNS reply message with the desired mapping after a delay ranging from milliseconds to seconds. This received mapping (IP address) is then passed to the invoking application.
Categories of DNS servers
When a webpage is loaded, four separate DNS servers are involved.
1. DNS recursor: The recursor can be compared to a librarian who is asked to locate a specific book in a library. This server is designed to handle requests sent directly from client machines via web browsers (and other similar applications).
The recursor then makes additional requests to satisfy the client's DNS query.
2. Root Nameserver: The root server is the first stage in converting (resolving) human-readable hostnames to IP addresses. It's similar to a library's index, which points to different book racks. It gives the address set of the concerned TLD servers.
3. TLD (Top-level domain) Nameserver: This server is responsible for classifying websites according to their type. It is the last part of the domain name. TLDs include ‘.com’, ‘.org’, and ‘.net’, among others. This server can be thought of as a particular shelf in a library.
4. Authoritative Nameserver: This server can be compared to a dictionary on a bookshelf, where words can be translated. In a DNS query, the authoritative server is the final stop. If the authoritative server contains the requested record, it will return the requested hostname's IP address to the DNS recursor (who initiated the request).
Process of DNS lookup
( Source )
1. When a user types 'example.com' into a web browser, the query is transmitted over the Internet and received by a DNS recursive resolver.
2. The DNS resolver then queries the root server
3. The root server then sends the resolver the address of a Top-Level Domain (TLD) DNS server (such as '.com' or '.net'), which keeps the information for a specific domain. When we search for 'example.com,' we are sent to the '.com' TLD.
4. The DNS resolver then queries the ‘.com’ TLD.
5. The TLD server then returns the address of the domain’s authoritative server (Example: example.com)
6. Now the DNS resolver makes a request to the domain’s authoritative server.
7. Authoritative server returns the IP address of the requested hostname, to the DNS resolver.
8. Finally, the DNS resolver sends the requested IP address to the web browser.
The browser can request the web page after the 8 steps of the DNS lookup have returned the IP address for example.com.
Types of DNS queries
Three types of queries occur in a typical DNS lookup. An efficient DNS resolution process can result in a reduction of distance travelled and lookup delay, by using a combination of these queries.
Types of DNS queries are:
1. Recursive Query: A DNS client expects a DNS server (usually a DNS recursive resolver) to respond to a recursive query with either the requested record (IP address) or an error message if the resolver is unable to locate it.
Fig: Recursive query in DNS
2. Iterative Query: In an iterative query, a DNS client allows a DNS server to offer the best possible answer. If the queried DNS server cannot find a match for the query name, it will return the address of a DNS server authoritative for a lower level of the domain namespace.
After that, the DNS client will query the referral address. This operation continues along the query chain with other DNS servers until an error or timeout occurs.
Fig: Iterative query in DNS
3. Non-Recursive Query: This type of query occurs when the requested mapping is already stored in the cache memory. That is, when a DNS resolver queries a DNS server, it returns the desired mapping without further redirecting to some other server (because that mapping was already in the cache memory of the DNS server).
You can also read about mime protocol.