Skip to content

Commit e904997

Browse files
committed
refactor: use netip.AddrPort
Signed-off-by: Oleksandr Redko <[email protected]>
1 parent 38b8280 commit e904997

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

pkg/guestagent/guestagent_linux.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,19 +271,20 @@ func (a *agent) LocalPorts(_ context.Context) ([]*api.IPPort, error) {
271271
}
272272

273273
for _, ipt := range ipts {
274+
port := int32(ipt.AddrPort.Port())
274275
// Make sure the port isn't already listed from procnettcp
275276
found := false
276277
for _, re := range res {
277-
if re.Port == int32(ipt.Port) {
278+
if re.Port == port {
278279
found = true
279280
}
280281
}
281282
if !found {
282283
if ipt.TCP {
283284
res = append(res,
284285
&api.IPPort{
285-
Ip: ipt.IP.String(),
286-
Port: int32(ipt.Port), // The port value is already ensured to be within int32 bounds in iptables.go
286+
Ip: ipt.AddrPort.Addr().String(),
287+
Port: port,
287288
Protocol: "tcp",
288289
})
289290
}

pkg/guestagent/iptables/iptables.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"errors"
99
"net"
10+
"net/netip"
1011
"os/exec"
1112
"regexp"
1213
"strconv"
@@ -15,9 +16,8 @@ import (
1516
)
1617

1718
type Entry struct {
18-
TCP bool
19-
IP net.IP
20-
Port int
19+
TCP bool
20+
AddrPort netip.AddrPort
2121
}
2222

2323
// This regex can detect a line in the iptables added by portmap to do the
@@ -72,23 +72,25 @@ func parsePortsFromRules(rules []string) ([]Entry, error) {
7272
if len(found) != 4 {
7373
continue
7474
}
75-
port64, err := strconv.ParseInt(found[3], 10, 32)
75+
port16, err := strconv.ParseUint(found[3], 10, 16)
7676
if err != nil {
7777
return nil, err
7878
}
79-
port := int(port64)
79+
port := uint16(port16)
8080

8181
isTCP := found[2] == "tcp"
8282

8383
// When no IP is present the rule applies to all interfaces.
84-
ip := found[1]
85-
if ip == "" {
86-
ip = "0.0.0.0"
84+
addr := netip.IPv4Unspecified()
85+
if s := found[1]; s != "" {
86+
addr, err = netip.ParseAddr(s)
87+
if err != nil {
88+
return nil, err
89+
}
8790
}
8891
ent := Entry{
89-
IP: net.ParseIP(ip),
90-
Port: port,
91-
TCP: isTCP,
92+
AddrPort: netip.AddrPortFrom(addr, port),
93+
TCP: isTCP,
9294
}
9395
entries = append(entries, ent)
9496
}
@@ -128,7 +130,7 @@ func checkPortsOpen(pts []Entry) ([]Entry, error) {
128130
var entries []Entry
129131
for _, pt := range pts {
130132
if pt.TCP {
131-
conn, err := net.DialTimeout("tcp", net.JoinHostPort(pt.IP.String(), strconv.Itoa(pt.Port)), time.Second)
133+
conn, err := net.DialTimeout("tcp", pt.AddrPort.String(), time.Second)
132134
if err == nil && conn != nil {
133135
conn.Close()
134136
entries = append(entries, pt)

pkg/guestagent/iptables/iptables_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package iptables
55

66
import (
7+
"net/netip"
78
"strings"
89
"testing"
910

@@ -84,10 +85,6 @@ func TestParsePortsFromRules(t *testing.T) {
8485
l := len(res)
8586
assert.Equal(t, l, 2, "unexpected number of ports parsed from iptables")
8687

87-
if res[0].IP.String() != "0.0.0.0" || res[0].Port != 8082 || res[0].TCP != true {
88-
t.Errorf("expected port 8082 on IP 0.0.0.0 with TCP true but got port %d on IP %s with TCP %t", res[0].Port, res[0].IP.String(), res[0].TCP)
89-
}
90-
if res[1].IP.String() != "127.0.0.1" || res[1].Port != 8081 || res[1].TCP != true {
91-
t.Errorf("expected port 8081 on IP 127.0.0.1 with TCP true but go port %d on IP %s with TCP %t", res[1].Port, res[1].IP.String(), res[1].TCP)
92-
}
88+
assert.Equal(t, res[0], Entry{AddrPort: netip.MustParseAddrPort("0.0.0.0:8082"), TCP: true})
89+
assert.Equal(t, res[1], Entry{AddrPort: netip.MustParseAddrPort("127.0.0.1:8081"), TCP: true})
9390
}

0 commit comments

Comments
 (0)