Click here to Skip to main content
Click here to Skip to main content

A Simple DNS Resolver

By , 27 Mar 2004
 

Sample Image - dns_resolver.jpg

Introduction

This is my first socks program. Actually, it's the first one I completed since I want to test it's fuctionality before adding it to my main project. It is a simple DNS resolver using the Winsock API (v1.1). The program is a console application that accepts hostname or IP address parameters which will be resolved using gethostbyname and/or gethostbyaddress functions.

I really had a hard time of looking for a DNS query code in the net using the winsock API so I decided to try and make one myself. I have actually tested the program and it works. Of course, you need an internet connection to query DNS outside your network, and if the IP/host you queried is dead or has no reverse DNS then it would just state a DNS lookup failed!

The source code has comments on it and is fairly understandable for intermediate and perhaps beginners in winsock programming. It is mainly focused on the hostent structure since I had a hard time implementing it before. It is compiled in Dev-C++ which you could download at http://www.bloodshed.net/ using its default libraries. I've also placed some comments on the source code for information on how to compile it.

Feel free to comment...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

JLèé
Philippines Philippines
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralBUILD problemmembermotz14313 Oct '09 - 7:01 
i have compiled the code but when i try to build it gives error can any one help me please ...
 
the error details are as follow
 
Linking...
dns.obj : error LNK2001: unresolved external symbol _inet_ntoa@4
dns.obj : error LNK2001: unresolved external symbol _gethostbyaddr@12
dns.obj : error LNK2001: unresolved external symbol _inet_addr@4
dns.obj : error LNK2001: unresolved external symbol _gethostbyname@4
dns.obj : error LNK2001: unresolved external symbol _gethostname@8
dns.obj : error LNK2001: unresolved external symbol _WSACleanup@0
dns.obj : error LNK2001: unresolved external symbol _WSAStartup@8
Debug/dns.exe : fatal error LNK1120: 7 unresolved externals
Error executing link.exe.
 
dns.exe - 8 error(s), 0 warning(s)
GeneralSlow if the host doesn't existmemberLudvik Jerabek23 Jan '08 - 15:27 
The timeout for a host that doesn't exist is very long. nslookup.exe is much faster when a host doesn't exist. This is the exact issue I came up against when I was writing some similar code. Do you know how to get the resolution to be faster? Other then this issue good work!
 
Thanks,
 
Lu
GeneralRe: Slow if the host doesn't existmemberJLèé23 Jan '08 - 23:21 
Just the same if you try to PING a host that does not exist. It takes very long at first.
Try lowering the timeout if that is possible.
QuestionProblem in Building....membercharlesra20522 Aug '06 - 4:02 
The compilation went ok, but when I try to build with visual C it gives 3 errors(2100 ,link errors).
Should I copy some file to some place
PLEASE DO HELP ME.....Cry | :((
AnswerRe: Problem in Building....memberJLèé22 Aug '06 - 10:48 
This project requires the winsock libraries.
Link with the appropriate winsock .lib to compile it.
 
P.S. emailed you..
GeneralcompilememberDave_Nunes24 Jan '06 - 10:08 
Hi!
Your program seems very good but im unable to compile it because i dont know what do you mean by "link with libwsock32.a when compiling", can you help me?
Thanks!
 
David Nunes
GeneralCongratulationsmemberTommahawk12 Jan '05 - 17:15 
Just want to congratulate you on this piece of code.
Some info:
Using gethostbyname is better that gethostbyaddr as gethostbyname is apparently a little faster and does not bomb port 137. As discussed here, http://www.codeguru.com/Cpp/I-N/internet/network/article.php/c6239/
 
One question is what can be done with ip's that do not resolve to hosts and is this due to the DNS server not having an A record for the host, or reverse lookup not being configured?
GeneralRe: CongratulationsmemberJLèé12 Jan '05 - 20:59 
Thanks for the info. I really appreciate it. =D
 
Actually, my code uses both. It will only use gethostbyname() if the parameters supplied is a hostname, and gethostbyaddress() if it's an ip address (because for now I don't know of any other way to implement a DNS query with an ip address w/o knowing the hostname).
 

JLèé
GeneralQueriesmemberAslFunky2 Jan '05 - 7:37 
Hi,
I think that it is a good work that you have put up. You deserve a 5 from me.
Your implementation was what i was looking for.
I was searching for a similar thing. But i have query. I am developing a program where i need to know the address of the MX(mail exchange) of a domain. A similar result is yielded by using:
nslookup -type=mx yahoo.com
in DOS.
But the problem is that, since i am developing a windows app, the above command in a system() function, pops up a command window. This looks quite crude in my app. Can you please send a piece of code that can internally use the Winsock and give me the result. If not can you tell me the logic instead.
Thanx in advance.
 
Long live MFC!
//========\\
|| AslFunky ||
\\========//
Who said nothing is impossible,
I have been doing it everyday

GeneralRe: QueriesmemberJLèé12 Jan '05 - 22:05 
Try this sites:
 
http://www.2cah.com/dns/[^]
http://www.rscott.org/dns/mx.html[^]
 
or if you liked nslookup better, try writing your mx lookups in a file like: nslookup -type=mx yahoo.com > lookup.tmp ..... then open the file and get the needed info, but I'm not sure whether a DOS window will pop-up.
 
There is no need to get mx records on a domain if you are writing an e-mail server/client, since the SMTP server of the domain (e.g. yahoo.com) will search the mx records for you and redirect you to mail.yahoo.com.
 
Maybe if I have the time, I would make an email server and post it here next time.
 

Big Grin | :-D JLèé
GeneralRe: QueriesmemberAslFunky15 Jan '05 - 8:01 
Thanx for the reply.
As, i said earlier, i have tried that output redirection with nslookup, but again that dos screen pops up.
Lets see what i can do.
Please do post a reply if you find any solution.
Thanx in advance.

 
Long live MFC!
//========\\
|| AslFunky ||
\\========//
Who said nothing is impossible,
I have been doing it everyday

GeneralRe: QueriesmemberEastMohican14 Jul '05 - 15:20 
<< There is no need to get mx records on a domain since the SMTP server of the domain will search the mx records for you >>
What the SMTP server has to do with the MX lookup request?? The SMTP server of the receiving domain just expects a connection from outside from a given port (usually 25). If you intend to send a mail to yahoo.com, 3 cases:
- either you prepare a mail and provide it to "sendmail" for instance which does all the work
- or you use one of the API on your computer to send the mail (eg MAPI), and this API will resolve the MX entries and actually establish connection to one of the results (send mail)
- or, and I think this is the case here, you write your own API and bypass your local mail sender subsystem (eg MAPI) to send the mail, in this case you have to resolve the MX entries yourself then connect to one of the answers from the NS.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 28 Mar 2004
Article Copyright 2004 by JLèé
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid