]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-daemon/main.go
Multicast areas
[nncp.git] / src / cmd / nncp-daemon / main.go
index 6319debf56957061cbb1f34a41bf0f0db0ae6ab7..b0628fe421a39c8ab37f21e8cb6f733d79b343c8 100644 (file)
@@ -24,9 +24,12 @@ import (
        "log"
        "net"
        "os"
+       "strconv"
+       "strings"
        "time"
 
-       "go.cypherpunks.ru/nncp/v5"
+       "github.com/dustin/go-humanize"
+       "go.cypherpunks.ru/nncp/v7"
        "golang.org/x/net/netutil"
 )
 
@@ -69,34 +72,63 @@ func (c InetdConn) Close() error {
 func performSP(
        ctx *nncp.Ctx,
        conn nncp.ConnDeadlined,
+       addr string,
        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.LEs{{K: "Node", V: state.Node.Id}}, "connected")
+               ctx.LogI(
+                       "call-started",
+                       nncp.LEs{{K: "Node", V: state.Node.Id}},
+                       func(les nncp.LEs) string {
+                               return fmt.Sprintf("Connection with %s (%s)", state.Node.Name, addr)
+                       },
+               )
                nodeIdC <- state.Node.Id
                state.Wait()
-               ctx.LogI("call-finish", nncp.LEs{
+               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())%60,
+                               humanize.IBytes(uint64(state.RxBytes)),
+                               humanize.IBytes(uint64(state.RxSpeed)),
+                               humanize.IBytes(uint64(state.TxBytes)),
+                               humanize.IBytes(uint64(state.TxSpeed)),
+                       )
+               })
        } else {
-               nodeId := "unknown"
+               var nodeId string
+               var nodeName string
                if state.Node == nil {
+                       nodeId = "unknown"
+                       nodeName = "unknown"
                        nodeIdC <- nil
                } else {
-                       nodeIdC <- state.Node.Id
                        nodeId = state.Node.Id.String()
+                       nodeName = state.Node.Name
+                       nodeIdC <- state.Node.Id
                }
-               ctx.LogI("call-start", nncp.LEs{{K: "Node", V: nodeId}}, "connected")
+               ctx.LogI(
+                       "call-started",
+                       nncp.LEs{{K: "Node", V: nodeId}},
+                       func(les nncp.LEs) string { return "Connected to " + nodeName },
+               )
        }
        close(nodeIdC)
 }
@@ -108,6 +140,8 @@ 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")
+               mcdOnce   = flag.Bool("mcd-once", false, "Send MCDs once and quit")
                spoolPath = flag.String("spool", "", "Override path to spool")
                logPath   = flag.String("log", "", "Override path to logfile")
                quiet     = flag.Bool("quiet", false, "Print only errors")
@@ -123,7 +157,9 @@ func main() {
                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")
+               autoTossNoArea = flag.Bool("autotoss-noarea", false, "Do not process \"area\" packets during tossing")
        )
+       log.SetFlags(log.Lshortfile)
        flag.Usage = usage
        flag.Parse()
        if *warranty {
@@ -160,7 +196,7 @@ func main() {
                os.Stderr.Close() // #nosec G104
                conn := &InetdConn{os.Stdin, os.Stdout}
                nodeIdC := make(chan *nncp.NodeId)
-               go performSP(ctx, conn, nice, nodeIdC)
+               go performSP(ctx, conn, "PIPE", nice, *noCK, nodeIdC)
                nodeId := <-nodeIdC
                var autoTossFinish chan struct{}
                var autoTossBadCode chan bool
@@ -173,6 +209,7 @@ func main() {
                                *autoTossNoFreq,
                                *autoTossNoExec,
                                *autoTossNoTrns,
+                               *autoTossNoArea,
                        )
                }
                <-nodeIdC // call completion
@@ -184,20 +221,48 @@ func main() {
                return
        }
 
+       cols := strings.Split(*bind, ":")
+       port, err := strconv.Atoi(cols[len(cols)-1])
+       if err != nil {
+               log.Fatalln("Can not parse port:", err)
+       }
+
+       if *mcdOnce {
+               for ifiName := range ctx.MCDTxIfis {
+                       if err = ctx.MCDTx(ifiName, port, 0); err != nil {
+                               log.Fatalln("Can not do MCD transmission:", err)
+                       }
+               }
+               return
+       }
+
        ln, err := net.Listen("tcp", *bind)
        if err != nil {
                log.Fatalln("Can not listen:", err)
        }
+
+       for ifiName, secs := range ctx.MCDTxIfis {
+               if err = ctx.MCDTx(ifiName, port, time.Duration(secs)*time.Second); err != nil {
+                       log.Fatalln("Can not run MCD transmission:", err)
+               }
+       }
+
        ln = netutil.LimitListener(ln, *maxConn)
        for {
                conn, err := ln.Accept()
                if err != nil {
                        log.Fatalln("Can not accept connection:", err)
                }
-               ctx.LogD("daemon", nncp.LEs{{K: "Addr", V: 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) {
                        nodeIdC := make(chan *nncp.NodeId)
-                       go performSP(ctx, conn, nice, nodeIdC)
+                       go performSP(ctx, conn, conn.RemoteAddr().String(), nice, *noCK, nodeIdC)
                        nodeId := <-nodeIdC
                        var autoTossFinish chan struct{}
                        var autoTossBadCode chan bool
@@ -210,6 +275,7 @@ func main() {
                                        *autoTossNoFreq,
                                        *autoTossNoExec,
                                        *autoTossNoTrns,
+                                       *autoTossNoArea,
                                )
                        }
                        <-nodeIdC // call completion