]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-daemon/main.go
Fix nncp-daemon segfault when SP fails
[nncp.git] / src / cmd / nncp-daemon / main.go
index b7aceb75e11f90eb1f99f26c7da7915c981739b8..a1b7982a8bdab14819674323cb11299f0b764049 100644 (file)
@@ -26,7 +26,8 @@ import (
        "os"
        "time"
 
-       "go.cypherpunks.ru/nncp/v5"
+       "github.com/dustin/go-humanize"
+       "go.cypherpunks.ru/nncp/v6"
        "golang.org/x/net/netutil"
 )
 
@@ -66,30 +67,65 @@ func (c InetdConn) Close() error {
        return c.w.Close()
 }
 
-func performSP(ctx *nncp.Ctx, conn nncp.ConnDeadlined, nice uint8) *nncp.SPState {
+func performSP(
+       ctx *nncp.Ctx,
+       conn nncp.ConnDeadlined,
+       nice uint8,
+       noCK bool,
+       nodeIdC chan *nncp.NodeId,
+) {
        state := nncp.SPState{
                Ctx:  ctx,
                Nice: nice,
+               NoCK: noCK,
        }
        if err := state.StartR(conn); err == nil {
-               ctx.LogI("call-start", nncp.SDS{"node": state.Node.Id}, "connected")
+               ctx.LogI(
+                       "call-started",
+                       nncp.LEs{{K: "Node", V: state.Node.Id}},
+                       func(les nncp.LEs) string { return "Connection with " + state.Node.Name },
+               )
+               nodeIdC <- state.Node.Id
                state.Wait()
-               ctx.LogI("call-finish", nncp.SDS{
-                       "node":     state.Node.Id,
-                       "duration": int64(state.Duration.Seconds()),
-                       "rxbytes":  state.RxBytes,
-                       "txbytes":  state.TxBytes,
-                       "rxspeed":  state.RxSpeed,
-                       "txspeed":  state.TxSpeed,
-               }, "")
+               ctx.LogI("call-finished", nncp.LEs{
+                       {K: "Node", V: state.Node.Id},
+                       {K: "Duration", V: int64(state.Duration.Seconds())},
+                       {K: "RxBytes", V: state.RxBytes},
+                       {K: "TxBytes", V: state.TxBytes},
+                       {K: "RxSpeed", V: state.RxSpeed},
+                       {K: "TxSpeed", V: state.TxSpeed},
+               }, func(les nncp.LEs) string {
+                       return fmt.Sprintf(
+                               "Finished call with %s (%d:%d:%d): %s received (%s/sec), %s transferred (%s/sec)",
+                               state.Node.Name,
+                               int(state.Duration.Hours()),
+                               int(state.Duration.Minutes()),
+                               int(state.Duration.Seconds()),
+                               humanize.IBytes(uint64(state.RxBytes)),
+                               humanize.IBytes(uint64(state.RxSpeed)),
+                               humanize.IBytes(uint64(state.TxBytes)),
+                               humanize.IBytes(uint64(state.TxSpeed)),
+                       )
+               })
        } else {
-               nodeId := "unknown"
-               if state.Node != nil {
+               var nodeId string
+               var nodeName string
+               if state.Node == nil {
+                       nodeId = "unknown"
+                       nodeName = "unknown"
+                       nodeIdC <- nil
+               } else {
                        nodeId = state.Node.Id.String()
+                       nodeName = state.Node.Name
+                       nodeIdC <- state.Node.Id
                }
-               ctx.LogE("call-start", nncp.SDS{"node": nodeId}, err, "")
+               ctx.LogI(
+                       "call-started",
+                       nncp.LEs{{K: "Node", V: nodeId}},
+                       func(les nncp.LEs) string { return "Connected to " + nodeName },
+               )
        }
-       return &state
+       close(nodeIdC)
 }
 
 func main() {
@@ -99,6 +135,7 @@ func main() {
                bind      = flag.String("bind", "[::]:5400", "Address to bind to")
                inetd     = flag.Bool("inetd", false, "Is it started as inetd service")
                maxConn   = flag.Int("maxconn", 128, "Maximal number of simultaneous connections")
+               noCK      = flag.Bool("nock", false, "Do no checksum checking")
                spoolPath = flag.String("spool", "", "Override path to spool")
                logPath   = flag.String("log", "", "Override path to logfile")
                quiet     = flag.Bool("quiet", false, "Print only errors")
@@ -108,12 +145,12 @@ func main() {
                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")
+               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()
@@ -150,7 +187,27 @@ func main() {
        if *inetd {
                os.Stderr.Close() // #nosec G104
                conn := &InetdConn{os.Stdin, os.Stdout}
-               performSP(ctx, conn, nice)
+               nodeIdC := make(chan *nncp.NodeId)
+               go performSP(ctx, conn, nice, *noCK, nodeIdC)
+               nodeId := <-nodeIdC
+               var autoTossFinish chan struct{}
+               var autoTossBadCode chan bool
+               if *autoToss && nodeId != nil {
+                       autoTossFinish, autoTossBadCode = ctx.AutoToss(
+                               nodeId,
+                               nice,
+                               *autoTossDoSeen,
+                               *autoTossNoFile,
+                               *autoTossNoFreq,
+                               *autoTossNoExec,
+                               *autoTossNoTrns,
+                       )
+               }
+               <-nodeIdC // call completion
+               if *autoToss {
+                       close(autoTossFinish)
+                       <-autoTossBadCode
+               }
                conn.Close() // #nosec G104
                return
        }
@@ -165,22 +222,36 @@ func main() {
                if err != nil {
                        log.Fatalln("Can not accept connection:", err)
                }
-               ctx.LogD("daemon", nncp.SDS{"addr": conn.RemoteAddr()}, "accepted")
+               ctx.LogD(
+                       "daemon-accepted",
+                       nncp.LEs{{K: "Addr", V: conn.RemoteAddr()}},
+                       func(les nncp.LEs) string {
+                               return "Accepted connection with " + conn.RemoteAddr().String()
+                       },
+               )
                go func(conn net.Conn) {
-                       state := performSP(ctx, conn, nice)
-                       conn.Close() // #nosec G104
-                       if *autotoss && state.Node != nil {
-                               ctx.Toss(
-                                       state.Node.Id,
+                       nodeIdC := make(chan *nncp.NodeId)
+                       go performSP(ctx, conn, nice, *noCK, nodeIdC)
+                       nodeId := <-nodeIdC
+                       var autoTossFinish chan struct{}
+                       var autoTossBadCode chan bool
+                       if *autoToss && nodeId != nil {
+                               autoTossFinish, autoTossBadCode = ctx.AutoToss(
+                                       nodeId,
                                        nice,
-                                       false,
-                                       *autotossDoSeen,
-                                       *autotossNoFile,
-                                       *autotossNoFreq,
-                                       *autotossNoExec,
-                                       *autotossNoTrns,
+                                       *autoTossDoSeen,
+                                       *autoTossNoFile,
+                                       *autoTossNoFreq,
+                                       *autoTossNoExec,
+                                       *autoTossNoTrns,
                                )
                        }
+                       <-nodeIdC // call completion
+                       if *autoToss {
+                               close(autoTossFinish)
+                               <-autoTossBadCode
+                       }
+                       conn.Close() // #nosec G104
                }(conn)
        }
 }