Skip to content

Instantly share code, notes, and snippets.

@vavrusa
Last active December 18, 2016 11:48
Show Gist options
  • Save vavrusa/acf6841b0e1bcf8daf85 to your computer and use it in GitHub Desktop.
Save vavrusa/acf6841b0e1bcf8daf85 to your computer and use it in GitHub Desktop.
BIND11
var net = require('net');
var dgram = require('dgram');
function ntohs(arr) {
return ((arr[0] & 0xFF) << 8) | (arr[1] & 0xFF);
}
function parse_name(data, pos)
{
for (base = pos; pos < data.length;) {
if (data[pos] == 0) {
return data.slice(base, pos + 1)
}
if (data[pos] > 64) {
throw "hackz label"; // SAFETY FIRST
}
pos += data[pos] + 1;
}
}
function parse_query(data, pos)
{
qst = {}
qst.qname = parse_name(data, pos)
pos += qst.qname.length
qst.qtype = ntohs(data.slice(pos, pos + 2))
pos += 2
qst.qclass = ntohs(data.slice(pos, pos + 2))
return qst;
}
function resolve(data, len)
{
data[2] = 0x81 + 0x04;
data[3] = 0x05;
try {
// parse header + query
qst = parse_query(data, 12)
// fuck ANY
if (qst.qtype == 0xFF) {
data[3] = 0x04;
}
// TODO: parse /etc/hosts
} catch (err) {
console.trace(err)
data[3] = 0x02; // SERVFAIL
}
return len;
}
var tcp = net.createServer(function (socket) {
socket.on('data', function (data) {
var len = ntohs(data.slice(0, 2));
resolve(data.slice(2), len);
socket.write(data);
});
});
var udp = dgram.createSocket('udp6');
udp.on('message', function (message, remote) {
var len = resolve(message, message.length);
udp.send(message, 0, len, remote.port, remote.address);
});
tcp.listen(5355, '::');
udp.bind(5355, '::');
@jpmens
Copy link

jpmens commented Mar 5, 2015

I love this! ;-)

@bortzmeyer
Copy link

What about an UDP version, now? :-)

@bortzmeyer
Copy link

I like the variety of possible rcodes...

@vavrusa
Copy link
Author

vavrusa commented Jul 30, 2015

I aim to please. UDP version now + "Happy Eyeballs". Performance on par with BIND9 on my localhost.

@bortzmeyer
Copy link

Wow, most paying software do not implement so quickly what I ask for!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment