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

import (
	"errors"
	"fmt"

7 8
	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"
9
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
	peer "github.com/jbenet/go-ipfs/p2p/peer"
11
	pb "github.com/jbenet/go-ipfs/routing/dht/pb"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13 14
	u "github.com/jbenet/go-ipfs/util"
)

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

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

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

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

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

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

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

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

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

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

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

		resp.Record = rec
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82 83 84
	}

	// Find closest peer on given cluster to desired key and reply with that info
85
	closer := dht.betterPeersToQuery(pmes, p, CloserPeerCount)
Jeromy's avatar
Jeromy committed
86
	if len(closer) > 0 {
87
		closerinfos := peer.PeerInfos(dht.peerstore, closer)
88 89 90 91 92 93 94
		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
95
			}
96
		}
97

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98
		resp.CloserPeers = pb.PeerInfosToPBPeers(dht.host.Network(), closerinfos)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99 100 101 102 103 104
	}

	return resp, nil
}

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

109
	if err := dht.verifyRecordLocally(pmes.GetRecord()); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110
		log.Debugf("Bad dht record in PUT from: %s. %s", u.Key(pmes.GetRecord().GetAuthor()), err)
111 112 113 114 115 116 117 118 119
		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
120
	log.Debugf("%s handlePutValue %v", dht.self, dskey)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
121
	return pmes, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122 123
}

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

129
func (dht *IpfsDHT) handleFindPeer(ctx context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
Jeromy's avatar
Jeromy committed
130
	defer log.EventBegin(ctx, "handleFindPeer", p).Done()
131
	resp := pb.NewMessage(pmes.GetType(), "", pmes.GetClusterLevel())
132
	var closest []peer.ID
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
133 134

	// if looking for self... special case where we send it on CloserPeers.
135 136
	if peer.ID(pmes.GetKey()) == dht.self {
		closest = []peer.ID{dht.self}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137
	} else {
138
		closest = dht.betterPeersToQuery(pmes, p, CloserPeerCount)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
139 140 141
	}

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

146 147 148 149 150 151
	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)
152
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
153 154
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
155
	resp.CloserPeers = pb.PeerInfosToPBPeers(dht.host.Network(), withAddresses)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
156 157 158
	return resp, nil
}

159
func (dht *IpfsDHT) handleGetProviders(ctx context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
Jeromy's avatar
Jeromy committed
160
	defer log.EventBegin(ctx, "handleGetProviders", p).Done()
161
	resp := pb.NewMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
162 163 164 165 166 167
	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
168 169

	// check if we have this value, to add ourselves as provider.
170
	has, err := dht.datastore.Has(key.DsKey())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
171
	if err != nil && err != ds.ErrNotFound {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
172
		log.Debugf("unexpected datastore error: %v\n", err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
173 174 175 176
		has = false
	}

	// setup providers
177
	providers := dht.providers.GetProviders(ctx, key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
178 179
	if has {
		providers = append(providers, dht.self)
180
		log.Debugf("%s have the value. added self as provider", reqDesc)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
181 182 183
	}

	if providers != nil && len(providers) > 0 {
184
		infos := peer.PeerInfos(dht.peerstore, providers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
185
		resp.ProviderPeers = pb.PeerInfosToPBPeers(dht.host.Network(), infos)
186
		log.Debugf("%s have %d providers: %s", reqDesc, len(providers), infos)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
187 188 189
	}

	// Also send closer peers.
190
	closer := dht.betterPeersToQuery(pmes, p, CloserPeerCount)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
191
	if closer != nil {
Jeromy's avatar
Jeromy committed
192
		infos := peer.PeerInfos(dht.peerstore, closer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
193
		resp.CloserPeers = pb.PeerInfosToPBPeers(dht.host.Network(), infos)
194
		log.Debugf("%s have %d closer peers: %s", reqDesc, len(closer), infos)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
195 196 197 198 199
	}

	return resp, nil
}

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

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

206
	// add provider should use the address given in the message
207 208 209 210 211
	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
212
			log.Debugf("handleAddProvider received provider %s from %s. Ignore.", pi.ID, p)
213 214
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
215

216
		if len(pi.Addrs) < 1 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
217
			log.Debugf("%s got no valid addresses for provider %s. Ignore.", dht.self, p)
218 219
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
220

221
		log.Infof("received provider %s for %s (addrs: %s)", p, key, pi.Addrs)
222
		if pi.ID != dht.self { // dont add own addrs.
223
			// add the received addresses to our peerstore.
224
			dht.peerstore.AddAddrs(pi.ID, pi.Addrs, peer.ProviderAddrTTL)
225
		}
226
		dht.providers.AddProvider(ctx, key, p)
227 228
	}

Jeromy's avatar
Jeromy committed
229
	return nil, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
230
}