github.com/Unheilbar/quorum@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 "github.com/ethereum/go-ethereum/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 // Quorum 98 // RaftPort is the "raftport" key, which holds the raftport of the node 99 type RaftPort uint16 100 101 func (v RaftPort) ENRKey() string { return "raftport" } 102 103 type Hostname string 104 105 func (v Hostname) ENRKey() string { return "hostname" } 106 107 // EncodeRLP implements rlp.Encoder. 108 func (v IP) EncodeRLP(w io.Writer) error { 109 if ip4 := net.IP(v).To4(); ip4 != nil { 110 return rlp.Encode(w, ip4) 111 } 112 if ip6 := net.IP(v).To16(); ip6 != nil { 113 return rlp.Encode(w, ip6) 114 } 115 return fmt.Errorf("invalid IP address: %v", net.IP(v)) 116 } 117 118 // DecodeRLP implements rlp.Decoder. 119 func (v *IP) DecodeRLP(s *rlp.Stream) error { 120 if err := s.Decode((*net.IP)(v)); err != nil { 121 return err 122 } 123 if len(*v) != 4 && len(*v) != 16 { 124 return fmt.Errorf("invalid IP address, want 4 or 16 bytes: %v", *v) 125 } 126 return nil 127 } 128 129 // IPv4 is the "ip" key, which holds the IP address of the node. 130 type IPv4 net.IP 131 132 func (v IPv4) ENRKey() string { return "ip" } 133 134 // EncodeRLP implements rlp.Encoder. 135 func (v IPv4) EncodeRLP(w io.Writer) error { 136 ip4 := net.IP(v).To4() 137 if ip4 == nil { 138 return fmt.Errorf("invalid IPv4 address: %v", net.IP(v)) 139 } 140 return rlp.Encode(w, ip4) 141 } 142 143 // DecodeRLP implements rlp.Decoder. 144 func (v *IPv4) DecodeRLP(s *rlp.Stream) error { 145 if err := s.Decode((*net.IP)(v)); err != nil { 146 return err 147 } 148 if len(*v) != 4 { 149 return fmt.Errorf("invalid IPv4 address, want 4 bytes: %v", *v) 150 } 151 return nil 152 } 153 154 // IPv6 is the "ip6" key, which holds the IP address of the node. 155 type IPv6 net.IP 156 157 func (v IPv6) ENRKey() string { return "ip6" } 158 159 // EncodeRLP implements rlp.Encoder. 160 func (v IPv6) EncodeRLP(w io.Writer) error { 161 ip6 := net.IP(v).To16() 162 if ip6 == nil { 163 return fmt.Errorf("invalid IPv6 address: %v", net.IP(v)) 164 } 165 return rlp.Encode(w, ip6) 166 } 167 168 // DecodeRLP implements rlp.Decoder. 169 func (v *IPv6) DecodeRLP(s *rlp.Stream) error { 170 if err := s.Decode((*net.IP)(v)); err != nil { 171 return err 172 } 173 if len(*v) != 16 { 174 return fmt.Errorf("invalid IPv6 address, want 16 bytes: %v", *v) 175 } 176 return nil 177 } 178 179 // KeyError is an error related to a key. 180 type KeyError struct { 181 Key string 182 Err error 183 } 184 185 // Error implements error. 186 func (err *KeyError) Error() string { 187 if err.Err == errNotFound { 188 return fmt.Sprintf("missing ENR key %q", err.Key) 189 } 190 return fmt.Sprintf("ENR key %q: %v", err.Key, err.Err) 191 } 192 193 // IsNotFound reports whether the given error means that a key/value pair is 194 // missing from a record. 195 func IsNotFound(err error) bool { 196 kerr, ok := err.(*KeyError) 197 return ok && kerr.Err == errNotFound 198 }