handlers.go 7.76 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7
package dht

import (
	"errors"
	"fmt"
	"time"

8 9 10
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11
	peer "github.com/jbenet/go-ipfs/p2p/peer"
12
	pb "github.com/jbenet/go-ipfs/routing/dht/pb"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14 15
	u "github.com/jbenet/go-ipfs/util"
)

Jeromy's avatar
Jeromy committed
16
// The number of closer peers to send on requests.
17 18
var CloserPeerCount = 4

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

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

41
func (dht *IpfsDHT) handleGetValue(ctx context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
Jeromy's avatar
Jeromy committed
42
	defer log.EventBegin(ctx, "handleGetValue", p).Done()
Jeromy's avatar
Jeromy committed
43
	log.Debugf("%s handleGetValue for key: %s", dht.self, pmes.GetKey())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45

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

48
	// first, is there even a key?
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50 51
	key := pmes.GetKey()
	if key == "" {
		return nil, errors.New("handleGetValue but no key was provided")
52
		// TODO: send back an error response? could be bad, but the other node's hanging.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53 54 55
	}

	// let's first check if we have the value locally.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56
	log.Debugf("%s handleGetValue looking into ds", dht.self)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57
	dskey := u.Key(pmes.GetKey()).DsKey()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
	iVal, err := dht.datastore.Get(dskey)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59
	log.Debugf("%s handleGetValue looking into ds GOT %v", dht.self, iVal)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60 61

	// if we got an unexpected error, bail.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62
	if err != nil && err != ds.ErrNotFound {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63 64 65
		return nil, err
	}

66 67 68 69
	// Note: changed the behavior here to return _as much_ info as possible
	// (potentially all of {value, closer peers, provider})

	// if we have the value, send it back
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70
	if err == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
		log.Debugf("%s handleGetValue success!", dht.self)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72 73 74 75 76 77

		byts, ok := iVal.([]byte)
		if !ok {
			return nil, fmt.Errorf("datastore had non byte-slice value for %v", dskey)
		}

78 79 80
		rec := new(pb.Record)
		err := proto.Unmarshal(byts, rec)
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81
			log.Debug("Failed to unmarshal dht record from datastore")
82 83 84 85
			return nil, err
		}

		resp.Record = rec
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86 87 88
	}

	// if we know any providers for the requested value, return those.
89
	provs := dht.providers.GetProviders(ctx, u.Key(pmes.GetKey()))
90
	provinfos := peer.PeerInfos(dht.peerstore, provs)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91
	if len(provs) > 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92
		log.Debugf("handleGetValue returning %d provider[s]", len(provs))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93
		resp.ProviderPeers = pb.PeerInfosToPBPeers(dht.host.Network(), provinfos)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94 95 96
	}

	// Find closest peer on given cluster to desired key and reply with that info
97
	closer := dht.betterPeersToQuery(pmes, p, CloserPeerCount)
98
	closerinfos := peer.PeerInfos(dht.peerstore, closer)
99
	if closer != nil {
100 101 102 103 104 105 106
		for _, pi := range closerinfos {
			log.Debugf("handleGetValue returning closer peer: '%s'", pi.ID)
			if len(pi.Addrs) < 1 {
				log.Criticalf(`no addresses on peer being sent!
					[local:%s]
					[sending:%s]
					[remote:%s]`, dht.self, pi.ID, p)
Jeromy's avatar
Jeromy committed
107
			}
108
		}
109

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110
		resp.CloserPeers = pb.PeerInfosToPBPeers(dht.host.Network(), closerinfos)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111 112 113 114 115 116
	}

	return resp, nil
}

// Store a value in this peer local storage
117
func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
Jeromy's avatar
Jeromy committed
118
	defer log.EventBegin(ctx, "handlePutValue", p).Done()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
119
	dskey := u.Key(pmes.GetKey()).DsKey()
120

121
	if err := dht.verifyRecordLocally(pmes.GetRecord()); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122
		log.Debugf("Bad dht record in PUT from: %s. %s", u.Key(pmes.GetRecord().GetAuthor()), err)
123 124 125 126 127 128 129 130 131
		return nil, err
	}

	data, err := proto.Marshal(pmes.GetRecord())
	if err != nil {
		return nil, err
	}

	err = dht.datastore.Put(dskey, data)
Jeromy's avatar
Jeromy committed
132
	log.Debugf("%s handlePutValue %v", dht.self, dskey)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
133
	return pmes, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
134 135
}

136
func (dht *IpfsDHT) handlePing(_ context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137
	log.Debugf("%s Responding to ping from %s!\n", dht.self, p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
138
	return pmes, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
139 140
}

141
func (dht *IpfsDHT) handleFindPeer(ctx context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
Jeromy's avatar
Jeromy committed
142
	defer log.EventBegin(ctx, "handleFindPeer", p).Done()
143
	resp := pb.NewMessage(pmes.GetType(), "", pmes.GetClusterLevel())
144
	var closest []peer.ID
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
145 146

	// if looking for self... special case where we send it on CloserPeers.
147 148
	if peer.ID(pmes.GetKey()) == dht.self {
		closest = []peer.ID{dht.self}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149
	} else {
150
		closest = dht.betterPeersToQuery(pmes, p, CloserPeerCount)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
151 152 153
	}

	if closest == nil {
154
		log.Warningf("%s handleFindPeer %s: could not find anything.", dht.self, p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
155 156 157
		return resp, nil
	}

158 159 160 161 162 163
	var withAddresses []peer.PeerInfo
	closestinfos := peer.PeerInfos(dht.peerstore, closest)
	for _, pi := range closestinfos {
		if len(pi.Addrs) > 0 {
			withAddresses = append(withAddresses, pi)
			log.Debugf("handleFindPeer: sending back '%s'", pi.ID)
164
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165 166
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
167
	resp.CloserPeers = pb.PeerInfosToPBPeers(dht.host.Network(), withAddresses)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
168 169 170
	return resp, nil
}

171
func (dht *IpfsDHT) handleGetProviders(ctx context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
Jeromy's avatar
Jeromy committed
172
	defer log.EventBegin(ctx, "handleGetProviders", p).Done()
173
	resp := pb.NewMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
174 175 176 177 178 179
	key := u.Key(pmes.GetKey())

	// debug logging niceness.
	reqDesc := fmt.Sprintf("%s handleGetProviders(%s, %s): ", dht.self, p, key)
	log.Debugf("%s begin", reqDesc)
	defer log.Debugf("%s end", reqDesc)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
180 181

	// check if we have this value, to add ourselves as provider.
182
	has, err := dht.datastore.Has(key.DsKey())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
183
	if err != nil && err != ds.ErrNotFound {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
184
		log.Debugf("unexpected datastore error: %v\n", err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
185 186 187 188
		has = false
	}

	// setup providers
189
	providers := dht.providers.GetProviders(ctx, key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
190 191
	if has {
		providers = append(providers, dht.self)
192
		log.Debugf("%s have the value. added self as provider", reqDesc)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
193 194 195
	}

	if providers != nil && len(providers) > 0 {
196
		infos := peer.PeerInfos(dht.peerstore, providers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
197
		resp.ProviderPeers = pb.PeerInfosToPBPeers(dht.host.Network(), infos)
198
		log.Debugf("%s have %d providers: %s", reqDesc, len(providers), infos)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
199 200 201
	}

	// Also send closer peers.
202
	closer := dht.betterPeersToQuery(pmes, p, CloserPeerCount)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
203
	if closer != nil {
204
		infos := peer.PeerInfos(dht.peerstore, providers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
205
		resp.CloserPeers = pb.PeerInfosToPBPeers(dht.host.Network(), infos)
206
		log.Debugf("%s have %d closer peers: %s", reqDesc, len(closer), infos)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
207 208 209 210 211 212 213
	}

	return resp, nil
}

type providerInfo struct {
	Creation time.Time
214
	Value    peer.ID
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
215 216
}

217
func (dht *IpfsDHT) handleAddProvider(ctx context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
Jeromy's avatar
Jeromy committed
218
	defer log.EventBegin(ctx, "handleAddProvider", p).Done()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
219
	key := u.Key(pmes.GetKey())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
220

221
	log.Debugf("%s adding %s as a provider for '%s'\n", dht.self, p, key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
222

223
	// add provider should use the address given in the message
224 225 226 227 228
	pinfos := pb.PBPeersToPeerInfos(pmes.GetProviderPeers())
	for _, pi := range pinfos {
		if pi.ID != p {
			// we should ignore this provider reccord! not from originator.
			// (we chould sign them and check signature later...)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
229
			log.Debugf("handleAddProvider received provider %s from %s. Ignore.", pi.ID, p)
230 231
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
232

233
		if len(pi.Addrs) < 1 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
234
			log.Debugf("%s got no valid addresses for provider %s. Ignore.", dht.self, p)
235 236
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
237

238
		log.Infof("received provider %s for %s (addrs: %s)", p, key, pi.Addrs)
239
		if pi.ID != dht.self { // dont add own addrs.
240
			// add the received addresses to our peerstore.
241
			dht.peerstore.AddAddrs(pi.ID, pi.Addrs, peer.ProviderAddrTTL)
242
		}
243
		dht.providers.AddProvider(key, p)
244 245
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
246
	return pmes, nil // send back same msg as confirmation.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
247
}