]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-daemon/main.go
Autotoss ability
[nncp.git] / src / cmd / nncp-daemon / main.go
index 3fdba332684f1e0e0cdfecd5128ff60049256ae9..b7aceb75e11f90eb1f99f26c7da7915c981739b8 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 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
@@ -59,10 +59,14 @@ func (c InetdConn) SetWriteDeadline(t time.Time) error {
 }
 
 func (c InetdConn) Close() error {
+       if err := c.r.Close(); err != nil {
+               c.w.Close() // #nosec G104
+               return err
+       }
        return c.w.Close()
 }
 
-func performSP(ctx *nncp.Ctx, conn nncp.ConnDeadlined, nice uint8) {
+func performSP(ctx *nncp.Ctx, conn nncp.ConnDeadlined, nice uint8) *nncp.SPState {
        state := nncp.SPState{
                Ctx:  ctx,
                Nice: nice,
@@ -72,7 +76,7 @@ func performSP(ctx *nncp.Ctx, conn nncp.ConnDeadlined, nice uint8) {
                state.Wait()
                ctx.LogI("call-finish", nncp.SDS{
                        "node":     state.Node.Id,
-                       "duration": state.Duration.Seconds(),
+                       "duration": int64(state.Duration.Seconds()),
                        "rxbytes":  state.RxBytes,
                        "txbytes":  state.TxBytes,
                        "rxspeed":  state.RxSpeed,
@@ -85,6 +89,7 @@ func performSP(ctx *nncp.Ctx, conn nncp.ConnDeadlined, nice uint8) {
                }
                ctx.LogE("call-start", nncp.SDS{"node": nodeId}, err, "")
        }
+       return &state
 }
 
 func main() {
@@ -102,6 +107,13 @@ func main() {
                debug     = flag.Bool("debug", false, "Print debug messages")
                version   = flag.Bool("version", false, "Print version information")
                warranty  = flag.Bool("warranty", false, "Print warranty information")
+
+               autotoss       = flag.Bool("autotoss", false, "Toss after call is finished")
+               autotossDoSeen = flag.Bool("autotoss-seen", false, "Create .seen files during tossing")
+               autotossNoFile = flag.Bool("autotoss-nofile", false, "Do not process \"file\" packets during tossing")
+               autotossNoFreq = flag.Bool("autotoss-nofreq", false, "Do not process \"freq\" packets during tossing")
+               autotossNoExec = flag.Bool("autotoss-noexec", false, "Do not process \"exec\" packets during tossing")
+               autotossNoTrns = flag.Bool("autotoss-notrns", false, "Do not process \"trns\" packets during tossing")
        )
        flag.Usage = usage
        flag.Parse()
@@ -136,9 +148,10 @@ func main() {
        ctx.Umask()
 
        if *inetd {
-               os.Stderr.Close()
+               os.Stderr.Close() // #nosec G104
                conn := &InetdConn{os.Stdin, os.Stdout}
                performSP(ctx, conn, nice)
+               conn.Close() // #nosec G104
                return
        }
 
@@ -154,8 +167,20 @@ func main() {
                }
                ctx.LogD("daemon", nncp.SDS{"addr": conn.RemoteAddr()}, "accepted")
                go func(conn net.Conn) {
-                       performSP(ctx, conn, nice)
-                       conn.Close()
+                       state := performSP(ctx, conn, nice)
+                       conn.Close() // #nosec G104
+                       if *autotoss && state.Node != nil {
+                               ctx.Toss(
+                                       state.Node.Id,
+                                       nice,
+                                       false,
+                                       *autotossDoSeen,
+                                       *autotossNoFile,
+                                       *autotossNoFreq,
+                                       *autotossNoExec,
+                                       *autotossNoTrns,
+                               )
+                       }
                }(conn)
        }
 }