Writing your own nslookup in C++

Usually looking up the name of a computer or server is going on behind the scenes and we don't care about it. If some piece of software wants to talk with another computer it calls some kind of gethostbyname() function and the operating system does the job for you using the IP configuration. At work I encountered a situation where during the logon phase on a Windows terminal server an IP of another domain was needed. Of course the DNS servers knew of each other and could resolve names of the other domain but given the local DNS cache and some other DNS specific stuff there was some blindness regarding changing IP addresses.

So we used nslookup inside the logon script and parsed the output to be more up-to-date.

Fine, thank you for reading, good bye...

Err...not. If the one DNS server configured in the script was offline, everything was on the fritz. Now we could have asked more than one server, parse the output, yada yada, but logon scripts should not take too long. What we wanted was a single command for the terminal that would do the job. And there the question raised: could I do some kind of nslookup on my own?

First impressions of the native Windows API were not very promising (it may be my fault but I didn't find a way to tell which server to ask). After reading this blog entry I thought about doing it with Qt.

Qt provides me a lean and quite modern network interface, signals & slots for asynchronous execution and platform independence as the cherry on top. The rest would be more or less a bare implementation of the RFC 1035.

Feasibility study

The idea was to implement just enough of the protocol to send a request to some DNS servers and get an answer. The communication should be captured with Wireshark. After spending some days reading about the RFCs, the way other people implemented it, I came to the conclusion: to develop this on my own is madness! Why should I reinvent the wheel here?


After a while I stumbled upon an entry in stackoverflow. So the MSND documentation is just wrong. On the other hand this isn't really C++ (IMHO), it's a C API.

Just look in the example code, there is a goto. Yes, I am quite new in the C++ universe but please, just please.

Light at the end of the tunnel

A twitter tweet brought me on the homepage of the POCO C++ libraries again and I discovered the DNS in Poco::Net. So I downloaded the code and started hacking...