]> Cypherpunks.ru repositories - nncp.git/commitdiff
Search for bundles in badly formatted streams
authorSergey Matveev <stargrave@stargrave.org>
Mon, 20 Nov 2017 19:55:10 +0000 (22:55 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Mon, 20 Nov 2017 19:55:10 +0000 (22:55 +0300)
doc/bundles.texi
src/cypherpunks.ru/nncp/cmd/nncp-bundle/main.go
src/cypherpunks.ru/nncp/pkt.go

index 9b2f8fc3c07b887d20a6ae83b36d8d5ced433b47..bb965b2e7f02eb45aa9180b47ac6dffee74228c0 100644 (file)
@@ -51,5 +51,5 @@ can contain mix of various recipients.
 Technically bundle is valid POSIX.1
 @url{http://www.freebsd.org/cgi/man.cgi?query=tar&sektion=5, tar archive},
 with directory/files hierarchy identical to that is used in
-@ref{nncp-xfer}. So bundle can also be created by manual tar-ing of
-@command{nncp-xfer} resulting directory.
+@ref{nncp-xfer}: @file{NNCP/RECIPIENT/SENDER/PACKET}. So bundle can also
+be created by manual tar-ing of @command{nncp-xfer} resulting directory.
index 210a14024dfe464669bf4ae5a6a65a8fdbd6c803..10682511f627af9190d78c0a1935ea3588e2e91f 100644 (file)
@@ -124,6 +124,7 @@ func main() {
                                }
                                if err = tarWr.WriteHeader(&tar.Header{
                                        Name: strings.Join([]string{
+                                               nncp.NNCPBundlePrefix,
                                                nodeId.String(),
                                                ctx.SelfId.String(),
                                                pktName,
@@ -158,9 +159,11 @@ func main() {
                        log.Fatalln("Error during tar closing:", err)
                }
        } else {
-               tarR := tar.NewReader(bufio.NewReaderSize(os.Stdin, CopyBufSize))
+               bufStdin := bufio.NewReaderSize(os.Stdin, CopyBufSize*2)
+               var peeked []byte
+               var prefixIdx int
+               var tarR *tar.Reader
                var entry *tar.Header
-               var sepIndex int
                var exists bool
                pktEncBuf := make([]byte, nncp.PktEncOverhead)
                var pktEnc *nncp.PktEnc
@@ -168,25 +171,38 @@ func main() {
                var selfPath string
                var dstPath string
                for {
+                       peeked, err = bufStdin.Peek(CopyBufSize)
+                       if err != nil && err != io.EOF {
+                               log.Fatalln("Error during reading:", err)
+                       }
+                       prefixIdx = bytes.Index(peeked, []byte(nncp.NNCPBundlePrefix))
+                       if prefixIdx == -1 {
+                               if err == io.EOF {
+                                       break
+                               }
+                               bufStdin.Discard(bufStdin.Buffered() - (len(nncp.NNCPBundlePrefix) - 1))
+                               continue
+                       }
+                       bufStdin.Discard(prefixIdx)
+                       tarR = tar.NewReader(bufStdin)
                        sds["xx"] = string(nncp.TRx)
                        entry, err = tarR.Next()
                        if err != nil {
-                               if err == io.EOF {
-                                       break
+                               if err != io.EOF {
+                                       ctx.LogD(
+                                               "nncp-bundle",
+                                               nncp.SdsAdd(sds, nncp.SDS{"err": err}),
+                                               "error reading tar",
+                                       )
                                }
-                               log.Fatalln("Error during tar reading:", err)
+                               continue
                        }
                        sds["pkt"] = entry.Name
                        if entry.Size < nncp.PktEncOverhead {
                                ctx.LogD("nncp-bundle", sds, "Too small packet")
                                continue
                        }
-                       sepIndex = strings.LastIndex(entry.Name, "/")
-                       if sepIndex == -1 {
-                               ctx.LogD("nncp-bundle", sds, "Bad packet name")
-                               continue
-                       }
-                       pktName = entry.Name[sepIndex+1:]
+                       pktName = filepath.Base(entry.Name)
                        if _, err = nncp.FromBase32(pktName); err != nil {
                                ctx.LogD("nncp-bundle", sds, "Bad packet name")
                                continue
@@ -195,7 +211,7 @@ func main() {
                                ctx.LogD("nncp-bundle", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
                                continue
                        }
-                       if _, err = xdr.Unmarshal(bytes.NewBuffer(pktEncBuf), &pktEnc); err != nil {
+                       if _, err = xdr.Unmarshal(bytes.NewReader(pktEncBuf), &pktEnc); err != nil {
                                ctx.LogD("nncp-bundle", sds, "Bad packet structure")
                                continue
                        }
index fa46282c3b9354d5ea8382ec57d46096cd3a53e9..1af1816a61c917edde3d700052f19c8d3d864ad3 100644 (file)
@@ -49,6 +49,8 @@ const (
        DefaultNiceMail = 64
        DefaultNiceFreq = 64
        DefaultNiceFile = 196
+
+       NNCPBundlePrefix = "NNCP/"
 )
 
 var (