aboutsummaryrefslogtreecommitdiff
path: root/dht/krpc.go
blob: bf66e20e7b141f79092e772da2fe551a99de3928 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package dht

import (
	"errors"
	"fmt"
	"math/rand"
	"net"
	"strconv"
)

const transIDBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func newTransactionID() string {
	b := make([]byte, 2)
	for i := range b {
		b[i] = transIDBytes[rand.Int63()%int64(len(transIDBytes))]
	}
	return string(b)
}

// 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,
	}
}

// makeResponse returns a response-formed data.
func makeResponse(t string, r map[string]interface{}) map[string]interface{} {
	return map[string]interface{}{
		"t": t,
		"y": "r",
		"r": r,
	}
}

func getStringKey(data map[string]interface{}, key string) (string, error) {
	val, ok := data[key]
	if !ok {
		return "", fmt.Errorf("krpc: missing key %s", key)
	}
	out, ok := val.(string)
	if !ok {
		return "", fmt.Errorf("krpc: key type mismatch")
	}
	return out, nil
}

func getMapKey(data map[string]interface{}, key string) (map[string]interface{}, error) {
	val, ok := data[key]
	if !ok {
		return nil, fmt.Errorf("krpc: missing key %s", key)
	}
	out, ok := val.(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("krpc: key type mismatch")
	}
	return out, nil
}

func getListKey(data map[string]interface{}, key string) ([]interface{}, error) {
	val, ok := data[key]
	if !ok {
		return nil, fmt.Errorf("krpc: missing key %s", key)
	}
	out, ok := val.([]interface{})
	if !ok {
		return nil, fmt.Errorf("krpc: key type mismatch")
	}
	return out, nil
}

// parseKeys parses keys. It just wraps parseKey.
func checkKeys(data map[string]interface{}, pairs [][]string) (err error) {
	for _, args := range pairs {
		key, t := args[0], args[1]
		if err = checkKey(data, key, t); err != nil {
			break
		}
	}
	return err
}

// parseKey parses the key in dict data. `t` is type of the keyed value.
// It's one of "int", "string", "map", "list".
func checkKey(data map[string]interface{}, key string, t string) error {
	val, ok := data[key]
	if !ok {
		return fmt.Errorf("krpc: missing key %s", key)
	}

	switch t {
	case "string":
		_, ok = val.(string)
	case "int":
		_, ok = val.(int)
	case "map":
		_, ok = val.(map[string]interface{})
	case "list":
		_, ok = val.([]interface{})
	default:
		return errors.New("krpc: invalid type")
	}

	if !ok {
		return errors.New("krpc: key type mismatch")
	}

	return nil
}

// Swiped from nictuku
func decodeCompactNodeAddr(cni string) string {
	if len(cni) == 6 {
		return fmt.Sprintf("%d.%d.%d.%d:%d", cni[0], cni[1], cni[2], cni[3], (uint16(cni[4])<<8)|uint16(cni[5]))
	} else if len(cni) == 18 {
		b := []byte(cni[:16])
		return fmt.Sprintf("[%s]:%d", net.IP.String(b), (uint16(cni[16])<<8)|uint16(cni[17]))
	} else {
		return ""
	}
}

func encodeCompactNodeAddr(addr string) string {
	var a []uint8
	host, port, _ := net.SplitHostPort(addr)
	ip := net.ParseIP(host)
	if ip == nil {
		return ""
	}
	aa, _ := strconv.ParseUint(port, 10, 16)
	c := uint16(aa)
	if ip2 := net.IP.To4(ip); ip2 != nil {
		a = make([]byte, net.IPv4len+2, net.IPv4len+2)
		copy(a, ip2[0:net.IPv4len]) // ignore bytes IPv6 bytes if it's IPv4.
		a[4] = byte(c >> 8)
		a[5] = byte(c)
	} else {
		a = make([]byte, net.IPv6len+2, net.IPv6len+2)
		copy(a, ip)
		a[16] = byte(c >> 8)
		a[17] = byte(c)
	}
	return string(a)
}

func int2bytes(val int64) []byte {
	data, j := make([]byte, 8), -1
	for i := 0; i < 8; i++ {
		shift := uint64((7 - i) * 8)
		data[i] = byte((val & (0xff << shift)) >> shift)

		if j == -1 && data[i] != 0 {
			j = i
		}
	}

	if j != -1 {
		return data[j:]
	}
	return data[:1]
}