]> Cypherpunks.ru repositories - nncp.git/blob - src/node.go
Generate ACKs during tossing
[nncp.git] / src / node.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package nncp
19
20 import (
21         "crypto/rand"
22         "errors"
23         "fmt"
24         "sync"
25         "time"
26
27         "github.com/flynn/noise"
28         "golang.org/x/crypto/blake2b"
29         "golang.org/x/crypto/ed25519"
30         "golang.org/x/crypto/nacl/box"
31 )
32
33 const DummyB32Id = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
34
35 type NodeId [blake2b.Size256]byte
36
37 func (id NodeId) String() string {
38         return Base32Codec.EncodeToString(id[:])
39 }
40
41 type Node struct {
42         Name           string
43         Id             *NodeId
44         ExchPub        *[32]byte
45         SignPub        ed25519.PublicKey
46         NoisePub       *[32]byte
47         Exec           map[string][]string
48         Incoming       *string
49         FreqPath       *string
50         FreqChunked    int64
51         FreqMinSize    int64
52         FreqMaxSize    int64
53         ACKNice        uint8
54         ACKMinSize     int64
55         Via            []*NodeId
56         Addrs          map[string]string
57         RxRate         int
58         TxRate         int
59         OnlineDeadline time.Duration
60         MaxOnlineTime  time.Duration
61         Calls          []*Call
62
63         Busy bool
64         sync.Mutex
65 }
66
67 type NodeOur struct {
68         Id       *NodeId
69         ExchPub  *[32]byte
70         ExchPrv  *[32]byte
71         SignPub  ed25519.PublicKey
72         SignPrv  ed25519.PrivateKey
73         NoisePub *[32]byte
74         NoisePrv *[32]byte
75 }
76
77 func NewNodeGenerate() (*NodeOur, error) {
78         exchPub, exchPrv, err := box.GenerateKey(rand.Reader)
79         if err != nil {
80                 return nil, err
81         }
82         signPub, signPrv, err := ed25519.GenerateKey(rand.Reader)
83         if err != nil {
84                 return nil, err
85         }
86         noiseKey, err := noise.DH25519.GenerateKeypair(rand.Reader)
87         if err != nil {
88                 return nil, err
89         }
90         noisePub := new([32]byte)
91         noisePrv := new([32]byte)
92         copy(noisePrv[:], noiseKey.Private)
93         copy(noisePub[:], noiseKey.Public)
94
95         id := NodeId(blake2b.Sum256([]byte(signPub)))
96         node := NodeOur{
97                 Id:       &id,
98                 ExchPub:  exchPub,
99                 ExchPrv:  exchPrv,
100                 SignPub:  signPub,
101                 SignPrv:  signPrv,
102                 NoisePub: noisePub,
103                 NoisePrv: noisePrv,
104         }
105         return &node, nil
106 }
107
108 func (nodeOur *NodeOur) Their() *Node {
109         return &Node{
110                 Name:        "self",
111                 Id:          nodeOur.Id,
112                 ExchPub:     nodeOur.ExchPub,
113                 SignPub:     nodeOur.SignPub,
114                 FreqChunked: MaxFileSize,
115                 FreqMaxSize: MaxFileSize,
116         }
117 }
118
119 func NodeIdFromString(raw string) (*NodeId, error) {
120         decoded, err := Base32Codec.DecodeString(raw)
121         if err != nil {
122                 return nil, fmt.Errorf("Can not parse node: %s: %s", raw, err)
123         }
124         if len(decoded) != blake2b.Size256 {
125                 return nil, errors.New("Invalid node id size")
126         }
127         nodeId := new(NodeId)
128         copy(nodeId[:], decoded)
129         return nodeId, nil
130 }
131
132 func (ctx *Ctx) NodeName(id *NodeId) string {
133         idS := id.String()
134         node, err := ctx.FindNode(idS)
135         if err == nil {
136                 return node.Name
137         }
138         return idS
139 }