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"
40 type NodeYAML struct {
44 NoisePub *string `noisepub,omitempty`
46 Incoming *string `incoming,omitempty`
47 Freq *string `freq,omitempty`
48 Via []string `via,omitempty`
49 Calls []CallYAML `calls,omitempty`
51 Addrs map[string]string `addrs,omitempty`
53 OnlineDeadline *uint `onlinedeadline,omitempty`
54 MaxOnlineTime *uint `maxonlinetime,omitempty`
57 type CallYAML struct {
59 Nice *int `nice,omitempty`
60 Xx *string `xx,omitempty`
61 Addr *string `addr,omitempty`
62 OnlineDeadline *uint `onlinedeadline,omitempty`
63 MaxOnlineTime *uint `maxonlinetime,omitempty`
66 type NodeOurYAML struct {
76 type FromToYAML struct {
81 type NotifyYAML struct {
82 File *FromToYAML `file,omitempty`
83 Freq *FromToYAML `freq,omitempty`
88 Neigh map[string]NodeYAML
92 Notify *NotifyYAML `notify,omitempty`
95 func NewNode(name string, yml NodeYAML) (*Node, error) {
96 nodeId, err := NodeIdFromString(yml.Id)
101 exchPub, err := FromBase32(yml.ExchPub)
105 if len(exchPub) != 32 {
106 return nil, errors.New("Invalid exchPub size")
109 signPub, err := FromBase32(yml.SignPub)
113 if len(signPub) != ed25519.PublicKeySize {
114 return nil, errors.New("Invalid signPub size")
118 if yml.NoisePub != nil {
119 noisePub, err = FromBase32(*yml.NoisePub)
123 if len(noisePub) != 32 {
124 return nil, errors.New("Invalid noisePub size")
129 if yml.Incoming != nil {
130 inc := path.Clean(*yml.Incoming)
131 if !path.IsAbs(inc) {
132 return nil, errors.New("Incoming path must be absolute")
139 fr := path.Clean(*yml.Freq)
141 return nil, errors.New("Freq path must be absolute")
146 defOnlineDeadline := uint(DefaultDeadline)
147 if yml.OnlineDeadline != nil {
148 if *yml.OnlineDeadline <= 0 {
149 return nil, errors.New("OnlineDeadline must be at least 1 second")
151 defOnlineDeadline = *yml.OnlineDeadline
153 var defMaxOnlineTime uint
154 if yml.MaxOnlineTime != nil {
155 defMaxOnlineTime = *yml.MaxOnlineTime
159 for _, callYml := range yml.Calls {
160 expr, err := cronexpr.Parse(callYml.Cron)
165 if callYml.Nice != nil {
166 if *callYml.Nice < 1 || *callYml.Nice > 255 {
167 return nil, errors.New("Nice must be between 1 and 255")
169 nice = uint8(*callYml.Nice)
172 if callYml.Xx != nil {
179 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
183 if callYml.Addr != nil {
184 if a, exists := yml.Addrs[*callYml.Addr]; exists {
190 onlineDeadline := defOnlineDeadline
191 if callYml.OnlineDeadline != nil {
192 if *yml.OnlineDeadline <= 0 {
193 return nil, errors.New("OnlineDeadline must be at least 1 second")
195 onlineDeadline = *callYml.OnlineDeadline
197 var maxOnlineTime uint
198 if callYml.MaxOnlineTime != nil {
199 maxOnlineTime = *callYml.MaxOnlineTime
201 calls = append(calls, &Call{
206 OnlineDeadline: onlineDeadline,
207 MaxOnlineTime: maxOnlineTime,
214 ExchPub: new([32]byte),
215 SignPub: ed25519.PublicKey(signPub),
216 Sendmail: yml.Sendmail,
221 OnlineDeadline: defOnlineDeadline,
222 MaxOnlineTime: defMaxOnlineTime,
224 copy(node.ExchPub[:], exchPub)
225 if len(noisePub) > 0 {
226 node.NoisePub = new([32]byte)
227 copy(node.NoisePub[:], noisePub)
232 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
233 id, err := NodeIdFromString(yml.Id)
238 exchPub, err := FromBase32(yml.ExchPub)
242 if len(exchPub) != 32 {
243 return nil, errors.New("Invalid exchPub size")
246 exchPrv, err := FromBase32(yml.ExchPrv)
250 if len(exchPrv) != 32 {
251 return nil, errors.New("Invalid exchPrv size")
254 signPub, err := FromBase32(yml.SignPub)
258 if len(signPub) != ed25519.PublicKeySize {
259 return nil, errors.New("Invalid signPub size")
262 signPrv, err := FromBase32(yml.SignPrv)
266 if len(signPrv) != ed25519.PrivateKeySize {
267 return nil, errors.New("Invalid signPrv size")
270 noisePub, err := FromBase32(yml.NoisePub)
274 if len(noisePub) != 32 {
275 return nil, errors.New("Invalid noisePub size")
278 noisePrv, err := FromBase32(yml.NoisePrv)
282 if len(noisePrv) != 32 {
283 return nil, errors.New("Invalid noisePrv size")
288 ExchPub: new([32]byte),
289 ExchPrv: new([32]byte),
290 SignPub: ed25519.PublicKey(signPub),
291 SignPrv: ed25519.PrivateKey(signPrv),
292 NoisePub: new([32]byte),
293 NoisePrv: new([32]byte),
295 copy(node.ExchPub[:], exchPub)
296 copy(node.ExchPrv[:], exchPrv)
297 copy(node.NoisePub[:], noisePub)
298 copy(node.NoisePrv[:], noisePrv)
302 func (nodeOur *NodeOur) ToYAML() string {
304 Id: nodeOur.Id.String(),
305 ExchPub: ToBase32(nodeOur.ExchPub[:]),
306 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
307 SignPub: ToBase32(nodeOur.SignPub[:]),
308 SignPrv: ToBase32(nodeOur.SignPrv[:]),
309 NoisePub: ToBase32(nodeOur.NoisePub[:]),
310 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
312 raw, err := yaml.Marshal(&yml)
319 func CfgParse(data []byte) (*Ctx, error) {
321 err := yaml.Unmarshal(data, &cfgYAML)
325 self, err := NewNodeOur(cfgYAML.Self)
329 spoolPath := path.Clean(cfgYAML.Spool)
330 if !path.IsAbs(spoolPath) {
331 return nil, errors.New("Spool path must be absolute")
333 logPath := path.Clean(cfgYAML.Log)
334 if !path.IsAbs(logPath) {
335 return nil, errors.New("Log path must be absolute")
341 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
342 Alias: make(map[string]*NodeId),
344 if cfgYAML.Notify != nil {
345 if cfgYAML.Notify.File != nil {
346 ctx.NotifyFile = cfgYAML.Notify.File
348 if cfgYAML.Notify.Freq != nil {
349 ctx.NotifyFreq = cfgYAML.Notify.Freq
352 vias := make(map[NodeId][]string)
353 for name, neighYAML := range cfgYAML.Neigh {
354 neigh, err := NewNode(name, neighYAML)
358 ctx.Neigh[*neigh.Id] = neigh
359 if _, already := ctx.Alias[name]; already {
360 return nil, errors.New("Node names conflict")
362 ctx.Alias[name] = neigh.Id
363 vias[*neigh.Id] = neighYAML.Via
365 for neighId, viasRaw := range vias {
366 for _, viaRaw := range viasRaw {
367 foundNodeId, err := ctx.FindNode(viaRaw)
371 ctx.Neigh[neighId].Via = append(
372 ctx.Neigh[neighId].Via,
380 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
381 p = os.Getenv(CfgPathEnv)