github.com/ebceco/ebc@v1.8.19-0.20190309150932-8cb0b9e06484/p2p/enr/entries.go (about) 1 // Copyright 2017 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 enr 18 19 import ( 20 "fmt" 21 "io" 22 "net" 23 24 "github.com/ebceco/ebc/rlp" 25 ) 26 27 // Entry is implemented by known node record entry types. 28 // 29 // To define a new entry that is to be included in a node record, 30 // create a Go type that satisfies this interface. The type should 31 // also implement rlp.Decoder if additional checks are needed on the value. 32 type Entry interface { 33 ENRKey() string 34 } 35 36 type generic struct { 37 key string 38 value interface{} 39 } 40 41 func (g generic) ENRKey() string { return g.key } 42 43 func (g generic) EncodeRLP(w io.Writer) error { 44 return rlp.Encode(w, g.value) 45 } 46 47 func (g *generic) DecodeRLP(s *rlp.Stream) error { 48 return s.Decode(g.value) 49 } 50 51 // WithEntry wraps any value with a key name. It can be used to set and load arbitrary values 52 // in a record. The value v must be supported by rlp. To use WithEntry with Load, the value 53 // must be a pointer. 54 func WithEntry(k string, v interface{}) Entry { 55 return &generic{key: k, value: v} 56 } 57 58 // TCP is the "tcp" key, which holds the TCP port of the node. 59 type TCP uint16 60 61 func (v TCP) ENRKey() string { return "tcp" } 62 63 // UDP is the "udp" key, which holds the UDP port of the node. 64 type UDP uint16 65 66 func (v UDP) ENRKey() string { return "udp" } 67 68 // ID is the "id" key, which holds the name of the identity scheme. 69 type ID string 70 71 const IDv4 = ID("v4") // the default identity scheme 72 73 func (v ID) ENRKey() string { return "id" } 74 75 // IP is the "ip" key, which holds the IP address of the node. 76 type IP net.IP 77 78 func (v IP) ENRKey() string { return "ip" } 79 80 // EncodeRLP implements rlp.Encoder. 81 func (v IP) EncodeRLP(w io.Writer) error { 82 if ip4 := net.IP(v).To4(); ip4 != nil { 83 return rlp.Encode(w, ip4) 84 } 85 return rlp.Encode(w, net.IP(v)) 86 } 87 88 // DecodeRLP implements rlp.Decoder. 89 func (v *IP) DecodeRLP(s *rlp.Stream) error { 90 if err := s.Decode((*net.IP)(v)); err != nil { 91 return err 92 } 93 if len(*v) != 4 && len(*v) != 16 { 94 return fmt.Errorf("invalid IP address, want 4 or 16 bytes: %v", *v) 95 } 96 return nil 97 } 98 99 // KeyError is an error related to a key. 100 type KeyError struct { 101 Key string 102 Err error 103 } 104 105 // Error implements error. 106 func (err *KeyError) Error() string { 107 if err.Err == errNotFound { 108 return fmt.Sprintf("missing ENR key %q", err.Key) 109 } 110 return fmt.Sprintf("ENR key %q: %v", err.Key, err.Err) 111 } 112 113 // IsNotFound reports whether the given error means that a key/value pair is 114 // missing from a record. 115 func IsNotFound(err error) bool { 116 kerr, ok := err.(*KeyError) 117 return ok && kerr.Err == errNotFound 118 }