Now fixed 19 byte long AddrPort field

This commit is contained in:
David Vennik
2022-12-30 12:28:43 +00:00
parent e8835aee22
commit acc6de85d2
5 changed files with 108 additions and 27 deletions

View File

@@ -42,7 +42,7 @@ func TestPing(t *testing.T) {
t.FailNow()
}
pldPub := pub.Derive(pldPrv)
addr := slice.GenerateRandomAddrPortIPv4()
addr := slice.GenerateRandomAddrPortIPv6()
nodes[i], _ = node.New(addr, hdrPub, pldPub, hdrPrv, pldPrv, transports[i])
if clients[i], e = New(transports[i], hdrPrv, nodes[i], nil); check(e) {
t.Error(e)
@@ -181,3 +181,82 @@ func TestSendKeys(t *testing.T) {
v.Shutdown()
}
}
func TestSendPurchase(t *testing.T) {
log2.CodeLoc = true
// log2.SetLogLevel(log2.Trace)
const nTotal = 6
var clients [nTotal]*Client
var nodes [nTotal]*node.Node
var transports [nTotal]ifc.Transport
var e error
for i := range transports {
transports[i] = transport.NewSim(nTotal)
}
for i := range nodes {
var hdrPrv, pldPrv *prv.Key
if hdrPrv, e = prv.GenerateKey(); check(e) {
t.Error(e)
t.FailNow()
}
hdrPub := pub.Derive(hdrPrv)
if pldPrv, e = prv.GenerateKey(); check(e) {
t.Error(e)
t.FailNow()
}
pldPub := pub.Derive(pldPrv)
addr := slice.GenerateRandomAddrPortIPv4()
nodes[i], _ = node.New(addr, hdrPub, pldPub, hdrPrv, pldPrv, transports[i])
if clients[i], e = New(transports[i], hdrPrv, nodes[i], nil); check(e) {
t.Error(e)
t.FailNow()
}
clients[i].AddrPort = nodes[i].AddrPort
}
// add each node to each other's Nodes except itself.
for i := range nodes {
for j := range nodes {
if i == j {
continue
}
clients[i].Nodes = append(clients[i].Nodes, nodes[j])
}
}
// Start up the clients.
for _, v := range clients {
go v.Start()
}
var ks *signer.KeySet
if _, ks, e = signer.New(); check(e) {
t.Error(e)
t.FailNow()
}
var hop [nTotal - 1]*node.Node
for i := range clients[0].Nodes {
hop[i] = clients[0].Nodes[i]
}
const nBytes = 2342342
os := wire.SendPurchase(nBytes, clients[0].Node, hop, ks)
// log.I.S(os)
quit := qu.T()
// log.I.S("sending sendkeys with ID", os[len(os)-1].(*confirm.OnionSkin))
// clients[0].RegisterConfirmation(
// os[len(os)-1].(*confirm.OnionSkin).ID,
// func(cf *confirm.OnionSkin) {
// log.I.S("received sendkeys confirmation ID", cf)
// quit.Q()
// },
// )
o := os.Assemble()
b := wire.EncodeOnion(o)
hop[0].Send(b)
go func() {
time.Sleep(time.Second * 2)
quit.Q()
t.Error("sendpurchase got stuck")
}()
<-quit.Wait()
for _, v := range clients {
v.Shutdown()
}
}

View File

@@ -249,3 +249,19 @@ func GenerateRandomAddrPortIPv4() (ap *netip.AddrPort) {
a, e = netip.ParseAddrPort(str)
return &a
}
func GenerateRandomAddrPortIPv6() (ap *netip.AddrPort) {
a := netip.AddrPort{}
b := make([]byte, 19)
_, e := rand.Read(b)
if check(e) {
log.E.Ln(e)
}
port := DecodeUint16(b[5:7])
str := fmt.Sprintf("[%x:%x:%x:%x:%x:%x:%x:%x]:%d",
b[1:3], b[3:5], b[5:7], b[7:9],
b[9:11], b[11:13], b[13:15], b[15:17],
port)
a, e = netip.ParseAddrPort(str)
return &a
}

View File

@@ -17,7 +17,7 @@ var (
check = log.E.Chk
MagicString = "fw"
Magic = slice.Bytes(MagicString)
MinLen = magicbytes.Len + 1 + net.IPv4len
MinLen = magicbytes.Len + 1 + net.IPv6len + 2
_ types.Onion = &OnionSkin{}
)
@@ -34,14 +34,7 @@ func (x *OnionSkin) String() string {
func (x *OnionSkin) Inner() types.Onion { return x.Onion }
func (x *OnionSkin) Insert(o types.Onion) { x.Onion = o }
func (x *OnionSkin) Len() int {
var ap []byte
var e error
if ap, e = x.AddrPort.MarshalBinary(); check(e) {
return -1
}
return magicbytes.Len + 1 + len(ap) + x.Onion.Len()
}
func (x *OnionSkin) Len() int { return MinLen + x.Onion.Len() }
func (x *OnionSkin) Encode(b slice.Bytes, c *slice.Cursor) {
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
@@ -51,7 +44,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)
copy(b[c.Inc(1):c.Inc(MinLen-magicbytes.Len-1)], ap)
x.Onion.Encode(b, c)
}
@@ -60,9 +53,9 @@ func (x *OnionSkin) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
return magicbytes.TooShort(len(b[*c:]), MinLen-magicbytes.Len, string(Magic))
}
apLen := b[*c]
apBytes := b[c.Inc(1):c.Inc(int(apLen))]
apBytes := b[c.Inc(1):c.Inc(MinLen-magicbytes.Len-1)]
x.AddrPort = &netip.AddrPort{}
if e = x.AddrPort.UnmarshalBinary(apBytes); check(e) {
if e = x.AddrPort.UnmarshalBinary(apBytes[:apLen]); check(e) {
return
}
return

View File

@@ -16,7 +16,7 @@ var (
check = log.E.Chk
MagicString = "rl"
Magic = slice.Bytes(MagicString)
MinLen = magicbytes.Len + 1 + net.IPv4len
MinLen = magicbytes.Len + 1 + net.IPv6len + 2
_ types.Onion = &OnionSkin{}
)
@@ -34,14 +34,7 @@ type OnionSkin struct {
func (x *OnionSkin) Inner() types.Onion { return x.Onion }
func (x *OnionSkin) Insert(o types.Onion) { x.Onion = o }
func (x *OnionSkin) Len() int {
var ap []byte
var e error
if ap, e = x.AddrPort.MarshalBinary(); check(e) {
return -1
}
return magicbytes.Len + 1 + len(ap) + x.Onion.Len()
}
func (x *OnionSkin) Len() int { return MinLen + x.Onion.Len() }
func (x *OnionSkin) Encode(b slice.Bytes, c *slice.Cursor) {
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
@@ -51,7 +44,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)
copy(b[c.Inc(1):c.Inc(MinLen-magicbytes.Len-1)], ap)
x.Onion.Encode(b, c)
}
@@ -60,9 +53,9 @@ func (x *OnionSkin) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
return magicbytes.TooShort(len(b[*c:]), MinLen-magicbytes.Len, string(Magic))
}
apLen := b[*c]
apBytes := b[c.Inc(1):c.Inc(int(apLen))]
apBytes := b[c.Inc(1):c.Inc(MinLen-magicbytes.Len-1)]
x.AddrPort = &netip.AddrPort{}
if e = x.AddrPort.UnmarshalBinary(apBytes); check(e) {
if e = x.AddrPort.UnmarshalBinary(apBytes[:apLen]); check(e) {
return
}
return

View File

@@ -10,9 +10,9 @@ var (
// GitRef is the gitref, as in refs/heads/branchname.
GitRef = "refs/heads/protocol"
// ParentGitCommit is the commit hash of the parent HEAD.
ParentGitCommit = "c9ec58220a32504d3879e3d73cf4b066bade224f"
ParentGitCommit = "072f42c0f222facb65f6d9478f9682c65789bf37"
// BuildTime stores the time when the current binary was built.
BuildTime = "2022-12-29T16:17:07Z"
BuildTime = "2022-12-30T12:28:43Z"
// SemVer lists the (latest) git tag on the release.
SemVer = "v0.1.0"
// PathBase is the path base returned from runtime caller.