graph/db+chanbackup: dns add encoding/decoding for persistence

This commit is contained in:
Elle Mouton authored and Mohamed Awnallah committed 2025-09-03 01:11:35 +00:00
1 parent 4bd2bbca27
commit 6ef80db746
7 files changed
+107 -11

No files matched your search

+54
View File
@@ -31,8 +31,36 @@ const (
// opaqueAddrs denotes an address (or a set of addresses) that LND was
// not able to parse since LND is not yet aware of the address type.
opaqueAddrs addressType = 4
// dnsAddr denotes a DNS address type.
dnsAddr addressType = 5
)
// encodeDNSAddr encodes a DNS address.
func encodeDNSAddr(w io.Writer, addr *lnwire.DNSAddress) error {
if _, err := w.Write([]byte{byte(dnsAddr)}); err != nil {
return err
}
// Write the length of the hostname.
hostLen := len(addr.Hostname)
if _, err := w.Write([]byte{byte(hostLen)}); err != nil {
return err
}
if _, err := w.Write([]byte(addr.Hostname)); err != nil {
return err
}
var port [2]byte
byteOrder.PutUint16(port[:], addr.Port)
if _, err := w.Write(port[:]); err != nil {
return err
}
return nil
}
// encodeTCPAddr serializes a TCP address into its compact raw bytes
// representation.
func encodeTCPAddr(w io.Writer, addr *net.TCPAddr) error {
@@ -230,6 +258,30 @@ func DeserializeAddr(r io.Reader) (net.Addr, error) {
Port: port,
}
case dnsAddr:
// Read the length of the hostname.
var hostLen [1]byte
if _, err := r.Read(hostLen[:]); err != nil {
return nil, err
}
// Read the hostname.
hostname := make([]byte, hostLen[0])
if _, err := r.Read(hostname); err != nil {
return nil, err
}
// Read the port.
var port [2]byte
if _, err := r.Read(port[:]); err != nil {
return nil, err
}
address = &lnwire.DNSAddress{
Hostname: string(hostname),
Port: binary.BigEndian.Uint16(port[:]),
}
case opaqueAddrs:
// Read the length of the payload.
var l [2]byte
@@ -264,6 +316,8 @@ func SerializeAddr(w io.Writer, address net.Addr) error {
return encodeOnionAddr(w, addr)
case *lnwire.OpaqueAddrs:
return encodeOpaqueAddrs(w, addr)
case *lnwire.DNSAddress:
return encodeDNSAddr(w, addr)
default:
return ErrUnknownAddressType
}