handlers.go 12.1 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package dht

import (
4
	"bytes"
Jeromy's avatar
Jeromy committed
5
	"context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7
	"errors"
	"fmt"
8
	"time"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9

10 11 12 13 14
	"github.com/libp2p/go-libp2p-core/network"
	"github.com/libp2p/go-libp2p-core/peer"
	"github.com/libp2p/go-libp2p-core/peerstore"
	pstore "github.com/libp2p/go-libp2p-peerstore"

15 16
	"github.com/gogo/protobuf/proto"
	"github.com/ipfs/go-cid"
George Antoniadis's avatar
George Antoniadis committed
17
	ds "github.com/ipfs/go-datastore"
18
	u "github.com/ipfs/go-ipfs-util"
19
	pb "github.com/libp2p/go-libp2p-kad-dht/pb"
George Antoniadis's avatar
George Antoniadis committed
20
	recpb "github.com/libp2p/go-libp2p-record/pb"
Steven Allen's avatar
Steven Allen committed
21
	"github.com/multiformats/go-base32"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22 23 24
)

// dhthandler specifies the signature of functions that handle DHT messages.
25
type dhtHandler func(context.Context, peer.ID, *pb.Message) (*pb.Message, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26

27
func (dht *IpfsDHT) handlerForMsgType(t pb.Message_MessageType) dhtHandler {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28
	switch t {
29
	case pb.Message_GET_VALUE:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
		return dht.handleGetValue
31
	case pb.Message_PUT_VALUE:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32
		return dht.handlePutValue
33
	case pb.Message_FIND_NODE:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34
		return dht.handleFindPeer
35
	case pb.Message_ADD_PROVIDER:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
		return dht.handleAddProvider
37
	case pb.Message_GET_PROVIDERS:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
		return dht.handleGetProviders
39
	case pb.Message_PING:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41 42 43 44 45
		return dht.handlePing
	default:
		return nil
	}
}

ForrestWeston's avatar
ForrestWeston committed
46
func (dht *IpfsDHT) handleGetValue(ctx context.Context, p peer.ID, pmes *pb.Message) (_ *pb.Message, err error) {
Matt Joiner's avatar
Matt Joiner committed
47 48 49 50
	ctx = logger.Start(ctx, "handleGetValue")
	logger.SetTag(ctx, "peer", p)
	defer func() { logger.FinishWithErr(ctx, err) }()
	logger.Debugf("%s handleGetValue for key: %s", dht.self, pmes.GetKey())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51 52

	// setup response
53
	resp := pb.NewMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54

55
	// first, is there even a key?
56
	k := pmes.GetKey()
57
	if len(k) == 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
		return nil, errors.New("handleGetValue but no key was provided")
59
		// TODO: send back an error response? could be bad, but the other node's hanging.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60 61
	}

62 63
	rec, err := dht.checkLocalDatastore(k)
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64 65
		return nil, err
	}
66
	resp.Record = rec
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67 68

	// Find closest peer on given cluster to desired key and reply with that info
69
	closer := dht.betterPeersToQuery(pmes, p, dht.bucketSize)
Jeromy's avatar
Jeromy committed
70
	if len(closer) > 0 {
71
		// TODO: pstore.PeerInfos should move to core (=> peerstore.AddrInfos).
Jeromy's avatar
Jeromy committed
72
		closerinfos := pstore.PeerInfos(dht.peerstore, closer)
73
		for _, pi := range closerinfos {
Matt Joiner's avatar
Matt Joiner committed
74
			logger.Debugf("handleGetValue returning closer peer: '%s'", pi.ID)
75
			if len(pi.Addrs) < 1 {
Matt Joiner's avatar
Matt Joiner committed
76
				logger.Warningf(`no addresses on peer being sent!
77 78 79
					[local:%s]
					[sending:%s]
					[remote:%s]`, dht.self, pi.ID, p)
Jeromy's avatar
Jeromy committed
80
			}
81
		}
82

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83
		resp.CloserPeers = pb.PeerInfosToPBPeers(dht.host.Network(), closerinfos)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84 85 86 87 88
	}

	return resp, nil
}

89
func (dht *IpfsDHT) checkLocalDatastore(k []byte) (*recpb.Record, error) {
Matt Joiner's avatar
Matt Joiner committed
90
	logger.Debugf("%s handleGetValue looking into ds", dht.self)
91
	dskey := convertToDsKey(k)
92
	buf, err := dht.datastore.Get(dskey)
Matt Joiner's avatar
Matt Joiner committed
93
	logger.Debugf("%s handleGetValue looking into ds GOT %v", dht.self, buf)
94 95 96 97 98 99 100 101 102 103 104

	if err == ds.ErrNotFound {
		return nil, nil
	}

	// if we got an unexpected error, bail.
	if err != nil {
		return nil, err
	}

	// if we have the value, send it back
Matt Joiner's avatar
Matt Joiner committed
105
	logger.Debugf("%s handleGetValue success!", dht.self)
106

George Antoniadis's avatar
George Antoniadis committed
107
	rec := new(recpb.Record)
108
	err = proto.Unmarshal(buf, rec)
109
	if err != nil {
Matt Joiner's avatar
Matt Joiner committed
110
		logger.Debug("failed to unmarshal DHT record from datastore")
111 112 113 114 115 116
		return nil, err
	}

	var recordIsBad bool
	recvtime, err := u.ParseRFC3339(rec.GetTimeReceived())
	if err != nil {
Matt Joiner's avatar
Matt Joiner committed
117
		logger.Info("either no receive time set on record, or it was invalid: ", err)
118 119 120
		recordIsBad = true
	}

Aarsh Shah's avatar
Aarsh Shah committed
121
	if time.Since(recvtime) > dht.maxRecordAge {
Matt Joiner's avatar
Matt Joiner committed
122
		logger.Debug("old record found, tossing.")
123 124 125
		recordIsBad = true
	}

126
	// NOTE: We do not verify the record here beyond checking these timestamps.
127 128 129 130 131 132
	// we put the burden of checking the records on the requester as checking a record
	// may be computationally expensive

	if recordIsBad {
		err := dht.datastore.Delete(dskey)
		if err != nil {
Matt Joiner's avatar
Matt Joiner committed
133
			logger.Error("Failed to delete bad record from datastore: ", err)
134 135 136 137 138 139 140 141
		}

		return nil, nil // can treat this as not having the record at all
	}

	return rec, nil
}

142 143
// Cleans the record (to avoid storing arbitrary data).
func cleanRecord(rec *recpb.Record) {
144
	rec.TimeReceived = ""
145 146
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
147
// Store a value in this peer local storage
ForrestWeston's avatar
ForrestWeston committed
148
func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Message) (_ *pb.Message, err error) {
Matt Joiner's avatar
Matt Joiner committed
149 150 151
	ctx = logger.Start(ctx, "handlePutValue")
	logger.SetTag(ctx, "peer", p)
	defer func() { logger.FinishWithErr(ctx, err) }()
ForrestWeston's avatar
ForrestWeston committed
152

153 154
	rec := pmes.GetRecord()
	if rec == nil {
Matt Joiner's avatar
Matt Joiner committed
155
		logger.Infof("Got nil record from: %s", p.Pretty())
156 157
		return nil, errors.New("nil record")
	}
158

159
	if !bytes.Equal(pmes.GetKey(), rec.GetKey()) {
160 161 162
		return nil, errors.New("put key doesn't match record key")
	}

163
	cleanRecord(rec)
164

165
	// Make sure the record is valid (not expired, valid signature etc)
166
	if err = dht.Validator.Validate(string(rec.GetKey()), rec.GetValue()); err != nil {
Matt Joiner's avatar
Matt Joiner committed
167
		logger.Warningf("Bad dht record in PUT from: %s. %s", p.Pretty(), err)
168 169 170
		return nil, err
	}

171 172
	dskey := convertToDsKey(rec.GetKey())

173 174 175 176 177 178 179 180 181 182 183
	// fetch the striped lock for this key
	var indexForLock byte
	if len(rec.GetKey()) == 0 {
		indexForLock = 0
	} else {
		indexForLock = rec.GetKey()[len(rec.GetKey())-1]
	}
	lk := &dht.stripedPutLocks[indexForLock]
	lk.Lock()
	defer lk.Unlock()

184 185 186 187 188 189 190 191 192 193
	// Make sure the new record is "better" than the record we have locally.
	// This prevents a record with for example a lower sequence number from
	// overwriting a record with a higher sequence number.
	existing, err := dht.getRecordFromDatastore(dskey)
	if err != nil {
		return nil, err
	}

	if existing != nil {
		recs := [][]byte{rec.GetValue(), existing.GetValue()}
194
		i, err := dht.Validator.Select(string(rec.GetKey()), recs)
195
		if err != nil {
Matt Joiner's avatar
Matt Joiner committed
196
			logger.Warningf("Bad dht record in PUT from %s: %s", p.Pretty(), err)
197 198 199
			return nil, err
		}
		if i != 0 {
Matt Joiner's avatar
Matt Joiner committed
200
			logger.Infof("DHT record in PUT from %s is older than existing record. Ignoring", p.Pretty())
201 202 203 204
			return nil, errors.New("old record")
		}
	}

205
	// record the time we receive every record
206
	rec.TimeReceived = u.FormatRFC3339(time.Now())
207 208

	data, err := proto.Marshal(rec)
209 210 211 212 213
	if err != nil {
		return nil, err
	}

	err = dht.datastore.Put(dskey, data)
Matt Joiner's avatar
Matt Joiner committed
214
	logger.Debugf("%s handlePutValue %v", dht.self, dskey)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
215
	return pmes, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
216 217
}

Steven Allen's avatar
Steven Allen committed
218 219
// returns nil, nil when either nothing is found or the value found doesn't properly validate.
// returns nil, some_error when there's a *datastore* error (i.e., something goes very wrong)
220
func (dht *IpfsDHT) getRecordFromDatastore(dskey ds.Key) (*recpb.Record, error) {
221
	buf, err := dht.datastore.Get(dskey)
222 223 224 225
	if err == ds.ErrNotFound {
		return nil, nil
	}
	if err != nil {
Matt Joiner's avatar
Matt Joiner committed
226
		logger.Errorf("Got error retrieving record with key %s from datastore: %s", dskey, err)
227 228 229
		return nil, err
	}
	rec := new(recpb.Record)
230
	err = proto.Unmarshal(buf, rec)
231 232
	if err != nil {
		// Bad data in datastore, log it but don't return an error, we'll just overwrite it
Matt Joiner's avatar
Matt Joiner committed
233
		logger.Errorf("Bad record data stored in datastore with key %s: could not unmarshal record", dskey)
234 235 236
		return nil, nil
	}

237
	err = dht.Validator.Validate(string(rec.GetKey()), rec.GetValue())
238 239 240
	if err != nil {
		// Invalid record in datastore, probably expired but don't return an error,
		// we'll just overwrite it
Matt Joiner's avatar
Matt Joiner committed
241
		logger.Debugf("Local record verify failed: %s (discarded)", err)
242 243 244 245 246 247
		return nil, nil
	}

	return rec, nil
}

248
func (dht *IpfsDHT) handlePing(_ context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
Matt Joiner's avatar
Matt Joiner committed
249
	logger.Debugf("%s Responding to ping from %s!\n", dht.self, p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
250
	return pmes, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
251 252
}

253
func (dht *IpfsDHT) handleFindPeer(ctx context.Context, p peer.ID, pmes *pb.Message) (_ *pb.Message, _err error) {
Matt Joiner's avatar
Matt Joiner committed
254 255 256
	ctx = logger.Start(ctx, "handleFindPeer")
	defer func() { logger.FinishWithErr(ctx, _err) }()
	logger.SetTag(ctx, "peer", p)
257
	resp := pb.NewMessage(pmes.GetType(), nil, pmes.GetClusterLevel())
258
	var closest []peer.ID
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
259 260

	// if looking for self... special case where we send it on CloserPeers.
261 262
	targetPid := peer.ID(pmes.GetKey())
	if targetPid == dht.self {
263
		closest = []peer.ID{dht.self}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
264
	} else {
265
		closest = dht.betterPeersToQuery(pmes, p, dht.bucketSize)
266 267 268 269 270 271 272 273 274 275 276 277 278

		// Never tell a peer about itself.
		if targetPid != p {
			// If we're connected to the target peer, report their
			// peer info. This makes FindPeer work even if the
			// target peer isn't in our routing table.
			//
			// Alternatively, we could just check our peerstore.
			// However, we don't want to return out of date
			// information. We can change this in the future when we
			// add a progressive, asynchronous `SearchPeer` function
			// and improve peer routing in the host.
			switch dht.host.Network().Connectedness(targetPid) {
279
			case network.Connected, network.CanConnect:
280 281 282
				closest = append(closest, targetPid)
			}
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
283 284 285
	}

	if closest == nil {
Matt Joiner's avatar
Matt Joiner committed
286
		logger.Infof("%s handleFindPeer %s: could not find anything.", dht.self, p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
287 288 289
		return resp, nil
	}

290
	// TODO: pstore.PeerInfos should move to core (=> peerstore.AddrInfos).
Jeromy's avatar
Jeromy committed
291
	closestinfos := pstore.PeerInfos(dht.peerstore, closest)
Steven Allen's avatar
Steven Allen committed
292
	// possibly an over-allocation but this array is temporary anyways.
293
	withAddresses := make([]peer.AddrInfo, 0, len(closestinfos))
294 295 296
	for _, pi := range closestinfos {
		if len(pi.Addrs) > 0 {
			withAddresses = append(withAddresses, pi)
297
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
298 299
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
300
	resp.CloserPeers = pb.PeerInfosToPBPeers(dht.host.Network(), withAddresses)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
301 302 303
	return resp, nil
}

304
func (dht *IpfsDHT) handleGetProviders(ctx context.Context, p peer.ID, pmes *pb.Message) (_ *pb.Message, _err error) {
Matt Joiner's avatar
Matt Joiner committed
305 306 307
	ctx = logger.Start(ctx, "handleGetProviders")
	defer func() { logger.FinishWithErr(ctx, _err) }()
	logger.SetTag(ctx, "peer", p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
308

309
	resp := pb.NewMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
310 311 312 313
	c, err := cid.Cast([]byte(pmes.GetKey()))
	if err != nil {
		return nil, err
	}
Matt Joiner's avatar
Matt Joiner committed
314
	logger.SetTag(ctx, "key", c)
315 316

	// debug logging niceness.
317
	reqDesc := fmt.Sprintf("%s handleGetProviders(%s, %s): ", dht.self, p, c)
Matt Joiner's avatar
Matt Joiner committed
318 319
	logger.Debugf("%s begin", reqDesc)
	defer logger.Debugf("%s end", reqDesc)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
320 321

	// check if we have this value, to add ourselves as provider.
322
	has, err := dht.datastore.Has(convertToDsKey(c.Bytes()))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
323
	if err != nil && err != ds.ErrNotFound {
Matt Joiner's avatar
Matt Joiner committed
324
		logger.Debugf("unexpected datastore error: %v\n", err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
325 326 327 328
		has = false
	}

	// setup providers
329
	providers := dht.providers.GetProviders(ctx, c)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
330 331
	if has {
		providers = append(providers, dht.self)
Matt Joiner's avatar
Matt Joiner committed
332
		logger.Debugf("%s have the value. added self as provider", reqDesc)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
333 334
	}

335
	if len(providers) > 0 {
336
		// TODO: pstore.PeerInfos should move to core (=> peerstore.AddrInfos).
Jeromy's avatar
Jeromy committed
337
		infos := pstore.PeerInfos(dht.peerstore, providers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
338
		resp.ProviderPeers = pb.PeerInfosToPBPeers(dht.host.Network(), infos)
Matt Joiner's avatar
Matt Joiner committed
339
		logger.Debugf("%s have %d providers: %s", reqDesc, len(providers), infos)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
340 341 342
	}

	// Also send closer peers.
343
	closer := dht.betterPeersToQuery(pmes, p, dht.bucketSize)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
344
	if closer != nil {
345
		// TODO: pstore.PeerInfos should move to core (=> peerstore.AddrInfos).
Jeromy's avatar
Jeromy committed
346
		infos := pstore.PeerInfos(dht.peerstore, closer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
347
		resp.CloserPeers = pb.PeerInfosToPBPeers(dht.host.Network(), infos)
Matt Joiner's avatar
Matt Joiner committed
348
		logger.Debugf("%s have %d closer peers: %s", reqDesc, len(closer), infos)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
349 350 351 352 353
	}

	return resp, nil
}

354
func (dht *IpfsDHT) handleAddProvider(ctx context.Context, p peer.ID, pmes *pb.Message) (_ *pb.Message, _err error) {
Matt Joiner's avatar
Matt Joiner committed
355 356 357
	ctx = logger.Start(ctx, "handleAddProvider")
	defer func() { logger.FinishWithErr(ctx, _err) }()
	logger.SetTag(ctx, "peer", p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
358

359 360 361 362
	c, err := cid.Cast([]byte(pmes.GetKey()))
	if err != nil {
		return nil, err
	}
Matt Joiner's avatar
Matt Joiner committed
363
	logger.SetTag(ctx, "key", c)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
364

Matt Joiner's avatar
Matt Joiner committed
365
	logger.Debugf("%s adding %s as a provider for '%s'\n", dht.self, p, c)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
366

367
	// add provider should use the address given in the message
368 369 370
	pinfos := pb.PBPeersToPeerInfos(pmes.GetProviderPeers())
	for _, pi := range pinfos {
		if pi.ID != p {
371 372
			// we should ignore this provider record! not from originator.
			// (we should sign them and check signature later...)
Matt Joiner's avatar
Matt Joiner committed
373
			logger.Debugf("handleAddProvider received provider %s from %s. Ignore.", pi.ID, p)
374 375
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
376

377
		if len(pi.Addrs) < 1 {
Matt Joiner's avatar
Matt Joiner committed
378
			logger.Debugf("%s got no valid addresses for provider %s. Ignore.", dht.self, p)
379 380
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
381

Matt Joiner's avatar
Matt Joiner committed
382
		logger.Debugf("received provider %s for %s (addrs: %s)", p, c, pi.Addrs)
383
		if pi.ID != dht.self { // don't add own addrs.
384
			// add the received addresses to our peerstore.
385
			dht.peerstore.AddAddrs(pi.ID, pi.Addrs, peerstore.ProviderAddrTTL)
386
		}
387
		dht.providers.AddProvider(ctx, c, p)
388 389
	}

Jeromy's avatar
Jeromy committed
390
	return nil, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
391
}
392

393 394
func convertToDsKey(s []byte) ds.Key {
	return ds.NewKey(base32.RawStdEncoding.EncodeToString(s))
395
}