X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcmd%2Fnncp-daemon%2Fmain.go;h=252527f56a1520044d681419e1d7e10967768729;hb=0367cce2741e1ce6a89a49fd5c4e9df6005c9744;hp=e3a73c9e72c162da46a8914a1b6709aed83d41b6;hpb=16ac75d51758c249f5fc4e8fac9cf71b84331c55;p=nncp.git diff --git a/src/cmd/nncp-daemon/main.go b/src/cmd/nncp-daemon/main.go index e3a73c9..252527f 100644 --- a/src/cmd/nncp-daemon/main.go +++ b/src/cmd/nncp-daemon/main.go @@ -1,6 +1,6 @@ /* NNCP -- Node to Node copy, utilities for store-and-forward data exchange -Copyright (C) 2016-2021 Sergey Matveev +Copyright (C) 2016-2022 Sergey Matveev 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 @@ -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/v8" "golang.org/x/net/netutil" ) @@ -37,38 +40,10 @@ func usage() { flag.PrintDefaults() } -type InetdConn struct { - r *os.File - w *os.File -} - -func (c InetdConn) Read(p []byte) (n int, err error) { - return c.r.Read(p) -} - -func (c InetdConn) Write(p []byte) (n int, err error) { - return c.w.Write(p) -} - -func (c InetdConn) SetReadDeadline(t time.Time) error { - return c.r.SetReadDeadline(t) -} - -func (c InetdConn) SetWriteDeadline(t time.Time) error { - return c.w.SetWriteDeadline(t) -} - -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, + addr string, nice uint8, noCK bool, nodeIdC chan *nncp.NodeId, @@ -79,26 +54,52 @@ func performSP( 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,9 +109,11 @@ func main() { cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness") bind = flag.String("bind", "[::]:5400", "Address to bind to") - inetd = flag.Bool("inetd", false, "Is it started as inetd service") + ucspi = flag.Bool("ucspi", false, "Is it started as UCSPI-TCP server") + inetd = flag.Bool("inetd", false, "Obsolete, use -ucspi") 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") @@ -121,12 +124,14 @@ func main() { 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") + 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") + autoTossNoArea = flag.Bool("autotoss-noarea", false, "Do not process \"area\" packets during tossing") ) + log.SetFlags(log.Lshortfile) flag.Usage = usage flag.Parse() if *warranty { @@ -141,6 +146,9 @@ func main() { if err != nil { log.Fatalln(err) } + if *inetd { + *ucspi = true + } ctx, err := nncp.CtxFromCmdline( *cfgPath, @@ -159,11 +167,15 @@ func main() { } ctx.Umask() - if *inetd { - os.Stderr.Close() // #nosec G104 - conn := &InetdConn{os.Stdin, os.Stdout} + if *ucspi { + os.Stderr.Close() + conn := &nncp.UCSPIConn{R: os.Stdin, W: os.Stdout} nodeIdC := make(chan *nncp.NodeId) - go performSP(ctx, conn, nice, *noCK, nodeIdC) + addr := nncp.UCSPITCPRemoteAddr() + if addr == "" { + addr = "PIPE" + } + go performSP(ctx, conn, addr, nice, *noCK, nodeIdC) nodeId := <-nodeIdC var autoTossFinish chan struct{} var autoTossBadCode chan bool @@ -176,6 +188,7 @@ func main() { *autoTossNoFreq, *autoTossNoExec, *autoTossNoTrns, + *autoTossNoArea, ) } <-nodeIdC // call completion @@ -183,7 +196,22 @@ func main() { close(autoTossFinish) <-autoTossBadCode } - conn.Close() // #nosec G104 + conn.Close() + 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 } @@ -191,16 +219,29 @@ func main() { 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, *noCK, nodeIdC) + go performSP(ctx, conn, conn.RemoteAddr().String(), nice, *noCK, nodeIdC) nodeId := <-nodeIdC var autoTossFinish chan struct{} var autoTossBadCode chan bool @@ -213,6 +254,7 @@ func main() { *autoTossNoFreq, *autoTossNoExec, *autoTossNoTrns, + *autoTossNoArea, ) } <-nodeIdC // call completion @@ -220,7 +262,7 @@ func main() { close(autoTossFinish) <-autoTossBadCode } - conn.Close() // #nosec G104 + conn.Close() }(conn) } }