From 93d1afd3678622491f027b7fe76e33e8c244efad Mon Sep 17 00:00:00 2001 From: Vadym Vikulin Date: Sat, 16 Dec 2023 01:55:44 +0200 Subject: [PATCH] Update core with new NewPacketConn --- go.mod | 2 +- go.sum | 4 ++-- src/core/address.go | 9 ++++----- src/core/core.go | 45 +++++++++++++++++++++++++++++++++++++----- src/core/pool.go | 17 ++++++++++++++++ src/ipv6rwc/ipv6rwc.go | 3 +++ 6 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 src/core/pool.go diff --git a/go.mod b/go.mod index 319917689..156a45dfa 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/RiV-chain/RiV-mesh go 1.21 -replace github.com/Arceliar/ironwood => github.com/RiV-chain/ironwood v0.0.0-20231215220801-c35d010e1cc0 +replace github.com/Arceliar/ironwood => github.com/RiV-chain/ironwood v0.0.0-20231215234453-2727995ce8e9 replace github.com/mikispag/dns-over-tls-forwarder => github.com/RiV-chain/dns-over-tls-forwarder v0.0.0-20230828114909-c2cd9f8d79d3 diff --git a/go.sum b/go.sum index 58510826c..e4091765f 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/Arceliar/phony v0.0.0-20220903101357-530938a4b13d h1:UK9fsWbWqwIQkMCz github.com/Arceliar/phony v0.0.0-20220903101357-530938a4b13d/go.mod h1:BCnxhRf47C/dy/e/D2pmB8NkB3dQVIrkD98b220rx5Q= github.com/RiV-chain/dns-over-tls-forwarder v0.0.0-20230828114909-c2cd9f8d79d3 h1:gz71d+oEAMXYUYw54JKT4A6CxniQx6B9J0G5CYJeLok= github.com/RiV-chain/dns-over-tls-forwarder v0.0.0-20230828114909-c2cd9f8d79d3/go.mod h1:V2Irj3BjF2tLZ3xXp6TMQCu0I+eJWQPEktbLMHQN8XA= -github.com/RiV-chain/ironwood v0.0.0-20231215220801-c35d010e1cc0 h1:17qzo0LTxGO1Z841EFldw3jHudk0R6ZQj7RrBbFX6RU= -github.com/RiV-chain/ironwood v0.0.0-20231215220801-c35d010e1cc0/go.mod h1:O9iIMM9iVSXUIKNcrjossDuuXLwoGNuLSDXqjtTBHJk= +github.com/RiV-chain/ironwood v0.0.0-20231215234453-2727995ce8e9 h1:Y9Gq1Icnnw/7bpi7G6UD4vSa3ZUxbBTLOBCmCTJ6UuA= +github.com/RiV-chain/ironwood v0.0.0-20231215234453-2727995ce8e9/go.mod h1:O9iIMM9iVSXUIKNcrjossDuuXLwoGNuLSDXqjtTBHJk= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/bits-and-blooms/bitset v1.3.1/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA= diff --git a/src/core/address.go b/src/core/address.go index f387e5625..f2a106d79 100644 --- a/src/core/address.go +++ b/src/core/address.go @@ -100,9 +100,8 @@ func (c *Core) SubnetForDomain(domain iwt.Domain) *Subnet { return &snet } -// GetKet returns the Domain.Name for the Address. -// This is used for key lookup. - +// Returns the partial Domain for the Address. +// This is used for domain lookup. func (c *Core) GetAddressDomain(a Address) iwt.Domain { name, err := decodeIPv6(a) if err != nil { @@ -114,8 +113,8 @@ func (c *Core) GetAddressDomain(a Address) iwt.Domain { return iwt.NewDomain(string(name), bytes[:]) } -// GetKet returns the partial ed25519.PublicKey for the Subnet. -// This is used for key lookup. +// Returns the partial Domain for the Subnet. +// This is used for domain lookup. func (c *Core) GetSubnetDomain(s Subnet) iwt.Domain { var addr Address copy(addr[:], s[:]) diff --git a/src/core/core.go b/src/core/core.go index 2d64208bf..815024070 100644 --- a/src/core/core.go +++ b/src/core/core.go @@ -11,6 +11,7 @@ import ( "time" iwe "github.com/Arceliar/ironwood/encrypted" + iwn "github.com/Arceliar/ironwood/network" iwt "github.com/Arceliar/ironwood/types" "github.com/Arceliar/phony" @@ -47,19 +48,24 @@ type Core struct { networkdomain NetworkDomain // immutable after startup ddnsserver DDnsServer // ddns config } + pathNotify func(iwt.Domain) } func New(secret ed25519.PrivateKey, logger Logger, opts ...SetupOption) (*Core, error) { c := &Core{ log: logger, } + c.ctx, c.cancel = context.WithCancel(context.Background()) + if c.log == nil { + c.log = log.New(io.Discard, "", 0) + } if name := version.BuildName(); name != "unknown" { c.log.Infoln("Build name:", name) } if version := version.BuildVersion(); version != "unknown" { c.log.Infoln("Build version:", version) } - c.ctx, c.cancel = context.WithCancel(context.Background()) + // Take a copy of the private key so that it is in our own memory space. if len(secret) != ed25519.PrivateKeySize { return nil, fmt.Errorf("private key is incorrect length") @@ -74,7 +80,16 @@ func New(secret ed25519.PrivateKey, logger Logger, opts ...SetupOption) (*Core, } c.public = iwt.NewDomain(string(c.config.domain), secret.Public().(ed25519.PublicKey)) var err error - if c.PacketConn, err = iwe.NewPacketConn(c.secret, c.public); err != nil { + keyXform := func(key iwt.Domain) iwt.Domain { + return key + } + if c.PacketConn, err = iwe.NewPacketConn( + c.secret, + c.public, + iwn.WithBloomTransform(keyXform), + iwn.WithPeerMaxMessageSize(65535*2), + iwn.WithPathNotify(c.doPathNotify), + ); err != nil { return nil, fmt.Errorf("error creating encryption: %w", err) } if c.log == nil { @@ -173,11 +188,16 @@ func (c *Core) _close() error { func (c *Core) MTU() uint64 { const sessionTypeOverhead = 1 - return c.PacketConn.MTU() - sessionTypeOverhead + MTU := c.PacketConn.MTU() - sessionTypeOverhead + if MTU > 65535 { + MTU = 65535 + } + return MTU } func (c *Core) ReadFrom(p []byte) (n int, from net.Addr, err error) { - buf := make([]byte, c.PacketConn.MTU(), 65535) + buf := allocBytes(int(c.PacketConn.MTU())) + defer freeBytes(buf) for { bs := buf n, from, err = c.PacketConn.ReadFrom(bs) @@ -210,7 +230,8 @@ func (c *Core) ReadFrom(p []byte) (n int, from net.Addr, err error) { } func (c *Core) WriteTo(p []byte, addr net.Addr) (n int, err error) { - buf := make([]byte, 0, 65535) + buf := allocBytes(0) + defer freeBytes(buf) buf = append(buf, typeSessionTraffic) buf = append(buf, p...) n, err = c.PacketConn.WriteTo(buf, addr) @@ -220,6 +241,20 @@ func (c *Core) WriteTo(p []byte, addr net.Addr) (n int, err error) { return } +func (c *Core) doPathNotify(key iwt.Domain) { + c.Act(nil, func() { + if c.pathNotify != nil { + c.pathNotify(key) + } + }) +} + +func (c *Core) SetPathNotify(notify func(iwt.Domain)) { + c.Act(nil, func() { + c.pathNotify = notify + }) +} + type Logger interface { Printf(string, ...interface{}) Println(...interface{}) diff --git a/src/core/pool.go b/src/core/pool.go new file mode 100644 index 000000000..7b1e93ed2 --- /dev/null +++ b/src/core/pool.go @@ -0,0 +1,17 @@ +package core + +import "sync" + +var bytePool = sync.Pool{New: func() interface{} { return []byte(nil) }} + +func allocBytes(size int) []byte { + bs := bytePool.Get().([]byte) + if cap(bs) < size { + bs = make([]byte, size) + } + return bs[:size] +} + +func freeBytes(bs []byte) { + bytePool.Put(bs[:0]) //nolint:staticcheck +} diff --git a/src/ipv6rwc/ipv6rwc.go b/src/ipv6rwc/ipv6rwc.go index a2891c151..e90f13de1 100644 --- a/src/ipv6rwc/ipv6rwc.go +++ b/src/ipv6rwc/ipv6rwc.go @@ -51,6 +51,9 @@ func (k *keyStore) init(c *core.Core) { k.core = c k.address = *c.AddrForDomain(k.core.GetSelf().Domain) k.subnet = *c.SubnetForDomain(k.core.GetSelf().Domain) + k.core.SetPathNotify(func(key types.Domain) { + k.update(key) + }) k.keyToInfo = make(map[keyArray]*keyInfo) k.addrToInfo = make(map[core.Address]*keyInfo) k.addrBuffer = make(map[core.Address]*buffer)