How to Implement Reverse DNS Look Up Cache? - GeeksforGeeks
How to Implement Reverse DNS Look Up Cache? Reverse DNS look up is using an internet IP address to find a domain name.
Read full article from How to Implement Reverse DNS Look Up Cache? - GeeksforGeeks
How to Implement Reverse DNS Look Up Cache? Reverse DNS look up is using an internet IP address to find a domain name.
How to implement Reverse DNS Look Up cache? Following are the operations needed from cache.
1) Add a IP address to URL Mapping in cache.
2) Find URL for a given IP address.
1) Add a IP address to URL Mapping in cache.
2) Find URL for a given IP address.
One solution is to use Hashing.
In this post, a Trie based solution is discussed.
void
insert(
struct
trieNode *root,
char
*ipAdd,
char
*URL)
{
// Length of the ip address
int
len =
strlen
(ipAdd);
struct
trieNode *pCrawl = root;
// Traversing over the length of the ip address.
for
(
int
level=0; level<len; level++)
{
// Get index of child node from current character
// in ipAdd[]. Index must be from 0 to 10 where
// 0 to 9 is used for digits and 10 for dot
int
index = getIndex(ipAdd[level]);
// Create a new child if not exist already
if
(!pCrawl->child[index])
pCrawl->child[index] = newTrieNode();
// Move to the child
pCrawl = pCrawl->child[index];
}
//Below needs to be carried out for the last node.
//Save the corresponding URL of the ip address in the
//last node of trie.
pCrawl->isLeaf =
true
;
pCrawl->URL =
new
char
[
strlen
(URL) + 1];
strcpy
(pCrawl->URL, URL);
}
// This function returns URL if given IP address is present in DNS cache.
// Else returns NULL
char
*searchDNSCache(
struct
trieNode *root,
char
*ipAdd)
{
// Root node of trie.
struct
trieNode *pCrawl = root;
int
len =
strlen
(ipAdd);
// Traversal over the length of ip address.
for
(
int
level=0; level<len; level++)
{
int
index = getIndex(ipAdd[level]);
if
(!pCrawl->child[index])
return
NULL;
pCrawl = pCrawl->child[index];
}
// If we find the last node for a given ip address, print the URL.
if
(pCrawl!=NULL && pCrawl->isLeaf)
return
pCrawl->URL;
return
NULL;
}