Skip to content

Instantly share code, notes, and snippets.

@azimut
Created March 4, 2020 02:06
Show Gist options
  • Save azimut/a2b69ab0b4a02e5d9d57727f83810270 to your computer and use it in GitHub Desktop.
Save azimut/a2b69ab0b4a02e5d9d57727f83810270 to your computer and use it in GitHub Desktop.
cannot resolve
(emacs@localhost)155> snitch_resolver:do_query("starbucks.com",txt).
snitch_resolver:do_query("starbucks.com",txt).
{dns_rec,{dns_header,49263,true,query,false,true,true,true,
false,0},
[{dns_query,"starbucks.com",txt,in}],
[],[],[]}
-module(snitch_resolver).
-include_lib("kernel/src/inet_dns.hrl").
-define(DNS_SERVERS, [{8,8,8,8},{1,1,1,1},{9,9,9,9}]).
-export([do_query/2]).
build_query(Domain, Type) ->
inet_dns:encode(
#dns_rec{
header = #dns_header{
id = rand:uniform(65535),
opcode = 'query',
rd = 1,
aa = 1
},
qdlist = [#dns_query{
domain = Domain,
type = Type,
class = in
}]
}).
get_dns_server() ->
lists:nth(rand:uniform(erlang:length(?DNS_SERVERS)),?DNS_SERVERS).
do_query(Domain, Type) ->
Query = build_query(Domain, Type),
NS = get_dns_server(),
{ok, Socket} = gen_udp:open(0, [binary, {active, false}, {recbuf, 4 * 65535}]),
gen_udp:send(Socket, NS, 53, Query),
{ok, {NS, 53, Reply}} = gen_udp:recv(Socket, 5 * 65535, 5 * 1000),
{ok, R} = inet_dns:decode(Reply),
gen_udp:close(Socket),
R.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment