entries.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 UDP port of the node.
  52. type UDP uint16
  53. func (v UDP) ENRKey() string { return "udp" }
  54. // ID is the "id" key, which holds the name of the identity scheme.
  55. type ID string
  56. const IDv4 = ID("v4") // the default identity scheme
  57. func (v ID) ENRKey() string { return "id" }
  58. // IP is the "ip" key, which holds the IP address of the node.
  59. type IP net.IP
  60. func (v IP) ENRKey() string { return "ip" }
  61. // EncodeRLP implements rlp.Encoder.
  62. func (v IP) EncodeRLP(w io.Writer) error {
  63. if ip4 := net.IP(v).To4(); ip4 != nil {
  64. return rlp.Encode(w, ip4)
  65. }
  66. return rlp.Encode(w, net.IP(v))
  67. }
  68. // DecodeRLP implements rlp.Decoder.
  69. func (v *IP) DecodeRLP(s *rlp.Stream) error {
  70. if err := s.Decode((*net.IP)(v)); err != nil {
  71. return err
  72. }
  73. if len(*v) != 4 && len(*v) != 16 {
  74. return fmt.Errorf("invalid IP address, want 4 or 16 bytes: %v", *v)
  75. }
  76. return nil
  77. }
  78. // KeyError is an error related to a key.
  79. type KeyError struct {
  80. Key string
  81. Err error
  82. }
  83. // Error implements error.
  84. func (err *KeyError) Error() string {
  85. if err.Err == errNotFound {
  86. return fmt.Sprintf("missing ENR key %q", err.Key)
  87. }
  88. return fmt.Sprintf("ENR key %q: %v", err.Key, err.Err)
  89. }
  90. // IsNotFound reports whether the given error means that a key/value pair is
  91. // missing from a record.
  92. func IsNotFound(err error) bool {
  93. kerr, ok := err.(*KeyError)
  94. return ok && kerr.Err == errNotFound
  95. }