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/v7"
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 UCSPIConn struct {
48 func (c UCSPIConn) Read(p []byte) (n int, err error) {
52 func (c UCSPIConn) Write(p []byte) (n int, err error) {
56 func (c UCSPIConn) SetReadDeadline(t time.Time) error {
57 return c.r.SetReadDeadline(t)
60 func (c UCSPIConn) SetWriteDeadline(t time.Time) error {
61 return c.w.SetWriteDeadline(t)
64 func (c UCSPIConn) Close() error {
65 if err := c.r.Close(); err != nil {
66 c.w.Close() // #nosec G104
74 conn nncp.ConnDeadlined,
78 nodeIdC chan *nncp.NodeId,
80 state := nncp.SPState{
85 if err := state.StartR(conn); err == nil {
88 nncp.LEs{{K: "Node", V: state.Node.Id}},
89 func(les nncp.LEs) string {
90 return fmt.Sprintf("Connection with %s (%s)", state.Node.Name, addr)
93 nodeIdC <- state.Node.Id
95 ctx.LogI("call-finished", nncp.LEs{
96 {K: "Node", V: state.Node.Id},
97 {K: "Duration", V: int64(state.Duration.Seconds())},
98 {K: "RxBytes", V: state.RxBytes},
99 {K: "TxBytes", V: state.TxBytes},
100 {K: "RxSpeed", V: state.RxSpeed},
101 {K: "TxSpeed", V: state.TxSpeed},
102 }, func(les nncp.LEs) string {
104 "Finished call with %s (%d:%d:%d): %s received (%s/sec), %s transferred (%s/sec)",
106 int(state.Duration.Hours()),
107 int(state.Duration.Minutes()),
108 int(state.Duration.Seconds())%60,
109 humanize.IBytes(uint64(state.RxBytes)),
110 humanize.IBytes(uint64(state.RxSpeed)),
111 humanize.IBytes(uint64(state.TxBytes)),
112 humanize.IBytes(uint64(state.TxSpeed)),
118 if state.Node == nil {
123 nodeId = state.Node.Id.String()
124 nodeName = state.Node.Name
125 nodeIdC <- state.Node.Id
129 nncp.LEs{{K: "Node", V: nodeId}},
130 func(les nncp.LEs) string { return "Connected to " + nodeName },
138 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
139 niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
140 bind = flag.String("bind", "[::]:5400", "Address to bind to")
141 ucspi = flag.Bool("ucspi", false, "Is it started as UCSPI-TCP server")
142 inetd = flag.Bool("inetd", false, "Obsolete, use -ucspi")
143 maxConn = flag.Int("maxconn", 128, "Maximal number of simultaneous connections")
144 noCK = flag.Bool("nock", false, "Do no checksum checking")
145 mcdOnce = flag.Bool("mcd-once", false, "Send MCDs once and quit")
146 spoolPath = flag.String("spool", "", "Override path to spool")
147 logPath = flag.String("log", "", "Override path to logfile")
148 quiet = flag.Bool("quiet", false, "Print only errors")
149 showPrgrs = flag.Bool("progress", false, "Force progress showing")
150 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
151 debug = flag.Bool("debug", false, "Print debug messages")
152 version = flag.Bool("version", false, "Print version information")
153 warranty = flag.Bool("warranty", false, "Print warranty information")
155 autoToss = flag.Bool("autotoss", false, "Toss after call is finished")
156 autoTossDoSeen = flag.Bool("autotoss-seen", false, "Create .seen files during tossing")
157 autoTossNoFile = flag.Bool("autotoss-nofile", false, "Do not process \"file\" packets during tossing")
158 autoTossNoFreq = flag.Bool("autotoss-nofreq", false, "Do not process \"freq\" packets during tossing")
159 autoTossNoExec = flag.Bool("autotoss-noexec", false, "Do not process \"exec\" packets during tossing")
160 autoTossNoTrns = flag.Bool("autotoss-notrns", false, "Do not process \"trns\" packets during tossing")
161 autoTossNoArea = flag.Bool("autotoss-noarea", false, "Do not process \"area\" packets during tossing")
163 log.SetFlags(log.Lshortfile)
167 fmt.Println(nncp.Warranty)
171 fmt.Println(nncp.VersionGet())
174 nice, err := nncp.NicenessParse(*niceRaw)
182 ctx, err := nncp.CtxFromCmdline(
192 log.Fatalln("Error during initialization:", err)
195 log.Fatalln("Config lacks private keys")
200 os.Stderr.Close() // #nosec G104
201 conn := &UCSPIConn{os.Stdin, os.Stdout}
202 nodeIdC := make(chan *nncp.NodeId)
204 if proto := os.Getenv("PROTO"); proto == "TCP" {
205 port := os.Getenv("TCPREMOTEPORT")
206 if host := os.Getenv("TCPREMOTEHOST"); host == "" {
207 addr = fmt.Sprintf("[%s]:%s", os.Getenv("TCPREMOTEIP"), port)
209 addr = fmt.Sprintf("%s:%s", host, port)
212 go performSP(ctx, conn, addr, nice, *noCK, nodeIdC)
214 var autoTossFinish chan struct{}
215 var autoTossBadCode chan bool
216 if *autoToss && nodeId != nil {
217 autoTossFinish, autoTossBadCode = ctx.AutoToss(
228 <-nodeIdC // call completion
230 close(autoTossFinish)
233 conn.Close() // #nosec G104
237 cols := strings.Split(*bind, ":")
238 port, err := strconv.Atoi(cols[len(cols)-1])
240 log.Fatalln("Can not parse port:", err)
244 for ifiName := range ctx.MCDTxIfis {
245 if err = ctx.MCDTx(ifiName, port, 0); err != nil {
246 log.Fatalln("Can not do MCD transmission:", err)
252 ln, err := net.Listen("tcp", *bind)
254 log.Fatalln("Can not listen:", err)
257 for ifiName, secs := range ctx.MCDTxIfis {
258 if err = ctx.MCDTx(ifiName, port, time.Duration(secs)*time.Second); err != nil {
259 log.Fatalln("Can not run MCD transmission:", err)
263 ln = netutil.LimitListener(ln, *maxConn)
265 conn, err := ln.Accept()
267 log.Fatalln("Can not accept connection:", err)
271 nncp.LEs{{K: "Addr", V: conn.RemoteAddr()}},
272 func(les nncp.LEs) string {
273 return "Accepted connection with " + conn.RemoteAddr().String()
276 go func(conn net.Conn) {
277 nodeIdC := make(chan *nncp.NodeId)
278 go performSP(ctx, conn, conn.RemoteAddr().String(), nice, *noCK, nodeIdC)
280 var autoTossFinish chan struct{}
281 var autoTossBadCode chan bool
282 if *autoToss && nodeId != nil {
283 autoTossFinish, autoTossBadCode = ctx.AutoToss(
294 <-nodeIdC // call completion
296 close(autoTossFinish)
299 conn.Close() // #nosec G104