2018-07-04 10:51:47 +00:00
|
|
|
package relay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
pb "github.com/libp2p/go-libp2p-circuit/pb"
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
ggio "github.com/gogo/protobuf/io"
|
|
|
|
proto "github.com/gogo/protobuf/proto"
|
2019-06-09 07:24:20 +00:00
|
|
|
pool "github.com/libp2p/go-buffer-pool"
|
2018-07-04 10:51:47 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
)
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func peerToPeerInfo(p *pb.CircuitRelay_Peer) (peer.AddrInfo, error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
if p == nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
return peer.AddrInfo{}, errors.New("nil peer")
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
id, err := peer.IDFromBytes(p.Id)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
return peer.AddrInfo{}, err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addrs := make([]ma.Multiaddr, 0, len(p.Addrs))
|
|
|
|
for _, addrBytes := range p.Addrs {
|
|
|
|
a, err := ma.NewMultiaddrBytes(addrBytes)
|
|
|
|
if err == nil {
|
|
|
|
addrs = append(addrs, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
return peer.AddrInfo{ID: id, Addrs: addrs}, nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func peerInfoToPeer(pi peer.AddrInfo) *pb.CircuitRelay_Peer {
|
2018-07-04 10:51:47 +00:00
|
|
|
addrs := make([][]byte, len(pi.Addrs))
|
|
|
|
for i, addr := range pi.Addrs {
|
|
|
|
addrs[i] = addr.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(pb.CircuitRelay_Peer)
|
|
|
|
p.Id = []byte(pi.ID)
|
|
|
|
p.Addrs = addrs
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func incrementTag(v int) int {
|
|
|
|
return v + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func decrementTag(v int) int {
|
|
|
|
if v > 0 {
|
|
|
|
return v - 1
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
type delimitedReader struct {
|
|
|
|
r io.Reader
|
|
|
|
buf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// The gogo protobuf NewDelimitedReader is buffered, which may eat up stream data.
|
|
|
|
// So we need to implement a compatible delimited reader that reads unbuffered.
|
|
|
|
// There is a slowdown from unbuffered reading: when reading the message
|
|
|
|
// it can take multiple single byte Reads to read the length and another Read
|
|
|
|
// to read the message payload.
|
|
|
|
// However, this is not critical performance degradation as
|
|
|
|
// - the reader is utilized to read one (dialer, stop) or two messages (hop) during
|
|
|
|
// the handshake, so it's a drop in the water for the connection lifetime.
|
|
|
|
// - messages are small (max 4k) and the length fits in a couple of bytes,
|
|
|
|
// so overall we have at most three reads per message.
|
|
|
|
func newDelimitedReader(r io.Reader, maxSize int) *delimitedReader {
|
2019-06-09 07:24:20 +00:00
|
|
|
return &delimitedReader{r: r, buf: pool.Get(maxSize)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *delimitedReader) Close() {
|
|
|
|
if d.buf != nil {
|
|
|
|
pool.Put(d.buf)
|
|
|
|
d.buf = nil
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *delimitedReader) ReadByte() (byte, error) {
|
|
|
|
buf := d.buf[:1]
|
|
|
|
_, err := d.r.Read(buf)
|
|
|
|
return buf[0], err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *delimitedReader) ReadMsg(msg proto.Message) error {
|
|
|
|
mlen, err := binary.ReadUvarint(d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint64(len(d.buf)) < mlen {
|
|
|
|
return errors.New("Message too large")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := d.buf[:mlen]
|
|
|
|
_, err = io.ReadFull(d.r, buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto.Unmarshal(buf, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDelimitedWriter(w io.Writer) ggio.WriteCloser {
|
|
|
|
return ggio.NewDelimitedWriter(w)
|
|
|
|
}
|