2017-08-16 17:52:19 +02:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2017-08-16 17:56:35 +02:00
|
|
|
"fmt"
|
2017-08-16 23:34:53 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2017-08-16 17:56:35 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-08-16 23:34:53 +02:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
"github.com/zeebo/bencode"
|
2017-08-16 17:52:19 +02:00
|
|
|
"net"
|
2017-08-16 23:34:53 +02:00
|
|
|
"reflect"
|
2017-08-16 17:52:19 +02:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-08-16 23:34:53 +02:00
|
|
|
pingMethod = "ping"
|
|
|
|
storeMethod = "store"
|
|
|
|
findNodeMethod = "findNode"
|
|
|
|
findValueMethod = "findValue"
|
2017-08-16 17:52:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
generalError = 201 + iota
|
|
|
|
serverError
|
|
|
|
protocolError
|
|
|
|
unknownError
|
|
|
|
)
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
const (
|
|
|
|
requestType = 0
|
|
|
|
responseType = 1
|
|
|
|
errorType = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// these are strings because bencode requires bytestring keys
|
|
|
|
headerTypeField = "0"
|
|
|
|
headerMessageIDField = "1"
|
|
|
|
headerNodeIDField = "2"
|
|
|
|
headerPayloadField = "3"
|
|
|
|
headerArgsField = "4"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Message interface {
|
|
|
|
GetID() string
|
|
|
|
Encode() ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
ID string
|
|
|
|
NodeID string
|
|
|
|
Method string
|
|
|
|
Args []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Request) GetID() string { return r.ID }
|
|
|
|
func (r Request) Encode() ([]byte, error) {
|
|
|
|
return bencode.EncodeBytes(map[string]interface{}{
|
|
|
|
headerTypeField: requestType,
|
|
|
|
headerMessageIDField: r.ID,
|
|
|
|
headerNodeIDField: r.NodeID,
|
|
|
|
headerPayloadField: r.Method,
|
|
|
|
headerArgsField: r.Args,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
ID string
|
|
|
|
NodeID string
|
|
|
|
Response string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Response) GetID() string { return r.ID }
|
|
|
|
func (r Response) Encode() ([]byte, error) {
|
|
|
|
return bencode.EncodeBytes(map[string]interface{}{
|
|
|
|
headerTypeField: responseType,
|
|
|
|
headerMessageIDField: r.ID,
|
|
|
|
headerNodeIDField: r.NodeID,
|
|
|
|
headerPayloadField: r.Response,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Error struct {
|
|
|
|
ID string
|
|
|
|
NodeID string
|
|
|
|
Response []string
|
|
|
|
ExceptionType string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Error) GetID() string { return e.ID }
|
|
|
|
func (e Error) Encode() ([]byte, error) {
|
|
|
|
return bencode.EncodeBytes(map[string]interface{}{
|
|
|
|
headerTypeField: errorType,
|
|
|
|
headerMessageIDField: e.ID,
|
|
|
|
headerNodeIDField: e.NodeID,
|
|
|
|
headerPayloadField: e.ExceptionType,
|
|
|
|
headerArgsField: e.Response,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-16 17:52:19 +02:00
|
|
|
// packet represents the information receive from udp.
|
|
|
|
type packet struct {
|
|
|
|
data []byte
|
|
|
|
raddr *net.UDPAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// token represents the token when response getPeers request.
|
|
|
|
type token struct {
|
|
|
|
data string
|
|
|
|
createTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// tokenManager managers the tokens.
|
|
|
|
type tokenManager struct {
|
|
|
|
*syncedMap
|
|
|
|
expiredAfter time.Duration
|
|
|
|
dht *DHT
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTokenManager returns a new tokenManager.
|
|
|
|
func newTokenManager(expiredAfter time.Duration, dht *DHT) *tokenManager {
|
|
|
|
return &tokenManager{
|
|
|
|
syncedMap: newSyncedMap(),
|
|
|
|
expiredAfter: expiredAfter,
|
|
|
|
dht: dht,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// token returns a token. If it doesn't exist or is expired, it will add a
|
|
|
|
// new token.
|
|
|
|
func (tm *tokenManager) token(addr *net.UDPAddr) string {
|
|
|
|
v, ok := tm.Get(addr.IP.String())
|
|
|
|
tk, _ := v.(token)
|
|
|
|
|
|
|
|
if !ok || time.Now().Sub(tk.createTime) > tm.expiredAfter {
|
|
|
|
tk = token{
|
|
|
|
data: randomString(5),
|
|
|
|
createTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
tm.Set(addr.IP.String(), tk)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tk.data
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear removes expired tokens.
|
|
|
|
func (tm *tokenManager) clear() {
|
|
|
|
for _ = range time.Tick(time.Minute * 3) {
|
|
|
|
keys := make([]interface{}, 0, 100)
|
|
|
|
|
|
|
|
for item := range tm.Iter() {
|
|
|
|
if time.Now().Sub(item.val.(token).createTime) > tm.expiredAfter {
|
|
|
|
keys = append(keys, item.key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tm.DeleteMulti(keys)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check returns whether the token is valid.
|
|
|
|
func (tm *tokenManager) check(addr *net.UDPAddr, tokenString string) bool {
|
|
|
|
key := addr.IP.String()
|
|
|
|
v, ok := tm.Get(key)
|
|
|
|
tk, _ := v.(token)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
tm.Delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok && tokenString == tk.data
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeQuery returns a query-formed data.
|
|
|
|
func makeQuery(t, q string, a map[string]interface{}) map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"t": t,
|
|
|
|
"y": "q",
|
|
|
|
"q": q,
|
|
|
|
"a": a,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
// send sends data to the udp.
|
|
|
|
func send(dht *DHT, addr *net.UDPAddr, data Message) error {
|
|
|
|
log.Infof("Sending %s", spew.Sdump(data))
|
|
|
|
encoded, err := data.Encode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
2017-08-16 23:34:53 +02:00
|
|
|
log.Infof("Encoded: %s", string(encoded))
|
2017-08-16 17:52:19 +02:00
|
|
|
|
|
|
|
dht.conn.SetWriteDeadline(time.Now().Add(time.Second * 15))
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
_, err = dht.conn.WriteToUDP(encoded, addr)
|
2017-08-16 17:52:19 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// query represents the query data included queried node and query-formed data.
|
|
|
|
type query struct {
|
2017-08-16 23:34:53 +02:00
|
|
|
node *node
|
|
|
|
request Request
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// transaction implements transaction.
|
|
|
|
type transaction struct {
|
|
|
|
*query
|
|
|
|
id string
|
|
|
|
response chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// transactionManager represents the manager of transactions.
|
|
|
|
type transactionManager struct {
|
|
|
|
*sync.RWMutex
|
|
|
|
transactions *syncedMap
|
|
|
|
index *syncedMap
|
|
|
|
cursor uint64
|
|
|
|
maxCursor uint64
|
|
|
|
queryChan chan *query
|
|
|
|
dht *DHT
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTransactionManager returns new transactionManager pointer.
|
|
|
|
func newTransactionManager(maxCursor uint64, dht *DHT) *transactionManager {
|
|
|
|
return &transactionManager{
|
|
|
|
RWMutex: &sync.RWMutex{},
|
|
|
|
transactions: newSyncedMap(),
|
|
|
|
index: newSyncedMap(),
|
|
|
|
maxCursor: maxCursor,
|
|
|
|
queryChan: make(chan *query, 1024),
|
|
|
|
dht: dht,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// genTransID generates a transaction id and returns it.
|
|
|
|
func (tm *transactionManager) genTransID() string {
|
|
|
|
tm.Lock()
|
|
|
|
defer tm.Unlock()
|
|
|
|
|
|
|
|
tm.cursor = (tm.cursor + 1) % tm.maxCursor
|
|
|
|
return string(int2bytes(tm.cursor))
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTransaction creates a new transaction.
|
|
|
|
func (tm *transactionManager) newTransaction(id string, q *query) *transaction {
|
|
|
|
return &transaction{
|
|
|
|
id: id,
|
|
|
|
query: q,
|
|
|
|
response: make(chan struct{}, tm.dht.Try+1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// genIndexKey generates an indexed key which consists of queryType and
|
|
|
|
// address.
|
|
|
|
func (tm *transactionManager) genIndexKey(queryType, address string) string {
|
|
|
|
return strings.Join([]string{queryType, address}, ":")
|
|
|
|
}
|
|
|
|
|
|
|
|
// genIndexKeyByTrans generates an indexed key by a transaction.
|
|
|
|
func (tm *transactionManager) genIndexKeyByTrans(trans *transaction) string {
|
2017-08-16 23:34:53 +02:00
|
|
|
return tm.genIndexKey(trans.request.Method, trans.node.addr.String())
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert adds a transaction to transactionManager.
|
|
|
|
func (tm *transactionManager) insert(trans *transaction) {
|
|
|
|
tm.Lock()
|
|
|
|
defer tm.Unlock()
|
|
|
|
|
|
|
|
tm.transactions.Set(trans.id, trans)
|
|
|
|
tm.index.Set(tm.genIndexKeyByTrans(trans), trans)
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete removes a transaction from transactionManager.
|
|
|
|
func (tm *transactionManager) delete(transID string) {
|
|
|
|
v, ok := tm.transactions.Get(transID)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tm.Lock()
|
|
|
|
defer tm.Unlock()
|
|
|
|
|
|
|
|
trans := v.(*transaction)
|
|
|
|
tm.transactions.Delete(trans.id)
|
|
|
|
tm.index.Delete(tm.genIndexKeyByTrans(trans))
|
|
|
|
}
|
|
|
|
|
|
|
|
// len returns how many transactions are requesting now.
|
|
|
|
func (tm *transactionManager) len() int {
|
|
|
|
return tm.transactions.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
// transaction returns a transaction. keyType should be one of 0, 1 which
|
|
|
|
// represents transId and index each.
|
2017-08-16 23:34:53 +02:00
|
|
|
func (tm *transactionManager) transaction(key string, keyType int) *transaction {
|
2017-08-16 17:52:19 +02:00
|
|
|
|
|
|
|
sm := tm.transactions
|
|
|
|
if keyType == 1 {
|
|
|
|
sm = tm.index
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := sm.Get(key)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.(*transaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getByTransID returns a transaction by transID.
|
|
|
|
func (tm *transactionManager) getByTransID(transID string) *transaction {
|
|
|
|
return tm.transaction(transID, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getByIndex returns a transaction by indexed key.
|
|
|
|
func (tm *transactionManager) getByIndex(index string) *transaction {
|
|
|
|
return tm.transaction(index, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// transaction gets the proper transaction with whose id is transId and
|
|
|
|
// address is addr.
|
2017-08-16 23:34:53 +02:00
|
|
|
func (tm *transactionManager) filterOne(transID string, addr *net.UDPAddr) *transaction {
|
2017-08-16 17:52:19 +02:00
|
|
|
trans := tm.getByTransID(transID)
|
|
|
|
if trans == nil || trans.node.addr.String() != addr.String() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return trans
|
|
|
|
}
|
|
|
|
|
|
|
|
// query sends the query-formed data to udp and wait for the response.
|
|
|
|
// When timeout, it will retry `try - 1` times, which means it will query
|
|
|
|
// `try` times totally.
|
|
|
|
func (tm *transactionManager) query(q *query, try int) {
|
2017-08-16 23:34:53 +02:00
|
|
|
trans := tm.newTransaction(q.request.ID, q)
|
2017-08-16 17:52:19 +02:00
|
|
|
|
|
|
|
tm.insert(trans)
|
|
|
|
defer tm.delete(trans.id)
|
|
|
|
|
|
|
|
success := false
|
|
|
|
for i := 0; i < try; i++ {
|
2017-08-16 23:34:53 +02:00
|
|
|
if err := send(tm.dht, q.node.addr, q.request); err != nil {
|
2017-08-16 17:52:19 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-trans.response:
|
|
|
|
success = true
|
|
|
|
break
|
|
|
|
case <-time.After(time.Second * 15):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !success && q.node.id != nil {
|
|
|
|
tm.dht.routingTable.RemoveByAddr(q.node.addr.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run starts to listen and consume the query chan.
|
|
|
|
func (tm *transactionManager) run() {
|
|
|
|
var q *query
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case q = <-tm.queryChan:
|
|
|
|
go tm.query(q, tm.dht.Try)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendQuery send query-formed data to the chan.
|
2017-08-16 23:34:53 +02:00
|
|
|
func (tm *transactionManager) sendQuery(no *node, request Request) {
|
2017-08-16 17:52:19 +02:00
|
|
|
// If the target is self, then stop.
|
|
|
|
if no.id != nil && no.id.RawString() == tm.dht.node.id.RawString() ||
|
2017-08-16 23:34:53 +02:00
|
|
|
tm.getByIndex(tm.genIndexKey(request.Method, no.addr.String())) != nil {
|
|
|
|
|
2017-08-16 17:52:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
request.ID = tm.genTransID()
|
|
|
|
request.NodeID = tm.dht.id(no.id.RawString())
|
|
|
|
tm.queryChan <- &query{node: no, request: request}
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ping sends ping query to the chan.
|
|
|
|
func (tm *transactionManager) ping(no *node) {
|
2017-08-16 23:34:53 +02:00
|
|
|
tm.sendQuery(no, Request{Method: pingMethod})
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// findNode sends find_node query to the chan.
|
|
|
|
func (tm *transactionManager) findNode(no *node, target string) {
|
2017-08-16 23:34:53 +02:00
|
|
|
tm.sendQuery(no, Request{Method: findNodeMethod, Args: []string{target}})
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
// handle handles packets received from udp.
|
|
|
|
func handle(dht *DHT, pkt packet) {
|
|
|
|
log.Infof("Received message from %s: %s", pkt.raddr.IP.String(), string(pkt.data))
|
|
|
|
if len(dht.workerTokens) == dht.PacketWorkerLimit {
|
|
|
|
return
|
|
|
|
}
|
2017-08-16 17:52:19 +02:00
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
dht.workerTokens <- struct{}{}
|
2017-08-16 17:52:19 +02:00
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
<-dht.workerTokens
|
|
|
|
}()
|
2017-08-16 17:52:19 +02:00
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
var data map[string]interface{}
|
|
|
|
err := bencode.DecodeBytes(pkt.data, &data)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error decoding data: %s\n%s", err, pkt.data)
|
|
|
|
return
|
|
|
|
}
|
2017-08-16 17:52:19 +02:00
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
msgType, ok := data[headerTypeField]
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("Decoded data has no message type: %s", data)
|
|
|
|
return
|
|
|
|
}
|
2017-08-16 17:52:19 +02:00
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
switch msgType.(int64) {
|
|
|
|
case requestType:
|
|
|
|
request := Request{
|
|
|
|
ID: data[headerMessageIDField].(string),
|
|
|
|
NodeID: data[headerNodeIDField].(string),
|
|
|
|
Method: data[headerPayloadField].(string),
|
|
|
|
Args: getArgs(data[headerArgsField]),
|
|
|
|
}
|
|
|
|
spew.Dump(request)
|
|
|
|
handleRequest(dht, pkt.raddr, request)
|
|
|
|
|
|
|
|
case responseType:
|
|
|
|
response := Response{
|
|
|
|
ID: data[headerMessageIDField].(string),
|
|
|
|
NodeID: data[headerNodeIDField].(string),
|
|
|
|
Response: data[headerPayloadField].(string),
|
|
|
|
}
|
|
|
|
handleResponse(dht, pkt.raddr, response)
|
|
|
|
|
|
|
|
case errorType:
|
|
|
|
e := Error{
|
|
|
|
ID: data[headerMessageIDField].(string),
|
|
|
|
NodeID: data[headerNodeIDField].(string),
|
|
|
|
ExceptionType: data[headerPayloadField].(string),
|
|
|
|
Response: getArgs(data[headerArgsField]),
|
|
|
|
}
|
|
|
|
handleError(dht, pkt.raddr, e)
|
2017-08-16 17:52:19 +02:00
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
default:
|
|
|
|
log.Errorf("Invalid message type: %s", msgType)
|
|
|
|
return
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
2017-08-16 23:34:53 +02:00
|
|
|
}()
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
func getArgs(argsInt interface{}) (args []string) {
|
|
|
|
if reflect.TypeOf(argsInt).Kind() == reflect.Slice {
|
|
|
|
v := reflect.ValueOf(argsInt)
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
args = append(args, cast.ToString(v.Index(i).Interface()))
|
|
|
|
}
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
2017-08-16 23:34:53 +02:00
|
|
|
return
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleRequest handles the requests received from udp.
|
2017-08-16 23:34:53 +02:00
|
|
|
func handleRequest(dht *DHT, addr *net.UDPAddr, request Request) (success bool) {
|
|
|
|
if request.NodeID == dht.node.id.RawString() {
|
2017-08-16 17:52:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
if len(request.NodeID) != nodeIDLength {
|
|
|
|
send(dht, addr, Error{ID: request.ID, NodeID: dht.node.id.RawString(), Response: []string{"Invalid ID"}})
|
2017-08-16 17:52:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
if no, ok := dht.routingTable.GetNodeByAddress(addr.String()); ok && no.id.RawString() != request.NodeID {
|
2017-08-16 17:52:19 +02:00
|
|
|
dht.routingTable.RemoveByAddr(addr.String())
|
2017-08-16 23:34:53 +02:00
|
|
|
send(dht, addr, Error{ID: request.ID, NodeID: dht.node.id.RawString(), Response: []string{"Invalid ID"}})
|
2017-08-16 17:52:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
switch request.Method {
|
|
|
|
case pingMethod:
|
|
|
|
send(dht, addr, Response{ID: request.ID, NodeID: dht.node.id.RawString(), Response: "pong"})
|
|
|
|
case findNodeMethod:
|
|
|
|
if len(request.Args) < 1 {
|
|
|
|
send(dht, addr, Error{ID: request.ID, NodeID: dht.node.id.RawString(), Response: []string{"No target"}})
|
2017-08-16 17:56:35 +02:00
|
|
|
return
|
|
|
|
}
|
2017-08-16 17:52:19 +02:00
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
target := request.Args[0]
|
2017-08-16 17:59:03 +02:00
|
|
|
if len(target) != nodeIDLength {
|
2017-08-16 23:34:53 +02:00
|
|
|
send(dht, addr, Error{ID: request.ID, NodeID: dht.node.id.RawString(), Response: []string{"Invalid target"}})
|
2017-08-16 17:56:35 +02:00
|
|
|
return
|
|
|
|
}
|
2017-08-16 17:52:19 +02:00
|
|
|
|
2017-08-16 17:56:35 +02:00
|
|
|
var nodes string
|
|
|
|
targetID := newBitmapFromString(target)
|
2017-08-16 17:52:19 +02:00
|
|
|
|
2017-08-16 17:56:35 +02:00
|
|
|
no, _ := dht.routingTable.GetNodeKBucktByID(targetID)
|
|
|
|
if no != nil {
|
|
|
|
nodes = no.CompactNodeInfo()
|
|
|
|
} else {
|
2017-08-16 23:34:53 +02:00
|
|
|
nodes = strings.Join(dht.routingTable.GetNeighborCompactInfos(targetID, dht.K), "")
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
send(dht, addr, Response{ID: request.ID, NodeID: dht.node.id.RawString(), Response: nodes})
|
2017-08-16 17:52:19 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
// send(dht, addr, makeError(t, protocolError, "invalid q"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
no, _ := newNode(request.NodeID, addr.Network(), addr.String())
|
2017-08-16 17:52:19 +02:00
|
|
|
dht.routingTable.Insert(no)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// findOn puts nodes in the response to the routingTable, then if target is in
|
|
|
|
// the nodes or all nodes are in the routingTable, it stops. Otherwise it
|
|
|
|
// continues to findNode or getPeers.
|
2017-08-16 23:34:53 +02:00
|
|
|
func findOn(dht *DHT, nodes string, target *bitmap, queryType string) error {
|
2017-08-16 17:59:03 +02:00
|
|
|
if len(nodes)%compactNodeInfoLength != 0 {
|
|
|
|
return fmt.Errorf("the length of nodes should can be divided by %d", compactNodeInfoLength)
|
2017-08-16 17:52:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hasNew, found := false, false
|
2017-08-16 17:59:03 +02:00
|
|
|
for i := 0; i < len(nodes)/compactNodeInfoLength; i++ {
|
2017-08-16 17:52:19 +02:00
|
|
|
no, _ := newNodeFromCompactInfo(
|
2017-08-16 17:59:03 +02:00
|
|
|
string(nodes[i*compactNodeInfoLength:(i+1)*compactNodeInfoLength]), dht.Network)
|
2017-08-16 17:52:19 +02:00
|
|
|
|
|
|
|
if no.id.RawString() == target.RawString() {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if dht.routingTable.Insert(no) {
|
|
|
|
hasNew = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found || !hasNew {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
targetID := target.RawString()
|
|
|
|
for _, no := range dht.routingTable.GetNeighbors(target, dht.K) {
|
|
|
|
switch queryType {
|
2017-08-16 23:34:53 +02:00
|
|
|
case findNodeMethod:
|
2017-08-16 17:52:19 +02:00
|
|
|
dht.transactionManager.findNode(no, targetID)
|
|
|
|
default:
|
|
|
|
panic("invalid find type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleResponse handles responses received from udp.
|
2017-08-16 23:34:53 +02:00
|
|
|
func handleResponse(dht *DHT, addr *net.UDPAddr, response Response) (success bool) {
|
|
|
|
trans := dht.transactionManager.filterOne(response.ID, addr)
|
2017-08-16 17:52:19 +02:00
|
|
|
if trans == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If response's node id is not the same with the node id in the
|
|
|
|
// transaction, raise error.
|
2017-08-16 23:34:53 +02:00
|
|
|
// TODO: is this necessary??? why??
|
|
|
|
if trans.node.id != nil && trans.node.id.RawString() != response.NodeID {
|
2017-08-16 17:52:19 +02:00
|
|
|
dht.routingTable.RemoveByAddr(addr.String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
node, err := newNode(response.NodeID, addr.Network(), addr.String())
|
2017-08-16 17:52:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:34:53 +02:00
|
|
|
switch trans.request.Method {
|
|
|
|
case pingMethod:
|
|
|
|
case findNodeMethod:
|
|
|
|
target := trans.request.Args[0]
|
|
|
|
if findOn(dht, response.Response, newBitmapFromString(target), findNodeMethod) != nil {
|
2017-08-16 17:52:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// inform transManager to delete transaction.
|
|
|
|
trans.response <- struct{}{}
|
|
|
|
|
|
|
|
dht.routingTable.Insert(node)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleError handles errors received from udp.
|
2017-08-16 23:34:53 +02:00
|
|
|
func handleError(dht *DHT, addr *net.UDPAddr, e Error) (success bool) {
|
|
|
|
if trans := dht.transactionManager.filterOne(e.ID, addr); trans != nil {
|
2017-08-16 17:52:19 +02:00
|
|
|
trans.response <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|