From 6dc8ef3b800aa15476f1c745339d4445e622efbe Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Fri, 4 Mar 2022 13:19:38 +0300 Subject: [PATCH] Do not ACK ACKs --- doc/news.ru.texi | 4 ++ doc/news.texi | 4 ++ ports/nncp/Makefile | 2 +- src/cmd/nncp-ack/main.go | 90 ++++++++++++++++++++++++++++++++++++++++ src/nncp.go | 2 +- 5 files changed, 100 insertions(+), 2 deletions(-) diff --git a/doc/news.ru.texi b/doc/news.ru.texi index e814570..f4bdb57 100644 --- a/doc/news.ru.texi +++ b/doc/news.ru.texi @@ -5,6 +5,10 @@ @subsection Релиз 8.7.0 @itemize +@item +@command{nncp-ack} не подтверждает ACK-пакеты, предотвращая бесконечную +петлю из ACK-ов. + @item В прошлом, @command{nncp-ack} не удаляла соответствующие @file{hdr/} файлы. diff --git a/doc/news.texi b/doc/news.texi index 4ed60a0..33d5384 100644 --- a/doc/news.texi +++ b/doc/news.texi @@ -8,6 +8,10 @@ See also this page @ref{Новости, on russian}. @section Release 8.7.0 @itemize +@item +@command{nncp-ack} does not acknowledge ACK-packets, preventing an +endless loop of ACKs. + @item @command{nncp-ack} previously did not remove corresponding @file{hdr/} files. diff --git a/ports/nncp/Makefile b/ports/nncp/Makefile index 05c0ad1..b959a32 100644 --- a/ports/nncp/Makefile +++ b/ports/nncp/Makefile @@ -1,5 +1,5 @@ PORTNAME= nncp -DISTVERSION= 8.6.0 +DISTVERSION= 8.7.0 CATEGORIES= net MASTER_SITES= http://www.nncpgo.org/download/ diff --git a/src/cmd/nncp-ack/main.go b/src/cmd/nncp-ack/main.go index e972028..950295a 100644 --- a/src/cmd/nncp-ack/main.go +++ b/src/cmd/nncp-ack/main.go @@ -19,13 +19,17 @@ along with this program. If not, see . package main import ( + "bufio" + "errors" "flag" "fmt" + "io" "log" "os" "path/filepath" "strings" + xdr "github.com/davecgh/go-xdr/xdr2" "go.cypherpunks.ru/nncp/v8" ) @@ -127,12 +131,98 @@ func main() { return } + isBad := false for _, node := range nodes { for job := range ctx.Jobs(node.Id, nncp.TRx) { pktName := filepath.Base(job.Path) + sender := ctx.Neigh[*job.PktEnc.Sender] + les := nncp.LEs{ + {K: "Node", V: job.PktEnc.Sender}, + {K: "Pkt", V: pktName}, + } + logMsg := func(les nncp.LEs) string { + return fmt.Sprintf( + "ACKing %s/%s", + ctx.NodeName(job.PktEnc.Sender), pktName, + ) + } + if sender == nil { + err := errors.New("unknown node") + ctx.LogE("ack-read", les, err, logMsg) + isBad = true + continue + } + fd, err := os.Open(job.Path) + if err != nil { + ctx.LogE("ack-read-open", les, err, func(les nncp.LEs) string { + return logMsg(les) + ": opening" + job.Path + }) + isBad = true + continue + } + pktEnc, _, err := ctx.HdrRead(fd) + if err != nil { + fd.Close() + ctx.LogE("ack-read-read", les, err, func(les nncp.LEs) string { + return logMsg(les) + ": reading" + job.Path + }) + isBad = true + continue + } + switch pktEnc.Magic { + case nncp.MagicNNCPEv1.B: + err = nncp.MagicNNCPEv1.TooOld() + case nncp.MagicNNCPEv2.B: + err = nncp.MagicNNCPEv2.TooOld() + case nncp.MagicNNCPEv3.B: + err = nncp.MagicNNCPEv3.TooOld() + case nncp.MagicNNCPEv4.B: + err = nncp.MagicNNCPEv4.TooOld() + case nncp.MagicNNCPEv5.B: + err = nncp.MagicNNCPEv5.TooOld() + case nncp.MagicNNCPEv6.B: + default: + err = errors.New("is not an encrypted packet") + } + if err != nil { + fd.Close() + ctx.LogE("ack-read-magic", les, err, logMsg) + isBad = true + continue + } + if _, err = fd.Seek(0, io.SeekStart); err != nil { + fd.Close() + ctx.LogE("ack-read-seek", les, err, func(les nncp.LEs) string { + return logMsg(les) + ": seeking" + }) + isBad = true + continue + } + pipeR, pipeW := io.Pipe() + go nncp.PktEncRead(ctx.Self, ctx.Neigh, bufio.NewReader(fd), pipeW, true, nil) + var pkt nncp.Pkt + _, err = xdr.Unmarshal(pipeR, &pkt) + fd.Close() + pipeW.Close() + if err != nil { + ctx.LogE("ack-read-unmarshal", les, err, func(les nncp.LEs) string { + return logMsg(les) + ": unmarshal" + }) + isBad = true + continue + } + if pkt.Type == nncp.PktTypeACK { + ctx.LogI("ack-read-if-ack", les, func(les nncp.LEs) string { + return logMsg(les) + ": it is ACK, skipping" + }) + continue + } if err = ctx.TxACK(node, nice, pktName, minSize); err != nil { log.Fatalln(err) } } } + if isBad { + os.Exit(1) + } } diff --git a/src/nncp.go b/src/nncp.go index 0ae3470..ff817da 100644 --- a/src/nncp.go +++ b/src/nncp.go @@ -40,7 +40,7 @@ along with this program. If not, see .` const Base32Encoded32Len = 52 var ( - Version string = "8.6.0" + Version string = "8.7.0" Base32Codec *base32.Encoding = base32.StdEncoding.WithPadding(base32.NoPadding) ) -- 2.44.0