diff --git a/cmd/genkeys/main.go b/cmd/genkeys/main.go index 30b7d604..db807ec0 100644 --- a/cmd/genkeys/main.go +++ b/cmd/genkeys/main.go @@ -19,7 +19,7 @@ import ( "net" "runtime" - "github.com/RiV-chain/RiV-mesh/src/address" + "github.com/RiV-chain/RiV-mesh/src/core" ) type keySet struct { @@ -41,7 +41,7 @@ func main() { fmt.Println("-----") fmt.Println("Priv:", hex.EncodeToString(newKey.priv)) fmt.Println("Pub:", hex.EncodeToString(newKey.pub)) - addr := address.AddrForKey(newKey.pub) + addr := core.AddrForKey(newKey.pub) fmt.Println("IP:", net.IP(addr[:]).String()) } } diff --git a/cmd/mesh/main.go b/cmd/mesh/main.go index adf0e467..5a70b9d7 100644 --- a/cmd/mesh/main.go +++ b/cmd/mesh/main.go @@ -25,7 +25,7 @@ import ( "github.com/kardianos/minwinsvc" "github.com/mitchellh/mapstructure" - "github.com/RiV-chain/RiV-mesh/src/address" + //"github.com/RiV-chain/RiV-mesh/src/address" "github.com/RiV-chain/RiV-mesh/src/admin" "github.com/RiV-chain/RiV-mesh/src/config" "github.com/RiV-chain/RiV-mesh/src/defaults" @@ -261,6 +261,7 @@ func run(args yggArgs, ctx context.Context) { if cfg == nil { return } + n := &node{} // Have we been asked for the node address yet? If so, print it and then stop. getNodeKey := func() ed25519.PublicKey { if pubkey, err := hex.DecodeString(cfg.PrivateKey); err == nil { @@ -271,14 +272,14 @@ func run(args yggArgs, ctx context.Context) { switch { case args.getaddr: if key := getNodeKey(); key != nil { - addr := address.AddrForKey(key) + addr := n.core.AddrForKey(key) ip := net.IP(addr[:]) fmt.Println(ip.String()) } return case args.getsnet: if key := getNodeKey(); key != nil { - snet := address.SubnetForKey(key) + snet := n.core.SubnetForKey(key) ipnet := net.IPNet{ IP: append(snet[:], 0, 0, 0, 0, 0, 0, 0, 0), Mask: net.CIDRMask(len(snet)*8, 128), @@ -291,7 +292,6 @@ func run(args yggArgs, ctx context.Context) { cfg.HttpAddress = args.httpaddress cfg.WwwRoot = args.wwwroot - n := &node{} // Setup the RiV-mesh node itself. { sk, err := hex.DecodeString(cfg.PrivateKey) diff --git a/contrib/mobile/mobile.go b/contrib/mobile/mobile.go index d5e01d2d..37a9244f 100644 --- a/contrib/mobile/mobile.go +++ b/contrib/mobile/mobile.go @@ -9,7 +9,7 @@ import ( "github.com/gologme/log" - "github.com/RiV-chain/RiV-mesh/src/address" + //"github.com/RiV-chain/RiV-mesh/src/address" "github.com/RiV-chain/RiV-mesh/src/config" "github.com/RiV-chain/RiV-mesh/src/core" "github.com/RiV-chain/RiV-mesh/src/defaults" @@ -176,7 +176,7 @@ func (m *Mesh) GetPeersJSON() (result string) { IP string }{} for _, v := range m.core.GetPeers() { - a := address.AddrForKey(v.Key) + a := m.core.AddrForKey(v.Key) ip := net.IP(a[:]).String() peers = append(peers, struct { core.PeerInfo diff --git a/src/admin/getdht.go b/src/admin/getdht.go index 074c717a..71248a2f 100644 --- a/src/admin/getdht.go +++ b/src/admin/getdht.go @@ -6,7 +6,7 @@ import ( "sort" "strings" - "github.com/RiV-chain/RiV-mesh/src/address" + //"github.com/RiV-chain/RiV-mesh/src/address" ) type GetDHTRequest struct{} @@ -26,7 +26,7 @@ func (a *AdminSocket) getDHTHandler(req *GetDHTRequest, res *GetDHTResponse) err dht := a.core.GetDHT() res.DHT = make([]DHTEntry, 0, len(dht)) for _, d := range dht { - addr := address.AddrForKey(d.Key) + addr := a.core.AddrForKey(d.Key) res.DHT = append(res.DHT, DHTEntry{ IPAddress: net.IP(addr[:]).String(), PublicKey: hex.EncodeToString(d.Key[:]), diff --git a/src/admin/getpaths.go b/src/admin/getpaths.go index 9324097b..45760db5 100644 --- a/src/admin/getpaths.go +++ b/src/admin/getpaths.go @@ -6,7 +6,7 @@ import ( "sort" "strings" - "github.com/RiV-chain/RiV-mesh/src/address" + //"github.com/RiV-chain/RiV-mesh/src/address" ) type GetPathsRequest struct { @@ -26,7 +26,7 @@ func (a *AdminSocket) getPathsHandler(req *GetPathsRequest, res *GetPathsRespons paths := a.core.GetPaths() res.Paths = make([]PathEntry, 0, len(paths)) for _, p := range paths { - addr := address.AddrForKey(p.Key) + addr := a.core.AddrForKey(p.Key) res.Paths = append(res.Paths, PathEntry{ IPAddress: net.IP(addr[:]).String(), PublicKey: hex.EncodeToString(p.Key), diff --git a/src/admin/getpeers.go b/src/admin/getpeers.go index 04927a6e..9a4d0454 100644 --- a/src/admin/getpeers.go +++ b/src/admin/getpeers.go @@ -5,7 +5,7 @@ import ( "net" "sort" - "github.com/RiV-chain/RiV-mesh/src/address" + //"github.com/RiV-chain/RiV-mesh/src/address" ) type GetPeersRequest struct { @@ -31,7 +31,7 @@ func (a *AdminSocket) getPeersHandler(req *GetPeersRequest, res *GetPeersRespons peers := a.core.GetPeers() res.Peers = make([]PeerEntry, 0, len(peers)) for _, p := range peers { - addr := address.AddrForKey(p.Key) + addr := a.core.AddrForKey(p.Key) res.Peers = append(res.Peers, PeerEntry{ IPAddress: net.IP(addr[:]).String(), PublicKey: hex.EncodeToString(p.Key), diff --git a/src/admin/getsessions.go b/src/admin/getsessions.go index 190edd7c..a5ae0d1b 100644 --- a/src/admin/getsessions.go +++ b/src/admin/getsessions.go @@ -6,7 +6,7 @@ import ( "sort" "strings" - "github.com/RiV-chain/RiV-mesh/src/address" + //"github.com/RiV-chain/RiV-mesh/src/address" ) type GetSessionsRequest struct{} @@ -27,7 +27,7 @@ func (a *AdminSocket) getSessionsHandler(req *GetSessionsRequest, res *GetSessio sessions := a.core.GetSessions() res.Sessions = make([]SessionEntry, 0, len(sessions)) for _, s := range sessions { - addr := address.AddrForKey(s.Key) + addr := a.core.AddrForKey(s.Key) res.Sessions = append(res.Sessions, SessionEntry{ IPAddress: net.IP(addr[:]).String(), PublicKey: hex.EncodeToString(s.Key[:]), diff --git a/src/address/address.go b/src/core/address.go similarity index 96% rename from src/address/address.go rename to src/core/address.go index 723f1aca..b9440a47 100644 --- a/src/address/address.go +++ b/src/core/address.go @@ -1,6 +1,6 @@ // Package address contains the types used by mesh to represent IPv6 addresses or prefixes, as well as functions for working with these types. // Of particular importance are the functions used to derive addresses or subnets from a NodeID, or to get the NodeID and bitmask of the bits visible from an address, which is needed for DHT searches. -package address +package core import ( "crypto/ed25519" @@ -48,7 +48,7 @@ func (s *Subnet) IsValid() bool { // This address begins with the contents of GetPrefix(), with the last bit set to 0 to indicate an address. // The following 8 bits are set to the number of leading 1 bits in the bitwise inverse of the public key. // The bitwise inverse of the key, excluding the leading 1 bits and the first leading 0 bit, is truncated to the appropriate length and makes up the remainder of the address. -func AddrForKey(publicKey ed25519.PublicKey) *Address { +func (c *Core) AddrForKey(publicKey ed25519.PublicKey) *Address { // 128 bit address // Begins with prefix // Next bit is a 0 @@ -98,11 +98,11 @@ func AddrForKey(publicKey ed25519.PublicKey) *Address { // The subnet begins with the address prefix, with the last bit set to 1 to indicate a prefix. // The following 8 bits are set to the number of leading 1 bits in the bitwise inverse of the key. // The bitwise inverse of the key, excluding the leading 1 bits and the first leading 0 bit, is truncated to the appropriate length and makes up the remainder of the subnet. -func SubnetForKey(publicKey ed25519.PublicKey) *Subnet { +func (c *Core) SubnetForKey(publicKey ed25519.PublicKey) *Subnet { // Exactly as the address version, with two exceptions: // 1) The first bit after the fixed prefix is a 1 instead of a 0 // 2) It's truncated to a subnet prefix length instead of 128 bits - addr := AddrForKey(publicKey) + addr := c.AddrForKey(publicKey) if addr == nil { return nil } diff --git a/src/address/address_test.go b/src/core/address_test.go similarity index 99% rename from src/address/address_test.go rename to src/core/address_test.go index c5e202eb..f7673ca4 100644 --- a/src/address/address_test.go +++ b/src/core/address_test.go @@ -1,4 +1,4 @@ -package address +package core import ( "bytes" diff --git a/src/core/api.go b/src/core/api.go index d93be4c7..f789af20 100644 --- a/src/core/api.go +++ b/src/core/api.go @@ -17,7 +17,7 @@ import ( //"time" "github.com/Arceliar/phony" - "github.com/RiV-chain/RiV-mesh/src/address" + //"github.com/RiV-chain/RiV-mesh/src/address" ) type SelfInfo struct { @@ -159,7 +159,7 @@ func (c *Core) Listen(u *url.URL, sintf string) (*Listener, error) { // that application also implements either VPN functionality or deals with IP // packets specifically. func (c *Core) Address() net.IP { - addr := net.IP(address.AddrForKey(c.public)[:]) + addr := net.IP(c.AddrForKey(c.public)[:]) return addr } @@ -169,7 +169,7 @@ func (c *Core) Address() net.IP { // that application also implements either VPN functionality or deals with IP // packets specifically. func (c *Core) Subnet() net.IPNet { - subnet := address.SubnetForKey(c.public)[:] + subnet := c.SubnetForKey(c.public)[:] subnet = append(subnet, 0, 0, 0, 0, 0, 0, 0, 0) return net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)} } diff --git a/src/core/core.go b/src/core/core.go index f5e1234b..3d5ec2e8 100644 --- a/src/core/core.go +++ b/src/core/core.go @@ -20,6 +20,7 @@ import ( // The Core object represents the Mesh node. You should create a Core // object for each Mesh node you plan to run. type Core struct { + address Address // This is the main data structure that holds everything else for a node // 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 diff --git a/src/core/link.go b/src/core/link.go index 6957cff3..8378b087 100644 --- a/src/core/link.go +++ b/src/core/link.go @@ -16,7 +16,7 @@ import ( "sync/atomic" "github.com/Arceliar/phony" - "github.com/RiV-chain/RiV-mesh/src/address" + //"github.com/RiV-chain/RiV-mesh/src/address" //"github.com/Arceliar/phony" // TODO? use instead of mutexes ) @@ -188,7 +188,7 @@ func (intf *link) handler() error { if intf.incoming { dir = "inbound" } - remoteAddr := net.IP(address.AddrForKey(meta.key)[:]).String() + remoteAddr := net.IP(intf.links.core.AddrForKey(meta.key)[:]).String() remoteStr := fmt.Sprintf("%s@%s", remoteAddr, intf.info.remote) localStr := intf.conn.LocalAddr() intf.links.core.log.Infof("Connected %s %s: %s, source %s", diff --git a/src/core/proto.go b/src/core/proto.go index a1749c53..76ad4b25 100644 --- a/src/core/proto.go +++ b/src/core/proto.go @@ -12,7 +12,7 @@ import ( iwt "github.com/Arceliar/ironwood/types" "github.com/Arceliar/phony" - "github.com/RiV-chain/RiV-mesh/src/address" + //"github.com/RiV-chain/RiV-mesh/src/address" ) const ( @@ -266,7 +266,7 @@ func (p *protoHandler) getSelfHandler(in json.RawMessage) (interface{}, error) { if err := msg.UnmarshalJSON(info); err != nil { return nil, err } - ip := net.IP(address.AddrForKey(kbs)[:]) + ip := net.IP(p.core.AddrForKey(kbs)[:]) res := DebugGetSelfResponse{ip.String(): msg} return res, nil } @@ -316,7 +316,7 @@ func (p *protoHandler) getPeersHandler(in json.RawMessage) (interface{}, error) if err := msg.UnmarshalJSON(js); err != nil { return nil, err } - ip := net.IP(address.AddrForKey(kbs)[:]) + ip := net.IP(p.core.AddrForKey(kbs)[:]) res := DebugGetPeersResponse{ip.String(): msg} return res, nil } @@ -366,7 +366,7 @@ func (p *protoHandler) getDHTHandler(in json.RawMessage) (interface{}, error) { if err := msg.UnmarshalJSON(js); err != nil { return nil, err } - ip := net.IP(address.AddrForKey(kbs)[:]) + ip := net.IP(p.core.AddrForKey(kbs)[:]) res := DebugGetDHTResponse{ip.String(): msg} return res, nil } diff --git a/src/ipv6rwc/ipv6rwc.go b/src/ipv6rwc/ipv6rwc.go index ee44e302..646bd74d 100644 --- a/src/ipv6rwc/ipv6rwc.go +++ b/src/ipv6rwc/ipv6rwc.go @@ -13,7 +13,7 @@ import ( iwt "github.com/Arceliar/ironwood/types" - "github.com/RiV-chain/RiV-mesh/src/address" + //"github.com/RiV-chain/RiV-mesh/src/address" "github.com/RiV-chain/RiV-mesh/src/core" ) @@ -30,21 +30,21 @@ type keyArray [ed25519.PublicKeySize]byte type keyStore struct { core *core.Core - address address.Address - subnet address.Subnet + address core.Address + subnet core.Subnet mutex sync.Mutex keyToInfo map[keyArray]*keyInfo - addrToInfo map[address.Address]*keyInfo - addrBuffer map[address.Address]*buffer - subnetToInfo map[address.Subnet]*keyInfo - subnetBuffer map[address.Subnet]*buffer + addrToInfo map[core.Address]*keyInfo + addrBuffer map[core.Address]*buffer + subnetToInfo map[core.Subnet]*keyInfo + subnetBuffer map[core.Subnet]*buffer mtu uint64 } type keyInfo struct { key keyArray - address address.Address - subnet address.Subnet + address core.Address + subnet core.Subnet timeout *time.Timer // From calling a time.AfterFunc to do cleanup } @@ -55,21 +55,21 @@ type buffer struct { func (k *keyStore) init(c *core.Core) { k.core = c - k.address = *address.AddrForKey(k.core.PublicKey()) - k.subnet = *address.SubnetForKey(k.core.PublicKey()) + k.address = *c.AddrForKey(k.core.PublicKey()) + k.subnet = *c.SubnetForKey(k.core.PublicKey()) if err := k.core.SetOutOfBandHandler(k.oobHandler); err != nil { err = fmt.Errorf("tun.core.SetOutOfBandHander: %w", err) panic(err) } k.keyToInfo = make(map[keyArray]*keyInfo) - k.addrToInfo = make(map[address.Address]*keyInfo) - k.addrBuffer = make(map[address.Address]*buffer) - k.subnetToInfo = make(map[address.Subnet]*keyInfo) - k.subnetBuffer = make(map[address.Subnet]*buffer) + k.addrToInfo = make(map[core.Address]*keyInfo) + k.addrBuffer = make(map[core.Address]*buffer) + k.subnetToInfo = make(map[core.Subnet]*keyInfo) + k.subnetBuffer = make(map[core.Subnet]*buffer) k.mtu = 1280 // Default to something safe, expect user to set this } -func (k *keyStore) sendToAddress(addr address.Address, bs []byte) { +func (k *keyStore) sendToAddress(addr core.Address, bs []byte) { k.mutex.Lock() if info := k.addrToInfo[addr]; info != nil { k.resetTimeout(info) @@ -98,7 +98,7 @@ func (k *keyStore) sendToAddress(addr address.Address, bs []byte) { } } -func (k *keyStore) sendToSubnet(subnet address.Subnet, bs []byte) { +func (k *keyStore) sendToSubnet(subnet core.Subnet, bs []byte) { k.mutex.Lock() if info := k.subnetToInfo[subnet]; info != nil { k.resetTimeout(info) @@ -136,8 +136,8 @@ func (k *keyStore) update(key ed25519.PublicKey) *keyInfo { if info = k.keyToInfo[kArray]; info == nil { info = new(keyInfo) info.key = kArray - info.address = *address.AddrForKey(ed25519.PublicKey(info.key[:])) - info.subnet = *address.SubnetForKey(ed25519.PublicKey(info.key[:])) + info.address = *k.core.AddrForKey(ed25519.PublicKey(info.key[:])) + info.subnet = *k.core.SubnetForKey(ed25519.PublicKey(info.key[:])) k.keyToInfo[info.key] = info k.addrToInfo[info.address] = info k.subnetToInfo[info.subnet] = info @@ -184,7 +184,7 @@ func (k *keyStore) oobHandler(fromKey, toKey ed25519.PublicKey, data []byte) { sig := data[1:] switch data[0] { case typeKeyLookup: - snet := *address.SubnetForKey(toKey) + snet := *k.core.SubnetForKey(toKey) if snet == k.subnet && ed25519.Verify(fromKey, toKey[:], sig) { // This is looking for at least our subnet (possibly our address) // Send a response @@ -248,8 +248,8 @@ func (k *keyStore) readPC(p []byte) (int, error) { } continue } - var srcAddr, dstAddr address.Address - var srcSubnet, dstSubnet address.Subnet + var srcAddr, dstAddr core.Address + var srcSubnet, dstSubnet core.Subnet copy(srcAddr[:], bs[8:]) copy(dstAddr[:], bs[24:]) copy(srcSubnet[:], bs[8:]) @@ -274,8 +274,8 @@ func (k *keyStore) writePC(bs []byte) (int, error) { strErr := fmt.Sprint("undersized IPv6 packet, length: ", len(bs)) return 0, errors.New(strErr) } - var srcAddr, dstAddr address.Address - var srcSubnet, dstSubnet address.Subnet + var srcAddr, dstAddr core.Address + var srcSubnet, dstSubnet core.Subnet copy(srcAddr[:], bs[8:]) copy(dstAddr[:], bs[24:]) copy(srcSubnet[:], bs[8:]) @@ -331,11 +331,11 @@ func NewReadWriteCloser(c *core.Core) *ReadWriteCloser { return rwc } -func (rwc *ReadWriteCloser) Address() address.Address { +func (rwc *ReadWriteCloser) Address() core.Address { return rwc.address } -func (rwc *ReadWriteCloser) Subnet() address.Subnet { +func (rwc *ReadWriteCloser) Subnet() core.Subnet { return rwc.subnet } diff --git a/src/tun/tun.go b/src/tun/tun.go index abef70ba..d4579095 100644 --- a/src/tun/tun.go +++ b/src/tun/tun.go @@ -13,7 +13,7 @@ import ( "github.com/Arceliar/phony" "golang.zx2c4.com/wireguard/tun" - "github.com/RiV-chain/RiV-mesh/src/address" + //"github.com/RiV-chain/RiV-mesh/src/address" "github.com/RiV-chain/RiV-mesh/src/core" "github.com/RiV-chain/RiV-mesh/src/defaults" "github.com/RiV-chain/RiV-mesh/src/ipv6rwc" @@ -28,8 +28,8 @@ type MTU uint16 type TunAdapter struct { rwc *ipv6rwc.ReadWriteCloser log core.Logger - addr address.Address - subnet address.Subnet + addr core.Address + subnet core.Subnet mtu uint64 iface tun.Device phony.Inbox // Currently only used for _handlePacket from the reader, TODO: all the stuff that currently needs a mutex below @@ -107,7 +107,7 @@ func (tun *TunAdapter) _start() error { } tun.addr = tun.rwc.Address() tun.subnet = tun.rwc.Subnet() - addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1) + addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(core.GetPrefix())-1) if tun.config.name == "none" || tun.config.name == "dummy" { tun.log.Debugln("Not starting TUN as ifname is none or dummy") tun.isEnabled = false