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