Live data from Hacker News

A year of Rust and DNS

bluejekyll.github.io

61–70 of 109 posts

Re: A year of Rust and DNS

#61
post #60

Earlier quoted context omitted.

I found that after I had already written a lot of that code. I haven't had much reason to go back and change it, but any PR's if someone wants to do it, would be accepted.

What about simply replacing all "== 0b..." with "!= 0"? That would already be a good improvement, both in terms of DRY and readability, without the need of adding another dependency.

It wouldn't be the same thing. For instance x & 0b11 != 0 can return true for multiple values of x, 0b10 and 0b01 respectively.

Re: A year of Rust and DNS

#62
post #42

The only technical comment I have is that it continues to be amazing to me that destructuring binary data is so stupidly verbose in so many languages. I think C and Erlang are the only two languages that got this right.

Erlang I can easily see (destructuring binary data in erlang is an absolute pleasure) but C? Most binary destructuring in C seems to be done with shifts and masks, there's some gain from implicit integral conversions but I don't remember it being "right". IIRC the memory layout of bitfields is not specified so you can't use them to portably destructure binary streams, only to define "packed" structures for memory saving.

Re: A year of Rust and DNS

#63
post #20

Earlier quoted context omitted.

I thought DJB generally donates his code to the public domain. Hard to be "friendlier" than that.

Public domain code is actually illegal in some places, if I recall correctly

Not so much illegal as legally undefined/nonsensical, in most of mainland europe "putting something in the public domain" makes no legal sense and aliases to "all rights reserved".

Re: A year of Rust and DNS

#64
with erlang you can parse the dns-header like so:

,----

| %% extract dns-header fields from a raw packet.

| parse_dns_header(raw_packet) ->

| | ID:16,

| QR:1, OPCODE:4, AA:1, TC:1, RD:1, RA:1, Z:3, RCODE:4,

| QDCOUNT:16,

| ANCOUNT:16,

| NSCOUNT:16, ARCOUNT:16,

| Tail/binary

| >> = raw_packet,

|

| {#dns_header_record {

| id = ID,

| qr = QR, opcode = OPCODE, aa = AA, tc = TC, rd = RD, ra = RA, z = Z, rcode = RCODE,

| qdcount = QDCOUNT,

| ancount = ANCOUNT,

| nscount = NSCOUNT,

| arcount = ARCOUNT

| }, Tail}.

|

`----

almost verbatim 'transliteration' of sec:4.1.1 of 1035 :)

Re: A year of Rust and DNS

#65

Love the idea of reimplementing DNS in Rust. Would love to see more efforts like this so that we have secure-by-design language implementation of core security services. But BIND isn't just failing because "it's written in C", it's failing because it's written in terrible C. That said, "terrible C" is probably most every C routine written by someone with less than 10 years of solid low level experience, so "writing g…

Well, djb's dns has bugs (zone corruption, lack of duplicate outbound surpression leading to trivial poisoning, query pool flushing) and missing essentials (IPv6, and most DNS since 2007). Some of these bugs were paid out in fact. There are patches, but not everything is fixed, and not all the patches play well together. The lack of maintenance and an upstream has caused some distros to consider dropping for security…

Rewriting things in Rust probably doesn't protect against logic bugs like that. Likely it would introduce new bugs of that kind.

Re: A year of Rust and DNS

#66
post #35

Nit: Rust doesn't prevent memory leak.

It makes it much more difficult. In fact safe rust provides similar guarantees to garbage collected languages as the heap-allocated classes will clean themselves up.

Not if they have circular references... (I agree that Rust makes it difficult to do this accidentally)

Also, remember mem::forget() is not unsafe

I'm pointing this out because I don't want people to have wrong expectation of Rust. Rust doesn't prevent memory leak. Rust doesn't consider memory leak to be "unsafe". Even the Rust team themselves have pointed this out multiple times.

Somehow people are still repeating that Rust can rid you of memory leaks. And I'm downvoted just because I'm pointing out facts. Sigh.

Re: A year of Rust and DNS

#67
post #61
post #60

Earlier quoted context omitted.

What about simply replacing all "== 0b..." with "!= 0"? That would already be a good improvement, both in terms of DRY and readability, without the need of adding another dependency.

It wouldn't be the same thing. For instance x & 0b11 != 0 can return true for multiple values of x, 0b10 and 0b01 respectively.

I see. You are sometimes checking for multiple bits at once. Didn't notice that.

Re: A year of Rust and DNS

#68
post #64

with erlang you can parse the dns-header like so: ,---- | %% extract dns-header fields from a raw packet. | parse_dns_header(raw_packet) -> | | ID:16, | QR:1, OPCODE:4, AA:1, TC:1, RD:1, RA:1, Z:3, RCODE:4, | QDCOUNT:16, | ANCOUNT:16, | NSCOUNT:16, ARCOUNT:16, | Tail/binary | >> = raw_packet, | | {#dns_header_record { | id = ID, | qr = QR, opcode = OPCODE, aa = AA, tc = TC, rd = RD, ra = RA, z = Z, rcode = RCODE, | q…

Syntax-wise this is brilliant, I think. Very similar to how a good parsing-combinator library looks almost like the grammar it is parsing.

I wish more programming languages would make an effort like this to optimize readability and be self-documenting.

Re: A year of Rust and DNS

#69
post #68
post #64

with erlang you can parse the dns-header like so: ,---- | %% extract dns-header fields from a raw packet. | parse_dns_header(raw_packet) -> | | ID:16, | QR:1, OPCODE:4, AA:1, TC:1, RD:1, RA:1, Z:3, RCODE:4, | QDCOUNT:16, | ANCOUNT:16, | NSCOUNT:16, ARCOUNT:16, | Tail/binary | >> = raw_packet, | | {#dns_header_record { | id = ID, | qr = QR, opcode = OPCODE, aa = AA, tc = TC, rd = RD, ra = RA, z = Z, rcode = RCODE, | q…

Syntax-wise this is brilliant, I think. Very similar to how a good parsing-combinator library looks almost like the grammar it is parsing. I wish more programming languages would make an effort like this to optimize readability and be self-documenting.

> Very similar to how a good parsing-combinator library looks almost like the grammar it is parsing.

yup. getting from an ascii art representation of the pdu, to something like this should be fairly trivial :) only if writing fsm's were that easy ;)

Re: A year of Rust and DNS

#70
post #64

with erlang you can parse the dns-header like so: ,---- | %% extract dns-header fields from a raw packet. | parse_dns_header(raw_packet) -> | | ID:16, | QR:1, OPCODE:4, AA:1, TC:1, RD:1, RA:1, Z:3, RCODE:4, | QDCOUNT:16, | ANCOUNT:16, | NSCOUNT:16, ARCOUNT:16, | Tail/binary | >> = raw_packet, | | {#dns_header_record { | id = ID, | qr = QR, opcode = OPCODE, aa = AA, tc = TC, rd = RD, ra = RA, z = Z, rcode = RCODE, | q…

FWIW you can get code-formatting by prefixing lines with a 4-space indent:

    parse_dns_header(raw_packet) ->
      > = raw_packet,
Also for readers, this does depend on customisable (per-segment/field) defaults: `ID:16` is a shorthand for `ID:16/big-unsigned-integer-unit:1` aka "16-bits wide segment parsed as an unsigned integer in big endian"
Post reply on HN