entries.go 5.2 KB

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