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 {
40 Incoming *string `incoming,omitempty`
41 Freq *string `freq,omitempty`
42 Via []string `via,omitempty`
44 Addrs map[string]string `addrs,omitempty`
47 type NodeOurYAML struct {
57 type FromToYAML struct {
62 type NotifyYAML struct {
63 File *FromToYAML `file,omitempty`
64 Freq *FromToYAML `freq,omitempty`
69 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),
130 Sendmail: yml.Sendmail,
135 copy(node.ExchPub[:], exchPub)
136 copy(node.NoisePub[:], noisePub)
140 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
141 id, err := NodeIdFromString(yml.Id)
146 exchPub, err := FromBase32(yml.ExchPub)
150 if len(exchPub) != 32 {
151 return nil, errors.New("Invalid exchPub size")
154 exchPrv, err := FromBase32(yml.ExchPrv)
158 if len(exchPrv) != 32 {
159 return nil, errors.New("Invalid exchPrv size")
162 signPub, err := FromBase32(yml.SignPub)
166 if len(signPub) != ed25519.PublicKeySize {
167 return nil, errors.New("Invalid signPub size")
170 signPrv, err := FromBase32(yml.SignPrv)
174 if len(signPrv) != ed25519.PrivateKeySize {
175 return nil, errors.New("Invalid signPrv size")
178 noisePub, err := FromBase32(yml.NoisePub)
182 if len(noisePub) != 32 {
183 return nil, errors.New("Invalid noisePub size")
186 noisePrv, err := FromBase32(yml.NoisePrv)
190 if len(noisePrv) != 32 {
191 return nil, errors.New("Invalid noisePrv size")
196 ExchPub: new([32]byte),
197 ExchPrv: new([32]byte),
198 SignPub: ed25519.PublicKey(signPub),
199 SignPrv: ed25519.PrivateKey(signPrv),
200 NoisePub: new([32]byte),
201 NoisePrv: new([32]byte),
203 copy(node.ExchPub[:], exchPub)
204 copy(node.ExchPrv[:], exchPrv)
205 copy(node.NoisePub[:], noisePub)
206 copy(node.NoisePrv[:], noisePrv)
210 func (nodeOur *NodeOur) ToYAML() string {
212 Id: nodeOur.Id.String(),
213 ExchPub: ToBase32(nodeOur.ExchPub[:]),
214 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
215 SignPub: ToBase32(nodeOur.SignPub[:]),
216 SignPrv: ToBase32(nodeOur.SignPrv[:]),
217 NoisePub: ToBase32(nodeOur.NoisePub[:]),
218 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
220 raw, err := yaml.Marshal(&yml)
227 func CfgParse(data []byte) (*Ctx, error) {
229 err := yaml.Unmarshal(data, &cfgYAML)
233 self, err := NewNodeOur(cfgYAML.Self)
237 spoolPath := path.Clean(cfgYAML.Spool)
238 if !path.IsAbs(spoolPath) {
239 return nil, errors.New("Spool path must be absolute")
241 logPath := path.Clean(cfgYAML.Log)
242 if !path.IsAbs(logPath) {
243 return nil, errors.New("Log path must be absolute")
249 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
250 Alias: make(map[string]*NodeId),
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,