nim-libp2p icon indicating copy to clipboard operation
nim-libp2p copied to clipboard

dnsaddr doesn't support responses >512 bytes

Open Menduist opened this issue 1 year ago • 0 comments

dnsclient doesn't send the required fields to allow responses bigger than 512 bytes This is an issue for instance for dnsaddr resolving

Quick and dirty fix: libp2p:

diff --git a/libp2p/nameresolving/dnsresolver.nim b/libp2p/nameresolving/dnsresolver.nim
index 2bda23c7..92def33e 100644
--- a/libp2p/nameresolving/dnsresolver.nim
+++ b/libp2p/nameresolving/dnsresolver.nim
@@ -29,9 +29,13 @@ proc questionToBuf(address: string, kind: QKind): seq[byte] =
     var
       header = initHeader()
       question = initQuestion(address, kind)
+      opts = initQuestion("", QKind.OPT)
 
+    header.arcount = 1
+    var
       requestStream = header.toStream()
     question.toStream(requestStream)
+    opts.toStream(requestStream)
 
     let dataLen = requestStream.getPosition()
     requestStream.setPosition(0)

dnsclient:

diff src/dnsclientpkg/protocol.nim dnsclientpkg/protocol.nim
71,76c71,77
<   for label in q.name.split('.'):
<     labelLen = label.len.uint8
<     if labelLen < 1.uint8:
<       raise newException(ValueError, q.name & "is not a legal name (empty label)")
<     if labelLen >= 64.uint8:
<       raise newException(ValueError, q.name & "is not a legal name (label too long)")
---
>   if q.name.len > 0:
>     for label in q.name.split('.'):
>       labelLen = label.len.uint8
>       if labelLen < 1.uint8:
>         raise newException(ValueError, q.name & "is not a legal name (empty label)")
>       if labelLen >= 64.uint8:
>         raise newException(ValueError, q.name & "is not a legal name (label too long)")
78,79c79,80
<     data.write(labelLen)
<     data.write(label)
---
>       data.write(labelLen)
>       data.write(label)
83c84,90
<   data.writeShort(q.class.uint16)
---
> 
>   if q.kind == QKind.OPT:
>     data.writeShort(1024)
>     data.writeShort(0)
>     data.writeShort(0)
>   else:
>     data.writeShort(q.class.uint16)
164c171
<   assert data.atEnd()
---
>   #assert data.atEnd()
diff src/dnsclientpkg/types.nim dnsclientpkg/types.nim
49a50
>       OPT = 41

We would need to do a proper fix on the dnsclient library And do better here too (only use it in TXT queries, etc..)

The RFC of the DNS extension

cc @jm-clius, if you stumble onto the same issue (txt not resolving over 512 bytes)

Menduist avatar Aug 01 '22 15:08 Menduist