2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 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.Fprintf(os.Stderr, nncp.UsageHeader())
40 fmt.Fprintf(os.Stderr, "nncp-daemon -- TCP daemon\n\n")
41 fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:\n", os.Args[0])
47 conn nncp.ConnDeadlined,
51 nodeIdC chan *nncp.NodeId,
53 state := nncp.SPState{
58 if err := state.StartR(conn); err == nil {
61 nncp.LEs{{K: "Node", V: state.Node.Id}},
62 func(les nncp.LEs) string {
63 return fmt.Sprintf("Connection with %s (%s)", state.Node.Name, addr)
66 nodeIdC <- state.Node.Id
68 ctx.LogI("call-finished", nncp.LEs{
69 {K: "Node", V: state.Node.Id},
70 {K: "Duration", V: int64(state.Duration.Seconds())},
71 {K: "RxBytes", V: state.RxBytes},
72 {K: "TxBytes", V: state.TxBytes},
73 {K: "RxSpeed", V: state.RxSpeed},
74 {K: "TxSpeed", V: state.TxSpeed},
75 }, func(les nncp.LEs) string {
77 "Finished call with %s (%d:%d:%d): %s received (%s/sec), %s transferred (%s/sec)",
79 int(state.Duration.Hours()),
80 int(state.Duration.Minutes()),
81 int(state.Duration.Seconds())%60,
82 humanize.IBytes(uint64(state.RxBytes)),
83 humanize.IBytes(uint64(state.RxSpeed)),
84 humanize.IBytes(uint64(state.TxBytes)),
85 humanize.IBytes(uint64(state.TxSpeed)),
91 if state.Node == nil {
96 nodeId = state.Node.Id.String()
97 nodeName = state.Node.Name
98 nodeIdC <- state.Node.Id
102 nncp.LEs{{K: "Node", V: nodeId}},
103 func(les nncp.LEs) string { return "Connected to " + nodeName },
109 func startMCDTx(ctx *nncp.Ctx, port int, zeroInterval bool) error {
110 ifis, err := net.Interfaces()
114 for ifiReString, secs := range ctx.MCDTxIfis {
115 ifiRe, err := regexp.CompilePOSIX(ifiReString)
119 var interval time.Duration
121 interval = time.Duration(secs) * time.Second
123 for _, ifi := range ifis {
124 if ifiRe.MatchString(ifi.Name) {
125 if err = ctx.MCDTx(ifi.Name, port, interval); err != nil {
136 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
137 niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
138 bind = flag.String("bind", "[::]:5400", "Address to bind to")
139 ucspi = flag.Bool("ucspi", false, "Is it started as UCSPI-TCP server")
140 inetd = flag.Bool("inetd", false, "Obsolete, use -ucspi")
141 yggdrasil = flag.String("yggdrasil", "", "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, "Toss after call is finished")
155 autoTossDoSeen = flag.Bool("autotoss-seen", false, "Create seen/ files during tossing")
156 autoTossNoFile = flag.Bool("autotoss-nofile", false, "Do not process \"file\" packets during tossing")
157 autoTossNoFreq = flag.Bool("autotoss-nofreq", false, "Do not process \"freq\" packets during tossing")
158 autoTossNoExec = flag.Bool("autotoss-noexec", false, "Do not process \"exec\" packets during tossing")
159 autoTossNoTrns = flag.Bool("autotoss-notrns", false, "Do not process \"trns\" packets during tossing")
160 autoTossNoArea = flag.Bool("autotoss-noarea", false, "Do not process \"area\" packets during tossing")
162 log.SetFlags(log.Lshortfile)
166 fmt.Println(nncp.Warranty)
170 fmt.Println(nncp.VersionGet())
173 nice, err := nncp.NicenessParse(*niceRaw)
181 ctx, err := nncp.CtxFromCmdline(
191 log.Fatalln("Error during initialization:", err)
194 log.Fatalln("Config lacks private keys")
200 conn := &nncp.UCSPIConn{R: os.Stdin, W: os.Stdout}
201 nodeIdC := make(chan *nncp.NodeId)
202 addr := nncp.UCSPITCPRemoteAddr()
206 go performSP(ctx, conn, addr, nice, *noCK, nodeIdC)
208 var autoTossFinish chan struct{}
209 var autoTossBadCode chan bool
210 if *autoToss && nodeId != nil {
211 autoTossFinish, autoTossBadCode = ctx.AutoToss(
222 <-nodeIdC // call completion
224 close(autoTossFinish)
231 conns := make(chan net.Conn)
233 cols := strings.Split(*bind, ":")
234 port, err := strconv.Atoi(cols[len(cols)-1])
236 log.Fatalln("Can not parse port:", err)
240 if err = startMCDTx(ctx, port, true); err != nil {
241 log.Fatalln("Can not do MCD transmission:", err)
246 ln, err := net.Listen("tcp", *bind)
248 log.Fatalln("Can not listen:", err)
250 if err = startMCDTx(ctx, port, false); err != nil {
251 log.Fatalln("Can not do MCD transmission:", err)
253 ln = netutil.LimitListener(ln, *maxConn)
256 conn, err := ln.Accept()
258 log.Fatalln("Can not accept connection on TCP:", err)
265 if *yggdrasil != "" {
266 ln, err := nncpYggdrasil.NewListener(ctx.YggdrasilAliases, *yggdrasil)
268 log.Fatalln("Can not listen:", err)
270 ln = netutil.LimitListener(ln, *maxConn)
273 conn, err := ln.Accept()
275 log.Fatalln("Can not accept connection on Yggdrasil:", err)
282 for conn := range conns {
285 nncp.LEs{{K: "Addr", V: conn.RemoteAddr()}},
286 func(les nncp.LEs) string {
287 return "Accepted connection with " + conn.RemoteAddr().String()
290 go func(conn net.Conn) {
291 nodeIdC := make(chan *nncp.NodeId)
292 go performSP(ctx, conn, conn.RemoteAddr().String(), nice, *noCK, nodeIdC)
294 var autoTossFinish chan struct{}
295 var autoTossBadCode chan bool
296 if *autoToss && nodeId != nil {
297 autoTossFinish, autoTossBadCode = ctx.AutoToss(
308 <-nodeIdC // call completion
310 close(autoTossFinish)