View all posts →

DNS

What is 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.

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

import pkt from 'dns-packet' import udp from 'dgram' const server = udp.createSocket('udp4') const rootServerIp = "199.7.83.42" const cache = {} function queryDNS(domain, ip) { return new Promise(function(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', function (msg) { res(pkt.decode(msg)) }) udpClient.send(query, 53, ip) }) } server.on('message', function 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(function(a) { return 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(function(a) { return 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) }) // DNS server listens on port 53 server.bind(53, () => { console.log("UDP listening") })
View all posts →