2018-01-30 03:00:23 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
2018-04-25 21:00:26 +03:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2018-01-30 03:00:23 +03:00
|
|
|
"io"
|
|
|
|
"net"
|
2018-02-03 09:24:43 +03:00
|
|
|
|
2018-04-25 21:00:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/tor"
|
2018-01-30 03:00:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// addressType specifies the network protocol and version that should be used
|
|
|
|
// when connecting to a node at a particular address.
|
|
|
|
type addressType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// tcp4Addr denotes an IPv4 TCP address.
|
|
|
|
tcp4Addr addressType = 0
|
|
|
|
|
|
|
|
// tcp6Addr denotes an IPv6 TCP address.
|
|
|
|
tcp6Addr addressType = 1
|
|
|
|
|
|
|
|
// v2OnionAddr denotes a version 2 Tor onion service address.
|
|
|
|
v2OnionAddr addressType = 2
|
|
|
|
|
2018-04-25 21:00:26 +03:00
|
|
|
// v3OnionAddr denotes a version 3 Tor (prop224) onion service address.
|
2018-01-30 03:00:23 +03:00
|
|
|
v3OnionAddr addressType = 3
|
|
|
|
)
|
|
|
|
|
2018-04-25 21:00:26 +03:00
|
|
|
// encodeTCPAddr serializes a TCP address into its compact raw bytes
|
|
|
|
// representation.
|
2018-02-03 09:24:43 +03:00
|
|
|
func encodeTCPAddr(w io.Writer, addr *net.TCPAddr) error {
|
2018-04-25 21:00:26 +03:00
|
|
|
var (
|
|
|
|
addrType byte
|
|
|
|
ip []byte
|
|
|
|
)
|
2018-02-03 09:24:43 +03:00
|
|
|
|
|
|
|
if addr.IP.To4() != nil {
|
2018-04-25 21:00:26 +03:00
|
|
|
addrType = byte(tcp4Addr)
|
|
|
|
ip = addr.IP.To4()
|
|
|
|
} else {
|
|
|
|
addrType = byte(tcp6Addr)
|
|
|
|
ip = addr.IP.To16()
|
|
|
|
}
|
2018-02-03 09:24:43 +03:00
|
|
|
|
2018-04-25 21:00:26 +03:00
|
|
|
if _, err := w.Write([]byte{addrType}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-03 09:24:43 +03:00
|
|
|
|
2018-04-25 21:00:26 +03:00
|
|
|
if _, err := w.Write(ip); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var port [2]byte
|
|
|
|
byteOrder.PutUint16(port[:], uint16(addr.Port))
|
|
|
|
if _, err := w.Write(port[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// encodeOnionAddr serializes an onion address into its compact raw bytes
|
|
|
|
// representation.
|
|
|
|
func encodeOnionAddr(w io.Writer, addr *tor.OnionAddr) error {
|
|
|
|
var suffixIndex int
|
|
|
|
switch len(addr.OnionService) {
|
|
|
|
case tor.V2Len:
|
|
|
|
if _, err := w.Write([]byte{byte(v2OnionAddr)}); err != nil {
|
2018-02-03 09:24:43 +03:00
|
|
|
return err
|
|
|
|
}
|
2018-04-25 21:00:26 +03:00
|
|
|
suffixIndex = tor.V2Len - tor.OnionSuffixLen
|
|
|
|
case tor.V3Len:
|
|
|
|
if _, err := w.Write([]byte{byte(v3OnionAddr)}); err != nil {
|
2018-02-03 09:24:43 +03:00
|
|
|
return err
|
|
|
|
}
|
2018-04-25 21:00:26 +03:00
|
|
|
suffixIndex = tor.V3Len - tor.OnionSuffixLen
|
|
|
|
default:
|
|
|
|
return errors.New("unknown onion service length")
|
|
|
|
}
|
|
|
|
|
|
|
|
host, err := tor.Base32Encoding.DecodeString(
|
|
|
|
addr.OnionService[:suffixIndex],
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := w.Write(host); err != nil {
|
|
|
|
return err
|
2018-02-03 09:24:43 +03:00
|
|
|
}
|
|
|
|
|
2018-04-25 21:00:26 +03:00
|
|
|
var port [2]byte
|
|
|
|
byteOrder.PutUint16(port[:], uint16(addr.Port))
|
|
|
|
if _, err := w.Write(port[:]); err != nil {
|
2018-02-03 09:24:43 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-30 03:00:23 +03:00
|
|
|
// deserializeAddr reads the serialized raw representation of an address and
|
2018-04-25 21:00:26 +03:00
|
|
|
// deserializes it into the actual address. This allows us to avoid address
|
|
|
|
// resolution within the channeldb package.
|
2018-01-30 03:00:23 +03:00
|
|
|
func deserializeAddr(r io.Reader) (net.Addr, error) {
|
2018-04-25 21:00:26 +03:00
|
|
|
var addrType [1]byte
|
|
|
|
if _, err := r.Read(addrType[:]); err != nil {
|
2018-01-30 03:00:23 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-25 21:00:26 +03:00
|
|
|
var address net.Addr
|
|
|
|
switch addressType(addrType[0]) {
|
2018-01-30 03:00:23 +03:00
|
|
|
case tcp4Addr:
|
|
|
|
var ip [4]byte
|
|
|
|
if _, err := r.Read(ip[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-25 21:00:26 +03:00
|
|
|
|
|
|
|
var port [2]byte
|
|
|
|
if _, err := r.Read(port[:]); err != nil {
|
2018-01-30 03:00:23 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-25 21:00:26 +03:00
|
|
|
|
|
|
|
address = &net.TCPAddr{
|
|
|
|
IP: net.IP(ip[:]),
|
|
|
|
Port: int(binary.BigEndian.Uint16(port[:])),
|
|
|
|
}
|
2018-01-30 03:00:23 +03:00
|
|
|
case tcp6Addr:
|
|
|
|
var ip [16]byte
|
|
|
|
if _, err := r.Read(ip[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-25 21:00:26 +03:00
|
|
|
|
|
|
|
var port [2]byte
|
|
|
|
if _, err := r.Read(port[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
address = &net.TCPAddr{
|
|
|
|
IP: net.IP(ip[:]),
|
|
|
|
Port: int(binary.BigEndian.Uint16(port[:])),
|
|
|
|
}
|
|
|
|
case v2OnionAddr:
|
|
|
|
var h [tor.V2DecodedLen]byte
|
|
|
|
if _, err := r.Read(h[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var p [2]byte
|
|
|
|
if _, err := r.Read(p[:]); err != nil {
|
2018-01-30 03:00:23 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-25 21:00:26 +03:00
|
|
|
|
|
|
|
onionService := tor.Base32Encoding.EncodeToString(h[:])
|
|
|
|
onionService += tor.OnionSuffix
|
|
|
|
port := int(binary.BigEndian.Uint16(p[:]))
|
|
|
|
|
|
|
|
address = &tor.OnionAddr{
|
|
|
|
OnionService: onionService,
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
case v3OnionAddr:
|
|
|
|
var h [tor.V3DecodedLen]byte
|
|
|
|
if _, err := r.Read(h[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var p [2]byte
|
|
|
|
if _, err := r.Read(p[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
onionService := tor.Base32Encoding.EncodeToString(h[:])
|
|
|
|
onionService += tor.OnionSuffix
|
|
|
|
port := int(binary.BigEndian.Uint16(p[:]))
|
|
|
|
|
|
|
|
address = &tor.OnionAddr{
|
|
|
|
OnionService: onionService,
|
|
|
|
Port: port,
|
|
|
|
}
|
2018-01-30 03:00:23 +03:00
|
|
|
default:
|
|
|
|
return nil, ErrUnknownAddressType
|
|
|
|
}
|
|
|
|
|
|
|
|
return address, nil
|
|
|
|
}
|
|
|
|
|
2018-04-25 21:00:26 +03:00
|
|
|
// serializeAddr serializes an address into its raw bytes representation so that
|
|
|
|
// it can be deserialized without requiring address resolution.
|
2018-01-30 03:00:23 +03:00
|
|
|
func serializeAddr(w io.Writer, address net.Addr) error {
|
2018-02-03 09:24:43 +03:00
|
|
|
switch addr := address.(type) {
|
|
|
|
case *net.TCPAddr:
|
|
|
|
return encodeTCPAddr(w, addr)
|
2018-04-25 21:00:26 +03:00
|
|
|
case *tor.OnionAddr:
|
|
|
|
return encodeOnionAddr(w, addr)
|
2018-06-28 03:26:17 +03:00
|
|
|
default:
|
|
|
|
return ErrUnknownAddressType
|
2018-01-30 03:00:23 +03:00
|
|
|
}
|
|
|
|
}
|