p2p/enode: store local port number as uint16 (#23926)

This commit is contained in:
Serhat Şevki Dinçer 2021-11-23 17:14:08 +03:00 committed by GitHub
parent 347c37b362
commit d15e423562
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 8 deletions

View File

@ -63,7 +63,7 @@ type LocalNode struct {
type lnEndpoint struct {
track *netutil.IPTracker
staticIP, fallbackIP net.IP
fallbackUDP int
fallbackUDP uint16 // port
}
// NewLocalNode creates a local node.
@ -208,8 +208,8 @@ func (ln *LocalNode) SetFallbackUDP(port int) {
ln.mu.Lock()
defer ln.mu.Unlock()
ln.endpoint4.fallbackUDP = port
ln.endpoint6.fallbackUDP = port
ln.endpoint4.fallbackUDP = uint16(port)
ln.endpoint6.fallbackUDP = uint16(port)
ln.updateEndpoints()
}
@ -261,7 +261,7 @@ func (ln *LocalNode) updateEndpoints() {
}
// get returns the endpoint with highest precedence.
func (e *lnEndpoint) get() (newIP net.IP, newPort int) {
func (e *lnEndpoint) get() (newIP net.IP, newPort uint16) {
newPort = e.fallbackUDP
if e.fallbackIP != nil {
newIP = e.fallbackIP
@ -277,15 +277,18 @@ func (e *lnEndpoint) get() (newIP net.IP, newPort int) {
// predictAddr wraps IPTracker.PredictEndpoint, converting from its string-based
// endpoint representation to IP and port types.
func predictAddr(t *netutil.IPTracker) (net.IP, int) {
func predictAddr(t *netutil.IPTracker) (net.IP, uint16) {
ep := t.PredictEndpoint()
if ep == "" {
return nil, 0
}
ipString, portString, _ := net.SplitHostPort(ep)
ip := net.ParseIP(ipString)
port, _ := strconv.Atoi(portString)
return ip, port
port, err := strconv.ParseUint(portString, 10, 16)
if err != nil {
return nil, 0
}
return ip, uint16(port)
}
func (ln *LocalNode) invalidate() {

View File

@ -242,7 +242,7 @@ func assignTCPPort() (uint16, error) {
if err != nil {
return 0, err
}
p, err := strconv.ParseInt(port, 10, 32)
p, err := strconv.ParseUint(port, 10, 16)
if err != nil {
return 0, err
}