2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
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, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 "golang.org/x/crypto/ed25519"
30 DefaultCfgPath string = "/usr/local/etc/nncp.yaml"
31 DefaultSendmailPath string = "/usr/sbin/sendmail"
34 type NodeYAML struct {
39 Incoming *string `incoming,omitempty`
40 Freq *string `freq,omitempty`
41 Via []string `via,omitempty`
43 Addrs map[string]string `addrs,omitempty`
46 type NodeOurYAML struct {
56 type FromToYAML struct {
61 type NotifyYAML struct {
62 File *FromToYAML `file,omitempty`
63 Freq *FromToYAML `freq,omitempty`
68 Neigh map[string]NodeYAML
73 Notify *NotifyYAML `notify,omitempty`
76 func NewNode(name string, yml NodeYAML) (*Node, error) {
77 nodeId, err := NodeIdFromString(yml.Id)
82 exchPub, err := FromBase32(yml.ExchPub)
86 if len(exchPub) != 32 {
87 return nil, errors.New("Invalid exchPub size")
90 signPub, err := FromBase32(yml.SignPub)
94 if len(signPub) != ed25519.PublicKeySize {
95 return nil, errors.New("Invalid signPub size")
98 noisePub, err := FromBase32(yml.NoisePub)
102 if len(noisePub) != 32 {
103 return nil, errors.New("Invalid noisePub size")
107 if yml.Incoming != nil {
108 inc := path.Clean(*yml.Incoming)
109 if !path.IsAbs(inc) {
110 return nil, errors.New("Incoming path must be absolute")
117 fr := path.Clean(*yml.Freq)
119 return nil, errors.New("Freq path must be absolute")
127 ExchPub: new([32]byte),
128 SignPub: ed25519.PublicKey(signPub),
129 NoisePub: new([32]byte),
134 copy(node.ExchPub[:], exchPub)
135 copy(node.NoisePub[:], noisePub)
139 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
140 id, err := NodeIdFromString(yml.Id)
145 exchPub, err := FromBase32(yml.ExchPub)
149 if len(exchPub) != 32 {
150 return nil, errors.New("Invalid exchPub size")
153 exchPrv, err := FromBase32(yml.ExchPrv)
157 if len(exchPrv) != 32 {
158 return nil, errors.New("Invalid exchPrv size")
161 signPub, err := FromBase32(yml.SignPub)
165 if len(signPub) != ed25519.PublicKeySize {
166 return nil, errors.New("Invalid signPub size")
169 signPrv, err := FromBase32(yml.SignPrv)
173 if len(signPrv) != ed25519.PrivateKeySize {
174 return nil, errors.New("Invalid signPrv size")
177 noisePub, err := FromBase32(yml.NoisePub)
181 if len(noisePub) != 32 {
182 return nil, errors.New("Invalid noisePub size")
185 noisePrv, err := FromBase32(yml.NoisePrv)
189 if len(noisePrv) != 32 {
190 return nil, errors.New("Invalid noisePrv size")
195 ExchPub: new([32]byte),
196 ExchPrv: new([32]byte),
197 SignPub: ed25519.PublicKey(signPub),
198 SignPrv: ed25519.PrivateKey(signPrv),
199 NoisePub: new([32]byte),
200 NoisePrv: new([32]byte),
202 copy(node.ExchPub[:], exchPub)
203 copy(node.ExchPrv[:], exchPrv)
204 copy(node.NoisePub[:], noisePub)
205 copy(node.NoisePrv[:], noisePrv)
209 func (nodeOur *NodeOur) ToYAML() string {
211 Id: nodeOur.Id.String(),
212 ExchPub: ToBase32(nodeOur.ExchPub[:]),
213 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
214 SignPub: ToBase32(nodeOur.SignPub[:]),
215 SignPrv: ToBase32(nodeOur.SignPrv[:]),
216 NoisePub: ToBase32(nodeOur.NoisePub[:]),
217 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
219 raw, err := yaml.Marshal(&yml)
226 func CfgParse(data []byte) (*Ctx, error) {
228 err := yaml.Unmarshal(data, &cfgYAML)
232 self, err := NewNodeOur(cfgYAML.Self)
236 spoolPath := path.Clean(cfgYAML.Spool)
237 if !path.IsAbs(spoolPath) {
238 return nil, errors.New("Spool path must be absolute")
240 logPath := path.Clean(cfgYAML.Log)
241 if !path.IsAbs(logPath) {
242 return nil, errors.New("Log path must be absolute")
248 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
249 Alias: make(map[string]*NodeId),
250 Sendmail: cfgYAML.Sendmail,
252 if cfgYAML.Notify != nil {
253 if cfgYAML.Notify.File != nil {
254 ctx.NotifyFile = cfgYAML.Notify.File
256 if cfgYAML.Notify.Freq != nil {
257 ctx.NotifyFreq = cfgYAML.Notify.Freq
260 vias := make(map[NodeId][]string)
261 for name, neighYAML := range cfgYAML.Neigh {
262 neigh, err := NewNode(name, neighYAML)
266 ctx.Neigh[*neigh.Id] = neigh
267 if _, already := ctx.Alias[name]; already {
268 return nil, errors.New("Node names conflict")
270 ctx.Alias[name] = neigh.Id
271 vias[*neigh.Id] = neighYAML.Via
273 for neighId, viasRaw := range vias {
274 for _, viaRaw := range viasRaw {
275 foundNodeId, err := ctx.FindNode(viaRaw)
279 ctx.Neigh[neighId].Via = append(
280 ctx.Neigh[neighId].Via,