2018-06-25 19:00:55 +02:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-10-03 09:49:33 +02:00
|
|
|
"encoding/json"
|
2018-06-25 19:00:55 +02:00
|
|
|
"net"
|
2018-07-25 17:44:11 +02:00
|
|
|
"sort"
|
2018-07-13 19:31:54 +02:00
|
|
|
"strconv"
|
2018-06-25 19:00:55 +02:00
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
"github.com/lbryio/lbry.go/v3/dht/bits"
|
2018-06-25 19:00:55 +02:00
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
"github.com/cockroachdb/errors"
|
2018-06-25 19:00:55 +02:00
|
|
|
"github.com/lyoshenka/bencode"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: if routing table is ever empty (aka the node is isolated), it should re-bootstrap
|
|
|
|
|
2018-07-13 19:31:54 +02:00
|
|
|
// Contact contains information for contacting another node on the network
|
2018-06-25 19:00:55 +02:00
|
|
|
type Contact struct {
|
2018-07-13 19:31:54 +02:00
|
|
|
ID bits.Bitmap
|
|
|
|
IP net.IP
|
2018-07-17 23:19:03 +02:00
|
|
|
Port int // the udp port used for the dht
|
|
|
|
PeerPort int // the tcp port a peer can be contacted on for blob requests
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 19:31:54 +02:00
|
|
|
// Equals returns true if two contacts are the same.
|
2018-06-25 19:00:55 +02:00
|
|
|
func (c Contact) Equals(other Contact, checkID bool) bool {
|
|
|
|
return c.IP.Equal(other.IP) && c.Port == other.Port && (!checkID || c.ID == other.ID)
|
|
|
|
}
|
|
|
|
|
2018-07-13 19:31:54 +02:00
|
|
|
// Addr returns the address of the contact.
|
2018-06-25 19:00:55 +02:00
|
|
|
func (c Contact) Addr() *net.UDPAddr {
|
|
|
|
return &net.UDPAddr{IP: c.IP, Port: c.Port}
|
|
|
|
}
|
|
|
|
|
2018-07-13 19:31:54 +02:00
|
|
|
// String returns a short string representation of the contact
|
2018-06-25 19:00:55 +02:00
|
|
|
func (c Contact) String() string {
|
2018-07-13 19:31:54 +02:00
|
|
|
str := c.ID.HexShort() + "@" + c.Addr().String()
|
|
|
|
if c.PeerPort != 0 {
|
|
|
|
str += "(" + strconv.Itoa(c.PeerPort) + ")"
|
|
|
|
}
|
|
|
|
return str
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 09:49:33 +02:00
|
|
|
func (c Contact) MarshalJSON() ([]byte, error) {
|
2022-10-14 17:18:38 +02:00
|
|
|
b, err := json.Marshal(&struct {
|
2021-10-05 04:21:59 +02:00
|
|
|
ID string
|
|
|
|
IP string
|
|
|
|
Port int
|
2021-10-03 09:49:33 +02:00
|
|
|
PeerPort int
|
|
|
|
}{
|
2021-10-05 04:21:59 +02:00
|
|
|
ID: c.ID.Hex(),
|
|
|
|
IP: c.IP.String(),
|
|
|
|
Port: c.Port,
|
2021-10-03 09:49:33 +02:00
|
|
|
PeerPort: c.PeerPort,
|
|
|
|
})
|
2022-10-14 17:18:38 +02:00
|
|
|
return b, errors.Wrap(err, "")
|
2021-10-03 09:49:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 19:31:54 +02:00
|
|
|
// MarshalCompact returns a compact byteslice representation of the contact
|
|
|
|
// NOTE: The compact representation always uses the tcp PeerPort, not the udp Port. This is dumb, but that's how the python daemon does it
|
2018-06-25 19:00:55 +02:00
|
|
|
func (c Contact) MarshalCompact() ([]byte, error) {
|
|
|
|
if c.IP.To4() == nil {
|
2022-10-14 17:18:38 +02:00
|
|
|
return nil, errors.Wrap(errors.New("ip not set"), "")
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
2018-07-13 19:31:54 +02:00
|
|
|
if c.PeerPort < 0 || c.PeerPort > 65535 {
|
2022-10-14 17:18:38 +02:00
|
|
|
return nil, errors.Wrap(errors.New("invalid port"), "")
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.Write(c.IP.To4())
|
2018-07-13 19:31:54 +02:00
|
|
|
buf.WriteByte(byte(c.PeerPort >> 8))
|
|
|
|
buf.WriteByte(byte(c.PeerPort))
|
2018-06-25 19:00:55 +02:00
|
|
|
buf.Write(c.ID[:])
|
|
|
|
|
|
|
|
if buf.Len() != compactNodeInfoLength {
|
2022-10-14 17:18:38 +02:00
|
|
|
return nil, errors.Wrap(errors.New("i dont know how this happened"), "")
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2018-07-13 19:31:54 +02:00
|
|
|
// UnmarshalCompact unmarshals the compact byteslice representation of a contact.
|
|
|
|
// NOTE: The compact representation always uses the tcp PeerPort, not the udp Port. This is dumb, but that's how the python daemon does it
|
2018-06-25 19:00:55 +02:00
|
|
|
func (c *Contact) UnmarshalCompact(b []byte) error {
|
|
|
|
if len(b) != compactNodeInfoLength {
|
2022-10-14 17:18:38 +02:00
|
|
|
return errors.Wrap(errors.New("invalid compact length"), "")
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
c.IP = net.IPv4(b[0], b[1], b[2], b[3]).To4()
|
2018-07-13 19:31:54 +02:00
|
|
|
c.PeerPort = int(uint16(b[5]) | uint16(b[4])<<8)
|
2018-06-25 19:00:55 +02:00
|
|
|
c.ID = bits.FromBytesP(b[6:])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalBencode returns the serialized byte slice representation of a contact.
|
|
|
|
func (c Contact) MarshalBencode() ([]byte, error) {
|
2022-10-14 17:18:38 +02:00
|
|
|
b, err := bencode.EncodeBytes([]interface{}{c.ID, c.IP.String(), c.Port})
|
|
|
|
return b, errors.Wrap(err, "")
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBencode unmarshals the serialized byte slice into the appropriate fields of the contact.
|
|
|
|
func (c *Contact) UnmarshalBencode(b []byte) error {
|
|
|
|
var raw []bencode.RawMessage
|
|
|
|
err := bencode.DecodeBytes(b, &raw)
|
|
|
|
if err != nil {
|
2022-10-14 17:18:38 +02:00
|
|
|
return errors.Wrap(err, "")
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(raw) != 3 {
|
2022-10-14 17:18:38 +02:00
|
|
|
return errors.Wrap(errors.Newf("contact must have 3 elements; got %d", len(raw)), "")
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = bencode.DecodeBytes(raw[0], &c.ID)
|
|
|
|
if err != nil {
|
2022-10-14 17:18:38 +02:00
|
|
|
return errors.Wrap(err, "")
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var ipStr string
|
|
|
|
err = bencode.DecodeBytes(raw[1], &ipStr)
|
|
|
|
if err != nil {
|
2022-10-14 17:18:38 +02:00
|
|
|
return errors.Wrap(err, "")
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
c.IP = net.ParseIP(ipStr).To4()
|
|
|
|
if c.IP == nil {
|
2022-10-14 17:18:38 +02:00
|
|
|
return errors.Wrap(errors.New("invalid IP"), "")
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 17:38:55 +02:00
|
|
|
return bencode.DecodeBytes(raw[2], &c.Port)
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
2018-07-25 17:44:11 +02:00
|
|
|
func sortByDistance(contacts []Contact, target bits.Bitmap) {
|
|
|
|
sort.Slice(contacts, func(i, j int) bool {
|
|
|
|
return contacts[i].ID.Xor(target).Cmp(contacts[j].ID.Xor(target)) < 0
|
|
|
|
})
|
2018-06-25 19:00:55 +02:00
|
|
|
}
|