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 *int `onlinedeadline,omitempty`
56 type CallYAML struct {
58 Nice *int `nice,omitempty`
59 Xx *string `xx,omitempty`
60 Addr *string `addr,omitempty`
61 OnlineDeadline *int `onlinedeadline,omitempty`
64 type NodeOurYAML struct {
74 type FromToYAML struct {
79 type NotifyYAML struct {
80 File *FromToYAML `file,omitempty`
81 Freq *FromToYAML `freq,omitempty`
86 Neigh map[string]NodeYAML
90 Notify *NotifyYAML `notify,omitempty`
93 func NewNode(name string, yml NodeYAML) (*Node, error) {
94 nodeId, err := NodeIdFromString(yml.Id)
99 exchPub, err := FromBase32(yml.ExchPub)
103 if len(exchPub) != 32 {
104 return nil, errors.New("Invalid exchPub size")
107 signPub, err := FromBase32(yml.SignPub)
111 if len(signPub) != ed25519.PublicKeySize {
112 return nil, errors.New("Invalid signPub size")
116 if yml.NoisePub != nil {
117 noisePub, err = FromBase32(*yml.NoisePub)
121 if len(noisePub) != 32 {
122 return nil, errors.New("Invalid noisePub size")
127 if yml.Incoming != nil {
128 inc := path.Clean(*yml.Incoming)
129 if !path.IsAbs(inc) {
130 return nil, errors.New("Incoming path must be absolute")
137 fr := path.Clean(*yml.Freq)
139 return nil, errors.New("Freq path must be absolute")
144 defOnlineDeadline := int(DefaultDeadline)
145 if yml.OnlineDeadline != nil {
146 if *yml.OnlineDeadline <= 0 {
147 return nil, errors.New("OnlineDeadline must be at least 1 second")
149 defOnlineDeadline = *yml.OnlineDeadline
153 for _, callYml := range yml.Calls {
154 expr, err := cronexpr.Parse(callYml.Cron)
159 if callYml.Nice != nil {
160 if *callYml.Nice < 1 || *callYml.Nice > 255 {
161 return nil, errors.New("Nice must be between 1 and 255")
163 nice = uint8(*callYml.Nice)
166 if callYml.Xx != nil {
173 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
177 if callYml.Addr != nil {
178 if a, exists := yml.Addrs[*callYml.Addr]; exists {
184 onlineDeadline := defOnlineDeadline
185 if callYml.OnlineDeadline != nil {
186 if *yml.OnlineDeadline <= 0 {
187 return nil, errors.New("OnlineDeadline must be at least 1 second")
189 onlineDeadline = *callYml.OnlineDeadline
191 calls = append(calls, &Call{
196 OnlineDeadline: onlineDeadline,
203 ExchPub: new([32]byte),
204 SignPub: ed25519.PublicKey(signPub),
205 Sendmail: yml.Sendmail,
210 OnlineDeadline: defOnlineDeadline,
212 copy(node.ExchPub[:], exchPub)
213 if len(noisePub) > 0 {
214 node.NoisePub = new([32]byte)
215 copy(node.NoisePub[:], noisePub)
220 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
221 id, err := NodeIdFromString(yml.Id)
226 exchPub, err := FromBase32(yml.ExchPub)
230 if len(exchPub) != 32 {
231 return nil, errors.New("Invalid exchPub size")
234 exchPrv, err := FromBase32(yml.ExchPrv)
238 if len(exchPrv) != 32 {
239 return nil, errors.New("Invalid exchPrv size")
242 signPub, err := FromBase32(yml.SignPub)
246 if len(signPub) != ed25519.PublicKeySize {
247 return nil, errors.New("Invalid signPub size")
250 signPrv, err := FromBase32(yml.SignPrv)
254 if len(signPrv) != ed25519.PrivateKeySize {
255 return nil, errors.New("Invalid signPrv size")
258 noisePub, err := FromBase32(yml.NoisePub)
262 if len(noisePub) != 32 {
263 return nil, errors.New("Invalid noisePub size")
266 noisePrv, err := FromBase32(yml.NoisePrv)
270 if len(noisePrv) != 32 {
271 return nil, errors.New("Invalid noisePrv size")
276 ExchPub: new([32]byte),
277 ExchPrv: new([32]byte),
278 SignPub: ed25519.PublicKey(signPub),
279 SignPrv: ed25519.PrivateKey(signPrv),
280 NoisePub: new([32]byte),
281 NoisePrv: new([32]byte),
283 copy(node.ExchPub[:], exchPub)
284 copy(node.ExchPrv[:], exchPrv)
285 copy(node.NoisePub[:], noisePub)
286 copy(node.NoisePrv[:], noisePrv)
290 func (nodeOur *NodeOur) ToYAML() string {
292 Id: nodeOur.Id.String(),
293 ExchPub: ToBase32(nodeOur.ExchPub[:]),
294 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
295 SignPub: ToBase32(nodeOur.SignPub[:]),
296 SignPrv: ToBase32(nodeOur.SignPrv[:]),
297 NoisePub: ToBase32(nodeOur.NoisePub[:]),
298 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
300 raw, err := yaml.Marshal(&yml)
307 func CfgParse(data []byte) (*Ctx, error) {
309 err := yaml.Unmarshal(data, &cfgYAML)
313 self, err := NewNodeOur(cfgYAML.Self)
317 spoolPath := path.Clean(cfgYAML.Spool)
318 if !path.IsAbs(spoolPath) {
319 return nil, errors.New("Spool path must be absolute")
321 logPath := path.Clean(cfgYAML.Log)
322 if !path.IsAbs(logPath) {
323 return nil, errors.New("Log path must be absolute")
329 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
330 Alias: make(map[string]*NodeId),
332 if cfgYAML.Notify != nil {
333 if cfgYAML.Notify.File != nil {
334 ctx.NotifyFile = cfgYAML.Notify.File
336 if cfgYAML.Notify.Freq != nil {
337 ctx.NotifyFreq = cfgYAML.Notify.Freq
340 vias := make(map[NodeId][]string)
341 for name, neighYAML := range cfgYAML.Neigh {
342 neigh, err := NewNode(name, neighYAML)
346 ctx.Neigh[*neigh.Id] = neigh
347 if _, already := ctx.Alias[name]; already {
348 return nil, errors.New("Node names conflict")
350 ctx.Alias[name] = neigh.Id
351 vias[*neigh.Id] = neighYAML.Via
353 for neighId, viasRaw := range vias {
354 for _, viaRaw := range viasRaw {
355 foundNodeId, err := ctx.FindNode(viaRaw)
359 ctx.Neigh[neighId].Via = append(
360 ctx.Neigh[neighId].Via,
368 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
369 p = os.Getenv(CfgPathEnv)