MCPcopy Create free account
hub / github.com/tendermint/tendermint / NewNetAddressString

Function NewNetAddressString

types/netaddress.go:69–115  ·  view source on GitHub ↗

NewNetAddressString returns a new NetAddress using the provided address in the form of "ID@IP:Port". Also resolves the host if host is not an IP. Errors are of type ErrNetAddressXxx where Xxx is in (NoID, Invalid, Lookup)

(addr string)

Source from the content-addressed store, hash-verified

67// Also resolves the host if host is not an IP.
68// Errors are of type ErrNetAddressXxx where Xxx is in (NoID, Invalid, Lookup)
69func NewNetAddressString(addr string) (*NetAddress, error) {
70 addrWithoutProtocol := removeProtocolIfDefined(addr)
71 spl := strings.Split(addrWithoutProtocol, "@")
72 if len(spl) != 2 {
73 return nil, ErrNetAddressNoID{addr}
74 }
75
76 id, err := NewNodeID(spl[0])
77 if err != nil {
78 return nil, ErrNetAddressInvalid{addrWithoutProtocol, err}
79 }
80
81 if err := id.Validate(); err != nil {
82 return nil, ErrNetAddressInvalid{addrWithoutProtocol, err}
83 }
84
85 addrWithoutProtocol = spl[1]
86
87 // get host and port
88 host, portStr, err := net.SplitHostPort(addrWithoutProtocol)
89 if err != nil {
90 return nil, ErrNetAddressInvalid{addrWithoutProtocol, err}
91 }
92 if len(host) == 0 {
93 return nil, ErrNetAddressInvalid{
94 addrWithoutProtocol,
95 errors.New("host is empty")}
96 }
97
98 ip := net.ParseIP(host)
99 if ip == nil {
100 ips, err := net.LookupIP(host)
101 if err != nil {
102 return nil, ErrNetAddressLookup{host, err}
103 }
104 ip = ips[0]
105 }
106
107 port, err := strconv.ParseUint(portStr, 10, 16)
108 if err != nil {
109 return nil, ErrNetAddressInvalid{portStr, err}
110 }
111
112 na := NewNetAddressIPPort(ip, uint16(port))
113 na.ID = id
114 return na, nil
115}
116
117// Equals reports whether na and other are the same addresses,
118// including their ID, IP, and Port.

Callers 13

initCorpusFunction · 0.92
NewNetAddressStringsFunction · 0.92
CreateRoutableAddrFunction · 0.92
MakeSwitchFunction · 0.92
randIPv4AddressFunction · 0.92
OnStartMethod · 0.92
ValidateMethod · 0.85
NetAddressMethod · 0.85
TestNewNetAddressStringFunction · 0.85
TestNetAddressPropertiesFunction · 0.85

Calls 4

ValidateMethod · 0.95
removeProtocolIfDefinedFunction · 0.85
NewNodeIDFunction · 0.85
NewNetAddressIPPortFunction · 0.85

Tested by 5

randIPv4AddressFunction · 0.74
TestNewNetAddressStringFunction · 0.68
TestNetAddressPropertiesFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…