]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/tx.go
ACK
[nncp.git] / src / tx.go
index 0f7d2b8d9369627d4cbf52eb63b996b5874a22a5..6e13f2ef3fbeb9e4f97fe61b99d1897f6a8f6c09 100644 (file)
--- a/src/tx.go
+++ b/src/tx.go
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2022 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -200,19 +200,24 @@ func (ctx *Ctx) Tx(
        }()
        var pktEncRaw []byte
        var pktEncMsg []byte
+       var payloadSize int64
        if area != nil {
-               pktEncMsg = (<-results).pktEncRaw
+               r := <-results
+               payloadSize = r.size
+               pktEncMsg = r.pktEncRaw
+               wrappers--
        }
-       var finalSize int64
        for i := 0; i <= wrappers; i++ {
                r := <-results
                if r.err != nil {
                        tmp.Fd.Close()
-                       return nil, 0, err
+                       return nil, 0, r.err
                }
                if r.pktEncRaw != nil {
-                       finalSize = r.size
                        pktEncRaw = r.pktEncRaw
+                       if payloadSize == 0 {
+                               payloadSize = r.size
+                       }
                }
        }
        nodePath := filepath.Join(ctx.Spool, lastNode.Id.String())
@@ -261,7 +266,7 @@ func (ctx *Ctx) Tx(
                }
                ctx.LogI("tx-area", les, logMsg)
        }
-       return lastNode, finalSize, err
+       return lastNode, payloadSize, err
 }
 
 type DummyCloser struct{}
@@ -325,11 +330,11 @@ func prepareTxFile(srcPath string) (
                if err != nil {
                        return err
                }
-               if info.IsDir() {
+               if info.Mode().IsDir() {
                        // directory header, PAX record header+contents
                        srcSize += TarBlockSize + 2*TarBlockSize
                        dirs = append(dirs, einfo{path: path, modTime: info.ModTime()})
-               } else {
+               } else if info.Mode().IsRegular() {
                        // file header, PAX record header+contents, file content
                        srcSize += TarBlockSize + 2*TarBlockSize + info.Size()
                        if n := info.Size() % TarBlockSize; n != 0 {
@@ -448,7 +453,7 @@ func (ctx *Ctx) TxFile(
                }
                logMsg := func(les LEs) string {
                        return fmt.Sprintf(
-                               "File %s (%s) sent to %s:%s",
+                               "File %s (%s) is sent to %s:%s",
                                srcPath,
                                humanize.IBytes(uint64(finalSize)),
                                ctx.NodeName(node.Id),
@@ -492,7 +497,7 @@ func (ctx *Ctx) TxFile(
                }
                logMsg := func(les LEs) string {
                        return fmt.Sprintf(
-                               "File %s (%s) sent to %s:%s",
+                               "File %s (%s) is sent to %s:%s",
                                srcPath,
                                humanize.IBytes(uint64(size)),
                                ctx.NodeName(node.Id),
@@ -553,7 +558,7 @@ func (ctx *Ctx) TxFile(
        }
        logMsg := func(les LEs) string {
                return fmt.Sprintf(
-                       "File %s (%s) sent to %s:%s",
+                       "File %s (%s) is sent to %s:%s",
                        srcPath,
                        humanize.IBytes(uint64(metaPktSize)),
                        ctx.NodeName(node.Id),
@@ -599,7 +604,7 @@ func (ctx *Ctx) TxFreq(
        }
        logMsg := func(les LEs) string {
                return fmt.Sprintf(
-                       "File request from %s:%s to %s sent",
+                       "File request from %s:%s to %s is sent",
                        ctx.NodeName(node.Id), srcPath,
                        dstPath,
                )
@@ -670,7 +675,7 @@ func (ctx *Ctx) TxExec(
        }
        logMsg := func(les LEs) string {
                return fmt.Sprintf(
-                       "Exec sent to %s@%s (%s)",
+                       "Exec is sent to %s@%s (%s)",
                        ctx.NodeName(node.Id), dst, humanize.IBytes(uint64(size)),
                )
        }
@@ -724,3 +729,39 @@ func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error
        os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name))
        return err
 }
+
+func (ctx *Ctx) TxACK(
+       node *Node,
+       nice uint8,
+       hsh string,
+       minSize int64,
+) error {
+       hshRaw, err := Base32Codec.DecodeString(hsh)
+       if err != nil {
+               return err
+       }
+       if len(hshRaw) != MTHSize {
+               return errors.New("Invalid packet id size")
+       }
+       pkt, err := NewPkt(PktTypeACK, nice, []byte(hshRaw))
+       if err != nil {
+               return err
+       }
+       src := bytes.NewReader([]byte{})
+       _, _, err = ctx.Tx(node, pkt, nice, 0, minSize, MaxFileSize, src, hsh, nil)
+       les := LEs{
+               {"Type", "ack"},
+               {"Node", node.Id},
+               {"Nice", int(nice)},
+               {"Pkt", hsh},
+       }
+       logMsg := func(les LEs) string {
+               return fmt.Sprintf("ACK to %s of %s is sent", ctx.NodeName(node.Id), hsh)
+       }
+       if err == nil {
+               ctx.LogI("tx", les, logMsg)
+       } else {
+               ctx.LogE("tx", les, err, logMsg)
+       }
+       return err
+}