Skip to content

Commit d15e423

Browse files
authored
p2p/enode: store local port number as uint16 (#23926)
1 parent 347c37b commit d15e423

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

p2p/enode/localnode.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type LocalNode struct {
6363
type lnEndpoint struct {
6464
track *netutil.IPTracker
6565
staticIP, fallbackIP net.IP
66-
fallbackUDP int
66+
fallbackUDP uint16 // port
6767
}
6868

6969
// NewLocalNode creates a local node.
@@ -208,8 +208,8 @@ func (ln *LocalNode) SetFallbackUDP(port int) {
208208
ln.mu.Lock()
209209
defer ln.mu.Unlock()
210210

211-
ln.endpoint4.fallbackUDP = port
212-
ln.endpoint6.fallbackUDP = port
211+
ln.endpoint4.fallbackUDP = uint16(port)
212+
ln.endpoint6.fallbackUDP = uint16(port)
213213
ln.updateEndpoints()
214214
}
215215

@@ -261,7 +261,7 @@ func (ln *LocalNode) updateEndpoints() {
261261
}
262262

263263
// get returns the endpoint with highest precedence.
264-
func (e *lnEndpoint) get() (newIP net.IP, newPort int) {
264+
func (e *lnEndpoint) get() (newIP net.IP, newPort uint16) {
265265
newPort = e.fallbackUDP
266266
if e.fallbackIP != nil {
267267
newIP = e.fallbackIP
@@ -277,15 +277,18 @@ func (e *lnEndpoint) get() (newIP net.IP, newPort int) {
277277

278278
// predictAddr wraps IPTracker.PredictEndpoint, converting from its string-based
279279
// endpoint representation to IP and port types.
280-
func predictAddr(t *netutil.IPTracker) (net.IP, int) {
280+
func predictAddr(t *netutil.IPTracker) (net.IP, uint16) {
281281
ep := t.PredictEndpoint()
282282
if ep == "" {
283283
return nil, 0
284284
}
285285
ipString, portString, _ := net.SplitHostPort(ep)
286286
ip := net.ParseIP(ipString)
287-
port, _ := strconv.Atoi(portString)
288-
return ip, port
287+
port, err := strconv.ParseUint(portString, 10, 16)
288+
if err != nil {
289+
return nil, 0
290+
}
291+
return ip, uint16(port)
289292
}
290293

291294
func (ln *LocalNode) invalidate() {

p2p/simulations/adapters/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func assignTCPPort() (uint16, error) {
242242
if err != nil {
243243
return 0, err
244244
}
245-
p, err := strconv.ParseInt(port, 10, 32)
245+
p, err := strconv.ParseUint(port, 10, 16)
246246
if err != nil {
247247
return 0, err
248248
}

0 commit comments

Comments
 (0)