entries.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. package enr
  17. import (
  18. "errors"
  19. "fmt"
  20. "io"
  21. "net"
  22. "github.com/ethereum/go-ethereum/rlp"
  23. )
  24. // Entry is implemented by known node record entry types.
  25. //
  26. // To define a new entry that is to be included in a node record,
  27. // create a Go type that satisfies this interface. The type should
  28. // also implement rlp.Decoder if additional checks are needed on the value.
  29. type Entry interface {
  30. ENRKey() string
  31. }
  32. type generic struct {
  33. key string
  34. value interface{}
  35. }
  36. func (g generic) ENRKey() string { return g.key }
  37. func (g generic) EncodeRLP(w io.Writer) error {
  38. return rlp.Encode(w, g.value)
  39. }
  40. func (g *generic) DecodeRLP(s *rlp.Stream) error {
  41. return s.Decode(g.value)
  42. }
  43. // WithEntry wraps any value with a key name. It can be used to set and load arbitrary values
  44. // in a record. The value v must be supported by rlp. To use WithEntry with Load, the value
  45. // must be a pointer.
  46. func WithEntry(k string, v interface{}) Entry {
  47. return &generic{key: k, value: v}
  48. }
  49. // TCP is the "tcp" key, which holds the TCP port of the node.
  50. type TCP uint16
  51. func (v TCP) ENRKey() string { return "tcp" }
  52. // UDP is the "udp" key, which holds the IPv6-specific UDP port of the node.
  53. type TCP6 uint16
  54. func (v TCP6) ENRKey() string { return "tcp6" }
  55. // UDP is the "udp" key, which holds the UDP port of the node.
  56. type UDP uint16
  57. func (v UDP) ENRKey() string { return "udp" }
  58. // UDP is the "udp" key, which holds the IPv6-specific UDP port of the node.
  59. type UDP6 uint16
  60. func (v UDP6) ENRKey() string { return "udp6" }
  61. // ID is the "id" key, which holds the name of the identity scheme.
  62. type ID string
  63. const IDv4 = ID("v4") // the default identity scheme
  64. func (v ID) ENRKey() string { return "id" }
  65. // IP is either the "ip" or "ip6" key, depending on the value.
  66. // Use this value to encode IP addresses that can be either v4 or v6.
  67. // To load an address from a record use the IPv4 or IPv6 types.
  68. type IP net.IP
  69. func (v IP) ENRKey() string {
  70. if net.IP(v).To4() == nil {
  71. return "ip6"
  72. }
  73. return "ip"
  74. }
  75. // EncodeRLP implements rlp.Encoder.
  76. func (v IP) EncodeRLP(w io.Writer) error {
  77. if ip4 := net.IP(v).To4(); ip4 != nil {
  78. return rlp.Encode(w, ip4)
  79. }
  80. if ip6 := net.IP(v).To16(); ip6 != nil {
  81. return rlp.Encode(w, ip6)
  82. }
  83. return fmt.Errorf("invalid IP address: %v", net.IP(v))
  84. }
  85. // DecodeRLP implements rlp.Decoder.
  86. func (v *IP) DecodeRLP(s *rlp.Stream) error {
  87. if err := s.Decode((*net.IP)(v)); err != nil {
  88. return err
  89. }
  90. if len(*v) != 4 && len(*v) != 16 {
  91. return fmt.Errorf("invalid IP address, want 4 or 16 bytes: %v", *v)
  92. }
  93. return nil
  94. }
  95. // IPv4 is the "ip" key, which holds the IP address of the node.
  96. type IPv4 net.IP
  97. func (v IPv4) ENRKey() string { return "ip" }
  98. // EncodeRLP implements rlp.Encoder.
  99. func (v IPv4) EncodeRLP(w io.Writer) error {
  100. ip4 := net.IP(v).To4()
  101. if ip4 == nil {
  102. return fmt.Errorf("invalid IPv4 address: %v", net.IP(v))
  103. }
  104. return rlp.Encode(w, ip4)
  105. }
  106. // DecodeRLP implements rlp.Decoder.
  107. func (v *IPv4) DecodeRLP(s *rlp.Stream) error {
  108. if err := s.Decode((*net.IP)(v)); err != nil {
  109. return err
  110. }
  111. if len(*v) != 4 {
  112. return fmt.Errorf("invalid IPv4 address, want 4 bytes: %v", *v)
  113. }
  114. return nil
  115. }
  116. // IPv6 is the "ip6" key, which holds the IP address of the node.
  117. type IPv6 net.IP
  118. func (v IPv6) ENRKey() string { return "ip6" }
  119. // EncodeRLP implements rlp.Encoder.
  120. func (v IPv6) EncodeRLP(w io.Writer) error {
  121. ip6 := net.IP(v).To16()
  122. if ip6 == nil {
  123. return fmt.Errorf("invalid IPv6 address: %v", net.IP(v))
  124. }
  125. return rlp.Encode(w, ip6)
  126. }
  127. // DecodeRLP implements rlp.Decoder.
  128. func (v *IPv6) DecodeRLP(s *rlp.Stream) error {
  129. if err := s.Decode((*net.IP)(v)); err != nil {
  130. return err
  131. }
  132. if len(*v) != 16 {
  133. return fmt.Errorf("invalid IPv6 address, want 16 bytes: %v", *v)
  134. }
  135. return nil
  136. }
  137. // KeyError is an error related to a key.
  138. type KeyError struct {
  139. Key string
  140. Err error
  141. }
  142. // Error implements error.
  143. func (err *KeyError) Error() string {
  144. if err.Err == errNotFound {
  145. return fmt.Sprintf("missing ENR key %q", err.Key)
  146. }
  147. return fmt.Sprintf("ENR key %q: %v", err.Key, err.Err)
  148. }
  149. func (err *KeyError) Unwrap() error {
  150. return err.Err
  151. }
  152. // IsNotFound reports whether the given error means that a key/value pair is
  153. // missing from a record.
  154. func IsNotFound(err error) bool {
  155. var ke *KeyError
  156. if errors.As(err, &ke) {
  157. return ke.Err == errNotFound
  158. }
  159. return false
  160. }