News for hackers. Stuff that matters.
Re:A toy DNS resolver by NoWayDude1 on Tuesday February 01, 2022 @05:24PM (#30170380)
One can do even better. How about a DNS resolver in 1 line of bash code:

/lib/systemd/systemd-resolved

Using miekg/dns to write a DNS resolver in "80 lines" is like microwaving pizza and then saying "I made a pizza in just 5 minutes in one easy step".

Re:A toy DNS resolver by dang on Tuesday February 01, 2022 @09:18PM (#30172726)
"Please don't post shallow dismissals, especially of other people's work. A good critical comment teaches us something."

https://news.ycombinator.com/newsguidelines.html

Re:A toy DNS resolver by takeda on Tuesday February 01, 2022 @11:44PM (#30173813)
I know it starts like a shallow dismissal, but mentioning that code uses another DNS package as a dependency (miekg/dns) has a point. I originally missed that, and I believe many probably too.

I don't think author can claim "in 80 lines of code" when the code depends on a much bigger library than itself.

Re:A toy DNS resolver by comonoid on Wednesday February 02, 2022 @01:59AM (#30174632)
It's just a generic DNS packet parser, it has no any resolving logic (or it is not used). For learning the resolving process, it is a perfect combo.
Re:A toy DNS resolver by chlorion on Wednesday February 02, 2022 @12:17AM (#30174018)
The original title was "Writing a DNS resolver in 80 lines of Go".

The comment might seem like its being dismissive with the current title "A toy DNS resolver", but at the time of writing it was not just a shallow dismissal.

Re:A toy DNS resolver by tptacek on Wednesday February 02, 2022 @12:46AM (#30174209)
It was a shallow dismissal then too.
Re:A toy DNS resolver by tptacek on Tuesday February 01, 2022 @07:20PM (#30171788)
This is a surpassingly facile putdown. `miekg/dns` is being used here as a DNS request/response parser library. There's almost nothing interesting about parsing DNS messages. The interesting logic in the DNS is the recursive lookup, which is the part this post captures.

As far as I know (we use the same library for our authority servers) `miekg/dns` doesn't even do recursive lookups.

It's not just that, though. It's also that recursive lookups are the probably the most mystifying aspect of DNS. DNS message parsers are straight-line code; there are lots of them, including in POSIX libc. What you don't have in POSIX libc is a function that does a complete from-the-root recursive lookup for a name; most DNS "client" implementations stop at "send the request to the local recursive cache".

To see that function implemented in such a tiny amount of code is itself super interesting.

Essentially, what you're saying is that the article disappointed you because it purported to be about making pizza, but didn't first specify how to make the oven.

Re:A toy DNS resolver by BeefWellington on Tuesday February 01, 2022 @08:37PM (#30172374)
I disagree. The entire client connection is being handled by the imported DNS library:

    func dnsQuery(name string, server net.IP) *dns.Msg {
        fmt.Printf("dig -r @%s %s\n", server.String(), name)
        msg := new(dns.Msg)
        msg.SetQuestion(name, dns.TypeA)
        c := new(dns.Client)
        reply, _, _ := c.Exchange(msg, server.String()+":53")
        return reply
    }

    func main() {
        name := os.Args[1]
        if !strings.HasSuffix(name, ".") {
            name = name + "."
        }
        fmt.Println("Result:", resolve(name))
    }
While I agree it's impressively simply laid out and as an exercise good to see people getting hands-on with underpinning protocols (I'm a fan of this), it also does its job as a follow up to her previous blog post on the subject.

People taking umbrage with the title as posted here (and the original site title) are IMO not wrong. The article title has been changed to "A toy DNS resolver" now, so obviously it was contentious enough that she thought to change it.

Re:A toy DNS resolver by tptacek on Tuesday February 01, 2022 @08:45PM (#30172443)
What do you think an "entire client connection" is? It's a read and a write from a net.Conn. Read client.go's (c *Client) exchange() (the lowercase one). It's 30 lines of code, and that includes stuff like EDNS0, TSIG, and read deadlines that have nothing to do with the functionality the author is demonstrating.

If the author took the time to write a 15 line roundTrip() function that called m.Pack(), udpConn.Write(), udpConn.SetDeadline(1s), and udpConn.Read(), would your argument here evaporate? Then it's not a very good argument, is it?

Re:A toy DNS resolver by BeefWellington on Tuesday February 01, 2022 @09:14PM (#30172694)
Opening a connection and performing parsing on record formats is the "heavy lifting" in this example.
Re:A toy DNS resolver by tptacek on Tuesday February 01, 2022 @09:26PM (#30172795)
Yes, we're clear on where the disagreement is between the two of us.
Re:A toy DNS resolver by charcircuit on Tuesday February 01, 2022 @08:38PM (#30172383)
>Essentially, what you're saying is that the article disappointed you because it purported to be about making pizza, but didn't first specify how to make the oven.

No it's more akin about him saying he made a pizza, but all that was done was import a pizza and then sprinkle some pepperoni on it.

Imagine if I said I made an HTTP server in 6 lines.

    const express = require("express");
    const app = express();
    app.get("/", (req, res) => {
      res.send('Hello World!');
    });
    app.listen(8080);
I think it would be reasonable for people to say that I didn't make a HTTP server in 6 lines.

Just how I used a library to make a HTTP server me and I handled requests/responses, the author of this article used a library to handle DNS requests/responses. The 80 lines figure for making a DNS resolver is misleading when there are 26k lines of Go code (according to cloc) for handling DNS that are just in a library instead of the main file.

Re:A toy DNS resolver by tptacek on Tuesday February 01, 2022 @08:40PM (#30172399)
You have managed simultaneously to radically underestimate the complexity of Node and Express's HTTP handling, while at the same time radically overestimating the complexity of what miekg/dns is being asked to do here.

The majority of the 20,000 lines of non-test code in miekg/dns are just handlers for record types the author of this article didn't need; much of the rest of it is stuff like zone file parsing and DNSSEC signatures, which again have nothing to do with what the author wrote.

What you conspicuously won't find in miekg/dns: a recursive lookup routine.

Re:A toy DNS resolver by charcircuit on Wednesday February 02, 2022 @12:10AM (#30173980)
>What you conspicuously won't find in miekg/dns: a recursive lookup routine.

Sure, but it implements the building blocks that you need to create one. Those building blocks are going to be where a good chunk of code is going to be. (Yes, there is a lot of code from that library that will be unused)

Re:A toy DNS resolver by taeric on Tuesday February 01, 2022 @05:34PM (#30170523)
More than a little unfair when the following is in the article:

    I’m not going to write this completely from 
    scratch – I think parsing DNS packets is really 
    interesting, but it’s definitely more than 80 
    lines of code, and I find that it kind of 
    distracts from the algorithm.
Which, seems legit to me. This article was a fun read to refresh on exactly what goes on in resolving basic records.
Re:A toy DNS resolver by dixie_land on Tuesday February 01, 2022 @05:40PM (#30170592)
I agree it’s a fun article but you can’t deny the title is at the very least clickbait-ish
Re:A toy DNS resolver by tptacek on Tuesday February 01, 2022 @07:43PM (#30171985)
I hereby do fully deny that there is anything the least bit clickbait-ish about this article, which is super interesting and all the more an achievement for its brevity. It demystifies what is the most annoying thing about trying to code the client side of DNS, and the fact that it uses the same DNS message parsing library every other Go programmer does isn't of the slightest import --- if the article hadn't used miekg/dns, some other lowbrow comment would be taking the author to task for reinventing the message codec wheel.

The world needs more simple recursive lookup examples, and absolutely does not need more explication of DNS message parsing, any more than than an article talking about etcd's Raft implementation would benefit from a hand-rolled implementation of Protobufs.

Yikes, everybody.

Re:A toy DNS resolver by tedunangst on Tuesday February 01, 2022 @11:45PM (#30173826)
It's (sadly) telling that all the dismissals are focused on packet parsing, and not like, bailiwick checking, which is understandably missing, but also really important.
Re:A toy DNS resolver by userbinator on Tuesday February 01, 2022 @08:48PM (#30172468)
but it’s definitely more than 80 lines of code

To this old demoscener, that sounds like a challenge...

Re:A toy DNS resolver by zamadatix on Tuesday February 01, 2022 @05:50PM (#30170722)
This is the key case I wish HN allowed editorializing the title. Regardless how good the article is (or isn't) when they have titles like this it's inevitable the conversation will be about how the title was overreaching. If people were encouraged to make titles like this into something like "Making a simple DNS resolver with miekg/dns in Go" or even just "A DNS resolver in Go" when posting we'd avoid this multiple times a day recurring problem.

Or maybe it already is encouraged and people just aren't aware this counts as "misleading" or "linkbait" since it's not well described what the cutoff point is.

Either way both titles like this one and discussions of have long gotten old.

Edit: looks like the article moved to "A toy DNS resolver", excellent choice of wording IMO.

Re:A toy DNS resolver by djur on Tuesday February 01, 2022 @06:01PM (#30170857)
"Making a simple DNS resolver with miekg/dns in Go" would have given me the false impression that the article was about how to use miekg/dns. "A DNS resolver in Go" would have given me the false impression that it was about a full-fledged DNS resolver meant for integrating with Go applications. The current title at least pushed me in the direction of understanding what the article is about, which is demonstrating how DNS resolution works using a short Go program.
Re:A toy DNS resolver by zamadatix on Tuesday February 01, 2022 @06:24PM (#30171169)
That wouldn't be a false impression, this article is indeed about how to use miekg/dns to make a resolver. Your comments on the other title already apply to the current title as is, it's one of things people hate about these titles but at least the cut title avoids the line count/"nothing is from scratch" debates.

Increasingly more accurate titles always accepted of course though but one thing for sure is the class of "<x> in <y lines>" are demonstrably a continuous problem to the point they are talked about instead of the articles they represent, they are not just minor wording nits one could come up with after staring at a title long enough.

Edit: I really like the title the article itself changed to "A toy DNS resolver".

Re:A toy DNS resolver by pvg on Tuesday February 01, 2022 @07:52PM (#30172062)
A small nit but it's actually important for the HN Rules of Titles: 'edit' and 'editorialize' are different things. You can sometimes edit a title, for instance if it's misleading or clickbait. You can't editorialize in titles no matter what.
Re:A toy DNS resolver by justsomehnguy on Tuesday February 01, 2022 @05:54PM (#30170789)
> This is the key case I wish HN allowed editorializing the title

The ability to leave a short (less than 100? 140?) description would be one the ways which would allows not to clickba^W editorialize the titles.

Or the submission author can do the same by leaving the comment about it in the first comment.

Re:A toy DNS resolver by throwaway1777 on Tuesday February 01, 2022 @05:59PM (#30170844)
Leaving description opens up other bad avenues though. Like posting an article with a description about how stupid the article or the author is.
Re:A toy DNS resolver by zamadatix on Tuesday February 01, 2022 @06:32PM (#30171284)
The more general take I have seen is that it would give a "priority comment". That can result in problems like that but also more commonly just cause the comments to all act like a response to the "main" comment rather than general discourse on the topic. I can definitely see that though I also see the poster having a large amount of that control already in most cases since they get to pick the source they post.
Re:A toy DNS resolver by airstrike on Tuesday February 01, 2022 @07:29PM (#30171863)
> Like posting an article with a description about how stupid the article or the author is.

The community can moderate that, though

Re:A toy DNS resolver by chomp on Tuesday February 01, 2022 @05:36PM (#30170562)
I smiled at this, though the Sagan quote "If you wish to make an apple pie from scratch, you must first invent the universe" springs to mind. At some point to be reasonable you have to cut off the lower layers of abstraction or else it's madness.

Unbound leans on libresolv, so I don't think it's cheating to lean on external libraries - most of the hard part about resolvers is all the crufty RFCs you have to support anyway, not the basic nuts and bolts of resolving an A record.

Re:A toy DNS resolver by LinuxBender on Wednesday February 02, 2022 @10:55AM (#30179281)
Somewhat off-topic: Adding song [1] for completeness sake and so that it is stuck in other peoples heads too.

[1] - https://amorningfilledwith400billionsuns.ytmnd.com/

Re:A toy DNS resolver by silisili on Tuesday February 01, 2022 @05:36PM (#30170557)
I normally don't like comments like this but I think in this case your point stands. DNS is not 'easy' in that you have troves of RFCs and undocumented but expected behaviors to follow, etc. miekg/dns does all of the heavy lifting here.

Writing a resolver in it is a fun project, but bragging about the line count is silly.

Re:A toy DNS resolver by djur on Tuesday February 01, 2022 @05:55PM (#30170794)
I didn't read the article as "bragging" about the line count, but saying it was short enough to be read casually as a companion to the comic.
Re:A toy DNS resolver by zaphar on Tuesday February 01, 2022 @08:01PM (#30172130)
miekg/dns literally only handles building/parsing the dns packets. Which may be a complicated activity and perhaps for you equals "doing all the heavy lifting" But it is absolutely not doing the "recursive resolve" algorithm at all here. I think parsing network packets is interesting but it's not the point of the article and saying that miekg/dns is doing all the heavy lifting doesn't give the article or it's author enough credit.

Honestly the comments of this type on this article sound more like you didn't actually read the article and are just knee jerk putting someone down. I hope that is not the case but maybe it would be good to examine why the message is coming off so distastefully.

Re:A toy DNS resolver by wanderer_ on Tuesday February 01, 2022 @05:52PM (#30170749)
I think the line count was just to emphasize simplicity and accessibility, rather than to brag.
Re:A toy DNS resolver by mevdschee on Tuesday February 01, 2022 @06:08PM (#30170974)
>bragging about the line count is silly.

and gatekeeping is silly too..

Re:A toy DNS resolver by tptacek on Tuesday February 01, 2022 @08:01PM (#30172134)
miekg/dns is doing almost none of the heavy lifting here, which you can verify for yourself by counting the number of record types this code actually needs to parse to function, and comparing it to the number of record types miekg/dns supports and how much of the code exists just to dispatch to random record type handlers.
Re:A toy DNS resolver by silisili on Tuesday February 01, 2022 @08:37PM (#30172373)
It only builds the requests, crafts the packets, sends them, receives the reply, and parses it back into useable objects. No heavy lifting at all.
Re:A toy DNS resolver by tptacek on Tuesday February 01, 2022 @08:39PM (#30172393)
Correct; none of that is the heavy lifting of a recursive lookup. One way you know that is that similar code lives in every libc, but no libc I know of has a recursive resolver; the pattern of fobbing off complicated recursive queries to cache servers is so common that there's a name for library-style resolvers: "stub resolver".

You're not going to win this argument, no matter how many synonyms for "encode" and "decode" you come up with.

Re:A toy DNS resolver by silisili on Tuesday February 01, 2022 @08:52PM (#30172502)
There's no argument I'm trying to win. I've only stated my opinion.

If you hold the opinion that a 12 line for loop is heavier lifting than the underlying library doing the aforementioned things, that's your right.

Re:A toy DNS resolver by tptacek on Tuesday February 01, 2022 @08:58PM (#30172561)
You have one extra "your" the final clause of that last sentence.

I'm not messing with you; as someone who does an unfortunate amount of DNS hacking, it is crazymaking to see so many people express the opinion that the hard part of doing DNS is just formatting the records. The argument you're presenting is a little like saying that "malloc" would be doing the heavy lifting in a C implementation of a graph minimum cost spanning tree.

This also isn't just an aesthetic argument. There is something profound about it. No language standard library I'm aware of includes a recursive lookup, despite the fact that you've pegged it as a "12 line for loop". They all in some way or other include DNS message codecs, but not the recursive lookup, despite the fact that it would be immensely useful to be able to write programs that did recursive lookups directly rather than relying on the system's configured recursive resolver. The reason for that is at least partly that recursive lookup is mystifying and spooks library implementors.

I've had the displeasure of writing both a series of DNS codecs and a recursive lookup routine. The codecs I've done throughout my career, going back to like 1997 with exploits for the Kashpureff cache poisoning bug. The recursor I finally got around to writing just a couple months ago, because recursive lookups are freaking complicated.

That the author got this recursive lookup so small that it broke everyone's brains is just more reason to be interested in this article. It's certainly not a reason to dismiss it. The reactions on this thread are pretty embarrassing.

Re:A toy DNS resolver by rootlocus on Wednesday February 02, 2022 @04:43AM (#30175548)
> That the author got this recursive lookup so small that it broke everyone's brains is just more reason to be interested in this article.

https://github.com/jvns/tiny-resolver/blob/main/resolver.sh - the recursive lookup is 16 lines for + switch.

I'm sorry to say this, but if the recursive lookup can be implemented with a for and a 3-way switch that a CS 101 student can write, it's really not doing the heavy lifting. It may be interesting to know about it, it may be the case that multiple resolves don't have the implementation, but it's a trivial piece of code, let's not idolize it.

For me personally, having the power of hitting an endpoint and receiving useful information is really satisfying. Creating the request and parsing the reply are probably 90% of that process. And frankly, that was the first think I was looking for when I skimmed the article. "Are DNS replies really that easy to parse?". The fact that I need to make a switch on the reply and potentially make a recursive request somewhere else is trivial once you get the actual useful info from the remote.

I'm not criticising the article or the title. It's my opinion that parsing is more important, I'm not faulting the author. But I'm having a hard time accepting your arguments.

Re:A toy DNS resolver by tptacek on Wednesday February 02, 2022 @12:04PM (#30180338)
It's not my argument that cs 101 students can't write recursive resolvers. Everyone can, and more people should, just like more people should write emulators, compilers, database engines, TCP/IP stacks, ray tracers, and computer algebra systems. Not because writing them is a huge achievement for humanity, but because people writing them and then documenting the process is vitally important for demystifying these things, in an era where most programming is just piping data from one database to another and we've lost touch with so much basic computer science.

My argument is that DNS codecs are not the interesting or tricky part of writing recursive resolvers.

Having had the pleasure of writing a bunch of DNS codecs, I'm having trouble even conceptualizing what's interesting about writing one. I don't think most people look at DNS and think "I could do this, but for the difficulty of constructing an NS record".

Re:A toy DNS resolver by vidarh on Wednesday February 02, 2022 @03:52AM (#30175278)
I'd add to this that if you just want to demonstrate the encode/send/receive/decode part, that too can be done in a nearly trivial amount of code.

I know because I've done it (I wrote a an authoritative DNS server from scratch for a registry platform we did for .name).

To write a full fledged library for it for a resolver (an authoritative server can take plenty of shortcuts depending on purpose) is complex, but I absolutely agree the basics that you'd demonstrate if writing a toy one to demonstrate would be just a distraction.

Re:A toy DNS resolver by dapids on Tuesday February 01, 2022 @08:51PM (#30172497)
Okay, now do it recursively for all supported record types. Oh, wait.
Re:A toy DNS resolver by bragr on Tuesday February 01, 2022 @05:57PM (#30170812)
Cannot second this enough. miekg/dns is probably tens of thousands of mature lines of code covering all aspects of the DNS protocol. This feels like bragging about creating an minimal http client but really you just used libcurl to do the heavy lifting.
Re:A toy DNS resolver by tptacek on Tuesday February 01, 2022 @07:29PM (#30171864)
It is nothing at all like using libcurl to do an HTTP request, as libcurl handles the semantics of the HTTP protocol, and not just marshaling and unmarshaling HTTP requests and responses.

miekg/dns is also 20,000 lines of code most of which are not relevant to the task of doing a recursive lookup.

Re:A toy DNS resolver by djur on Tuesday February 01, 2022 @06:05PM (#30170931)
Seems more like writing an article where you explain how HTTP caching headers work by using libcurl to write a program to mirror a website. The purpose isn't showing off, it's education.
Re:A toy DNS resolver by whimsicalism on Tuesday February 01, 2022 @05:48PM (#30170697)
Agree only because of how the post was titled.
Re:A toy DNS resolver by dapids on Tuesday February 01, 2022 @06:37PM (#30171349)
I guess we are calling paths to executables "bash code" now.