gitee.com/liu-zhao234568/cntest@v1.0.0/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 "gitee.com/liu-zhao234568/cntest/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 IPv6-specific UDP port of the node. 64 type TCP6 uint16 65 66 func (v TCP6) ENRKey() string { return "tcp6" } 67 68 // UDP is the "udp" key, which holds the UDP port of the node. 69 type UDP uint16 70 71 func (v UDP) ENRKey() string { return "udp" } 72 73 // UDP is the "udp" key, which holds the IPv6-specific UDP port of the node. 74 type UDP6 uint16 75 76 func (v UDP6) ENRKey() string { return "udp6" } 77 78 // ID is the "id" key, which holds the name of the identity scheme. 79 type ID string 80 81 const IDv4 = ID("v4") // the default identity scheme 82 83 func (v ID) ENRKey() string { return "id" } 84 85 // IP is either the "ip" or "ip6" key, depending on the value. 86 // Use this value to encode IP addresses that can be either v4 or v6. 87 // To load an address from a record use the IPv4 or IPv6 types. 88 type IP net.IP 89 90 func (v IP) ENRKey() string { 91 if net.IP(v).To4() == nil { 92 return "ip6" 93 } 94 return "ip" 95 } 96 97 // EncodeRLP implements rlp.Encoder. 98 func (v IP) EncodeRLP(w io.Writer) error { 99 if ip4 := net.IP(v).To4(); ip4 != nil { 100 return rlp.Encode(w, ip4) 101 } 102 if ip6 := net.IP(v).To16(); ip6 != nil { 103 return rlp.Encode(w, ip6) 104 } 105 return fmt.Errorf("invalid IP address: %v", net.IP(v)) 106 } 107 108 // DecodeRLP implements rlp.Decoder. 109 func (v *IP) DecodeRLP(s *rlp.Stream) error { 110 if err := s.Decode((*net.IP)(v)); err != nil { 111 return err 112 } 113 if len(*v) != 4 && len(*v) != 16 { 114 return fmt.Errorf("invalid IP address, want 4 or 16 bytes: %v", *v) 115 } 116 return nil 117 } 118 119 // IPv4 is the "ip" key, which holds the IP address of the node. 120 type IPv4 net.IP 121 122 func (v IPv4) ENRKey() string { return "ip" } 123 124 // EncodeRLP implements rlp.Encoder. 125 func (v IPv4) EncodeRLP(w io.Writer) error { 126 ip4 := net.IP(v).To4() 127 if ip4 == nil { 128 return fmt.Errorf("invalid IPv4 address: %v", net.IP(v)) 129 } 130 return rlp.Encode(w, ip4) 131 } 132 133 // DecodeRLP implements rlp.Decoder. 134 func (v *IPv4) DecodeRLP(s *rlp.Stream) error { 135 if err := s.Decode((*net.IP)(v)); err != nil { 136 return err 137 } 138 if len(*v) != 4 { 139 return fmt.Errorf("invalid IPv4 address, want 4 bytes: %v", *v) 140 } 141 return nil 142 } 143 144 // IPv6 is the "ip6" key, which holds the IP address of the node. 145 type IPv6 net.IP 146 147 func (v IPv6) ENRKey() string { return "ip6" } 148 149 // EncodeRLP implements rlp.Encoder. 150 func (v IPv6) EncodeRLP(w io.Writer) error { 151 ip6 := net.IP(v).To16() 152 if ip6 == nil { 153 return fmt.Errorf("invalid IPv6 address: %v", net.IP(v)) 154 } 155 return rlp.Encode(w, ip6) 156 } 157 158 // DecodeRLP implements rlp.Decoder. 159 func (v *IPv6) DecodeRLP(s *rlp.Stream) error { 160 if err := s.Decode((*net.IP)(v)); err != nil { 161 return err 162 } 163 if len(*v) != 16 { 164 return fmt.Errorf("invalid IPv6 address, want 16 bytes: %v", *v) 165 } 166 return nil 167 } 168 169 // KeyError is an error related to a key. 170 type KeyError struct { 171 Key string 172 Err error 173 } 174 175 // Error implements error. 176 func (err *KeyError) Error() string { 177 if err.Err == errNotFound { 178 return fmt.Sprintf("missing ENR key %q", err.Key) 179 } 180 return fmt.Sprintf("ENR key %q: %v", err.Key, err.Err) 181 } 182 183 // IsNotFound reports whether the given error means that a key/value pair is 184 // missing from a record. 185 func IsNotFound(err error) bool { 186 kerr, ok := err.(*KeyError) 187 return ok && kerr.Err == errNotFound 188 }