aboutsummaryrefslogtreecommitdiffstats
path: root/p2p
diff options
context:
space:
mode:
Diffstat (limited to 'p2p')
-rw-r--r--p2p/discover/node.go14
-rw-r--r--p2p/discv5/node.go14
-rw-r--r--p2p/netutil/net.go25
-rw-r--r--p2p/server.go34
4 files changed, 70 insertions, 17 deletions
diff --git a/p2p/discover/node.go b/p2p/discover/node.go
index 6a7ab814e..d9cbd9448 100644
--- a/p2p/discover/node.go
+++ b/p2p/discover/node.go
@@ -207,6 +207,20 @@ func MustParseNode(rawurl string) *Node {
return n
}
+// MarshalText implements encoding.TextMarshaler.
+func (n *Node) MarshalText() ([]byte, error) {
+ return []byte(n.String()), nil
+}
+
+// UnmarshalText implements encoding.TextUnmarshaler.
+func (n *Node) UnmarshalText(text []byte) error {
+ dec, err := ParseNode(string(text))
+ if err == nil {
+ *n = *dec
+ }
+ return err
+}
+
// NodeID is a unique identifier for each node.
// The node identifier is a marshaled elliptic curve public key.
type NodeID [NodeIDBits / 8]byte
diff --git a/p2p/discv5/node.go b/p2p/discv5/node.go
index c99b4da14..2db7a508f 100644
--- a/p2p/discv5/node.go
+++ b/p2p/discv5/node.go
@@ -215,6 +215,20 @@ func MustParseNode(rawurl string) *Node {
return n
}
+// MarshalText implements encoding.TextMarshaler.
+func (n *Node) MarshalText() ([]byte, error) {
+ return []byte(n.String()), nil
+}
+
+// UnmarshalText implements encoding.TextUnmarshaler.
+func (n *Node) UnmarshalText(text []byte) error {
+ dec, err := ParseNode(string(text))
+ if err == nil {
+ *n = *dec
+ }
+ return err
+}
+
// type nodeQueue []*Node
//
// // pushNew adds n to the end if it is not present.
diff --git a/p2p/netutil/net.go b/p2p/netutil/net.go
index 3c3715788..f6005afd2 100644
--- a/p2p/netutil/net.go
+++ b/p2p/netutil/net.go
@@ -84,6 +84,31 @@ func ParseNetlist(s string) (*Netlist, error) {
return &l, nil
}
+// MarshalTOML implements toml.MarshalerRec.
+func (l Netlist) MarshalTOML() interface{} {
+ list := make([]string, 0, len(l))
+ for _, net := range l {
+ list = append(list, net.String())
+ }
+ return list
+}
+
+// UnmarshalTOML implements toml.UnmarshalerRec.
+func (l *Netlist) UnmarshalTOML(fn func(interface{}) error) error {
+ var masks []string
+ if err := fn(&masks); err != nil {
+ return err
+ }
+ for _, mask := range masks {
+ _, n, err := net.ParseCIDR(mask)
+ if err != nil {
+ return err
+ }
+ *l = append(*l, *n)
+ }
+ return nil
+}
+
// Add parses a CIDR mask and appends it to the list. It panics for invalid masks and is
// intended to be used for setting up static lists.
func (l *Netlist) Add(cidr string) {
diff --git a/p2p/server.go b/p2p/server.go
index b2b8c9762..d7909d53a 100644
--- a/p2p/server.go
+++ b/p2p/server.go
@@ -58,7 +58,7 @@ var errServerStopped = errors.New("server stopped")
// Config holds Server options.
type Config struct {
// This field must be set to a valid secp256k1 private key.
- PrivateKey *ecdsa.PrivateKey
+ PrivateKey *ecdsa.PrivateKey `toml:"-"`
// MaxPeers is the maximum number of peers that can be
// connected. It must be greater than zero.
@@ -67,22 +67,22 @@ type Config struct {
// MaxPendingPeers is the maximum number of peers that can be pending in the
// handshake phase, counted separately for inbound and outbound connections.
// Zero defaults to preset values.
- MaxPendingPeers int
+ MaxPendingPeers int `toml:",omitempty"`
- // Discovery specifies whether the peer discovery mechanism should be started
- // or not. Disabling is usually useful for protocol debugging (manual topology).
- Discovery bool
+ // NoDiscovery can be used to disable the peer discovery mechanism.
+ // Disabling is useful for protocol debugging (manual topology).
+ NoDiscovery bool
// DiscoveryV5 specifies whether the the new topic-discovery based V5 discovery
// protocol should be started or not.
- DiscoveryV5 bool
+ DiscoveryV5 bool `toml:",omitempty"`
// Listener address for the V5 discovery protocol UDP traffic.
- DiscoveryV5Addr string
+ DiscoveryV5Addr string `toml:",omitempty"`
// Name sets the node name of this server.
// Use common.MakeName to create a name that follows existing conventions.
- Name string
+ Name string `toml:"-"`
// BootstrapNodes are used to establish connectivity
// with the rest of the network.
@@ -91,7 +91,7 @@ type Config struct {
// BootstrapNodesV5 are used to establish connectivity
// with the rest of the network using the V5 discovery
// protocol.
- BootstrapNodesV5 []*discv5.Node
+ BootstrapNodesV5 []*discv5.Node `toml:",omitempty"`
// Static nodes are used as pre-configured connections which are always
// maintained and re-connected on disconnects.
@@ -104,16 +104,16 @@ type Config struct {
// Connectivity can be restricted to certain IP networks.
// If this option is set to a non-nil value, only hosts which match one of the
// IP networks contained in the list are considered.
- NetRestrict *netutil.Netlist
+ NetRestrict *netutil.Netlist `toml:",omitempty"`
// NodeDatabase is the path to the database containing the previously seen
// live nodes in the network.
- NodeDatabase string
+ NodeDatabase string `toml:",omitempty"`
// Protocols should contain the protocols supported
// by the server. Matching protocols are launched for
// each peer.
- Protocols []Protocol
+ Protocols []Protocol `toml:"-"`
// If ListenAddr is set to a non-nil address, the server
// will listen for incoming connections.
@@ -126,14 +126,14 @@ type Config struct {
// If set to a non-nil value, the given NAT port mapper
// is used to make the listening port available to the
// Internet.
- NAT nat.Interface
+ NAT nat.Interface `toml:",omitempty"`
// If Dialer is set to a non-nil value, the given Dialer
// is used to dial outbound peer connections.
- Dialer *net.Dialer
+ Dialer *net.Dialer `toml:"-"`
// If NoDial is true, the server will not dial any peers.
- NoDial bool
+ NoDial bool `toml:",omitempty"`
}
// Server manages all peer connections.
@@ -370,7 +370,7 @@ func (srv *Server) Start() (err error) {
srv.peerOpDone = make(chan struct{})
// node table
- if srv.Discovery {
+ if !srv.NoDiscovery {
ntab, err := discover.ListenUDP(srv.PrivateKey, srv.ListenAddr, srv.NAT, srv.NodeDatabase, srv.NetRestrict)
if err != nil {
return err
@@ -393,7 +393,7 @@ func (srv *Server) Start() (err error) {
}
dynPeers := (srv.MaxPeers + 1) / 2
- if !srv.Discovery {
+ if srv.NoDiscovery {
dynPeers = 0
}
dialer := newDialState(srv.StaticNodes, srv.BootstrapNodes, srv.ntab, dynPeers, srv.NetRestrict)