lnwire: add encoding/decoding for DNS addr

Co-authored-by: Elle Mouton <elle.mouton@gmail.com>
This commit is contained in:
Elle Mouton
2025-08-14 13:11:06 +00:00
committed by Mohamed Awnallah
parent 4d9f884c3f
commit 41bd519859
4 changed files with 117 additions and 9 deletions

View File

@@ -44,6 +44,10 @@ const (
// v3OnionAddrLen is the length of a version 3 Tor onion service address
// (35 bytes decoded onion + 2 bytes port).
v3OnionAddrLen = 37
// dnsAddrOverhead is the fixed overhead for a DNS address: 1 byte for
// the hostname length and 2 bytes for the port.
dnsAddrOverhead = 3
)
// PkScript is simple type definition which represents a raw serialized public
@@ -70,6 +74,9 @@ const (
// v3OnionAddr denotes a version 3 Tor (prop224) onion service address.
v3OnionAddr addressType = 4
// dnsAddr denotes a DNS address.
dnsAddr addressType = 5
)
// WriteElement is a one-stop shop to write the big endian representation of
@@ -347,6 +354,11 @@ func WriteElement(w *bytes.Buffer, element interface{}) error {
return err
}
case *DNSAddress:
if err := WriteDNSAddress(w, e); err != nil {
return err
}
case *OpaqueAddrs:
if err := WriteOpaqueAddrs(w, e); err != nil {
return err
@@ -820,6 +832,34 @@ func ReadElement(r io.Reader, element interface{}) error {
}
addrBytesRead += v3OnionAddrLen
case dnsAddr:
var hostnameLen [1]byte
_, err := io.ReadFull(addrBuf, hostnameLen[:])
if err != nil {
return err
}
hostname := make([]byte, hostnameLen[0])
_, err = io.ReadFull(addrBuf, hostname)
if err != nil {
return err
}
var port [2]byte
_, err = io.ReadFull(addrBuf, port[:])
if err != nil {
return err
}
address = &DNSAddress{
Hostname: string(hostname),
Port: binary.BigEndian.Uint16(
port[:],
),
}
addrBytesRead += dnsAddrOverhead +
uint16(len(hostname))
default:
// If we don't understand this address type,
// we just store it along with the remaining