2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 "github.com/dustin/go-humanize"
32 "go.cypherpunks.ru/nncp/v6"
33 "golang.org/x/net/netutil"
37 fmt.Fprintf(os.Stderr, nncp.UsageHeader())
38 fmt.Fprintf(os.Stderr, "nncp-daemon -- TCP daemon\n\n")
39 fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:\n", os.Args[0])
43 type InetdConn struct {
48 func (c InetdConn) Read(p []byte) (n int, err error) {
52 func (c InetdConn) Write(p []byte) (n int, err error) {
56 func (c InetdConn) SetReadDeadline(t time.Time) error {
57 return c.r.SetReadDeadline(t)
60 func (c InetdConn) SetWriteDeadline(t time.Time) error {
61 return c.w.SetWriteDeadline(t)
64 func (c InetdConn) Close() error {
65 if err := c.r.Close(); err != nil {
66 c.w.Close() // #nosec G104
74 conn nncp.ConnDeadlined,
77 nodeIdC chan *nncp.NodeId,
79 state := nncp.SPState{
84 if err := state.StartR(conn); err == nil {
87 nncp.LEs{{K: "Node", V: state.Node.Id}},
88 func(les nncp.LEs) string { return "Connection with " + state.Node.Name },
90 nodeIdC <- state.Node.Id
92 ctx.LogI("call-finished", nncp.LEs{
93 {K: "Node", V: state.Node.Id},
94 {K: "Duration", V: int64(state.Duration.Seconds())},
95 {K: "RxBytes", V: state.RxBytes},
96 {K: "TxBytes", V: state.TxBytes},
97 {K: "RxSpeed", V: state.RxSpeed},
98 {K: "TxSpeed", V: state.TxSpeed},
99 }, func(les nncp.LEs) string {
101 "Finished call with %s (%d:%d:%d): %s received (%s/sec), %s transferred (%s/sec)",
103 int(state.Duration.Hours()),
104 int(state.Duration.Minutes()),
105 int(state.Duration.Seconds()/60),
106 humanize.IBytes(uint64(state.RxBytes)),
107 humanize.IBytes(uint64(state.RxSpeed)),
108 humanize.IBytes(uint64(state.TxBytes)),
109 humanize.IBytes(uint64(state.TxSpeed)),
115 if state.Node == nil {
120 nodeId = state.Node.Id.String()
121 nodeName = state.Node.Name
122 nodeIdC <- state.Node.Id
126 nncp.LEs{{K: "Node", V: nodeId}},
127 func(les nncp.LEs) string { return "Connected to " + nodeName },
135 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
136 niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
137 bind = flag.String("bind", "[::]:5400", "Address to bind to")
138 inetd = flag.Bool("inetd", false, "Is it started as inetd service")
139 maxConn = flag.Int("maxconn", 128, "Maximal number of simultaneous connections")
140 noCK = flag.Bool("nock", false, "Do no checksum checking")
141 mcdOnce = flag.Bool("mcd-once", false, "Send MCDs once and quit")
142 spoolPath = flag.String("spool", "", "Override path to spool")
143 logPath = flag.String("log", "", "Override path to logfile")
144 quiet = flag.Bool("quiet", false, "Print only errors")
145 showPrgrs = flag.Bool("progress", false, "Force progress showing")
146 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
147 debug = flag.Bool("debug", false, "Print debug messages")
148 version = flag.Bool("version", false, "Print version information")
149 warranty = flag.Bool("warranty", false, "Print warranty information")
151 autoToss = flag.Bool("autotoss", false, "Toss after call is finished")
152 autoTossDoSeen = flag.Bool("autotoss-seen", false, "Create .seen files during tossing")
153 autoTossNoFile = flag.Bool("autotoss-nofile", false, "Do not process \"file\" packets during tossing")
154 autoTossNoFreq = flag.Bool("autotoss-nofreq", false, "Do not process \"freq\" packets during tossing")
155 autoTossNoExec = flag.Bool("autotoss-noexec", false, "Do not process \"exec\" packets during tossing")
156 autoTossNoTrns = flag.Bool("autotoss-notrns", false, "Do not process \"trns\" packets during tossing")
158 log.SetFlags(log.Lshortfile)
162 fmt.Println(nncp.Warranty)
166 fmt.Println(nncp.VersionGet())
169 nice, err := nncp.NicenessParse(*niceRaw)
174 ctx, err := nncp.CtxFromCmdline(
184 log.Fatalln("Error during initialization:", err)
187 log.Fatalln("Config lacks private keys")
192 os.Stderr.Close() // #nosec G104
193 conn := &InetdConn{os.Stdin, os.Stdout}
194 nodeIdC := make(chan *nncp.NodeId)
195 go performSP(ctx, conn, nice, *noCK, nodeIdC)
197 var autoTossFinish chan struct{}
198 var autoTossBadCode chan bool
199 if *autoToss && nodeId != nil {
200 autoTossFinish, autoTossBadCode = ctx.AutoToss(
210 <-nodeIdC // call completion
212 close(autoTossFinish)
215 conn.Close() // #nosec G104
219 cols := strings.Split(*bind, ":")
220 port, err := strconv.Atoi(cols[len(cols)-1])
222 log.Fatalln("Can not parse port:", err)
226 for ifiName := range ctx.MCDTxIfis {
227 if err = ctx.MCDTx(ifiName, port, 0); err != nil {
228 log.Fatalln("Can not do MCD transmission:", err)
234 ln, err := net.Listen("tcp", *bind)
236 log.Fatalln("Can not listen:", err)
239 for ifiName, secs := range ctx.MCDTxIfis {
240 if err = ctx.MCDTx(ifiName, port, time.Duration(secs)*time.Second); err != nil {
241 log.Fatalln("Can not run MCD transmission:", err)
245 ln = netutil.LimitListener(ln, *maxConn)
247 conn, err := ln.Accept()
249 log.Fatalln("Can not accept connection:", err)
253 nncp.LEs{{K: "Addr", V: conn.RemoteAddr()}},
254 func(les nncp.LEs) string {
255 return "Accepted connection with " + conn.RemoteAddr().String()
258 go func(conn net.Conn) {
259 nodeIdC := make(chan *nncp.NodeId)
260 go performSP(ctx, conn, nice, *noCK, nodeIdC)
262 var autoTossFinish chan struct{}
263 var autoTossBadCode chan bool
264 if *autoToss && nodeId != nil {
265 autoTossFinish, autoTossBadCode = ctx.AutoToss(
275 <-nodeIdC // call completion
277 close(autoTossFinish)
280 conn.Close() // #nosec G104