2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 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/>.
32 "github.com/dustin/go-humanize"
33 "go.cypherpunks.ru/nncp/v8"
34 nncpYggdrasil "go.cypherpunks.ru/nncp/v8/yggdrasil"
35 "golang.org/x/net/netutil"
39 fmt.Fprint(os.Stderr, "nncp-daemon -- TCP daemon\n\n")
40 fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:\n", os.Args[0])
46 conn nncp.ConnDeadlined,
50 nodeIdC chan *nncp.NodeId,
52 state := nncp.SPState{
57 if err := state.StartR(conn); err == nil {
60 nncp.LEs{{K: "Node", V: state.Node.Id}},
61 func(les nncp.LEs) string {
62 return fmt.Sprintf("Connection with %s (%s)", state.Node.Name, addr)
65 nodeIdC <- state.Node.Id
67 ctx.LogI("call-finished", nncp.LEs{
68 {K: "Node", V: state.Node.Id},
69 {K: "Duration", V: int64(state.Duration.Seconds())},
70 {K: "RxBytes", V: state.RxBytes},
71 {K: "TxBytes", V: state.TxBytes},
72 {K: "RxSpeed", V: state.RxSpeed},
73 {K: "TxSpeed", V: state.TxSpeed},
74 }, func(les nncp.LEs) string {
76 "Finished call with %s (%d:%d:%d): %s received (%s/sec), %s transferred (%s/sec)",
78 int(state.Duration.Hours()),
79 int(state.Duration.Minutes()),
80 int(state.Duration.Seconds())%60,
81 humanize.IBytes(uint64(state.RxBytes)),
82 humanize.IBytes(uint64(state.RxSpeed)),
83 humanize.IBytes(uint64(state.TxBytes)),
84 humanize.IBytes(uint64(state.TxSpeed)),
90 if state.Node == nil {
95 nodeId = state.Node.Id.String()
96 nodeName = state.Node.Name
97 nodeIdC <- state.Node.Id
101 nncp.LEs{{K: "Node", V: nodeId}},
102 func(les nncp.LEs) string { return "Connected to " + nodeName },
108 func startMCDTx(ctx *nncp.Ctx, port int, zeroInterval bool) error {
109 ifis, err := net.Interfaces()
113 for ifiReString, secs := range ctx.MCDTxIfis {
114 ifiRe, err := regexp.CompilePOSIX(ifiReString)
118 var interval time.Duration
120 interval = time.Duration(secs) * time.Second
122 for _, ifi := range ifis {
123 if ifiRe.MatchString(ifi.Name) {
124 if err = ctx.MCDTx(ifi.Name, port, interval); err != nil {
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 ucspi = flag.Bool("ucspi", false, "Is it started as UCSPI-TCP server")
139 inetd = flag.Bool("inetd", false, "Obsolete, use -ucspi")
140 yggdrasil = flag.String("yggdrasil", "",
141 "Start Yggdrasil listener: yggdrasils://PRV[:PORT]?[bind=BIND][&pub=PUB][&peer=PEER][&mcast=REGEX[:PORT]]")
142 maxConn = flag.Int("maxconn", 128, "Maximal number of simultaneous connections")
143 noCK = flag.Bool("nock", false, "Do no checksum checking")
144 mcdOnce = flag.Bool("mcd-once", false, "Send MCDs once and quit")
145 spoolPath = flag.String("spool", "", "Override path to spool")
146 logPath = flag.String("log", "", "Override path to logfile")
147 quiet = flag.Bool("quiet", false, "Print only errors")
148 showPrgrs = flag.Bool("progress", false, "Force progress showing")
149 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
150 debug = flag.Bool("debug", false, "Print debug messages")
151 version = flag.Bool("version", false, "Print version information")
152 warranty = flag.Bool("warranty", false, "Print warranty information")
154 autoToss = flag.Bool("autotoss", false,
155 "Toss after call is finished")
156 autoTossDoSeen = flag.Bool("autotoss-seen", false,
157 "Create seen/ files during tossing")
158 autoTossNoFile = flag.Bool("autotoss-nofile", false,
159 "Do not process \"file\" packets during tossing")
160 autoTossNoFreq = flag.Bool("autotoss-nofreq", false,
161 "Do not process \"freq\" packets during tossing")
162 autoTossNoExec = flag.Bool("autotoss-noexec", false,
163 "Do not process \"exec\" packets during tossing")
164 autoTossNoTrns = flag.Bool("autotoss-notrns", false,
165 "Do not process \"trns\" packets during tossing")
166 autoTossNoArea = flag.Bool("autotoss-noarea", false,
167 "Do not process \"area\" packets during tossing")
168 autoTossNoACK = flag.Bool("autotoss-noack", false,
169 "Do not process \"ack\" packets during tossing")
170 autoTossGenACK = flag.Bool("autotoss-gen-ack", false,
171 "Generate ACK packets")
173 log.SetFlags(log.Lshortfile)
177 fmt.Println(nncp.Warranty)
181 fmt.Println(nncp.VersionGet())
184 nice, err := nncp.NicenessParse(*niceRaw)
192 ctx, err := nncp.CtxFromCmdline(
202 log.Fatalln("Error during initialization:", err)
205 log.Fatalln("Config lacks private keys")
211 conn := &nncp.UCSPIConn{R: os.Stdin, W: os.Stdout}
212 nodeIdC := make(chan *nncp.NodeId)
213 addr := nncp.UCSPITCPRemoteAddr()
217 go performSP(ctx, conn, addr, nice, *noCK, nodeIdC)
219 var autoTossFinish chan struct{}
220 var autoTossBadCode chan bool
221 if *autoToss && nodeId != nil {
222 autoTossFinish, autoTossBadCode = ctx.AutoToss(
226 DoSeen: *autoTossDoSeen,
227 NoFile: *autoTossNoFile,
228 NoFreq: *autoTossNoFreq,
229 NoExec: *autoTossNoExec,
230 NoTrns: *autoTossNoTrns,
231 NoArea: *autoTossNoArea,
232 NoACK: *autoTossNoACK,
233 GenACK: *autoTossGenACK,
237 <-nodeIdC // call completion
238 if *autoToss && nodeId != nil {
239 close(autoTossFinish)
246 conns := make(chan net.Conn)
248 cols := strings.Split(*bind, ":")
249 port, err := strconv.Atoi(cols[len(cols)-1])
251 log.Fatalln("Can not parse port:", err)
255 if err = startMCDTx(ctx, port, true); err != nil {
256 log.Fatalln("Can not do MCD transmission:", err)
261 ln, err := net.Listen("tcp", *bind)
263 log.Fatalln("Can not listen:", err)
265 if err = startMCDTx(ctx, port, false); err != nil {
266 log.Fatalln("Can not do MCD transmission:", err)
268 ln = netutil.LimitListener(ln, *maxConn)
271 conn, err := ln.Accept()
273 log.Fatalln("Can not accept connection on TCP:", err)
280 if *yggdrasil != "" {
281 ln, err := nncpYggdrasil.NewListener(ctx.YggdrasilAliases, *yggdrasil)
283 log.Fatalln("Can not listen:", err)
285 ln = netutil.LimitListener(ln, *maxConn)
288 conn, err := ln.Accept()
290 log.Fatalln("Can not accept connection on Yggdrasil:", err)
297 for conn := range conns {
300 nncp.LEs{{K: "Addr", V: conn.RemoteAddr()}},
301 func(les nncp.LEs) string {
302 return "Accepted connection with " + conn.RemoteAddr().String()
305 go func(conn net.Conn) {
306 nodeIdC := make(chan *nncp.NodeId)
307 go performSP(ctx, conn, conn.RemoteAddr().String(), nice, *noCK, nodeIdC)
309 var autoTossFinish chan struct{}
310 var autoTossBadCode chan bool
311 if *autoToss && nodeId != nil {
312 autoTossFinish, autoTossBadCode = ctx.AutoToss(
316 DoSeen: *autoTossDoSeen,
317 NoFile: *autoTossNoFile,
318 NoFreq: *autoTossNoFreq,
319 NoExec: *autoTossNoExec,
320 NoTrns: *autoTossNoTrns,
321 NoArea: *autoTossNoArea,
322 NoACK: *autoTossNoACK,
323 GenACK: *autoTossGenACK,
327 <-nodeIdC // call completion
328 if *autoToss && nodeId != nil {
329 close(autoTossFinish)