github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/p2p/discv5/udp.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package discv5 19 20 import ( 21 "bytes" 22 "crypto/ecdsa" 23 "errors" 24 "fmt" 25 "net" 26 "time" 27 28 "github.com/AigarNetwork/aigar/common" 29 "github.com/AigarNetwork/aigar/crypto" 30 "github.com/AigarNetwork/aigar/log" 31 "github.com/AigarNetwork/aigar/p2p/nat" 32 "github.com/AigarNetwork/aigar/p2p/netutil" 33 "github.com/AigarNetwork/aigar/rlp" 34 ) 35 36 const Version = 4 37 38 // Errors 39 var ( 40 errPacketTooSmall = errors.New("too small") 41 errBadPrefix = errors.New("bad prefix") 42 errTimeout = errors.New("RPC timeout") 43 ) 44 45 // Timeouts 46 const ( 47 respTimeout = 500 * time.Millisecond 48 expiration = 20 * time.Second 49 50 driftThreshold = 10 * time.Second // Allowed clock drift before warning user 51 ) 52 53 // RPC request structures 54 type ( 55 ping struct { 56 Version uint 57 From, To rpcEndpoint 58 Expiration uint64 59 60 // v5 61 Topics []Topic 62 63 // Ignore additional fields (for forward compatibility). 64 Rest []rlp.RawValue `rlp:"tail"` 65 } 66 67 // pong is the reply to ping. 68 pong struct { 69 // This field should mirror the UDP envelope address 70 // of the ping packet, which provides a way to discover the 71 // the external address (after NAT). 72 To rpcEndpoint 73 74 ReplyTok []byte // This contains the hash of the ping packet. 75 Expiration uint64 // Absolute timestamp at which the packet becomes invalid. 76 77 // v5 78 TopicHash common.Hash 79 TicketSerial uint32 80 WaitPeriods []uint32 81 82 // Ignore additional fields (for forward compatibility). 83 Rest []rlp.RawValue `rlp:"tail"` 84 } 85 86 // findnode is a query for nodes close to the given target. 87 findnode struct { 88 Target NodeID // doesn't need to be an actual public key 89 Expiration uint64 90 // Ignore additional fields (for forward compatibility). 91 Rest []rlp.RawValue `rlp:"tail"` 92 } 93 94 // findnode is a query for nodes close to the given target. 95 findnodeHash struct { 96 Target common.Hash 97 Expiration uint64 98 // Ignore additional fields (for forward compatibility). 99 Rest []rlp.RawValue `rlp:"tail"` 100 } 101 102 // reply to findnode 103 neighbors struct { 104 Nodes []rpcNode 105 Expiration uint64 106 // Ignore additional fields (for forward compatibility). 107 Rest []rlp.RawValue `rlp:"tail"` 108 } 109 110 topicRegister struct { 111 Topics []Topic 112 Idx uint 113 Pong []byte 114 } 115 116 topicQuery struct { 117 Topic Topic 118 Expiration uint64 119 } 120 121 // reply to topicQuery 122 topicNodes struct { 123 Echo common.Hash 124 Nodes []rpcNode 125 } 126 127 rpcNode struct { 128 IP net.IP // len 4 for IPv4 or 16 for IPv6 129 UDP uint16 // for discovery protocol 130 TCP uint16 // for RLPx protocol 131 ID NodeID 132 } 133 134 rpcEndpoint struct { 135 IP net.IP // len 4 for IPv4 or 16 for IPv6 136 UDP uint16 // for discovery protocol 137 TCP uint16 // for RLPx protocol 138 } 139 ) 140 141 var ( 142 versionPrefix = []byte("temporary discovery v5") 143 versionPrefixSize = len(versionPrefix) 144 sigSize = 520 / 8 145 headSize = versionPrefixSize + sigSize // space of packet frame data 146 ) 147 148 // Neighbors replies are sent across multiple packets to 149 // stay below the 1280 byte limit. We compute the maximum number 150 // of entries by stuffing a packet until it grows too large. 151 var maxNeighbors = func() int { 152 p := neighbors{Expiration: ^uint64(0)} 153 maxSizeNode := rpcNode{IP: make(net.IP, 16), UDP: ^uint16(0), TCP: ^uint16(0)} 154 for n := 0; ; n++ { 155 p.Nodes = append(p.Nodes, maxSizeNode) 156 size, _, err := rlp.EncodeToReader(p) 157 if err != nil { 158 // If this ever happens, it will be caught by the unit tests. 159 panic("cannot encode: " + err.Error()) 160 } 161 if headSize+size+1 >= 1280 { 162 return n 163 } 164 } 165 }() 166 167 var maxTopicNodes = func() int { 168 p := topicNodes{} 169 maxSizeNode := rpcNode{IP: make(net.IP, 16), UDP: ^uint16(0), TCP: ^uint16(0)} 170 for n := 0; ; n++ { 171 p.Nodes = append(p.Nodes, maxSizeNode) 172 size, _, err := rlp.EncodeToReader(p) 173 if err != nil { 174 // If this ever happens, it will be caught by the unit tests. 175 panic("cannot encode: " + err.Error()) 176 } 177 if headSize+size+1 >= 1280 { 178 return n 179 } 180 } 181 }() 182 183 func makeEndpoint(addr *net.UDPAddr, tcpPort uint16) rpcEndpoint { 184 ip := addr.IP.To4() 185 if ip == nil { 186 ip = addr.IP.To16() 187 } 188 return rpcEndpoint{IP: ip, UDP: uint16(addr.Port), TCP: tcpPort} 189 } 190 191 func (e1 rpcEndpoint) equal(e2 rpcEndpoint) bool { 192 return e1.UDP == e2.UDP && e1.TCP == e2.TCP && e1.IP.Equal(e2.IP) 193 } 194 195 func nodeFromRPC(sender *net.UDPAddr, rn rpcNode) (*Node, error) { 196 if err := netutil.CheckRelayIP(sender.IP, rn.IP); err != nil { 197 return nil, err 198 } 199 n := NewNode(rn.ID, rn.IP, rn.UDP, rn.TCP) 200 err := n.validateComplete() 201 return n, err 202 } 203 204 func nodeToRPC(n *Node) rpcNode { 205 return rpcNode{ID: n.ID, IP: n.IP, UDP: n.UDP, TCP: n.TCP} 206 } 207 208 type ingressPacket struct { 209 remoteID NodeID 210 remoteAddr *net.UDPAddr 211 ev nodeEvent 212 hash []byte 213 data interface{} // one of the RPC structs 214 rawData []byte 215 } 216 217 type conn interface { 218 ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) 219 WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error) 220 Close() error 221 LocalAddr() net.Addr 222 } 223 224 // udp implements the RPC protocol. 225 type udp struct { 226 conn conn 227 priv *ecdsa.PrivateKey 228 ourEndpoint rpcEndpoint 229 nat nat.Interface 230 net *Network 231 } 232 233 // ListenUDP returns a new table that listens for UDP packets on laddr. 234 func ListenUDP(priv *ecdsa.PrivateKey, conn conn, nodeDBPath string, netrestrict *netutil.Netlist) (*Network, error) { 235 realaddr := conn.LocalAddr().(*net.UDPAddr) 236 transport, err := listenUDP(priv, conn, realaddr) 237 if err != nil { 238 return nil, err 239 } 240 net, err := newNetwork(transport, priv.PublicKey, nodeDBPath, netrestrict) 241 if err != nil { 242 return nil, err 243 } 244 log.Info("UDP listener up", "net", net.tab.self) 245 transport.net = net 246 go transport.readLoop() 247 return net, nil 248 } 249 250 func listenUDP(priv *ecdsa.PrivateKey, conn conn, realaddr *net.UDPAddr) (*udp, error) { 251 return &udp{conn: conn, priv: priv, ourEndpoint: makeEndpoint(realaddr, uint16(realaddr.Port))}, nil 252 } 253 254 func (t *udp) localAddr() *net.UDPAddr { 255 return t.conn.LocalAddr().(*net.UDPAddr) 256 } 257 258 func (t *udp) Close() { 259 t.conn.Close() 260 } 261 262 func (t *udp) send(remote *Node, ptype nodeEvent, data interface{}) (hash []byte) { 263 hash, _ = t.sendPacket(remote.ID, remote.addr(), byte(ptype), data) 264 return hash 265 } 266 267 func (t *udp) sendPing(remote *Node, toaddr *net.UDPAddr, topics []Topic) (hash []byte) { 268 hash, _ = t.sendPacket(remote.ID, toaddr, byte(pingPacket), ping{ 269 Version: Version, 270 From: t.ourEndpoint, 271 To: makeEndpoint(toaddr, uint16(toaddr.Port)), // TODO: maybe use known TCP port from DB 272 Expiration: uint64(time.Now().Add(expiration).Unix()), 273 Topics: topics, 274 }) 275 return hash 276 } 277 278 func (t *udp) sendFindnode(remote *Node, target NodeID) { 279 t.sendPacket(remote.ID, remote.addr(), byte(findnodePacket), findnode{ 280 Target: target, 281 Expiration: uint64(time.Now().Add(expiration).Unix()), 282 }) 283 } 284 285 func (t *udp) sendNeighbours(remote *Node, results []*Node) { 286 // Send neighbors in chunks with at most maxNeighbors per packet 287 // to stay below the 1280 byte limit. 288 p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())} 289 for i, result := range results { 290 p.Nodes = append(p.Nodes, nodeToRPC(result)) 291 if len(p.Nodes) == maxNeighbors || i == len(results)-1 { 292 t.sendPacket(remote.ID, remote.addr(), byte(neighborsPacket), p) 293 p.Nodes = p.Nodes[:0] 294 } 295 } 296 } 297 298 func (t *udp) sendFindnodeHash(remote *Node, target common.Hash) { 299 t.sendPacket(remote.ID, remote.addr(), byte(findnodeHashPacket), findnodeHash{ 300 Target: target, 301 Expiration: uint64(time.Now().Add(expiration).Unix()), 302 }) 303 } 304 305 func (t *udp) sendTopicRegister(remote *Node, topics []Topic, idx int, pong []byte) { 306 t.sendPacket(remote.ID, remote.addr(), byte(topicRegisterPacket), topicRegister{ 307 Topics: topics, 308 Idx: uint(idx), 309 Pong: pong, 310 }) 311 } 312 313 func (t *udp) sendTopicNodes(remote *Node, queryHash common.Hash, nodes []*Node) { 314 p := topicNodes{Echo: queryHash} 315 var sent bool 316 for _, result := range nodes { 317 if result.IP.Equal(t.net.tab.self.IP) || netutil.CheckRelayIP(remote.IP, result.IP) == nil { 318 p.Nodes = append(p.Nodes, nodeToRPC(result)) 319 } 320 if len(p.Nodes) == maxTopicNodes { 321 t.sendPacket(remote.ID, remote.addr(), byte(topicNodesPacket), p) 322 p.Nodes = p.Nodes[:0] 323 sent = true 324 } 325 } 326 if !sent || len(p.Nodes) > 0 { 327 t.sendPacket(remote.ID, remote.addr(), byte(topicNodesPacket), p) 328 } 329 } 330 331 func (t *udp) sendPacket(toid NodeID, toaddr *net.UDPAddr, ptype byte, req interface{}) (hash []byte, err error) { 332 //fmt.Println("sendPacket", nodeEvent(ptype), toaddr.String(), toid.String()) 333 packet, hash, err := encodePacket(t.priv, ptype, req) 334 if err != nil { 335 //fmt.Println(err) 336 return hash, err 337 } 338 log.Trace(fmt.Sprintf(">>> %v to %x@%v", nodeEvent(ptype), toid[:8], toaddr)) 339 if nbytes, err := t.conn.WriteToUDP(packet, toaddr); err != nil { 340 log.Trace(fmt.Sprint("UDP send failed:", err)) 341 } else { 342 egressTrafficMeter.Mark(int64(nbytes)) 343 } 344 //fmt.Println(err) 345 return hash, err 346 } 347 348 // zeroed padding space for encodePacket. 349 var headSpace = make([]byte, headSize) 350 351 func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) (p, hash []byte, err error) { 352 b := new(bytes.Buffer) 353 b.Write(headSpace) 354 b.WriteByte(ptype) 355 if err := rlp.Encode(b, req); err != nil { 356 log.Error(fmt.Sprint("error encoding packet:", err)) 357 return nil, nil, err 358 } 359 packet := b.Bytes() 360 sig, err := crypto.Sign(crypto.Keccak256(packet[headSize:]), priv) 361 if err != nil { 362 log.Error(fmt.Sprint("could not sign packet:", err)) 363 return nil, nil, err 364 } 365 copy(packet, versionPrefix) 366 copy(packet[versionPrefixSize:], sig) 367 hash = crypto.Keccak256(packet[versionPrefixSize:]) 368 return packet, hash, nil 369 } 370 371 // readLoop runs in its own goroutine. it injects ingress UDP packets 372 // into the network loop. 373 func (t *udp) readLoop() { 374 defer t.conn.Close() 375 // Discovery packets are defined to be no larger than 1280 bytes. 376 // Packets larger than this size will be cut at the end and treated 377 // as invalid because their hash won't match. 378 buf := make([]byte, 1280) 379 for { 380 nbytes, from, err := t.conn.ReadFromUDP(buf) 381 ingressTrafficMeter.Mark(int64(nbytes)) 382 if netutil.IsTemporaryError(err) { 383 // Ignore temporary read errors. 384 log.Debug(fmt.Sprintf("Temporary read error: %v", err)) 385 continue 386 } else if err != nil { 387 // Shut down the loop for permament errors. 388 log.Debug(fmt.Sprintf("Read error: %v", err)) 389 return 390 } 391 t.handlePacket(from, buf[:nbytes]) 392 } 393 } 394 395 func (t *udp) handlePacket(from *net.UDPAddr, buf []byte) error { 396 pkt := ingressPacket{remoteAddr: from} 397 if err := decodePacket(buf, &pkt); err != nil { 398 log.Debug(fmt.Sprintf("Bad packet from %v: %v", from, err)) 399 //fmt.Println("bad packet", err) 400 return err 401 } 402 t.net.reqReadPacket(pkt) 403 return nil 404 } 405 406 func decodePacket(buffer []byte, pkt *ingressPacket) error { 407 if len(buffer) < headSize+1 { 408 return errPacketTooSmall 409 } 410 buf := make([]byte, len(buffer)) 411 copy(buf, buffer) 412 prefix, sig, sigdata := buf[:versionPrefixSize], buf[versionPrefixSize:headSize], buf[headSize:] 413 if !bytes.Equal(prefix, versionPrefix) { 414 return errBadPrefix 415 } 416 fromID, err := recoverNodeID(crypto.Keccak256(buf[headSize:]), sig) 417 if err != nil { 418 return err 419 } 420 pkt.rawData = buf 421 pkt.hash = crypto.Keccak256(buf[versionPrefixSize:]) 422 pkt.remoteID = fromID 423 switch pkt.ev = nodeEvent(sigdata[0]); pkt.ev { 424 case pingPacket: 425 pkt.data = new(ping) 426 case pongPacket: 427 pkt.data = new(pong) 428 case findnodePacket: 429 pkt.data = new(findnode) 430 case neighborsPacket: 431 pkt.data = new(neighbors) 432 case findnodeHashPacket: 433 pkt.data = new(findnodeHash) 434 case topicRegisterPacket: 435 pkt.data = new(topicRegister) 436 case topicQueryPacket: 437 pkt.data = new(topicQuery) 438 case topicNodesPacket: 439 pkt.data = new(topicNodes) 440 default: 441 return fmt.Errorf("unknown packet type: %d", sigdata[0]) 442 } 443 s := rlp.NewStream(bytes.NewReader(sigdata[1:]), 0) 444 err = s.Decode(pkt.data) 445 return err 446 }