What is DNS>#

DNS

Am gonna tell u the plain old topic of what DNS is.

Every computer/server/machine etc needs to have a name or id or something unique, so that it can be reached by anyother computer/server/machine over the internet.

IP address is the one which solves this problem, so every computer in this world has either an internal IP/External IP so in order to communicate between one computer to another u just needs to specify its IP(sender or receiver).

For eg: if u want to communicate to google’s computer, enter its ip, facebook, youtube, yahoo etcc all are same. but how dumbsters(like me) can able to remember all those computer’s IP addresses, thats y some vice people introduces something called DNS, DNS is basically a hashmap that stores english words with IP address. something like below; const dns = { google.com: 1.2.3.4 } Who can access this map, or how will one knows this domain belongs to this IP or how can the sender/receiver access this map etc, all these things will see below.

On a highlevel, this explains on what is DNS and why do we need them.

Basic structure of DNS:

www.example.com => in this www is subdomain example is main domain (the one that u purchase)
com is TLD(Top Level Domain).
com, xyz, biz etc all comes under TLD’s.

How DNS resolves to IP address?#

There are several entities or things that u need to know before jumping into really how this resolves.

  • DNS resolver.

  • Root Server.

  • TLD Domain Server.

  • Autoritative Name Server.

  • DNS resolver : Consider this a computer/server can be ur router/ISP’s_router/google’s resolver(8.8.8.8) etcc, this will basically takes ur domain and asks the root server which root server got the information for the tld of the domain.

    for eg: if you query www.example.com it will ask for com tld, if u query for www.example.xyz, it will ask for xyz, to the root servers.

    Now u may have a question, how does DNS resolver knows the IP of root servers, coz if it doesn’t knows then root server domain name again needs to be resolved right(which can’t be the case). so basically there are only 13 IP’s in this planet(this 13 ip may be 1k or even 10k of servers, not sure), so every DNS resolver will have this 13 root servers IP configured as constants in their machines/computers/server etc.

    NOTE: DNS resolver can be ur router/ISP router, also everytime u(browser or app etc) request a ip address for domain, it will cache in router layer(even os) for next use for some time(TTL).

  • Root Server : Root server will basically gives u the list of servers(with IP address) to DNS resolver, it has all the TLD records and it is maintained by ICANN.

  • TLD Server : TLD Server will give u the authoritative server(the nameserver that ur domain lives),
    How TLD server will know our domain IP ?
    Whenever u buy any domain, for eg: you are buying xxx.com(SFW domain) from namecheap(registrar), after succesful buying, the namecheap default nameservers(authoritative server) will be make some API call request to registry, which inturn update the TLD servers and update the records.
    In some cases u will configure other nameservers in ur registrar, for eg: u may configure cloudflare nameservers in namecheap, then in that case, for ur domain cloudflare nameservers will be pushed to TLD servers.

  • Authoritative name Server : This gives ur final IP address for ur domain.

Below is a simple snippet of DNS server(without recursion).

import udp from 'dgram'
import pkt from 'dns-packet'

const server = udp.createSocket('udp4')
const rootServerIp = "199.7.83.42"

const cache = {}

const queryDNS = (domain, ip) => {
  return new Promise(res => {
    const udpClient = udp.createSocket('udp4')
    const query = pkt.encode({
      id: Math.floor(Math.random() * Math.pow(2, 16)),
      type: 'query',
      questions: [
        {
          name: domain,
          type: 'A'
        }
      ]
    })

    udpClient.on('message', (msg) => {
      res(pkt.decode(msg))
    })

    udpClient.send(query, 53, ip)
  })
}

server.on('message', async (msg, ipInfo) => {
  const query = pkt.decode(msg)
  const domain = query.questions[0].name
  const tld = domain.split('.')[domain.split('.').length - 1]

  // youtube.ads.com
  // fetch TLD server IPs from rootServer by passing TLD as input
  const tldResponse = await queryDNS(tld, rootServerIp)
  const tldIp = tldResponse?.additionals.filter(a => a.type === 'A')[0]?.data

  if(!tldIp) {

    return
  }

  // fetch Nameserver IPs from TLDServer by passing Domain as input
  const nsResponse = await queryDNS(domain, tldIp)
  const nsIp = nsResponse?.additionals.filter(a => a.type === 'A')[0]?.data

  // fetch the domain IP from Nameserver by passing Domain as input
  const domainDetails = await queryDNS(domain, nsIp)
  console.log(domainDetails)


  //header 16bytes
  //id 2
  // qyer
  const response = pkt.encode({
    type: 'response',
    id: query.id, questions: query.questions,
    answers: domainDetails.answers
  })

  server.send(response, ipInfo.port, ipInfo.address)
})

server.bind(53, () => {
  console.log("UDP listening")
})