Skip to content

Commit

Permalink
Update core with new NewPacketConn
Browse files Browse the repository at this point in the history
  • Loading branch information
vikulin committed Dec 15, 2023
1 parent 14f1b9e commit 93d1afd
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 4 additions & 5 deletions src/core/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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[:])
Expand Down
45 changes: 40 additions & 5 deletions src/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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{})
Expand Down
17 changes: 17 additions & 0 deletions src/core/pool.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions src/ipv6rwc/ipv6rwc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 93d1afd

Please sign in to comment.