Skip to content

refactor: use netip.AddrPort #3816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pkg/guestagent/guestagent_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,20 @@ func (a *agent) LocalPorts(_ context.Context) ([]*api.IPPort, error) {
}

for _, ipt := range ipts {
port := int32(ipt.AddrPort.Port())
// Make sure the port isn't already listed from procnettcp
found := false
for _, re := range res {
if re.Port == int32(ipt.Port) {
if re.Port == port {
found = true
}
}
if !found {
if ipt.TCP {
res = append(res,
&api.IPPort{
Ip: ipt.IP.String(),
Port: int32(ipt.Port), // The port value is already ensured to be within int32 bounds in iptables.go
Ip: ipt.AddrPort.Addr().String(),
Port: port,
Protocol: "tcp",
})
}
Expand Down
26 changes: 14 additions & 12 deletions pkg/guestagent/iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"errors"
"net"
"net/netip"
"os/exec"
"regexp"
"strconv"
Expand All @@ -15,9 +16,8 @@ import (
)

type Entry struct {
TCP bool
IP net.IP
Port int
TCP bool
AddrPort netip.AddrPort
}

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

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

// When no IP is present the rule applies to all interfaces.
ip := found[1]
if ip == "" {
ip = "0.0.0.0"
addr := netip.IPv4Unspecified()
if s := found[1]; s != "" {
addr, err = netip.ParseAddr(s)
if err != nil {
return nil, err
}
}
ent := Entry{
IP: net.ParseIP(ip),
Port: port,
TCP: isTCP,
AddrPort: netip.AddrPortFrom(addr, port),
TCP: isTCP,
}
entries = append(entries, ent)
}
Expand Down Expand Up @@ -128,7 +130,7 @@ func checkPortsOpen(pts []Entry) ([]Entry, error) {
var entries []Entry
for _, pt := range pts {
if pt.TCP {
conn, err := net.DialTimeout("tcp", net.JoinHostPort(pt.IP.String(), strconv.Itoa(pt.Port)), time.Second)
conn, err := net.DialTimeout("tcp", pt.AddrPort.String(), time.Second)
if err == nil && conn != nil {
conn.Close()
entries = append(entries, pt)
Expand Down
9 changes: 3 additions & 6 deletions pkg/guestagent/iptables/iptables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package iptables

import (
"net/netip"
"strings"
"testing"

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

if res[0].IP.String() != "0.0.0.0" || res[0].Port != 8082 || res[0].TCP != true {
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)
}
if res[1].IP.String() != "127.0.0.1" || res[1].Port != 8081 || res[1].TCP != true {
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)
}
assert.Equal(t, res[0], Entry{AddrPort: netip.MustParseAddrPort("0.0.0.0:8082"), TCP: true})
assert.Equal(t, res[1], Entry{AddrPort: netip.MustParseAddrPort("127.0.0.1:8081"), TCP: true})
}
Loading