4 ms·
with erlang you can parse the dns-header like so: ,---- | %% extract dns-header fields from a raw packet. | parse_dns_header(raw_packet) -> | << |
by signa11 10y ago
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 :)
- rkrzr 10y agoSyntax-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.
- signa11 10y ago> 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 ;)
- masklinn 10y agoFWIW you can get code-formatting by prefixing lines with a 4-space indent: 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, 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"
- signa11 10y ago> FWIW you can get code-formatting by prefixing lines with a 4-space indent ah thank you ! that is very good to know. will it be possible to have some formatting guidelines / examples on the site somewhere ? similar to what we see on reddit ?
- masklinn 10y ago> will it be possible to have some formatting guidelines / examples on the site somewhere ? https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc Sadly HN has jack shit formatting-wise (the most annoying part being you can't escape asterisks so half the time it's going to emphasise a comment section and remove your asterisks despite that not being what you wanted)
- plhk 10y agoYou can do something similar with `libpnet` in rust: #[packet] struct Dns { id: u16be, qr: u1, opcode: u4, aa: u1, tc: u1, ... #[payload] data: Vec<u8>, }
- baq 10y agoi don't know what to say, so i'll just leave this here: <3