ipfs_impl.go 6.58 KB
Newer Older
1 2 3
package network

import (
4
	"context"
5
	"fmt"
Jeromy's avatar
Jeromy committed
6
	"io"
7
	"time"
Jeromy's avatar
Jeromy committed
8

9
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
Jeromy's avatar
Jeromy committed
10

11 12 13 14 15
	host "gx/ipfs/QmQQGtcp6nVUrQjNsnU53YWV1q8fK1Kd9S7FEkYbRZzxry/go-libp2p-host"
	routing "gx/ipfs/QmUV9hDAAyjeGbxbXkJ2sYqZ6dTd1DXJ2REhYEkRm178Tg/go-libp2p-routing"
	ma "gx/ipfs/QmUxSEGbv2nmYNnfXi7839wwQqTN3kwQeUxe8dTjZWZs7J/go-multiaddr"
	peer "gx/ipfs/QmVf8hTAsLLFtn4WPCRNdnaF2Eag2qTBS6uR8AiHPZARXy/go-libp2p-peer"
	inet "gx/ipfs/QmXdgNhVEgjLxjUoMs5ViQL7pboAt3Y7V7eGHRiE4qrmTE/go-libp2p-net"
Jeromy's avatar
Jeromy committed
16
	ggio "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/io"
17 18 19 20
	pstore "gx/ipfs/QmZhsmorLpD9kmQ4ynbAu4vbKv2goMUnXazwGA4gnWHDjB/go-libp2p-peerstore"
	cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
	ifconnmgr "gx/ipfs/Qmav3fJzdn43FDvHyGkPdbQ5JVqqiDPmNdnuGa3vatpmwj/go-libp2p-interface-connmgr"
	logging "gx/ipfs/Qmbi1CTJsbnBZjCEgc2otwu8cUFPsGpzWXG7edVCLZ7Gvk/go-log"
21 22
)

Jeromy's avatar
Jeromy committed
23
var log = logging.Logger("bitswap_network")
Jeromy's avatar
Jeromy committed
24

25 26
var sendMessageTimeout = time.Minute * 10

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27
// NewFromIpfsHost returns a BitSwapNetwork supported by underlying IPFS host
28
func NewFromIpfsHost(host host.Host, r routing.ContentRouting) BitSwapNetwork {
29
	bitswapNetwork := impl{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
		host:    host,
31
		routing: r,
32
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
	host.SetStreamHandler(ProtocolBitswap, bitswapNetwork.handleNewStream)
34 35
	host.SetStreamHandler(ProtocolBitswapOne, bitswapNetwork.handleNewStream)
	host.SetStreamHandler(ProtocolBitswapNoVers, bitswapNetwork.handleNewStream)
36 37 38
	host.Network().Notify((*netNotifiee)(&bitswapNetwork))
	// TODO: StopNotify.

39
	return &bitswapNetwork
40 41
}

42 43
// impl transforms the ipfs network interface, which sends and receives
// NetMessage objects, into the bitswap network interface.
44
type impl struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
	host    host.Host
46
	routing routing.ContentRouting
47 48 49

	// inbound messages from the network are forwarded to the receiver
	receiver Receiver
50 51
}

Jeromy's avatar
Jeromy committed
52 53 54 55 56
type streamMessageSender struct {
	s inet.Stream
}

func (s *streamMessageSender) Close() error {
Steven Allen's avatar
Steven Allen committed
57
	return inet.FullClose(s.s)
Jeromy's avatar
Jeromy committed
58 59
}

60 61 62 63
func (s *streamMessageSender) Reset() error {
	return s.s.Reset()
}

64 65
func (s *streamMessageSender) SendMsg(ctx context.Context, msg bsmsg.BitSwapMessage) error {
	return msgToStream(ctx, s.s, msg)
66 67
}

68 69 70 71 72 73 74 75 76 77
func msgToStream(ctx context.Context, s inet.Stream, msg bsmsg.BitSwapMessage) error {
	deadline := time.Now().Add(sendMessageTimeout)
	if dl, ok := ctx.Deadline(); ok {
		deadline = dl
	}

	if err := s.SetWriteDeadline(deadline); err != nil {
		log.Warningf("error setting deadline: %s", err)
	}

78 79 80 81 82 83 84 85 86 87 88 89 90 91
	switch s.Protocol() {
	case ProtocolBitswap:
		if err := msg.ToNetV1(s); err != nil {
			log.Debugf("error: %s", err)
			return err
		}
	case ProtocolBitswapOne, ProtocolBitswapNoVers:
		if err := msg.ToNetV0(s); err != nil {
			log.Debugf("error: %s", err)
			return err
		}
	default:
		return fmt.Errorf("unrecognized protocol on remote: %s", s.Protocol())
	}
92 93 94 95

	if err := s.SetWriteDeadline(time.Time{}); err != nil {
		log.Warningf("error resetting deadline: %s", err)
	}
96
	return nil
Jeromy's avatar
Jeromy committed
97 98 99 100 101 102 103 104 105 106 107
}

func (bsnet *impl) NewMessageSender(ctx context.Context, p peer.ID) (MessageSender, error) {
	s, err := bsnet.newStreamToPeer(ctx, p)
	if err != nil {
		return nil, err
	}

	return &streamMessageSender{s: s}, nil
}

108
func (bsnet *impl) newStreamToPeer(ctx context.Context, p peer.ID) (inet.Stream, error) {
109
	return bsnet.host.NewStream(ctx, p, ProtocolBitswap, ProtocolBitswapOne, ProtocolBitswapNoVers)
110 111 112 113 114 115 116 117
}

func (bsnet *impl) SendMessage(
	ctx context.Context,
	p peer.ID,
	outgoing bsmsg.BitSwapMessage) error {

	s, err := bsnet.newStreamToPeer(ctx, p)
118 119 120
	if err != nil {
		return err
	}
121

Steven Allen's avatar
Steven Allen committed
122
	if err = msgToStream(ctx, s, outgoing); err != nil {
123
		s.Reset()
Steven Allen's avatar
Steven Allen committed
124
		return err
125
	}
Steven Allen's avatar
Steven Allen committed
126 127 128
	// Yes, return this error. We have no reason to believe that the block
	// was actually *sent* unless we see the EOF.
	return inet.FullClose(s)
129 130
}

131 132
func (bsnet *impl) SetDelegate(r Receiver) {
	bsnet.receiver = r
133
}
134

135
func (bsnet *impl) ConnectTo(ctx context.Context, p peer.ID) error {
Jeromy's avatar
Jeromy committed
136
	return bsnet.host.Connect(ctx, pstore.PeerInfo{ID: p})
137 138
}

139
// FindProvidersAsync returns a channel of providers for the given key
140
func (bsnet *impl) FindProvidersAsync(ctx context.Context, k *cid.Cid, max int) <-chan peer.ID {
141 142 143 144 145 146 147 148 149

	// Since routing queries are expensive, give bitswap the peers to which we
	// have open connections. Note that this may cause issues if bitswap starts
	// precisely tracking which peers provide certain keys. This optimization
	// would be misleading. In the long run, this may not be the most
	// appropriate place for this optimization, but it won't cause any harm in
	// the short term.
	connectedPeers := bsnet.host.Network().Peers()
	out := make(chan peer.ID, len(connectedPeers)) // just enough buffer for these connectedPeers
150
	for _, id := range connectedPeers {
151 152 153
		if id == bsnet.host.ID() {
			continue // ignore self as provider
		}
154 155 156
		out <- id
	}

157 158
	go func() {
		defer close(out)
159
		providers := bsnet.routing.FindProvidersAsync(ctx, k, max)
160
		for info := range providers {
161 162
			if info.ID == bsnet.host.ID() {
				continue // ignore self as provider
163
			}
Jeromy's avatar
Jeromy committed
164
			bsnet.host.Peerstore().AddAddrs(info.ID, info.Addrs, pstore.TempAddrTTL)
165 166
			select {
			case <-ctx.Done():
167
				return
168 169 170 171 172
			case out <- info.ID:
			}
		}
	}()
	return out
173 174 175
}

// Provide provides the key to the network
176
func (bsnet *impl) Provide(ctx context.Context, k *cid.Cid) error {
177
	return bsnet.routing.Provide(ctx, k, true)
178 179
}

180 181
// handleNewStream receives a new stream from the network.
func (bsnet *impl) handleNewStream(s inet.Stream) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
182
	defer s.Close()
183 184

	if bsnet.receiver == nil {
185
		s.Reset()
186 187 188
		return
	}

Jeromy's avatar
Jeromy committed
189
	reader := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
190
	for {
Jeromy's avatar
Jeromy committed
191
		received, err := bsmsg.FromPBReader(reader)
192
		if err != nil {
Jeromy's avatar
Jeromy committed
193
			if err != io.EOF {
194
				s.Reset()
Jeromy's avatar
Jeromy committed
195 196 197
				go bsnet.receiver.ReceiveError(err)
				log.Debugf("bitswap net handleNewStream from %s error: %s", s.Conn().RemotePeer(), err)
			}
198 199
			return
		}
200

201 202 203 204 205
		p := s.Conn().RemotePeer()
		ctx := context.Background()
		log.Debugf("bitswap net handleNewStream from %s", s.Conn().RemotePeer())
		bsnet.receiver.ReceiveMessage(ctx, p, received)
	}
206
}
207

208 209 210 211
func (bsnet *impl) ConnectionManager() ifconnmgr.ConnManager {
	return bsnet.host.ConnManager()
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
type netNotifiee impl

func (nn *netNotifiee) impl() *impl {
	return (*impl)(nn)
}

func (nn *netNotifiee) Connected(n inet.Network, v inet.Conn) {
	nn.impl().receiver.PeerConnected(v.RemotePeer())
}

func (nn *netNotifiee) Disconnected(n inet.Network, v inet.Conn) {
	nn.impl().receiver.PeerDisconnected(v.RemotePeer())
}

func (nn *netNotifiee) OpenedStream(n inet.Network, v inet.Stream) {}
func (nn *netNotifiee) ClosedStream(n inet.Network, v inet.Stream) {}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
228 229
func (nn *netNotifiee) Listen(n inet.Network, a ma.Multiaddr)      {}
func (nn *netNotifiee) ListenClose(n inet.Network, a ma.Multiaddr) {}