MS Suckage Beyond My Wildest Imagination

OK, so I’ve been working on some stuff related to the .tel domains – basically an extension for Thunderbird / Firefox for synchronizing contact info through a domain. Best I can tell the DNS support in Firefox already doesn’t support the NAPTR (Naming Authority Pointer) records used by .tel lookups. So I’m having to write an XPCOM component to handle it. On Windows I’m using MS’s windns lib (for the time being) and there the troubles began…

So let me state up front I’m NOT much of a C++ programmer. Mostly Java and C#, with some occaisional plain old C. So I’ve been wrestling a bit with the C++ elements of XPCOM, but getting through it. WinDNS is just C though, and should be straightforward. And in fact it is pretty simple to use to make DNS calls. Extracting and using the results however are another matter.

The DnsQuery call populates a DNS_RECORD structure with the results of the lookup. There are a few standard fields common to all lookups (including a message length), and then a Data field. Data is a union of MANY structs, one per type of lookup. Sounds reasonable enough, right? The struct I’m most interested in right now is DNS_NAPTR_DATA. NAPTR data consists of a couple of 16-bit unsigned ints for the record order and preference fields and 4 strings. DNS strings are interesting, with an 8-bit length followed by that number of characters. The last field is (when used, at least in .tel) a domain name, and rather than being a single dot-delimited string (like a typical address) it is a set of separate strings as defined above. The DNS_NAPTR_DATA struct mirrors this:


typedef struct _DNS_NAPTR_DATA {
WORD wOrder;
WORD wPreference;
PWSTR pFlags;
PWSTR pService;
PWSTR pRegularExpression;
PWSTR pReplacement;
}DNS_NAPTR_DATA, *PDNS_NAPTR_DATA;

Looks basic enough. The PWSTR are essentially pointers to Unicode strings. Seemed a little odd since DNS doesn’t use Unicode, but I figured the library translated them appropriately. So I put together some code, tried it out, and bada bing…

Garbage.

The order and preference were sort of OK, requiring conversion from network byte order to host. That should have set off some alarms right away. The strings were a mess. Debugging was showing some strange stuff, though I could see the expected data in there somewhere.

I’ll save the hours of frustration though and jump to the chase.

I finally realized that what is in the Data field as returned from DnsQuery ISN’T a nicely populated struct. It’s just the raw DNS message with a struct definition overlayed on it in memory. WTH? Maybe there’s a way to make it parse it out correctly, but I can’t find anything. So I ended up just ignoring the struct and parsing out the raw data on my own. Unless I’m doing something really wrong (I don’t think so) I can’t believe something so completely WRONG made it out of Microsoft. This isn’t even in the ballpark of correct. Sheesh!

This entry was posted in .tel, General, Windows, XPCOM. Bookmark the permalink.

2 Responses to MS Suckage Beyond My Wildest Imagination

  1. hasseily says:

    Hello Eddie, sorry I just got to reading this blog entry of yours. I may be able to help. First of all, there’s a simple shortcut to all this Windows garbage: Use a proper dns library like ldns (http://www.nlnetlabs.nl/projects/ldns/). It’s very small and very easy to use, and guaranteed to work with all the latest DNS specs.

    Then there’s another option for C#, which is to grab the code for the Outlook plugin in the Telnic developer svn repository. Specifically, you can use what’s in this directory: http://dev.telnic.org/rep/apps/outlook/trunk/DotTelSystem/DNS

    Clearly you’ve written your own by now, but the above may still help. Also there’s all the support for the write APIs using SOAP in the Outlook plugin that you could rip out and use for your own project. Let me know if you need anything else, I’m available at henri.tel

  2. Eddie says:

    Thanks for the tips. I pretty much abandoned the MS junk after all that – way too much work. And I really wanted something more portable anyway. I had started out playing with ldns on Linux, but when I went to try it on Windows I wasn’t having any luck. Couldn’t find a Windows port so I was trying to build it under VC++ and was running into a LOT of problems trying to make it compile there, and that’s when I tried the MS DNS stuff. After the problems above and your suggestion, I decided to see if I could use ldns by building under Cygwin. Took a little work, but much better! Main problem was ldns picks up the servers from /etc/resolv.conf (or a given file) which Windows doesn’t have. Need to rifle through the pit of despair, er, registry. Once I added support for that, it’s working like a champ!

    I wish the XPCOM framework would support something other than Javascript and C++, at least as far as writing components for Mozilla / Thunderbird. While I’ve been a programmer for years, C++ isn’t my strength. I’ve mainly used Java since it was released with some C and C# thrown in at times. But very little C++.

Leave a Reply