OSDN Git Service

edit the remoteAddr
[bytom/bytom.git] / p2p / node_info.go
1 package p2p
2
3 import (
4         "fmt"
5         "net"
6         "strconv"
7         "strings"
8
9         crypto "github.com/tendermint/go-crypto"
10 )
11
12 const maxNodeInfoSize = 10240 // 10Kb
13
14 //NodeInfo peer node info
15 type NodeInfo struct {
16         PubKey     crypto.PubKeyEd25519 `json:"pub_key"`
17         Moniker    string               `json:"moniker"`
18         Network    string               `json:"network"`
19         ListenAddr string               `json:"listen_addr"`
20         RemoteAddr string               `json:"remote_addr"`
21         Version    string               `json:"version"` // major.minor.revision
22         Other      []string             `json:"other"`   // other application specific data
23 }
24
25 // CompatibleWith checks if two NodeInfo are compatible with eachother.
26 // CONTRACT: two nodes are compatible if the major version matches and network match
27 // and they have at least one channel in common.
28 func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
29         iMajor, iMinor, _, iErr := splitVersion(info.Version)
30         oMajor, oMinor, _, oErr := splitVersion(other.Version)
31
32         // if our own version number is not formatted right, we messed up
33         if iErr != nil {
34                 return iErr
35         }
36
37         // version number must be formatted correctly ("x.x.x")
38         if oErr != nil {
39                 return oErr
40         }
41
42         // major version must match
43         if iMajor != oMajor {
44                 return fmt.Errorf("Peer is on a different major version. Got %v, expected %v", oMajor, iMajor)
45         }
46
47         // minor version must match
48         if iMinor != oMinor {
49                 return fmt.Errorf("Peer is on a different minor version. Got %v, expected %v", oMinor, iMinor)
50         }
51
52         // nodes must be on the same network
53         if info.Network != other.Network {
54                 return fmt.Errorf("Peer is on a different network. Got %v, expected %v", other.Network, info.Network)
55         }
56
57         return nil
58 }
59
60 //ListenHost peer listener ip address
61 func (info *NodeInfo) ListenHost() string {
62         host, _, _ := net.SplitHostPort(info.ListenAddr)
63         return host
64 }
65
66 //ListenPort peer listener port
67 func (info *NodeInfo) ListenPort() int {
68         _, port, _ := net.SplitHostPort(info.ListenAddr)
69         portInt, err := strconv.Atoi(port)
70         if err != nil {
71                 return -1
72         }
73         return portInt
74 }
75
76 //String representation
77 func (info NodeInfo) String() string {
78         return fmt.Sprintf("NodeInfo{pk: %v, moniker: %v, network: %v [listen %v], version: %v (%v)}", info.PubKey, info.Moniker, info.Network, info.ListenAddr, info.Version, info.Other)
79 }
80
81 func splitVersion(version string) (string, string, string, error) {
82         spl := strings.Split(version, ".")
83         if len(spl) != 3 {
84                 return "", "", "", fmt.Errorf("Invalid version format %v", version)
85         }
86         return spl[0], spl[1], spl[2], nil
87 }