mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 14:15:06 +03:00
have the core wrap and export the underlying PacketConn, move IPv6 ReadWriteCloser wrapper logic to a separate package
This commit is contained in:
parent
35e8ff7c9d
commit
f990a56046
11 changed files with 170 additions and 110 deletions
|
@ -7,10 +7,12 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
iw "github.com/Arceliar/ironwood/encrypted"
|
||||
iwe "github.com/Arceliar/ironwood/encrypted"
|
||||
iwt "github.com/Arceliar/ironwood/types"
|
||||
"github.com/Arceliar/phony"
|
||||
"github.com/gologme/log"
|
||||
|
||||
|
@ -26,13 +28,12 @@ type Core struct {
|
|||
// We're going to keep our own copy of the provided config - that way we can
|
||||
// guarantee that it will be covered by the mutex
|
||||
phony.Inbox
|
||||
pc *iw.PacketConn
|
||||
*iwe.PacketConn
|
||||
config *config.NodeConfig // Config
|
||||
secret ed25519.PrivateKey
|
||||
public ed25519.PublicKey
|
||||
links links
|
||||
proto protoHandler
|
||||
store keyStore
|
||||
log *log.Logger
|
||||
addPeerTimer *time.Timer
|
||||
ctx context.Context
|
||||
|
@ -62,9 +63,8 @@ func (c *Core) _init() error {
|
|||
c.public = c.secret.Public().(ed25519.PublicKey)
|
||||
// TODO check public against current.PublicKey, error if they don't match
|
||||
|
||||
c.pc, err = iw.NewPacketConn(c.secret)
|
||||
c.PacketConn, err = iwe.NewPacketConn(c.secret)
|
||||
c.ctx, c.ctxCancel = context.WithCancel(context.Background())
|
||||
c.store.init(c)
|
||||
c.proto.init(c)
|
||||
if err := c.proto.nodeinfo.setNodeInfo(c.config.NodeInfo, c.config.NodeInfoPrivacy); err != nil {
|
||||
return fmt.Errorf("setNodeInfo: %w", err)
|
||||
|
@ -168,7 +168,7 @@ func (c *Core) Stop() {
|
|||
func (c *Core) _stop() {
|
||||
c.log.Infoln("Stopping...")
|
||||
c.ctxCancel()
|
||||
c.pc.Close()
|
||||
c.PacketConn.Close() // TODO make c.Close() do the right thing (act like c.Stop())
|
||||
if c.addPeerTimer != nil {
|
||||
c.addPeerTimer.Stop()
|
||||
c.addPeerTimer = nil
|
||||
|
@ -181,3 +181,53 @@ func (c *Core) _stop() {
|
|||
*/
|
||||
c.log.Infoln("Stopped")
|
||||
}
|
||||
|
||||
func (c *Core) MTU() uint64 {
|
||||
const sessionTypeOverhead = 1
|
||||
return c.PacketConn.MTU() - sessionTypeOverhead
|
||||
}
|
||||
|
||||
func (c *Core) ReadFrom(p []byte) (n int, from net.Addr, err error) {
|
||||
buf := make([]byte, c.PacketConn.MTU(), 65535)
|
||||
for {
|
||||
bs := buf
|
||||
n, from, err = c.PacketConn.ReadFrom(bs)
|
||||
if err != nil {
|
||||
return 0, from, err
|
||||
}
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
switch bs[0] {
|
||||
case typeSessionTraffic:
|
||||
// This is what we want to handle here
|
||||
case typeSessionProto:
|
||||
var key keyArray
|
||||
copy(key[:], from.(iwt.Addr))
|
||||
data := append([]byte(nil), bs[1:n]...)
|
||||
c.proto.handleProto(nil, key, data)
|
||||
continue
|
||||
default:
|
||||
continue
|
||||
}
|
||||
bs = bs[1:n]
|
||||
copy(p, bs)
|
||||
if len(p) < len(bs) {
|
||||
n = len(p)
|
||||
} else {
|
||||
n = len(bs)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Core) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
||||
buf := make([]byte, 0, 65535)
|
||||
buf = append(buf, typeSessionTraffic)
|
||||
buf = append(buf, p...)
|
||||
n, err = c.PacketConn.WriteTo(buf, addr)
|
||||
if n > 0 {
|
||||
n -= 1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue