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/>.
26 "github.com/gorhill/cronexpr"
27 "golang.org/x/crypto/ed25519"
32 CfgPathEnv = "NNCPCFG"
36 DefaultCfgPath string = "/usr/local/etc/nncp.yaml"
37 DefaultSendmailPath string = "/usr/sbin/sendmail"
38 DefaultSpoolPath string = "/var/spool/nncp"
39 DefaultLogPath string = "/var/spool/nncp/log"
42 type NodeYAML struct {
46 NoisePub *string `noisepub,omitempty`
47 Sendmail []string `sendmail,omitempty`
48 Incoming *string `incoming,omitempty`
49 Freq *string `freq,omitempty`
50 FreqChunked *uint64 `freqchunked,omitempty`
51 FreqMinSize *uint64 `freqminsize,omitempty`
52 Via []string `via,omitempty`
53 Calls []CallYAML `calls,omitempty`
55 Addrs map[string]string `addrs,omitempty`
57 OnlineDeadline *uint `onlinedeadline,omitempty`
58 MaxOnlineTime *uint `maxonlinetime,omitempty`
61 type CallYAML struct {
63 Nice *int `nice,omitempty`
64 Xx *string `xx,omitempty`
65 Addr *string `addr,omitempty`
66 OnlineDeadline *uint `onlinedeadline,omitempty`
67 MaxOnlineTime *uint `maxonlinetime,omitempty`
70 type NodeOurYAML struct {
80 type FromToYAML struct {
85 type NotifyYAML struct {
86 File *FromToYAML `file,omitempty`
87 Freq *FromToYAML `freq,omitempty`
91 Self *NodeOurYAML `self,omitempty`
92 Neigh map[string]NodeYAML
96 Notify *NotifyYAML `notify,omitempty`
99 func NewNode(name string, yml NodeYAML) (*Node, error) {
100 nodeId, err := NodeIdFromString(yml.Id)
105 exchPub, err := FromBase32(yml.ExchPub)
109 if len(exchPub) != 32 {
110 return nil, errors.New("Invalid exchPub size")
113 signPub, err := FromBase32(yml.SignPub)
117 if len(signPub) != ed25519.PublicKeySize {
118 return nil, errors.New("Invalid signPub size")
122 if yml.NoisePub != nil {
123 noisePub, err = FromBase32(*yml.NoisePub)
127 if len(noisePub) != 32 {
128 return nil, errors.New("Invalid noisePub size")
133 if yml.Incoming != nil {
134 inc := path.Clean(*yml.Incoming)
135 if !path.IsAbs(inc) {
136 return nil, errors.New("Incoming path must be absolute")
143 fr := path.Clean(*yml.Freq)
145 return nil, errors.New("Freq path must be absolute")
149 var freqChunked int64
150 if yml.FreqChunked != nil {
151 if *yml.FreqChunked == 0 {
152 return nil, errors.New("freqchunked value must be greater than zero")
154 freqChunked = int64(*yml.FreqChunked) * 1024
156 var freqMinSize int64
157 if yml.FreqMinSize != nil {
158 freqMinSize = int64(*yml.FreqMinSize) * 1024
161 defOnlineDeadline := uint(DefaultDeadline)
162 if yml.OnlineDeadline != nil {
163 if *yml.OnlineDeadline <= 0 {
164 return nil, errors.New("OnlineDeadline must be at least 1 second")
166 defOnlineDeadline = *yml.OnlineDeadline
168 var defMaxOnlineTime uint
169 if yml.MaxOnlineTime != nil {
170 defMaxOnlineTime = *yml.MaxOnlineTime
174 for _, callYml := range yml.Calls {
175 expr, err := cronexpr.Parse(callYml.Cron)
180 if callYml.Nice != nil {
181 if *callYml.Nice < 1 || *callYml.Nice > 255 {
182 return nil, errors.New("Nice must be between 1 and 255")
184 nice = uint8(*callYml.Nice)
187 if callYml.Xx != nil {
194 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
198 if callYml.Addr != nil {
199 if a, exists := yml.Addrs[*callYml.Addr]; exists {
205 onlineDeadline := defOnlineDeadline
206 if callYml.OnlineDeadline != nil {
207 if *callYml.OnlineDeadline == 0 {
208 return nil, errors.New("OnlineDeadline must be at least 1 second")
210 onlineDeadline = *callYml.OnlineDeadline
212 var maxOnlineTime uint
213 if callYml.MaxOnlineTime != nil {
214 maxOnlineTime = *callYml.MaxOnlineTime
216 calls = append(calls, &Call{
221 OnlineDeadline: onlineDeadline,
222 MaxOnlineTime: maxOnlineTime,
229 ExchPub: new([32]byte),
230 SignPub: ed25519.PublicKey(signPub),
231 Sendmail: yml.Sendmail,
234 FreqChunked: freqChunked,
235 FreqMinSize: freqMinSize,
238 OnlineDeadline: defOnlineDeadline,
239 MaxOnlineTime: defMaxOnlineTime,
241 copy(node.ExchPub[:], exchPub)
242 if len(noisePub) > 0 {
243 node.NoisePub = new([32]byte)
244 copy(node.NoisePub[:], noisePub)
249 func NewNodeOur(yml *NodeOurYAML) (*NodeOur, error) {
250 id, err := NodeIdFromString(yml.Id)
255 exchPub, err := FromBase32(yml.ExchPub)
259 if len(exchPub) != 32 {
260 return nil, errors.New("Invalid exchPub size")
263 exchPrv, err := FromBase32(yml.ExchPrv)
267 if len(exchPrv) != 32 {
268 return nil, errors.New("Invalid exchPrv size")
271 signPub, err := FromBase32(yml.SignPub)
275 if len(signPub) != ed25519.PublicKeySize {
276 return nil, errors.New("Invalid signPub size")
279 signPrv, err := FromBase32(yml.SignPrv)
283 if len(signPrv) != ed25519.PrivateKeySize {
284 return nil, errors.New("Invalid signPrv size")
287 noisePub, err := FromBase32(yml.NoisePub)
291 if len(noisePub) != 32 {
292 return nil, errors.New("Invalid noisePub size")
295 noisePrv, err := FromBase32(yml.NoisePrv)
299 if len(noisePrv) != 32 {
300 return nil, errors.New("Invalid noisePrv size")
305 ExchPub: new([32]byte),
306 ExchPrv: new([32]byte),
307 SignPub: ed25519.PublicKey(signPub),
308 SignPrv: ed25519.PrivateKey(signPrv),
309 NoisePub: new([32]byte),
310 NoisePrv: new([32]byte),
312 copy(node.ExchPub[:], exchPub)
313 copy(node.ExchPrv[:], exchPrv)
314 copy(node.NoisePub[:], noisePub)
315 copy(node.NoisePrv[:], noisePrv)
319 func (nodeOur *NodeOur) ToYAML() string {
321 Id: nodeOur.Id.String(),
322 ExchPub: ToBase32(nodeOur.ExchPub[:]),
323 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
324 SignPub: ToBase32(nodeOur.SignPub[:]),
325 SignPrv: ToBase32(nodeOur.SignPrv[:]),
326 NoisePub: ToBase32(nodeOur.NoisePub[:]),
327 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
329 raw, err := yaml.Marshal(&yml)
336 func CfgParse(data []byte) (*Ctx, error) {
338 err := yaml.Unmarshal(data, &cfgYAML)
342 if _, exists := cfgYAML.Neigh["self"]; !exists {
343 return nil, errors.New("self neighbour missing")
346 if cfgYAML.Self != nil {
347 self, err = NewNodeOur(cfgYAML.Self)
352 spoolPath := path.Clean(cfgYAML.Spool)
353 if !path.IsAbs(spoolPath) {
354 return nil, errors.New("Spool path must be absolute")
356 logPath := path.Clean(cfgYAML.Log)
357 if !path.IsAbs(logPath) {
358 return nil, errors.New("Log path must be absolute")
364 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
365 Alias: make(map[string]*NodeId),
367 if cfgYAML.Notify != nil {
368 if cfgYAML.Notify.File != nil {
369 ctx.NotifyFile = cfgYAML.Notify.File
371 if cfgYAML.Notify.Freq != nil {
372 ctx.NotifyFreq = cfgYAML.Notify.Freq
375 vias := make(map[NodeId][]string)
376 for name, neighYAML := range cfgYAML.Neigh {
377 neigh, err := NewNode(name, neighYAML)
381 ctx.Neigh[*neigh.Id] = neigh
382 if _, already := ctx.Alias[name]; already {
383 return nil, errors.New("Node names conflict")
385 ctx.Alias[name] = neigh.Id
386 vias[*neigh.Id] = neighYAML.Via
388 ctx.SelfId = ctx.Alias["self"]
389 for neighId, viasRaw := range vias {
390 for _, viaRaw := range viasRaw {
391 foundNodeId, err := ctx.FindNode(viaRaw)
395 ctx.Neigh[neighId].Via = append(
396 ctx.Neigh[neighId].Via,
404 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
405 p = os.Getenv(CfgPathEnv)