Getting my head slowly around the event driven nature of NodeMCU, the code turned into a very compact, generic UDP proxy or forwarder.
The script is completely unaware of the structure of the data and could be used to proxy all sorts of UDP data.
Here is my code:
-- Simple DNS Proxy
-- 20150923 by Andy Reischle
-- Blog: www.AReResearch.net
-- Vids: www.youtube.com/AReResearch
--
-- Uses googles dns server 8.8.8.8
-- change to whatever suits you
cu=net.createConnection(net.UDP,0)
cu:on("receive",function(cu,c)
-- print("Got a reply")
s:send(c)
end)
s=net.createServer(net.UDP)
s:on("receive",function(s,d)
-- print ("Got a request!")
cu:connect(53,"8.8.8.8")
cu:send(d)
end)
s:listen(53)
The ESP-module is connected to a WiFi AP, of course. The IP address of the ESP module is 192.168.1.74 and will be different, depending on your home DHCP server.
I can query DNS information through the module now:
me@raspberrypi:~$ nslookup
> server 192.168.1.74
Default server: 192.168.1.74
Address: 192.168.1.74#53
> www.areresearch.net
Server: 192.168.1.74
Address: 192.168.1.74#53
Non-authoritative answer:
www.areresearch.net canonical name = ghs.google.com.
ghs.google.com canonical name = ghs.l.google.com.
Name: ghs.l.google.com
Address: 173.194.65.121
>
The corresponding tcpdump also looks very clean:
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
23:39:08.416736 IP noname.43027 > noname.domain: 56537+ A? www.areresearch.net. (37)
23:39:08.422009 IP noname.46122 > fritz.box.domain: 4560+ PTR? 58.1.168.192.in-addr.arpa. (43)
23:39:08.426857 IP fritz.box.domain > noname.46122: 4560* 1/0/0 PTR noname. (88)
23:39:08.428198 IP noname.45680 > fritz.box.domain: 56651+ PTR? 74.1.168.192.in-addr.arpa. (43)
23:39:08.430620 IP fritz.box.domain > noname.45680: 56651* 1/0/0 PTR noname. (88)
23:39:08.432969 IP noname.38314 > fritz.box.domain: 15794+ PTR? 1.1.168.192.in-addr.arpa. (42)
23:39:08.435987 IP fritz.box.domain > noname.38314: 15794* 1/0/0 PTR fritz.box. (89)
23:39:08.480105 IP noname.domain > noname.43027: 56537 3/0/0 CNAME ghs.google.com., CNAME ghs.l.google.com., A 173.194.65.121 (101)
While the rest of the project still requires a lot of tinkering, this little piece of the puzzle works nicely.