2 NNCP -- Node-to-Node CoPy
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 {
39 Incoming *string `incoming,omitempty`
40 Freq *string `freq,omitempty`
41 Via []string `via,omitempty`
44 type NodeOurYAML struct {
54 type FromToYAML struct {
59 type NotifyYAML struct {
60 File *FromToYAML `file,omitempty`
61 Freq *FromToYAML `freq,omitempty`
66 Neigh map[string]NodeYAML
71 Notify *NotifyYAML `notify,omitempty`
74 func NewNode(name string, yml NodeYAML) (*Node, error) {
75 nodeId, err := NodeIdFromString(yml.Id)
80 exchPub, err := FromBase32(yml.ExchPub)
84 if len(exchPub) != 32 {
85 return nil, errors.New("Invalid exchPub size")
88 signPub, err := FromBase32(yml.SignPub)
92 if len(signPub) != ed25519.PublicKeySize {
93 return nil, errors.New("Invalid signPub size")
96 noisePub, err := FromBase32(yml.NoisePub)
100 if len(noisePub) != 32 {
101 return nil, errors.New("Invalid noisePub size")
105 if yml.Incoming != nil {
106 inc := path.Clean(*yml.Incoming)
107 if !path.IsAbs(inc) {
108 return nil, errors.New("Incoming path must be absolute")
115 fr := path.Clean(*yml.Freq)
117 return nil, errors.New("Freq path must be absolute")
125 ExchPub: new([32]byte),
126 SignPub: ed25519.PublicKey(signPub),
127 NoisePub: new([32]byte),
131 copy(node.ExchPub[:], exchPub)
132 copy(node.NoisePub[:], noisePub)
136 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
137 id, err := NodeIdFromString(yml.Id)
142 exchPub, err := FromBase32(yml.ExchPub)
146 if len(exchPub) != 32 {
147 return nil, errors.New("Invalid exchPub size")
150 exchPrv, err := FromBase32(yml.ExchPrv)
154 if len(exchPrv) != 32 {
155 return nil, errors.New("Invalid exchPrv size")
158 signPub, err := FromBase32(yml.SignPub)
162 if len(signPub) != ed25519.PublicKeySize {
163 return nil, errors.New("Invalid signPub size")
166 signPrv, err := FromBase32(yml.SignPrv)
170 if len(signPrv) != ed25519.PrivateKeySize {
171 return nil, errors.New("Invalid signPrv size")
174 noisePub, err := FromBase32(yml.NoisePub)
178 if len(noisePub) != 32 {
179 return nil, errors.New("Invalid noisePub size")
182 noisePrv, err := FromBase32(yml.NoisePrv)
186 if len(noisePrv) != 32 {
187 return nil, errors.New("Invalid noisePrv size")
192 ExchPub: new([32]byte),
193 ExchPrv: new([32]byte),
194 SignPub: ed25519.PublicKey(signPub),
195 SignPrv: ed25519.PrivateKey(signPrv),
196 NoisePub: new([32]byte),
197 NoisePrv: new([32]byte),
199 copy(node.ExchPub[:], exchPub)
200 copy(node.ExchPrv[:], exchPrv)
201 copy(node.NoisePub[:], noisePub)
202 copy(node.NoisePrv[:], noisePrv)
206 func (nodeOur *NodeOur) ToYAML() string {
208 Id: nodeOur.Id.String(),
209 ExchPub: ToBase32(nodeOur.ExchPub[:]),
210 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
211 SignPub: ToBase32(nodeOur.SignPub[:]),
212 SignPrv: ToBase32(nodeOur.SignPrv[:]),
213 NoisePub: ToBase32(nodeOur.NoisePub[:]),
214 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
216 raw, err := yaml.Marshal(&yml)
223 func CfgParse(data []byte) (*Ctx, error) {
225 err := yaml.Unmarshal(data, &cfgYAML)
229 self, err := NewNodeOur(cfgYAML.Self)
233 spoolPath := path.Clean(cfgYAML.Spool)
234 if !path.IsAbs(spoolPath) {
235 return nil, errors.New("Spool path must be absolute")
237 logPath := path.Clean(cfgYAML.Log)
238 if !path.IsAbs(logPath) {
239 return nil, errors.New("Log path must be absolute")
245 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
246 Alias: make(map[string]*NodeId),
247 Sendmail: cfgYAML.Sendmail,
249 if cfgYAML.Notify != nil {
250 if cfgYAML.Notify.File != nil {
251 ctx.NotifyFile = cfgYAML.Notify.File
253 if cfgYAML.Notify.Freq != nil {
254 ctx.NotifyFreq = cfgYAML.Notify.Freq
257 vias := make(map[NodeId][]string)
258 for name, neighYAML := range cfgYAML.Neigh {
259 neigh, err := NewNode(name, neighYAML)
263 ctx.Neigh[*neigh.Id] = neigh
264 if _, already := ctx.Alias[name]; already {
265 return nil, errors.New("Node names conflict")
267 ctx.Alias[name] = neigh.Id
268 vias[*neigh.Id] = neighYAML.Via
270 for neighId, viasRaw := range vias {
271 for _, viaRaw := range viasRaw {
272 foundNodeId, err := ctx.FindNode(viaRaw)
276 ctx.Neigh[neighId].Via = append(
277 ctx.Neigh[neighId].Via,