netaddr.NewIPPort

This commit is contained in:
Josh Bleecher Snyder
2021-05-12 16:21:22 -07:00
parent ebcd7ab890
commit 920145a836
13 changed files with 41 additions and 52 deletions

View File

@@ -237,10 +237,7 @@ func (n *SNAT44) allocateMappedPort() (net.PacketConn, netaddr.IPPort) {
if err != nil {
panic(fmt.Sprintf("ran out of NAT ports: %v", err))
}
addr := netaddr.IPPort{
IP: ip,
Port: uint16(pc.LocalAddr().(*net.UDPAddr).Port),
}
addr := netaddr.NewIPPort(ip, uint16(pc.LocalAddr().(*net.UDPAddr).Port))
return pc, addr
}

View File

@@ -396,8 +396,8 @@ func (m *Machine) deliverLocalPacket(p *Packet, iface *Interface) {
}
possibleDsts := []netaddr.IPPort{
p.Dst,
netaddr.IPPort{IP: v6unspec, Port: p.Dst.Port},
netaddr.IPPort{IP: v4unspec, Port: p.Dst.Port},
netaddr.NewIPPort(v6unspec, p.Dst.Port),
netaddr.NewIPPort(v4unspec, p.Dst.Port),
}
for _, dest := range possibleDsts {
c, ok := conns[dest]
@@ -707,7 +707,7 @@ func (m *Machine) ListenPacket(ctx context.Context, network, address string) (ne
return nil, nil
}
}
ipp := netaddr.IPPort{IP: ip, Port: port}
ipp := netaddr.NewIPPort(ip, port)
c := &conn{
m: m,

View File

@@ -49,8 +49,8 @@ func TestSendPacket(t *testing.T) {
ifFoo := foo.Attach("eth0", internet)
ifBar := bar.Attach("enp0s1", internet)
fooAddr := netaddr.IPPort{IP: ifFoo.V4(), Port: 123}
barAddr := netaddr.IPPort{IP: ifBar.V4(), Port: 456}
fooAddr := netaddr.NewIPPort(ifFoo.V4(), 123)
barAddr := netaddr.NewIPPort(ifBar.V4(), 456)
ctx := context.Background()
fooPC, err := foo.ListenPacket(ctx, "udp4", fooAddr.String())
@@ -111,10 +111,10 @@ func TestMultiNetwork(t *testing.T) {
t.Fatal(err)
}
clientAddr := netaddr.IPPort{IP: ifClient.V4(), Port: 123}
natLANAddr := netaddr.IPPort{IP: ifNATLAN.V4(), Port: 456}
natWANAddr := netaddr.IPPort{IP: ifNATWAN.V4(), Port: 456}
serverAddr := netaddr.IPPort{IP: ifServer.V4(), Port: 789}
clientAddr := netaddr.NewIPPort(ifClient.V4(), 123)
natLANAddr := netaddr.NewIPPort(ifNATLAN.V4(), 456)
natWANAddr := netaddr.NewIPPort(ifNATWAN.V4(), 456)
serverAddr := netaddr.NewIPPort(ifServer.V4(), 789)
const msg1, msg2 = "hello", "world"
if _, err := natPC.WriteTo([]byte(msg1), clientAddr.UDPAddr()); err != nil {
@@ -216,7 +216,7 @@ func TestPacketHandler(t *testing.T) {
}
const msg = "some message"
serverAddr := netaddr.IPPort{IP: ifServer.V4(), Port: 456}
serverAddr := netaddr.NewIPPort(ifServer.V4(), 456)
if _, err := clientPC.WriteTo([]byte(msg), serverAddr.UDPAddr()); err != nil {
t.Fatal(err)
}
@@ -230,7 +230,7 @@ func TestPacketHandler(t *testing.T) {
if string(buf) != msg {
t.Errorf("read %q; want %q", buf, msg)
}
mappedAddr := netaddr.IPPort{IP: ifNATWAN.V4(), Port: 123}
mappedAddr := netaddr.NewIPPort(ifNATWAN.V4(), 123)
if addr.String() != mappedAddr.String() {
t.Errorf("addr = %q; want %q", addr, mappedAddr)
}