Forward and Reply now passing test with netip.AddrPort change

This commit is contained in:
David Vennik
2022-12-24 14:40:18 +00:00
parent 76f56b863c
commit 001d22ea92
6 changed files with 63 additions and 21 deletions

View File

@@ -24,14 +24,14 @@ var (
// except when the net.IP is known via the packet sender address.
type Node struct {
nonce.ID
netip.AddrPort
*netip.AddrPort
HeaderKey, PayloadKey *pub.Key
ifc.Transport
}
// New creates a new Node. net.IP is optional if the counterparty is not in
// direct connection.
func New(ip netip.AddrPort, fwd, rtn *pub.Key, tpt ifc.Transport) (n *Node, id nonce.ID) {
func New(ip *netip.AddrPort, fwd, rtn *pub.Key, tpt ifc.Transport) (n *Node, id nonce.ID) {
id = nonce.NewID()
n = &Node{
ID: id,
@@ -68,8 +68,8 @@ func (n Nodes) FindByID(i nonce.ID) (no *Node) {
return
}
// FindByIP searches for a Node by net.IP.
func (n Nodes) FindByIP(id netip.AddrPort) (no *Node) {
// FindByAddrPort searches for a Node by netip.AddrPort.
func (n Nodes) FindByAddrPort(id netip.AddrPort) (no *Node) {
for _, nn := range n {
if nn.AddrPort.String() == id.String() {
no = nn

View File

@@ -20,6 +20,7 @@ import (
"github.com/Indra-Labs/indra/pkg/wire/delay"
"github.com/Indra-Labs/indra/pkg/wire/exit"
"github.com/Indra-Labs/indra/pkg/wire/forward"
"github.com/Indra-Labs/indra/pkg/wire/reply"
log2 "github.com/cybriq/proc/pkg/log"
)
@@ -168,7 +169,7 @@ func TestOnionSkins_Forward(t *testing.T) {
port := uint16(rand.Uint32())
ap := netip.AddrPortFrom(adr, port)
on := OnionSkins{}.
Forward(ap).
Forward(&ap).
Assemble()
onb := EncodeOnion(on)
c := slice.NewCursor()
@@ -181,11 +182,11 @@ func TestOnionSkins_Forward(t *testing.T) {
t.Error("did not unwrap expected type", reflect.TypeOf(onf))
t.FailNow()
}
_ = cf
// if !cf.IP.Equal(ip) {
// t.Error("forward IP did not unwrap correctly")
// t.FailNow()
// }
if cf.AddrPort.String() != ap.String() {
log.I.S(cf.AddrPort, ap)
t.Error("forward AddrPort did not unwrap correctly")
t.FailNow()
}
}
}
@@ -198,7 +199,46 @@ func TestOnionSkins_Purchase(t *testing.T) {
}
func TestOnionSkins_Reply(t *testing.T) {
log2.CodeLoc = true
var e error
ipSizes := []int{net.IPv4len, net.IPv6len}
for i := range ipSizes {
n := nonce.New()
ip := net.IP(n[:ipSizes[i]])
var adr netip.Addr
if ipSizes[i] == net.IPv4len {
ip = ip.To4()
}
if ipSizes[i] == net.IPv6len {
ip = ip.To16()
}
var ok bool
if adr, ok = netip.AddrFromSlice(ip); !ok {
t.Error("unable to get netip.Addr")
t.FailNow()
}
port := uint16(rand.Uint32())
ap := netip.AddrPortFrom(adr, port)
on := OnionSkins{}.
Reply(&ap).
Assemble()
onb := EncodeOnion(on)
c := slice.NewCursor()
var onf types.Onion
if onf, e = PeelOnion(onb, c); check(e) {
t.FailNow()
}
var cf *reply.OnionSkin
if cf, ok = onf.(*reply.OnionSkin); !ok {
t.Error("did not unwrap expected type", reflect.TypeOf(onf))
t.FailNow()
}
if cf.AddrPort.String() != ap.String() {
log.I.S(cf.AddrPort, ap)
t.Error("reply AddrPort did not unwrap correctly")
t.FailNow()
}
}
}
func TestOnionSkins_Response(t *testing.T) {

View File

@@ -22,7 +22,7 @@ var (
// OnionSkin forward is just an IP address and a wrapper for another message.
type OnionSkin struct {
netip.AddrPort
*netip.AddrPort
types.Onion
}
@@ -45,7 +45,7 @@ func (x *OnionSkin) Encode(b slice.Bytes, c *slice.Cursor) {
return
}
b[*c] = byte(len(ap))
copy(b[c.Inc(1):c.Inc(len(ap))], ap)
x.Onion.Encode(b, c)
}
@@ -55,6 +55,7 @@ func (x *OnionSkin) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
}
apLen := b[*c]
apBytes := b[c.Inc(1):c.Inc(int(apLen))]
x.AddrPort = &netip.AddrPort{}
if e = x.AddrPort.UnmarshalBinary(apBytes); check(e) {
return
}

View File

@@ -62,7 +62,7 @@ func (o OnionSkins) Exit(port uint16, prvs [3]*prv.Key, pubs [3]*pub.Key,
Onion: &noop.OnionSkin{},
})
}
func (o OnionSkins) Forward(addr netip.AddrPort) OnionSkins {
func (o OnionSkins) Forward(addr *netip.AddrPort) OnionSkins {
return append(o, &forward.OnionSkin{AddrPort: addr, Onion: &noop.OnionSkin{}})
}
func (o OnionSkins) Message(to *address.Sender, from *prv.Key) OnionSkins {
@@ -79,7 +79,7 @@ func (o OnionSkins) Purchase(nBytes uint64, ciphers [3]sha256.Hash) OnionSkins {
Onion: &noop.OnionSkin{},
})
}
func (o OnionSkins) Reply(ip netip.AddrPort) OnionSkins {
func (o OnionSkins) Reply(ip *netip.AddrPort) OnionSkins {
return append(o, &reply.OnionSkin{AddrPort: ip, Onion: &noop.OnionSkin{}})
}
func (o OnionSkins) Response(res slice.Bytes) OnionSkins {

View File

@@ -28,7 +28,7 @@ var (
// csprng random bytes into the remainder to the same length.
type OnionSkin struct {
// AddrPort is the address of the next relay in the return leg of a circuit.
netip.AddrPort
*netip.AddrPort
types.Onion
}
@@ -51,7 +51,7 @@ func (x *OnionSkin) Encode(b slice.Bytes, c *slice.Cursor) {
return
}
b[*c] = byte(len(ap))
copy(b[c.Inc(1):c.Inc(len(ap))], ap)
x.Onion.Encode(b, c)
}
@@ -61,6 +61,7 @@ func (x *OnionSkin) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
}
apLen := b[*c]
apBytes := b[c.Inc(1):c.Inc(int(apLen))]
x.AddrPort = &netip.AddrPort{}
if e = x.AddrPort.UnmarshalBinary(apBytes); check(e) {
return
}

View File

@@ -10,11 +10,11 @@ var (
// GitRef is the gitref, as in refs/heads/branchname.
GitRef = "refs/heads/main"
// ParentGitCommit is the commit hash of the parent HEAD.
ParentGitCommit = "bdda61da0b46433a5dce707abd7f1b6063266e42"
ParentGitCommit = "d99cde172a75abd19b48386a59a79197f7f6596e"
// BuildTime stores the time when the current binary was built.
BuildTime = "2022-12-24T14:11:47Z"
BuildTime = "2022-12-24T14:40:18Z"
// SemVer lists the (latest) git tag on the build.
SemVer = "v0.0.230"
SemVer = "v0.0.231"
// PathBase is the path base returned from runtime caller.
PathBase = "/home/loki/src/github.com/Indra-Labs/indra/"
// Major is the major number from the tag.
@@ -22,7 +22,7 @@ var (
// Minor is the minor number from the tag.
Minor = 0
// Patch is the patch version number from the tag.
Patch = 230
Patch = 231
)
// Version returns a pretty printed version information string.