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 "golang.org/x/crypto/ed25519"
31 CfgPathEnv = "NNCPCFG"
35 DefaultCfgPath string = "/usr/local/etc/nncp.yaml"
36 DefaultSendmailPath string = "/usr/sbin/sendmail"
39 type NodeYAML struct {
43 NoisePub *string `noisepub,omitempty`
45 Incoming *string `incoming,omitempty`
46 Freq *string `freq,omitempty`
47 Via []string `via,omitempty`
49 Addrs map[string]string `addrs,omitempty`
51 OnlineDeadline *int `onlinedeadline,omitempty`
54 type NodeOurYAML struct {
64 type FromToYAML struct {
69 type NotifyYAML struct {
70 File *FromToYAML `file,omitempty`
71 Freq *FromToYAML `freq,omitempty`
76 Neigh map[string]NodeYAML
80 Notify *NotifyYAML `notify,omitempty`
83 func NewNode(name string, yml NodeYAML) (*Node, error) {
84 nodeId, err := NodeIdFromString(yml.Id)
89 exchPub, err := FromBase32(yml.ExchPub)
93 if len(exchPub) != 32 {
94 return nil, errors.New("Invalid exchPub size")
97 signPub, err := FromBase32(yml.SignPub)
101 if len(signPub) != ed25519.PublicKeySize {
102 return nil, errors.New("Invalid signPub size")
106 if yml.NoisePub != nil {
107 noisePub, err = FromBase32(*yml.NoisePub)
111 if len(noisePub) != 32 {
112 return nil, errors.New("Invalid noisePub size")
117 if yml.Incoming != nil {
118 inc := path.Clean(*yml.Incoming)
119 if !path.IsAbs(inc) {
120 return nil, errors.New("Incoming path must be absolute")
127 fr := path.Clean(*yml.Freq)
129 return nil, errors.New("Freq path must be absolute")
137 ExchPub: new([32]byte),
138 SignPub: ed25519.PublicKey(signPub),
139 Sendmail: yml.Sendmail,
143 OnlineDeadline: DefaultDeadline,
145 copy(node.ExchPub[:], exchPub)
146 if len(noisePub) > 0 {
147 node.NoisePub = new([32]byte)
148 copy(node.NoisePub[:], noisePub)
150 if yml.OnlineDeadline != nil {
151 if *yml.OnlineDeadline <= 0 {
152 return nil, errors.New("OnlineDeadline must be at least 1 second")
154 node.OnlineDeadline = *yml.OnlineDeadline
159 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
160 id, err := NodeIdFromString(yml.Id)
165 exchPub, err := FromBase32(yml.ExchPub)
169 if len(exchPub) != 32 {
170 return nil, errors.New("Invalid exchPub size")
173 exchPrv, err := FromBase32(yml.ExchPrv)
177 if len(exchPrv) != 32 {
178 return nil, errors.New("Invalid exchPrv size")
181 signPub, err := FromBase32(yml.SignPub)
185 if len(signPub) != ed25519.PublicKeySize {
186 return nil, errors.New("Invalid signPub size")
189 signPrv, err := FromBase32(yml.SignPrv)
193 if len(signPrv) != ed25519.PrivateKeySize {
194 return nil, errors.New("Invalid signPrv size")
197 noisePub, err := FromBase32(yml.NoisePub)
201 if len(noisePub) != 32 {
202 return nil, errors.New("Invalid noisePub size")
205 noisePrv, err := FromBase32(yml.NoisePrv)
209 if len(noisePrv) != 32 {
210 return nil, errors.New("Invalid noisePrv size")
215 ExchPub: new([32]byte),
216 ExchPrv: new([32]byte),
217 SignPub: ed25519.PublicKey(signPub),
218 SignPrv: ed25519.PrivateKey(signPrv),
219 NoisePub: new([32]byte),
220 NoisePrv: new([32]byte),
222 copy(node.ExchPub[:], exchPub)
223 copy(node.ExchPrv[:], exchPrv)
224 copy(node.NoisePub[:], noisePub)
225 copy(node.NoisePrv[:], noisePrv)
229 func (nodeOur *NodeOur) ToYAML() string {
231 Id: nodeOur.Id.String(),
232 ExchPub: ToBase32(nodeOur.ExchPub[:]),
233 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
234 SignPub: ToBase32(nodeOur.SignPub[:]),
235 SignPrv: ToBase32(nodeOur.SignPrv[:]),
236 NoisePub: ToBase32(nodeOur.NoisePub[:]),
237 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
239 raw, err := yaml.Marshal(&yml)
246 func CfgParse(data []byte) (*Ctx, error) {
248 err := yaml.Unmarshal(data, &cfgYAML)
252 self, err := NewNodeOur(cfgYAML.Self)
256 spoolPath := path.Clean(cfgYAML.Spool)
257 if !path.IsAbs(spoolPath) {
258 return nil, errors.New("Spool path must be absolute")
260 logPath := path.Clean(cfgYAML.Log)
261 if !path.IsAbs(logPath) {
262 return nil, errors.New("Log path must be absolute")
268 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
269 Alias: make(map[string]*NodeId),
271 if cfgYAML.Notify != nil {
272 if cfgYAML.Notify.File != nil {
273 ctx.NotifyFile = cfgYAML.Notify.File
275 if cfgYAML.Notify.Freq != nil {
276 ctx.NotifyFreq = cfgYAML.Notify.Freq
279 vias := make(map[NodeId][]string)
280 for name, neighYAML := range cfgYAML.Neigh {
281 neigh, err := NewNode(name, neighYAML)
285 ctx.Neigh[*neigh.Id] = neigh
286 if _, already := ctx.Alias[name]; already {
287 return nil, errors.New("Node names conflict")
289 ctx.Alias[name] = neigh.Id
290 vias[*neigh.Id] = neighYAML.Via
292 for neighId, viasRaw := range vias {
293 for _, viaRaw := range viasRaw {
294 foundNodeId, err := ctx.FindNode(viaRaw)
298 ctx.Neigh[neighId].Via = append(
299 ctx.Neigh[neighId].Via,
307 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
308 p = os.Getenv(CfgPathEnv)