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)
| 67 | // Also resolves the host if host is not an IP. |
| 68 | // Errors are of type ErrNetAddressXxx where Xxx is in (NoID, Invalid, Lookup) |
| 69 | func 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. |
searching dependent graphs…