discover.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2016 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. // Contains all the wrappers from the accounts package to support client side enode
  17. // management on mobile platforms.
  18. package geth
  19. import (
  20. "errors"
  21. "github.com/ethereum/go-ethereum/p2p/discv5"
  22. )
  23. // Enode represents a host on the network.
  24. type Enode struct {
  25. node *discv5.Node
  26. }
  27. // NewEnode parses a node designator.
  28. //
  29. // There are two basic forms of node designators
  30. // - incomplete nodes, which only have the public key (node ID)
  31. // - complete nodes, which contain the public key and IP/Port information
  32. //
  33. // For incomplete nodes, the designator must look like one of these
  34. //
  35. // enode://<hex node id>
  36. // <hex node id>
  37. //
  38. // For complete nodes, the node ID is encoded in the username portion
  39. // of the URL, separated from the host by an @ sign. The hostname can
  40. // only be given as an IP address, DNS domain names are not allowed.
  41. // The port in the host name section is the TCP listening port. If the
  42. // TCP and UDP (discovery) ports differ, the UDP port is specified as
  43. // query parameter "discport".
  44. //
  45. // In the following example, the node URL describes
  46. // a node with IP address 10.3.58.6, TCP listening port 30303
  47. // and UDP discovery port 30301.
  48. //
  49. // enode://<hex node id>@10.3.58.6:30303?discport=30301
  50. func NewEnode(rawurl string) (enode *Enode, _ error) {
  51. node, err := discv5.ParseNode(rawurl)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return &Enode{node}, nil
  56. }
  57. // Enodes represents a slice of accounts.
  58. type Enodes struct{ nodes []*discv5.Node }
  59. // NewEnodes creates a slice of uninitialized enodes.
  60. func NewEnodes(size int) *Enodes {
  61. return &Enodes{
  62. nodes: make([]*discv5.Node, size),
  63. }
  64. }
  65. // NewEnodesEmpty creates an empty slice of Enode values.
  66. func NewEnodesEmpty() *Enodes {
  67. return NewEnodes(0)
  68. }
  69. // Size returns the number of enodes in the slice.
  70. func (e *Enodes) Size() int {
  71. return len(e.nodes)
  72. }
  73. // Get returns the enode at the given index from the slice.
  74. func (e *Enodes) Get(index int) (enode *Enode, _ error) {
  75. if index < 0 || index >= len(e.nodes) {
  76. return nil, errors.New("index out of bounds")
  77. }
  78. return &Enode{e.nodes[index]}, nil
  79. }
  80. // Set sets the enode at the given index in the slice.
  81. func (e *Enodes) Set(index int, enode *Enode) error {
  82. if index < 0 || index >= len(e.nodes) {
  83. return errors.New("index out of bounds")
  84. }
  85. e.nodes[index] = enode.node
  86. return nil
  87. }
  88. // Append adds a new enode element to the end of the slice.
  89. func (e *Enodes) Append(enode *Enode) {
  90. e.nodes = append(e.nodes, enode.node)
  91. }