Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vikulin committed Dec 22, 2023
1 parent e19556a commit 471cad9
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 80 deletions.
4 changes: 2 additions & 2 deletions cmd/genkeys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"runtime"
"time"

"github.com/Arceliar/ironwood/types"
iwt "github.com/Arceliar/ironwood/types"
"github.com/RiV-chain/RiV-mesh/src/core"
)

Expand All @@ -45,7 +45,7 @@ func main() {
fmt.Println("Pub:", hex.EncodeToString(newKey.pub))
c := &core.Core{}
var bytes [ed25519.PublicKeySize]byte
addr := c.AddrForDomain(types.Domain{Key: bytes, Name: [32]byte(newKey.pub)})
addr := c.AddrForDomain(iwt.Domain{Key: bytes, Name: [32]byte(newKey.pub)})
fmt.Println("IP:", net.IP(addr[:]).String())
}
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/mesh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"syscall"
"time"

"github.com/Arceliar/ironwood/types"
iwt "github.com/Arceliar/ironwood/types"
"github.com/gologme/log"
gsyslog "github.com/hashicorp/go-syslog"
"github.com/hjson/hjson-go"
Expand Down Expand Up @@ -162,21 +162,21 @@ func run(sigCh chan os.Signal) {

n := &node{}

getNodeKey := func() types.Domain {
getNodeKey := func() iwt.Domain {
name := cfg.Domain
return types.Domain{Key: types.PublicKey(publicKey), Name: [ed25519.PublicKeySize]byte([]byte(name))}
return iwt.Domain{Key: iwt.PublicKey(publicKey), Name: [ed25519.PublicKeySize]byte([]byte(name))}
}

switch {
case *getaddr:
if key := getNodeKey(); !key.Equal(types.Domain{}) {
if key := getNodeKey(); !key.Equal(iwt.Domain{}) {
addr := n.core.AddrForDomain(key)
ip := net.IP(addr[:])
fmt.Println(ip.String())
}
return
case *getsnet:
if key := getNodeKey(); !key.Equal(types.Domain{}) {
if key := getNodeKey(); !key.Equal(iwt.Domain{}) {
snet := n.core.SubnetForDomain(key)
ipnet := net.IPNet{
IP: append(snet[:], 0, 0, 0, 0, 0, 0, 0, 0),
Expand Down
18 changes: 9 additions & 9 deletions src/core/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"crypto/tls"
"testing"

"github.com/Arceliar/ironwood/types"
iwt "github.com/Arceliar/ironwood/types"
)

func (c *Core) TestAddress_Address_IsValid(t *testing.T) {
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestAddrForDomain(t *testing.T) {
251, 141, 171, 8, 170, 152, 227, 5, 82, 138, 184, 79, 65, 158, 110, 251,
}

domain := types.NewDomain(name, key)
domain := iwt.NewDomain(name, key)
addr := core.AddrForDomain(domain)

if addr == nil {
Expand All @@ -105,7 +105,7 @@ func TestAddrForDomain(t *testing.T) {
func TestGetAddressDomain(t *testing.T) {
domainName := "example"
var bytes [ed25519.PublicKeySize]byte
expectedDomainName := types.NewDomain(domainName, bytes[:])
expectedDomainName := iwt.NewDomain(domainName, bytes[:])
// Mock Core instance with GetPrefix method
core := &Core{}

Expand All @@ -115,7 +115,7 @@ func TestGetAddressDomain(t *testing.T) {

domain := core.GetAddressDomain(ipv6Address)

if domain.Equal(types.Domain{}) {
if domain.Equal(iwt.Domain{}) {
t.Errorf("Expected non-empty domain, but got empty.")
}

Expand Down Expand Up @@ -153,7 +153,7 @@ func TestSubnetForDomain(t *testing.T) {
251, 141, 171, 8, 170, 152, 227, 5, 82, 138, 184, 79, 65, 158, 110, 251,
}

domain := types.NewDomain(name, key)
domain := iwt.NewDomain(name, key)
addr := core.SubnetForDomain(domain)

if addr == nil {
Expand All @@ -168,7 +168,7 @@ func TestSubnetForDomain(t *testing.T) {
func TestGetSubnetDomain(t *testing.T) {
domainName := "example"
var bytes [ed25519.PublicKeySize]byte
expectedDomainName := types.NewDomain(domainName, bytes[:])
expectedDomainName := iwt.NewDomain(domainName, bytes[:])
// Mock Core instance with GetPrefix method
core := &Core{}

Expand All @@ -178,7 +178,7 @@ func TestGetSubnetDomain(t *testing.T) {

domain := core.GetSubnetDomain(ipv6Address)

if domain.Equal(types.Domain{}) {
if domain.Equal(iwt.Domain{}) {
t.Errorf("Expected non-empty domain, but got empty.")
}

Expand Down Expand Up @@ -214,12 +214,12 @@ func TestMaxLengthDomain(t *testing.T) {
251, 141, 171, 8, 170, 152, 227, 5, 82, 138, 184, 79, 65, 158, 110, 251,
}

domain := types.NewDomain(name, key)
domain := iwt.NewDomain(name, key)
addr := *core.AddrForDomain(domain)

expectedDomain := core.GetAddressDomain(addr)

if expectedDomain.Equal(types.Domain{}) {
if expectedDomain.Equal(iwt.Domain{}) {
t.Errorf("Expected non-empty domain, but got empty.")
}

Expand Down
16 changes: 8 additions & 8 deletions src/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ import (
"net"
"net/url"

"github.com/Arceliar/ironwood/network"
"github.com/Arceliar/ironwood/types"
iwn "github.com/Arceliar/ironwood/network"
iwt "github.com/Arceliar/ironwood/types"
"github.com/Arceliar/phony"
)

type LabelInfo struct {
Sig []byte
Domain types.Domain
Root types.Domain
Domain iwt.Domain
Root iwt.Domain
Seq uint64
Beacon uint64
Path []uint64
}

type SelfInfo struct {
Domain types.Domain
Domain iwt.Domain
PrivateKey ed25519.PrivateKey
Tld string
RoutingEntries uint64
}

type PeerInfo struct {
Domain types.Domain
Root types.Domain
Domain iwt.Domain
Root iwt.Domain
URI string
Up bool
Inbound bool
Expand Down Expand Up @@ -93,7 +93,7 @@ func (c *Core) GetSelf() SelfInfo {

func (c *Core) GetPeers() []PeerInfo {
peers := []PeerInfo{}
conns := map[net.Conn]network.DebugPeerInfo{}
conns := map[net.Conn]iwn.DebugPeerInfo{}
iwpeers := c.PacketConn.PacketConn.Debug.GetPeers()
for _, p := range iwpeers {
conns[p.Conn] = p
Expand Down
8 changes: 4 additions & 4 deletions src/core/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"sync/atomic"
"time"

"github.com/Arceliar/ironwood/types"
iwt "github.com/Arceliar/ironwood/types"
"github.com/Arceliar/phony"

"golang.org/x/crypto/blake2b"
Expand Down Expand Up @@ -68,7 +68,7 @@ type link struct {
}

type linkOptions struct {
pinnedEd25519Keys map[types.PublicKey]struct{}
pinnedEd25519Keys map[iwt.PublicKey]struct{}
priority uint8
tlsSNI string
password []byte
Expand Down Expand Up @@ -161,10 +161,10 @@ func (l *links) add(u *url.URL, sintf string, linkType linkType) error {
retErr = ErrLinkPinnedKeyInvalid
return
}
var sigPubKey types.PublicKey
var sigPubKey iwt.PublicKey
copy(sigPubKey[:], sigPub)
if options.pinnedEd25519Keys == nil {
options.pinnedEd25519Keys = map[types.PublicKey]struct{}{}
options.pinnedEd25519Keys = map[iwt.PublicKey]struct{}{}
}
options.pinnedEd25519Keys[sigPubKey] = struct{}{}
}
Expand Down
26 changes: 12 additions & 14 deletions src/core/nodeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ import (
"runtime"
"time"

"github.com/Arceliar/ironwood/types"
iwt "github.com/Arceliar/ironwood/types"
"github.com/Arceliar/phony"

//"github.com/RiV-chain/RiV-mesh/src/crypto"

"github.com/RiV-chain/RiV-mesh/src/version"
)

type nodeinfo struct {
phony.Inbox
proto *protoHandler
myNodeInfo json.RawMessage
callbacks map[types.PublicKey]nodeinfoCallback
callbacks map[iwt.PublicKey]nodeinfoCallback
}

type nodeinfoCallback struct {
Expand All @@ -37,7 +35,7 @@ func (m *nodeinfo) init(proto *protoHandler) {

func (m *nodeinfo) _init(proto *protoHandler) {
m.proto = proto
m.callbacks = make(map[types.PublicKey]nodeinfoCallback)
m.callbacks = make(map[iwt.PublicKey]nodeinfoCallback)
m._cleanup()
}

Expand All @@ -52,15 +50,15 @@ func (m *nodeinfo) _cleanup() {
})
}

func (m *nodeinfo) _addCallback(sender types.PublicKey, call func(nodeinfo json.RawMessage)) {
func (m *nodeinfo) _addCallback(sender iwt.PublicKey, call func(nodeinfo json.RawMessage)) {
m.callbacks[sender] = nodeinfoCallback{
created: time.Now(),
call: call,
}
}

// Handles the callback, if there is one
func (m *nodeinfo) _callback(sender types.PublicKey, nodeinfo json.RawMessage) {
func (m *nodeinfo) _callback(sender iwt.PublicKey, nodeinfo json.RawMessage) {
if callback, ok := m.callbacks[sender]; ok {
callback.call(nodeinfo)
delete(m.callbacks, sender)
Expand Down Expand Up @@ -102,42 +100,42 @@ func (m *nodeinfo) _setNodeInfo(given map[string]interface{}, privacy bool) erro
}
}

func (m *nodeinfo) sendReq(from phony.Actor, key types.Domain, callback func(nodeinfo json.RawMessage)) {
func (m *nodeinfo) sendReq(from phony.Actor, key iwt.Domain, callback func(nodeinfo json.RawMessage)) {
m.Act(from, func() {
m._sendReq(key, callback)
})
}

func (m *nodeinfo) _sendReq(domain types.Domain, callback func(nodeinfo json.RawMessage)) {
func (m *nodeinfo) _sendReq(domain iwt.Domain, callback func(nodeinfo json.RawMessage)) {
if callback != nil {
key := domain.Key
m._addCallback(key, callback)
}
_, _ = m.proto.core.PacketConn.WriteTo([]byte{typeSessionProto, typeProtoNodeInfoRequest}, types.Addr(domain))
_, _ = m.proto.core.PacketConn.WriteTo([]byte{typeSessionProto, typeProtoNodeInfoRequest}, iwt.Addr(domain))
}

func (m *nodeinfo) handleReq(from phony.Actor, key types.Addr) {
func (m *nodeinfo) handleReq(from phony.Actor, key iwt.Addr) {
m.Act(from, func() {
m._sendRes(key)
})
}

func (m *nodeinfo) handleRes(from phony.Actor, domain types.Addr, info json.RawMessage) {
func (m *nodeinfo) handleRes(from phony.Actor, domain iwt.Addr, info json.RawMessage) {
m.Act(from, func() {
key := domain.Key
m._callback(key, info)
})
}

func (m *nodeinfo) _sendRes(key types.Addr) {
func (m *nodeinfo) _sendRes(key iwt.Addr) {
bs := append([]byte{typeSessionProto, typeProtoNodeInfoResponse}, m._getNodeInfo()...)
_, _ = m.proto.core.PacketConn.WriteTo(bs, key)
}

// Admin socket stuff

type GetNodeInfoRequest struct {
Key types.Domain `json:"key"`
Key iwt.Domain `json:"key"`
}
type GetNodeInfoResponse map[string]json.RawMessage

Expand Down

0 comments on commit 471cad9

Please sign in to comment.