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

3
import (
4
	"bytes"
5
	"crypto/rand"
6
	"fmt"
7 8
	"sync"
	"time"
9

10
	peer "github.com/jbenet/go-ipfs/peer"
Jeromy's avatar
Jeromy committed
11
	kb "github.com/jbenet/go-ipfs/routing/kbucket"
12 13
	swarm "github.com/jbenet/go-ipfs/swarm"
	u "github.com/jbenet/go-ipfs/util"
14 15

	ma "github.com/jbenet/go-multiaddr"
Jeromy's avatar
Jeromy committed
16 17 18

	ds "github.com/jbenet/datastore.go"

19
	"code.google.com/p/goprotobuf/proto"
20 21
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22 23 24 25 26
// TODO. SEE https://github.com/jbenet/node-ipfs/blob/master/submodules/ipfs-dht/index.js

// IpfsDHT is an implementation of Kademlia with Coral and S/Kademlia modifications.
// It is used to implement the base IpfsRouting module.
type IpfsDHT struct {
27 28
	// Array of routing tables for differently distanced nodes
	// NOTE: (currently, only a single table is used)
29
	routingTables []*kb.RoutingTable
30

31
	network swarm.Network
32
	netChan *swarm.Chan
33

Jeromy's avatar
Jeromy committed
34 35 36 37 38
	// Local peer (yourself)
	self *peer.Peer

	// Local data
	datastore ds.Datastore
39
	dslock    sync.Mutex
40

41
	providers *ProviderManager
42

43 44
	// Signal to shutdown dht
	shutdown chan struct{}
45 46 47

	// When this peer started up
	birth time.Time
48 49 50

	//lock to make diagnostics work better
	diaglock sync.Mutex
51

52
	// listener is a server to register to listen for responses to messages
53
	listener *swarm.MessageListener
54 55
}

Jeromy's avatar
Jeromy committed
56
// NewDHT creates a new DHT object with the given peer as the 'local' host
Jeromy's avatar
Jeromy committed
57
func NewDHT(p *peer.Peer, net swarm.Network, dstore ds.Datastore) *IpfsDHT {
58
	dht := new(IpfsDHT)
Jeromy's avatar
Jeromy committed
59
	dht.network = net
60
	dht.netChan = net.GetChannel(swarm.PBWrapper_DHT_MESSAGE)
Jeromy's avatar
Jeromy committed
61
	dht.datastore = dstore
62
	dht.self = p
63
	dht.providers = NewProviderManager(p.ID)
Jeromy's avatar
Jeromy committed
64
	dht.shutdown = make(chan struct{})
65

66 67 68 69
	dht.routingTables = make([]*kb.RoutingTable, 3)
	dht.routingTables[0] = kb.NewRoutingTable(20, kb.ConvertPeerID(p.ID), time.Millisecond*30)
	dht.routingTables[1] = kb.NewRoutingTable(20, kb.ConvertPeerID(p.ID), time.Millisecond*100)
	dht.routingTables[2] = kb.NewRoutingTable(20, kb.ConvertPeerID(p.ID), time.Hour)
70
	dht.listener = swarm.NewMessageListener()
71
	dht.birth = time.Now()
Jeromy's avatar
Jeromy committed
72
	return dht
73 74
}

75
// Start up background goroutines needed by the DHT
76 77 78 79
func (dht *IpfsDHT) Start() {
	go dht.handleMessages()
}

80
// Connect to a new peer at the given address, ping and add to the routing table
81
func (dht *IpfsDHT) Connect(addr *ma.Multiaddr) (*peer.Peer, error) {
82
	maddrstr, _ := addr.String()
83
	u.DOut("Connect to new peer: %s\n", maddrstr)
84
	npeer, err := dht.network.ConnectNew(addr)
85
	if err != nil {
86
		return nil, err
87 88
	}

Jeromy's avatar
Jeromy committed
89 90
	// Ping new peer to register in their routing table
	// NOTE: this should be done better...
91
	err = dht.Ping(npeer, time.Second*2)
Jeromy's avatar
Jeromy committed
92
	if err != nil {
93
		return nil, fmt.Errorf("failed to ping newly connected peer: %s\n", err)
Jeromy's avatar
Jeromy committed
94 95
	}

96 97
	dht.Update(npeer)

98
	return npeer, nil
Jeromy's avatar
Jeromy committed
99 100
}

101 102
// Read in all messages from swarm and handle them appropriately
// NOTE: this function is just a quick sketch
103
func (dht *IpfsDHT) handleMessages() {
Jeromy's avatar
Jeromy committed
104
	u.DOut("Begin message handling routine\n")
105

106
	errs := dht.network.GetErrChan()
107 108
	for {
		select {
109
		case mes, ok := <-dht.netChan.Incoming:
110
			if !ok {
Jeromy's avatar
Jeromy committed
111
				u.DOut("handleMessages closing, bad recv on incoming\n")
112 113
				return
			}
114
			pmes := new(PBDHTMessage)
115 116
			err := proto.Unmarshal(mes.Data, pmes)
			if err != nil {
117
				u.PErr("Failed to decode protobuf message: %s\n", err)
118 119 120
				continue
			}

121
			dht.Update(mes.Peer)
122

123
			// Note: not sure if this is the correct place for this
124
			if pmes.GetResponse() {
125
				dht.listener.Respond(pmes.GetId(), mes)
126
				continue
127
			}
128 129
			//

130
			u.DOut("[peer: %s]\nGot message type: '%s' [id = %x, from = %s]\n",
Jeromy's avatar
Jeromy committed
131
				dht.self.ID.Pretty(),
132
				PBDHTMessage_MessageType_name[int32(pmes.GetType())],
133
				pmes.GetId(), mes.Peer.ID.Pretty())
134
			switch pmes.GetType() {
135
			case PBDHTMessage_GET_VALUE:
136
				go dht.handleGetValue(mes.Peer, pmes)
137
			case PBDHTMessage_PUT_VALUE:
138
				go dht.handlePutValue(mes.Peer, pmes)
139
			case PBDHTMessage_FIND_NODE:
140
				go dht.handleFindPeer(mes.Peer, pmes)
141
			case PBDHTMessage_ADD_PROVIDER:
142
				go dht.handleAddProvider(mes.Peer, pmes)
143
			case PBDHTMessage_GET_PROVIDERS:
144
				go dht.handleGetProviders(mes.Peer, pmes)
145
			case PBDHTMessage_PING:
146
				go dht.handlePing(mes.Peer, pmes)
147
			case PBDHTMessage_DIAGNOSTIC:
148
				go dht.handleDiagnostic(mes.Peer, pmes)
149 150
			default:
				u.PErr("Recieved invalid message type")
151 152
			}

153
		case err := <-errs:
154
			u.PErr("dht err: %s\n", err)
155 156
		case <-dht.shutdown:
			return
157 158 159 160
		}
	}
}

Jeromy's avatar
Jeromy committed
161
func (dht *IpfsDHT) putValueToNetwork(p *peer.Peer, key string, value []byte) error {
162
	pmes := Message{
163 164
		Type:  PBDHTMessage_PUT_VALUE,
		Key:   key,
165
		Value: value,
166
		ID:    swarm.GenerateMessageID(),
167 168 169
	}

	mes := swarm.NewMessage(p, pmes.ToProtobuf())
170
	dht.netChan.Outgoing <- mes
171 172 173
	return nil
}

174
func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *PBDHTMessage) {
175
	u.DOut("handleGetValue for key: %s\n", pmes.GetKey())
Jeromy's avatar
Jeromy committed
176
	dskey := ds.NewKey(pmes.GetKey())
177
	resp := &Message{
Jeromy's avatar
Jeromy committed
178
		Response: true,
179
		ID:       pmes.GetId(),
Jeromy's avatar
Jeromy committed
180 181 182
		Key:      pmes.GetKey(),
	}
	iVal, err := dht.datastore.Get(dskey)
Jeromy's avatar
Jeromy committed
183
	if err == nil {
Jeromy's avatar
Jeromy committed
184
		u.DOut("handleGetValue success!\n")
Jeromy's avatar
Jeromy committed
185 186
		resp.Success = true
		resp.Value = iVal.([]byte)
Jeromy's avatar
Jeromy committed
187
	} else if err == ds.ErrNotFound {
Jeromy's avatar
Jeromy committed
188
		// Check if we know any providers for the requested value
189 190
		provs := dht.providers.GetProviders(u.Key(pmes.GetKey()))
		if len(provs) > 0 {
191
			u.DOut("handleGetValue returning %d provider[s]\n", len(provs))
192
			resp.Peers = provs
Jeromy's avatar
Jeromy committed
193 194 195
			resp.Success = true
		} else {
			// No providers?
196 197
			// Find closest peer on given cluster to desired key and reply with that info

198 199 200
			level := 0
			if len(pmes.GetValue()) < 1 {
				// TODO: maybe return an error? Defaulting isnt a good idea IMO
Jeromy's avatar
Jeromy committed
201
				u.PErr("handleGetValue: no routing level specified, assuming 0\n")
202 203 204
			} else {
				level = int(pmes.GetValue()[0]) // Using value field to specify cluster level
			}
205
			u.DOut("handleGetValue searching level %d clusters\n", level)
206

207
			closer := dht.routingTables[level].NearestPeer(kb.ConvertKey(u.Key(pmes.GetKey())))
208

Jeromy's avatar
Jeromy committed
209
			if closer.ID.Equal(dht.self.ID) {
Jeromy's avatar
Jeromy committed
210
				u.DOut("Attempted to return self! this shouldnt happen...\n")
Jeromy's avatar
Jeromy committed
211 212 213
				resp.Peers = nil
				goto out
			}
214 215 216
			// If this peer is closer than the one from the table, return nil
			if kb.Closer(dht.self.ID, closer.ID, u.Key(pmes.GetKey())) {
				resp.Peers = nil
Jeromy's avatar
Jeromy committed
217
				u.DOut("handleGetValue could not find a closer node than myself.\n")
218
			} else {
219
				u.DOut("handleGetValue returning a closer peer: '%s'\n", closer.ID.Pretty())
220 221
				resp.Peers = []*peer.Peer{closer}
			}
222
		}
Jeromy's avatar
Jeromy committed
223
	} else {
224
		//temp: what other errors can a datastore return?
Jeromy's avatar
Jeromy committed
225
		panic(err)
226
	}
227

Jeromy's avatar
Jeromy committed
228
out:
229
	mes := swarm.NewMessage(p, resp.ToProtobuf())
230
	dht.netChan.Outgoing <- mes
231 232
}

Jeromy's avatar
Jeromy committed
233
// Store a value in this peer local storage
234
func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *PBDHTMessage) {
235 236
	dht.dslock.Lock()
	defer dht.dslock.Unlock()
Jeromy's avatar
Jeromy committed
237 238 239 240 241 242
	dskey := ds.NewKey(pmes.GetKey())
	err := dht.datastore.Put(dskey, pmes.GetValue())
	if err != nil {
		// For now, just panic, handle this better later maybe
		panic(err)
	}
243 244
}

245
func (dht *IpfsDHT) handlePing(p *peer.Peer, pmes *PBDHTMessage) {
246
	u.DOut("[%s] Responding to ping from [%s]!\n", dht.self.ID.Pretty(), p.ID.Pretty())
247
	resp := Message{
248
		Type:     pmes.GetType(),
249
		Response: true,
250
		ID:       pmes.GetId(),
251
	}
252

253
	dht.netChan.Outgoing <- swarm.NewMessage(p, resp.ToProtobuf())
254 255
}

256
func (dht *IpfsDHT) handleFindPeer(p *peer.Peer, pmes *PBDHTMessage) {
257
	resp := Message{
258
		Type:     pmes.GetType(),
259
		ID:       pmes.GetId(),
260 261 262 263
		Response: true,
	}
	defer func() {
		mes := swarm.NewMessage(p, resp.ToProtobuf())
264
		dht.netChan.Outgoing <- mes
265 266
	}()
	level := pmes.GetValue()[0]
267
	u.DOut("handleFindPeer: searching for '%s'\n", peer.ID(pmes.GetKey()).Pretty())
268
	closest := dht.routingTables[level].NearestPeer(kb.ConvertKey(u.Key(pmes.GetKey())))
Jeromy's avatar
Jeromy committed
269
	if closest == nil {
Jeromy's avatar
Jeromy committed
270
		u.PErr("handleFindPeer: could not find anything.\n")
271
		return
Jeromy's avatar
Jeromy committed
272 273 274
	}

	if len(closest.Addresses) == 0 {
Jeromy's avatar
Jeromy committed
275
		u.PErr("handleFindPeer: no addresses for connected peer...\n")
276
		return
Jeromy's avatar
Jeromy committed
277 278
	}

279 280 281
	// If the found peer further away than this peer...
	if kb.Closer(dht.self.ID, closest.ID, u.Key(pmes.GetKey())) {
		return
Jeromy's avatar
Jeromy committed
282 283
	}

284
	u.DOut("handleFindPeer: sending back '%s'\n", closest.ID.Pretty())
285 286 287 288 289
	resp.Peers = []*peer.Peer{closest}
	resp.Success = true
}

func (dht *IpfsDHT) handleGetProviders(p *peer.Peer, pmes *PBDHTMessage) {
290
	resp := Message{
291 292
		Type:     PBDHTMessage_GET_PROVIDERS,
		Key:      pmes.GetKey(),
293
		ID:       pmes.GetId(),
294
		Response: true,
Jeromy's avatar
Jeromy committed
295 296
	}

297 298 299 300 301
	has, err := dht.datastore.Has(ds.NewKey(pmes.GetKey()))
	if err != nil {
		dht.netChan.Errors <- err
	}

302
	providers := dht.providers.GetProviders(u.Key(pmes.GetKey()))
303 304 305
	if has {
		providers = append(providers, dht.self)
	}
306
	if providers == nil || len(providers) == 0 {
Jeromy's avatar
Jeromy committed
307 308 309 310 311
		level := 0
		if len(pmes.GetValue()) > 0 {
			level = int(pmes.GetValue()[0])
		}

312
		closer := dht.routingTables[level].NearestPeer(kb.ConvertKey(u.Key(pmes.GetKey())))
Jeromy's avatar
Jeromy committed
313 314 315 316 317
		if kb.Closer(dht.self.ID, closer.ID, u.Key(pmes.GetKey())) {
			resp.Peers = nil
		} else {
			resp.Peers = []*peer.Peer{closer}
		}
318
	} else {
319
		resp.Peers = providers
320
		resp.Success = true
321 322 323
	}

	mes := swarm.NewMessage(p, resp.ToProtobuf())
324
	dht.netChan.Outgoing <- mes
325 326
}

327 328
type providerInfo struct {
	Creation time.Time
329
	Value    *peer.Peer
330 331
}

332
func (dht *IpfsDHT) handleAddProvider(p *peer.Peer, pmes *PBDHTMessage) {
333
	key := u.Key(pmes.GetKey())
Jeromy's avatar
Jeromy committed
334
	u.DOut("[%s] Adding [%s] as a provider for '%s'\n", dht.self.ID.Pretty(), p.ID.Pretty(), peer.ID(key).Pretty())
335
	dht.providers.AddProvider(key, p)
336 337
}

338
// Halt stops all communications from this peer and shut down
339 340 341
func (dht *IpfsDHT) Halt() {
	dht.shutdown <- struct{}{}
	dht.network.Close()
342 343
	dht.providers.Halt()
	dht.listener.Halt()
344
}
345

Jeromy's avatar
Jeromy committed
346
// NOTE: not yet finished, low priority
347
func (dht *IpfsDHT) handleDiagnostic(p *peer.Peer, pmes *PBDHTMessage) {
348
	seq := dht.routingTables[0].NearestPeers(kb.ConvertPeerID(dht.self.ID), 10)
349
	listenChan := dht.listener.Listen(pmes.GetId(), len(seq), time.Second*30)
350

351
	for _, ps := range seq {
352
		mes := swarm.NewMessage(ps, pmes)
353
		dht.netChan.Outgoing <- mes
354 355 356
	}

	buf := new(bytes.Buffer)
357 358 359
	di := dht.getDiagInfo()
	buf.Write(di.Marshal())

360 361 362 363 364 365 366 367
	// NOTE: this shouldnt be a hardcoded value
	after := time.After(time.Second * 20)
	count := len(seq)
	for count > 0 {
		select {
		case <-after:
			//Timeout, return what we have
			goto out
368 369 370
		case reqResp := <-listenChan:
			pmesOut := new(PBDHTMessage)
			err := proto.Unmarshal(reqResp.Data, pmesOut)
371 372 373 374
			if err != nil {
				// It broke? eh, whatever, keep going
				continue
			}
375
			buf.Write(reqResp.Data)
376 377 378 379 380
			count--
		}
	}

out:
381
	resp := Message{
382
		Type:     PBDHTMessage_DIAGNOSTIC,
383
		ID:       pmes.GetId(),
384
		Value:    buf.Bytes(),
385 386 387 388
		Response: true,
	}

	mes := swarm.NewMessage(p, resp.ToProtobuf())
389
	dht.netChan.Outgoing <- mes
390
}
391

392 393 394
func (dht *IpfsDHT) getValueOrPeers(p *peer.Peer, key u.Key, timeout time.Duration, level int) ([]byte, []*peer.Peer, error) {
	pmes, err := dht.getValueSingle(p, key, timeout, level)
	if err != nil {
395
		return nil, nil, err
396 397 398 399 400 401 402 403 404 405 406 407 408
	}

	if pmes.GetSuccess() {
		if pmes.Value == nil { // We were given provider[s]
			val, err := dht.getFromPeerList(key, timeout, pmes.GetPeers(), level)
			if err != nil {
				return nil, nil, err
			}
			return val, nil, nil
		}

		// Success! We were given the value
		return pmes.GetValue(), nil, nil
409
	}
410

411 412 413 414 415 416 417 418
	// We were given a closer node
	var peers []*peer.Peer
	for _, pb := range pmes.GetPeers() {
		if peer.ID(pb.GetId()).Equal(dht.self.ID) {
			continue
		}
		addr, err := ma.NewMultiaddr(pb.GetAddr())
		if err != nil {
419
			u.PErr("%v\n", err.Error())
420 421
			continue
		}
422

423 424
		np, err := dht.network.GetConnection(peer.ID(pb.GetId()), addr)
		if err != nil {
425
			u.PErr("%v\n", err.Error())
426
			continue
427
		}
428 429

		peers = append(peers, np)
430
	}
431
	return nil, peers, nil
432 433
}

434 435
// getValueSingle simply performs the get value RPC with the given parameters
func (dht *IpfsDHT) getValueSingle(p *peer.Peer, key u.Key, timeout time.Duration, level int) (*PBDHTMessage, error) {
436
	pmes := Message{
437 438 439
		Type:  PBDHTMessage_GET_VALUE,
		Key:   string(key),
		Value: []byte{byte(level)},
440
		ID:    swarm.GenerateMessageID(),
Jeromy's avatar
Jeromy committed
441
	}
442
	responseChan := dht.listener.Listen(pmes.ID, 1, time.Minute)
Jeromy's avatar
Jeromy committed
443 444

	mes := swarm.NewMessage(p, pmes.ToProtobuf())
445
	t := time.Now()
446
	dht.netChan.Outgoing <- mes
Jeromy's avatar
Jeromy committed
447 448 449 450 451

	// Wait for either the response or a timeout
	timeup := time.After(timeout)
	select {
	case <-timeup:
452
		dht.listener.Unlisten(pmes.ID)
Jeromy's avatar
Jeromy committed
453
		return nil, u.ErrTimeout
454
	case resp, ok := <-responseChan:
Jeromy's avatar
Jeromy committed
455
		if !ok {
Jeromy's avatar
Jeromy committed
456
			u.PErr("response channel closed before timeout, please investigate.\n")
Jeromy's avatar
Jeromy committed
457 458
			return nil, u.ErrTimeout
		}
459 460
		roundtrip := time.Since(t)
		resp.Peer.SetLatency(roundtrip)
461 462
		pmesOut := new(PBDHTMessage)
		err := proto.Unmarshal(resp.Data, pmesOut)
Jeromy's avatar
Jeromy committed
463 464 465
		if err != nil {
			return nil, err
		}
466
		return pmesOut, nil
Jeromy's avatar
Jeromy committed
467 468 469
	}
}

470 471 472 473 474 475 476 477 478 479
// TODO: Im not certain on this implementation, we get a list of peers/providers
// from someone what do we do with it? Connect to each of them? randomly pick
// one to get the value from? Or just connect to one at a time until we get a
// successful connection and request the value from it?
func (dht *IpfsDHT) getFromPeerList(key u.Key, timeout time.Duration,
	peerlist []*PBDHTMessage_PBPeer, level int) ([]byte, error) {
	for _, pinfo := range peerlist {
		p, _ := dht.Find(peer.ID(pinfo.GetId()))
		if p == nil {
			maddr, err := ma.NewMultiaddr(pinfo.GetAddr())
Jeromy's avatar
Jeromy committed
480
			if err != nil {
481
				u.PErr("getValue error: %s\n", err)
Jeromy's avatar
Jeromy committed
482 483
				continue
			}
484

485
			p, err = dht.network.GetConnection(peer.ID(pinfo.GetId()), maddr)
Jeromy's avatar
Jeromy committed
486
			if err != nil {
487
				u.PErr("getValue error: %s\n", err)
Jeromy's avatar
Jeromy committed
488 489 490
				continue
			}
		}
491
		pmes, err := dht.getValueSingle(p, key, timeout, level)
Jeromy's avatar
Jeromy committed
492
		if err != nil {
493
			u.DErr("getFromPeers error: %s\n", err)
Jeromy's avatar
Jeromy committed
494 495
			continue
		}
496
		dht.providers.AddProvider(key, p)
Jeromy's avatar
Jeromy committed
497

498 499 500 501
		// Make sure it was a successful get
		if pmes.GetSuccess() && pmes.Value != nil {
			return pmes.GetValue(), nil
		}
Jeromy's avatar
Jeromy committed
502 503 504 505
	}
	return nil, u.ErrNotFound
}

506
func (dht *IpfsDHT) getLocal(key u.Key) ([]byte, error) {
507 508
	dht.dslock.Lock()
	defer dht.dslock.Unlock()
509
	v, err := dht.datastore.Get(ds.NewKey(string(key)))
510 511 512 513 514 515
	if err != nil {
		return nil, err
	}
	return v.([]byte), nil
}

516
func (dht *IpfsDHT) putLocal(key u.Key, value []byte) error {
517 518
	return dht.datastore.Put(ds.NewKey(string(key)), value)
}
519

520
// Update TODO(chas) Document this function
521
func (dht *IpfsDHT) Update(p *peer.Peer) {
522
	for _, route := range dht.routingTables {
523 524 525 526
		removed := route.Update(p)
		// Only drop the connection if no tables refer to this peer
		if removed != nil {
			found := false
527
			for _, r := range dht.routingTables {
528 529 530 531 532 533 534 535 536
				if r.Find(removed.ID) != nil {
					found = true
					break
				}
			}
			if !found {
				dht.network.Drop(removed)
			}
		}
537 538
	}
}
Jeromy's avatar
Jeromy committed
539

540
// Find looks for a peer with a given ID connected to this dht and returns the peer and the table it was found in.
Jeromy's avatar
Jeromy committed
541
func (dht *IpfsDHT) Find(id peer.ID) (*peer.Peer, *kb.RoutingTable) {
542
	for _, table := range dht.routingTables {
Jeromy's avatar
Jeromy committed
543 544 545 546 547 548 549
		p := table.Find(id)
		if p != nil {
			return p, table
		}
	}
	return nil, nil
}
550 551

func (dht *IpfsDHT) findPeerSingle(p *peer.Peer, id peer.ID, timeout time.Duration, level int) (*PBDHTMessage, error) {
552
	pmes := Message{
553 554
		Type:  PBDHTMessage_FIND_NODE,
		Key:   string(id),
555
		ID:    swarm.GenerateMessageID(),
556 557 558 559
		Value: []byte{byte(level)},
	}

	mes := swarm.NewMessage(p, pmes.ToProtobuf())
560
	listenChan := dht.listener.Listen(pmes.ID, 1, time.Minute)
561
	t := time.Now()
562
	dht.netChan.Outgoing <- mes
563 564 565
	after := time.After(timeout)
	select {
	case <-after:
566
		dht.listener.Unlisten(pmes.ID)
567 568
		return nil, u.ErrTimeout
	case resp := <-listenChan:
569 570
		roundtrip := time.Since(t)
		resp.Peer.SetLatency(roundtrip)
571 572
		pmesOut := new(PBDHTMessage)
		err := proto.Unmarshal(resp.Data, pmesOut)
573 574 575 576
		if err != nil {
			return nil, err
		}

577
		return pmesOut, nil
578 579
	}
}
580

581 582
func (dht *IpfsDHT) printTables() {
	for _, route := range dht.routingTables {
583 584 585
		route.Print()
	}
}
Jeromy's avatar
Jeromy committed
586 587

func (dht *IpfsDHT) findProvidersSingle(p *peer.Peer, key u.Key, level int, timeout time.Duration) (*PBDHTMessage, error) {
588
	pmes := Message{
Jeromy's avatar
Jeromy committed
589 590
		Type:  PBDHTMessage_GET_PROVIDERS,
		Key:   string(key),
591
		ID:    swarm.GenerateMessageID(),
Jeromy's avatar
Jeromy committed
592 593 594 595 596
		Value: []byte{byte(level)},
	}

	mes := swarm.NewMessage(p, pmes.ToProtobuf())

597
	listenChan := dht.listener.Listen(pmes.ID, 1, time.Minute)
598
	dht.netChan.Outgoing <- mes
Jeromy's avatar
Jeromy committed
599 600 601
	after := time.After(timeout)
	select {
	case <-after:
602
		dht.listener.Unlisten(pmes.ID)
Jeromy's avatar
Jeromy committed
603 604
		return nil, u.ErrTimeout
	case resp := <-listenChan:
Jeromy's avatar
Jeromy committed
605
		u.DOut("FindProviders: got response.\n")
606 607
		pmesOut := new(PBDHTMessage)
		err := proto.Unmarshal(resp.Data, pmesOut)
Jeromy's avatar
Jeromy committed
608 609 610 611
		if err != nil {
			return nil, err
		}

612
		return pmesOut, nil
Jeromy's avatar
Jeromy committed
613 614 615
	}
}

616
// TODO: Could be done async
Jeromy's avatar
Jeromy committed
617
func (dht *IpfsDHT) addPeerList(key u.Key, peers []*PBDHTMessage_PBPeer) []*peer.Peer {
618
	var provArr []*peer.Peer
Jeromy's avatar
Jeromy committed
619 620 621 622 623 624 625 626
	for _, prov := range peers {
		// Dont add outselves to the list
		if peer.ID(prov.GetId()).Equal(dht.self.ID) {
			continue
		}
		// Dont add someone who is already on the list
		p := dht.network.Find(u.Key(prov.GetId()))
		if p == nil {
627
			u.DOut("given provider %s was not in our network already.\n", peer.ID(prov.GetId()).Pretty())
Jeromy's avatar
Jeromy committed
628 629
			var err error
			p, err = dht.peerFromInfo(prov)
Jeromy's avatar
Jeromy committed
630
			if err != nil {
631
				u.PErr("error connecting to new peer: %s\n", err)
Jeromy's avatar
Jeromy committed
632 633 634
				continue
			}
		}
635
		dht.providers.AddProvider(key, p)
636
		provArr = append(provArr, p)
Jeromy's avatar
Jeromy committed
637
	}
638
	return provArr
Jeromy's avatar
Jeromy committed
639
}
Jeromy's avatar
Jeromy committed
640 641 642 643 644 645 646 647 648

func (dht *IpfsDHT) peerFromInfo(pbp *PBDHTMessage_PBPeer) (*peer.Peer, error) {
	maddr, err := ma.NewMultiaddr(pbp.GetAddr())
	if err != nil {
		return nil, err
	}

	return dht.network.GetConnection(peer.ID(pbp.GetId()), maddr)
}
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

func (dht *IpfsDHT) loadProvidableKeys() error {
	kl := dht.datastore.KeyList()
	for _, k := range kl {
		dht.providers.AddProvider(u.Key(k.Bytes()), dht.self)
	}
	return nil
}

// Builds up list of peers by requesting random peer IDs
func (dht *IpfsDHT) Bootstrap() {
	id := make([]byte, 16)
	rand.Read(id)
	dht.FindPeer(peer.ID(id), time.Second*10)
}