/
packets.go
220 lines (190 loc) · 5.41 KB
/
packets.go
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package discover
import (
"net"
"github.com/kooksee/uspnet/rlp"
"time"
"github.com/kooksee/uspnet/crypto"
"github.com/kooksee/uspnet/p2p/netutil"
"github.com/kataras/iris/core/errors"
)
var PacketManager *Packets
func init() {
PacketManager = &Packets{}
PacketManager.Add(&ping{})
PacketManager.Add(&pong{})
PacketManager.Add(&findNode{})
PacketManager.Add(&neighbors{})
}
type Packets struct {
pmap map[byte]IPacket
}
func (t *Packets) Add(p IPacket) error {
if _, ok := t.pmap[p.Type()]; ok {
return errors.New("已经存在")
}
t.pmap[p.Type()] = p
return nil
}
func (t *Packets) Packet(id byte) IPacket {
if p, ok := t.pmap[id]; ok {
return p
}
return nil
}
// RPC request structures
type (
// 服务查找请求
queryReq struct {
SId []byte
SName []byte
SNode rpcEndpoint
Expiration uint64
}
// 服务定义
service struct {
name string
id string
desc string
tags map[string]interface{}
nid string
}
// 服务查询结果
queryResp struct {
Id []byte
Services map[string]service
}
// 多播用于服务之间的通信
// 中继用于信息的传递,把消息传递给指定的节点,最终目标节点收到消息
// 当本网络能够访问目标网络的时候,那么直接发送给目标网络,当本网络无法直接访问的时候,或者访问比较慢的时候
// 那么就把信息发送给其他的节点,然后中继传播
Multicast struct {
*Packet
To []rpcEndpoint
}
// 广播针对所有节点的通知,把消息传递给每一个节点,最终所有节点信息一致
Broadcast struct {
*Packet
}
// 单播用于一个节点的通信
Unicast struct {
*Packet
To rpcEndpoint
}
ping struct {
*Packet
Version uint
To rpcEndpoint
}
// pong is the reply to ping.
pong struct {
// This field should mirror the UDP envelope address
// of the ping packet, which provides a way to discover the
// the external address (after NAT).
To rpcEndpoint
ReplyTok []byte // This contains the hash of the ping packet.
Expiration uint64 // Absolute timestamp at which the packet becomes invalid.
// Ignore additional fields (for forward compatibility).
Rest []rlp.RawValue `rlp:"tail"`
}
// findnode is a query for nodes close to the given target.
findNode struct {
Target NodeID // doesn't need to be an actual public key
Expiration uint64
// Ignore additional fields (for forward compatibility).
Rest []rlp.RawValue `rlp:"tail"`
}
// reply to findnode
neighbors struct {
Nodes []rpcNode
Expiration uint64
// Ignore additional fields (for forward compatibility).
Rest []rlp.RawValue `rlp:"tail"`
}
rpcNode struct {
IP net.IP // len 4 for IPv4 or 16 for IPv6
UDP uint16 // for discovery protocol
ID NodeID
}
rpcEndpoint struct {
IP net.IP // len 4 for IPv4 or 16 for IPv6
UDP uint16 // for discovery protocol
}
)
func (n rpcEndpoint) addr() *net.UDPAddr {
return &net.UDPAddr{IP: n.IP, Port: int(n.UDP)}
}
func (req *findNode) Id() byte { return 0x1 }
func (req *neighbors) Id() byte { return 0x2 }
func (req *ping) Id() byte { return 0x3 }
func (req *pong) Id() byte { return 0x4 }
func (req *findNode) String() string { return "findnode" }
func (req *neighbors) String() string { return "neighbors" }
func (req *ping) String() string { return "ping" }
func (req *pong) String() string { return "pong" }
func (req *neighbors) OnHandle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
if expired(req.Expiration) {
return errExpired
}
if !t.handleReply(fromID, req.Id(), req) {
return errUnsolicitedReply
}
return nil
}
func (req *ping) OnHandle(t *KRpc, tx *Tx) error {
if expired(req.Expiration()) {
return errExpired
}
//t.Multicast()
t.send(from, &pong{
To: makeEndpoint(from),
ReplyTok: mac,
Expiration: uint64(time.Now().Add(expiration).Unix()),
})
if !t.handleReply(fromID, req.Id(), req) {
// Note: we're ignoring the provided IP address right now
go t.bond(true, fromID, from)
}
return nil
}
func (req *pong) OnHandle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
if expired(req.Expiration) {
return errExpired
}
if !t.handleReply(fromID, req.Id(), req) {
return errUnsolicitedReply
}
return nil
}
func (req *findNode) OnHandle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
if expired(req.Expiration) {
return errExpired
}
if t.db.node(fromID) == nil {
// No bond exists, we don't process the packet. This prevents
// an attack vector where the discovery protocol could be used
// to amplify traffic in a DDOS attack. A malicious actor
// would send a findnode request with the IP address and UDP
// port of the target as the source address. The recipient of
// the findnode packet would then send a neighbors packet
// (which is a much bigger packet than findnode) to the victim.
return errUnknownNode
}
target := crypto.Keccak256Hash(req.Target[:])
t.mutex.Lock()
closest := t.closest(target, bucketSize).entries
t.mutex.Unlock()
p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())}
// Send neighbors in chunks with at most maxNeighbors per packet
// to stay below the 1280 byte limit.
for i, n := range closest {
if netutil.CheckRelayIP(from.IP, n.IP) != nil {
continue
}
p.Nodes = append(p.Nodes, nodeToRPC(n))
if len(p.Nodes) == maxNeighbors || i == len(closest)-1 {
t.send(from, &p)
p.Nodes = p.Nodes[:0]
}
}
return nil
}