]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go
Merge branch 'develop'
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-xfer / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 // Copy NNCP inbound and outbounds packets
20 package main
21
22 import (
23         "bufio"
24         "flag"
25         "fmt"
26         "io"
27         "io/ioutil"
28         "log"
29         "os"
30         "path/filepath"
31         "strconv"
32
33         "cypherpunks.ru/nncp"
34         "github.com/davecgh/go-xdr/xdr2"
35 )
36
37 func usage() {
38         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
39         fmt.Fprintln(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n")
40         fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
41         flag.PrintDefaults()
42 }
43
44 func main() {
45         var (
46                 cfgPath  = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
47                 nodeRaw  = flag.String("node", "", "Process only that node")
48                 niceRaw  = flag.Int("nice", 255, "Minimal required niceness")
49                 rxOnly   = flag.Bool("rx", false, "Only receive packets")
50                 txOnly   = flag.Bool("tx", false, "Only transfer packets")
51                 force    = flag.Bool("force", false, "Force outbound directories creation")
52                 keep     = flag.Bool("keep", false, "Do not delete transferred packets")
53                 quiet    = flag.Bool("quiet", false, "Print only errors")
54                 debug    = flag.Bool("debug", false, "Print debug messages")
55                 version  = flag.Bool("version", false, "Print version information")
56                 warranty = flag.Bool("warranty", false, "Print warranty information")
57         )
58         flag.Usage = usage
59         flag.Parse()
60         if *warranty {
61                 fmt.Println(nncp.Warranty)
62                 return
63         }
64         if *version {
65                 fmt.Println(nncp.VersionGet())
66                 return
67         }
68         if flag.NArg() != 1 {
69                 usage()
70                 os.Exit(1)
71         }
72         if *niceRaw < 1 || *niceRaw > 255 {
73                 log.Fatalln("-nice must be between 1 and 255")
74         }
75         nice := uint8(*niceRaw)
76         if *rxOnly && *txOnly {
77                 log.Fatalln("-rx and -tx can not be set simultaneously")
78         }
79
80         cfgRaw, err := ioutil.ReadFile(nncp.CfgPathFromEnv(cfgPath))
81         if err != nil {
82                 log.Fatalln("Can not read config:", err)
83         }
84         ctx, err := nncp.CfgParse(cfgRaw)
85         if err != nil {
86                 log.Fatalln("Can not parse config:", err)
87         }
88         ctx.Quiet = *quiet
89         ctx.Debug = *debug
90
91         var nodeOnly *nncp.Node
92         if *nodeRaw != "" {
93                 nodeOnly, err = ctx.FindNode(*nodeRaw)
94                 if err != nil {
95                         log.Fatalln("Invalid -node specified:", err)
96                 }
97         }
98
99         selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
100         isBad := false
101         var dir *os.File
102         var fis []os.FileInfo
103         sds := nncp.SDS{}
104         if *txOnly {
105                 goto Tx
106         }
107         sds["xx"] = string(nncp.TRx)
108         sds["dir"] = selfPath
109         ctx.LogD("nncp-xfer", sds, "self")
110         if _, err = os.Stat(selfPath); err != nil {
111                 if os.IsNotExist(err) {
112                         ctx.LogD("nncp-xfer", sds, "no dir")
113                         goto Tx
114                 }
115                 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
116                 isBad = true
117                 goto Tx
118         }
119         dir, err = os.Open(selfPath)
120         if err != nil {
121                 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
122                 isBad = true
123                 goto Tx
124         }
125         fis, err = dir.Readdir(0)
126         dir.Close()
127         if err != nil {
128                 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
129                 isBad = true
130                 goto Tx
131         }
132         for _, fi := range fis {
133                 if !fi.IsDir() {
134                         continue
135                 }
136                 nodeId, err := nncp.NodeIdFromString(fi.Name())
137                 sds["node"] = fi.Name()
138                 if err != nil {
139                         ctx.LogD("nncp-xfer", sds, "is not NodeId")
140                         continue
141                 }
142                 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
143                         ctx.LogD("nncp-xfer", sds, "skip")
144                         continue
145                 }
146                 if _, known := ctx.Neigh[*nodeId]; !known {
147                         ctx.LogD("nncp-xfer", sds, "unknown")
148                         continue
149                 }
150                 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
151                 if err != nil {
152                         ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
153                         isBad = true
154                         continue
155                 }
156                 fisInt, err := dir.Readdir(0)
157                 dir.Close()
158                 if err != nil {
159                         ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
160                         isBad = true
161                         continue
162                 }
163                 for _, fiInt := range fisInt {
164                         if !fi.IsDir() {
165                                 continue
166                         }
167                         filename := filepath.Join(dir.Name(), fiInt.Name())
168                         sds["file"] = filename
169                         fd, err := os.Open(filename)
170                         if err != nil {
171                                 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
172                                 isBad = true
173                                 continue
174                         }
175                         var pktEnc nncp.PktEnc
176                         _, err = xdr.Unmarshal(fd, &pktEnc)
177                         if err != nil || pktEnc.Magic != nncp.MagicNNCPEv1 {
178                                 ctx.LogD("nncp-xfer", sds, "is not a packet")
179                                 fd.Close()
180                                 continue
181                         }
182                         if pktEnc.Nice > nice {
183                                 ctx.LogD("nncp-xfer", sds, "too nice")
184                                 fd.Close()
185                                 continue
186                         }
187                         fd.Seek(0, 0)
188                         tmp, err := ctx.NewTmpFileWHash()
189                         if err != nil {
190                                 log.Fatalln(err)
191                         }
192                         copied, err := io.Copy(tmp.W, bufio.NewReader(fd))
193                         if err != nil {
194                                 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
195                                 isBad = true
196                                 fd.Close()
197                                 tmp.Cancel()
198                                 continue
199                         }
200                         fd.Close()
201                         if err = tmp.Commit(filepath.Join(
202                                 ctx.Spool,
203                                 nodeId.String(),
204                                 string(nncp.TRx),
205                         )); err != nil {
206                                 log.Fatalln(err)
207                         }
208                         ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
209                                 "size": strconv.FormatInt(copied, 10),
210                         }), "")
211                         if !*keep {
212                                 if err = os.Remove(filename); err != nil {
213                                         ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
214                                         isBad = true
215                                 }
216                         }
217                 }
218         }
219
220 Tx:
221         if *rxOnly {
222                 if isBad {
223                         os.Exit(1)
224                 }
225                 return
226         }
227         sds["xx"] = string(nncp.TTx)
228         for nodeId, _ := range ctx.Neigh {
229                 sds["node"] = nodeId
230                 if nodeOnly != nil && nodeId != *nodeOnly.Id {
231                         ctx.LogD("nncp-xfer", sds, "skip")
232                         continue
233                 }
234                 dirLock, err := ctx.LockDir(&nodeId, nncp.TTx)
235                 if err != nil {
236                         continue
237                 }
238                 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
239                 sds["dir"] = nodePath
240                 _, err = os.Stat(nodePath)
241                 if err != nil {
242                         if os.IsNotExist(err) {
243                                 ctx.LogD("nncp-xfer", sds, "does not exist")
244                                 if !*force {
245                                         ctx.UnlockDir(dirLock)
246                                         continue
247                                 }
248                                 if err = os.Mkdir(nodePath, os.FileMode(0700)); err != nil {
249                                         ctx.UnlockDir(dirLock)
250                                         ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
251                                         isBad = true
252                                         continue
253                                 }
254                         } else {
255                                 ctx.UnlockDir(dirLock)
256                                 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
257                                 isBad = true
258                                 continue
259                         }
260                 }
261                 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
262                 sds["dir"] = dstPath
263                 _, err = os.Stat(dstPath)
264                 if err != nil {
265                         if os.IsNotExist(err) {
266                                 if err = os.Mkdir(dstPath, os.FileMode(0700)); err != nil {
267                                         ctx.UnlockDir(dirLock)
268                                         ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
269                                         isBad = true
270                                         continue
271                                 }
272                         } else {
273                                 ctx.UnlockDir(dirLock)
274                                 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
275                                 isBad = true
276                                 continue
277                         }
278                 }
279                 delete(sds, "dir")
280                 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
281                         pktName := filepath.Base(job.Fd.Name())
282                         sds["pkt"] = pktName
283                         if job.PktEnc.Nice > nice {
284                                 ctx.LogD("nncp-xfer", sds, "too nice")
285                                 job.Fd.Close()
286                                 continue
287                         }
288                         tmp, err := ioutil.TempFile(dstPath, "nncp-xfer")
289                         if err != nil {
290                                 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mktemp")
291                                 job.Fd.Close()
292                                 isBad = true
293                                 break
294                         }
295                         sds["tmp"] = tmp.Name()
296                         ctx.LogD("nncp-xfer", sds, "created")
297                         bufW := bufio.NewWriter(tmp)
298                         copied, err := io.Copy(bufW, bufio.NewReader(job.Fd))
299                         job.Fd.Close()
300                         if err != nil {
301                                 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
302                                 tmp.Close()
303                                 isBad = true
304                                 continue
305                         }
306                         err = bufW.Flush()
307                         tmp.Sync()
308                         tmp.Close()
309                         if err != nil {
310                                 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
311                                 isBad = true
312                                 continue
313                         }
314                         if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
315                                 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "rename")
316                                 isBad = true
317                                 continue
318                         }
319                         delete(sds, "tmp")
320                         ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
321                                 "size": strconv.FormatInt(copied, 10),
322                         }), "")
323                         if !*keep {
324                                 if err = os.Remove(job.Fd.Name()); err != nil {
325                                         ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
326                                         isBad = true
327                                 }
328                         }
329                 }
330                 ctx.UnlockDir(dirLock)
331         }
332         if isBad {
333                 os.Exit(1)
334         }
335 }