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/log/nncp.log"
42 type NodeYAML struct {
46 NoisePub *string `noisepub,omitempty`
48 Incoming *string `incoming,omitempty`
49 Freq *string `freq,omitempty`
50 Via []string `via,omitempty`
51 Calls []CallYAML `calls,omitempty`
53 Addrs map[string]string `addrs,omitempty`
55 OnlineDeadline *uint `onlinedeadline,omitempty`
56 MaxOnlineTime *uint `maxonlinetime,omitempty`
59 type CallYAML struct {
61 Nice *int `nice,omitempty`
62 Xx *string `xx,omitempty`
63 Addr *string `addr,omitempty`
64 OnlineDeadline *uint `onlinedeadline,omitempty`
65 MaxOnlineTime *uint `maxonlinetime,omitempty`
68 type NodeOurYAML struct {
78 type FromToYAML struct {
83 type NotifyYAML struct {
84 File *FromToYAML `file,omitempty`
85 Freq *FromToYAML `freq,omitempty`
90 Neigh map[string]NodeYAML
94 Notify *NotifyYAML `notify,omitempty`
97 func NewNode(name string, yml NodeYAML) (*Node, error) {
98 nodeId, err := NodeIdFromString(yml.Id)
103 exchPub, err := FromBase32(yml.ExchPub)
107 if len(exchPub) != 32 {
108 return nil, errors.New("Invalid exchPub size")
111 signPub, err := FromBase32(yml.SignPub)
115 if len(signPub) != ed25519.PublicKeySize {
116 return nil, errors.New("Invalid signPub size")
120 if yml.NoisePub != nil {
121 noisePub, err = FromBase32(*yml.NoisePub)
125 if len(noisePub) != 32 {
126 return nil, errors.New("Invalid noisePub size")
131 if yml.Incoming != nil {
132 inc := path.Clean(*yml.Incoming)
133 if !path.IsAbs(inc) {
134 return nil, errors.New("Incoming path must be absolute")
141 fr := path.Clean(*yml.Freq)
143 return nil, errors.New("Freq path must be absolute")
148 defOnlineDeadline := uint(DefaultDeadline)
149 if yml.OnlineDeadline != nil {
150 if *yml.OnlineDeadline <= 0 {
151 return nil, errors.New("OnlineDeadline must be at least 1 second")
153 defOnlineDeadline = *yml.OnlineDeadline
155 var defMaxOnlineTime uint
156 if yml.MaxOnlineTime != nil {
157 defMaxOnlineTime = *yml.MaxOnlineTime
161 for _, callYml := range yml.Calls {
162 expr, err := cronexpr.Parse(callYml.Cron)
167 if callYml.Nice != nil {
168 if *callYml.Nice < 1 || *callYml.Nice > 255 {
169 return nil, errors.New("Nice must be between 1 and 255")
171 nice = uint8(*callYml.Nice)
174 if callYml.Xx != nil {
181 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
185 if callYml.Addr != nil {
186 if a, exists := yml.Addrs[*callYml.Addr]; exists {
192 onlineDeadline := defOnlineDeadline
193 if callYml.OnlineDeadline != nil {
194 if *callYml.OnlineDeadline == 0 {
195 return nil, errors.New("OnlineDeadline must be at least 1 second")
197 onlineDeadline = *callYml.OnlineDeadline
199 var maxOnlineTime uint
200 if callYml.MaxOnlineTime != nil {
201 maxOnlineTime = *callYml.MaxOnlineTime
203 calls = append(calls, &Call{
208 OnlineDeadline: onlineDeadline,
209 MaxOnlineTime: maxOnlineTime,
216 ExchPub: new([32]byte),
217 SignPub: ed25519.PublicKey(signPub),
218 Sendmail: yml.Sendmail,
223 OnlineDeadline: defOnlineDeadline,
224 MaxOnlineTime: defMaxOnlineTime,
226 copy(node.ExchPub[:], exchPub)
227 if len(noisePub) > 0 {
228 node.NoisePub = new([32]byte)
229 copy(node.NoisePub[:], noisePub)
234 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
235 id, err := NodeIdFromString(yml.Id)
240 exchPub, err := FromBase32(yml.ExchPub)
244 if len(exchPub) != 32 {
245 return nil, errors.New("Invalid exchPub size")
248 exchPrv, err := FromBase32(yml.ExchPrv)
252 if len(exchPrv) != 32 {
253 return nil, errors.New("Invalid exchPrv size")
256 signPub, err := FromBase32(yml.SignPub)
260 if len(signPub) != ed25519.PublicKeySize {
261 return nil, errors.New("Invalid signPub size")
264 signPrv, err := FromBase32(yml.SignPrv)
268 if len(signPrv) != ed25519.PrivateKeySize {
269 return nil, errors.New("Invalid signPrv size")
272 noisePub, err := FromBase32(yml.NoisePub)
276 if len(noisePub) != 32 {
277 return nil, errors.New("Invalid noisePub size")
280 noisePrv, err := FromBase32(yml.NoisePrv)
284 if len(noisePrv) != 32 {
285 return nil, errors.New("Invalid noisePrv size")
290 ExchPub: new([32]byte),
291 ExchPrv: new([32]byte),
292 SignPub: ed25519.PublicKey(signPub),
293 SignPrv: ed25519.PrivateKey(signPrv),
294 NoisePub: new([32]byte),
295 NoisePrv: new([32]byte),
297 copy(node.ExchPub[:], exchPub)
298 copy(node.ExchPrv[:], exchPrv)
299 copy(node.NoisePub[:], noisePub)
300 copy(node.NoisePrv[:], noisePrv)
304 func (nodeOur *NodeOur) ToYAML() string {
306 Id: nodeOur.Id.String(),
307 ExchPub: ToBase32(nodeOur.ExchPub[:]),
308 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
309 SignPub: ToBase32(nodeOur.SignPub[:]),
310 SignPrv: ToBase32(nodeOur.SignPrv[:]),
311 NoisePub: ToBase32(nodeOur.NoisePub[:]),
312 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
314 raw, err := yaml.Marshal(&yml)
321 func CfgParse(data []byte) (*Ctx, error) {
323 err := yaml.Unmarshal(data, &cfgYAML)
327 self, err := NewNodeOur(cfgYAML.Self)
331 spoolPath := path.Clean(cfgYAML.Spool)
332 if !path.IsAbs(spoolPath) {
333 return nil, errors.New("Spool path must be absolute")
335 logPath := path.Clean(cfgYAML.Log)
336 if !path.IsAbs(logPath) {
337 return nil, errors.New("Log path must be absolute")
343 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
344 Alias: make(map[string]*NodeId),
346 if cfgYAML.Notify != nil {
347 if cfgYAML.Notify.File != nil {
348 ctx.NotifyFile = cfgYAML.Notify.File
350 if cfgYAML.Notify.Freq != nil {
351 ctx.NotifyFreq = cfgYAML.Notify.Freq
354 vias := make(map[NodeId][]string)
355 for name, neighYAML := range cfgYAML.Neigh {
356 neigh, err := NewNode(name, neighYAML)
360 ctx.Neigh[*neigh.Id] = neigh
361 if _, already := ctx.Alias[name]; already {
362 return nil, errors.New("Node names conflict")
364 ctx.Alias[name] = neigh.Id
365 vias[*neigh.Id] = neighYAML.Via
367 for neighId, viasRaw := range vias {
368 for _, viaRaw := range viasRaw {
369 foundNodeId, err := ctx.FindNode(viaRaw)
373 ctx.Neigh[neighId].Via = append(
374 ctx.Neigh[neighId].Via,
382 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
383 p = os.Getenv(CfgPathEnv)