X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcmd%2Fnncp-ack%2Fmain.go;h=950295a7d81029f7b8b4dfc11815b454d0ea4d42;hb=6dc8ef3b800aa15476f1c745339d4445e622efbe;hp=e97202811230ac269fd321cd6e9d4d0c2c7aa816;hpb=904e7d0c6b99b13e57a987d583fa7e769add8c09;p=nncp.git 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) + } }