From 646ff4cddf6370748e2a7e2d1e06535685329a62 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sun, 15 Jan 2017 17:06:52 +0300 Subject: [PATCH] -maxonlinetime option --- doc/call.texi | 17 +++++++++++------ doc/cfg.texi | 8 +++++++- doc/cmds.texi | 12 ++++++++---- src/cypherpunks.ru/nncp/call.go | 11 +++++++++-- src/cypherpunks.ru/nncp/cfg.go | 18 +++++++++++++++--- src/cypherpunks.ru/nncp/cmd/nncp-call/main.go | 8 ++++++-- .../nncp/cmd/nncp-caller/main.go | 9 ++++++++- src/cypherpunks.ru/nncp/node.go | 6 ++++-- src/cypherpunks.ru/nncp/sp.go | 15 ++++++++++++--- 9 files changed, 80 insertions(+), 24 deletions(-) diff --git a/doc/call.texi b/doc/call.texi index a0fe033..509da59 100644 --- a/doc/call.texi +++ b/doc/call.texi @@ -13,7 +13,8 @@ calls: nice: 64 - cron: "30 * * * 5-6" - onlinedeadline: 10 + onlinedeadline: 1800 + maxonlinetime: 1750 nice: 64 - cron: "0 * * * 5-6" @@ -25,11 +26,11 @@ tells that on work days of the week call that node every minute, disconnect after an hour of inactivity and process only relatively high priority packets (presumably mail ones). So we connect and hold connection for very long time to pass only emails. On weekends call that -node only each half-hour for processing high-priority packets and -quickly disconnect. Also only on weekends try to connect to that node -every hour only using LAN address and only receiving any (any priority) -packets (assume that low priority huge file transmission are done -additionally via offline connections). +node only each half-hour for processing high-priority packets. Also only +on weekends try to connect to that node every hour only using LAN +address and only receiving any (any priority) packets (assume that low +priority huge file transmission are done additionally via offline +connections). It contains the following fields (only @emph{cron} is required): @@ -170,4 +171,8 @@ from @emph{addrs} dictionary, or an ordinary @option{addr:port}. Optional. Override @ref{CfgOnlineDeadline, @emph{onlinedeadline}} configuration option when calling. +@item maxonlinetime +Optional. Override @ref{CfgMaxOnlineTime, @emph{maxonlinetime}} +configuration option when calling. + @end table diff --git a/doc/cfg.texi b/doc/cfg.texi index acfdbf7..3160d73 100644 --- a/doc/cfg.texi +++ b/doc/cfg.texi @@ -35,7 +35,8 @@ neigh: noisepub: UBM5K...VI42A sendmail: ["/bin/sh", "-c", "false"] incoming: /home/alice/incoming - onlinedeadline: 3600 + onlinedeadline: 1800 + maxonlinetime: 3600 addrs: lan: "[fe80::1234%igb0]:5400" internet: alice.com:3389 @@ -122,6 +123,11 @@ transmitted. This can be set to rather high values to keep connection alive (to reduce handshake overhead and delays), wait for appearing packets ready to send and notifying remote side about their appearance. +@anchor{CfgMaxOnlineTime} +@item maxonlinetime +If greater than zero, then it is maximal amount of time connect could be +alive. Forcefully disconnect if it is exceeded. + @anchor{CfgCalls} @item calls List of @ref{Call, call configuration}s. Can be omitted if diff --git a/doc/cmds.texi b/doc/cmds.texi index b71b202..1061cc2 100644 --- a/doc/cmds.texi +++ b/doc/cmds.texi @@ -34,7 +34,8 @@ Nearly all commands have the following common options: @section nncp-call @verbatim -% nncp-call [options] [-onlinedeadline INT] [-rx|-tx] NODE[:ADDR] [FORCEADDR] +% nncp-call [options] [-onlinedeadline INT] [-maxonlinetime INT] [-rx|-tx] + NODE[:ADDR] [FORCEADDR] @end verbatim Call (connect to) specified @option{NODE} and run @ref{Sync, @@ -44,9 +45,12 @@ either check for incoming packets, or to send out queued ones. Synchronization protocol allows resuming and bidirectional packets transfer. -If @option{-rx} option is specified then only inbound packets transmission -is performed. If @option{-tx} option is specified, then only outbound -transmission is performed. +If @option{-rx} option is specified then only inbound packets +transmission is performed. If @option{-tx} option is specified, then +only outbound transmission is performed. @option{-onlinedeadline} +overrides @ref{CfgOnlineDeadline, @emph{onlinedeadline}}. +@option{-maxonlinetime} overrides @ref{CfgMaxOnlineTime, +@emph{maxonlinetime}}. @node nncp-caller @section nncp-caller diff --git a/src/cypherpunks.ru/nncp/call.go b/src/cypherpunks.ru/nncp/call.go index fa1aff7..5b8e058 100644 --- a/src/cypherpunks.ru/nncp/call.go +++ b/src/cypherpunks.ru/nncp/call.go @@ -23,7 +23,7 @@ import ( "strconv" ) -func (ctx *Ctx) CallNode(node *Node, addrs []string, nice uint8, xxOnly *TRxTx, onlineDeadline int) (isGood bool) { +func (ctx *Ctx) CallNode(node *Node, addrs []string, nice uint8, xxOnly *TRxTx, onlineDeadline, maxOnlineTime uint) (isGood bool) { for _, addr := range addrs { sds := SDS{"node": node.Id, "addr": addr} ctx.LogD("call", sds, "dialing") @@ -33,7 +33,14 @@ func (ctx *Ctx) CallNode(node *Node, addrs []string, nice uint8, xxOnly *TRxTx, continue } ctx.LogD("call", sds, "connected") - state, err := ctx.StartI(conn, node.Id, nice, xxOnly, onlineDeadline) + state, err := ctx.StartI( + conn, + node.Id, + nice, + xxOnly, + onlineDeadline, + maxOnlineTime, + ) if err == nil { ctx.LogI("call-start", sds, "connected") state.Wait() diff --git a/src/cypherpunks.ru/nncp/cfg.go b/src/cypherpunks.ru/nncp/cfg.go index 10dd46b..0c7de2f 100644 --- a/src/cypherpunks.ru/nncp/cfg.go +++ b/src/cypherpunks.ru/nncp/cfg.go @@ -50,7 +50,8 @@ type NodeYAML struct { Addrs map[string]string `addrs,omitempty` - OnlineDeadline *int `onlinedeadline,omitempty` + OnlineDeadline *uint `onlinedeadline,omitempty` + MaxOnlineTime *uint `maxonlinetime,omitempty` } type CallYAML struct { @@ -58,7 +59,8 @@ type CallYAML struct { Nice *int `nice,omitempty` Xx *string `xx,omitempty` Addr *string `addr,omitempty` - OnlineDeadline *int `onlinedeadline,omitempty` + OnlineDeadline *uint `onlinedeadline,omitempty` + MaxOnlineTime *uint `maxonlinetime,omitempty` } type NodeOurYAML struct { @@ -141,13 +143,17 @@ func NewNode(name string, yml NodeYAML) (*Node, error) { freq = &fr } - defOnlineDeadline := int(DefaultDeadline) + defOnlineDeadline := uint(DefaultDeadline) if yml.OnlineDeadline != nil { if *yml.OnlineDeadline <= 0 { return nil, errors.New("OnlineDeadline must be at least 1 second") } defOnlineDeadline = *yml.OnlineDeadline } + var defMaxOnlineTime uint + if yml.MaxOnlineTime != nil { + defMaxOnlineTime = *yml.MaxOnlineTime + } var calls []*Call for _, callYml := range yml.Calls { @@ -188,12 +194,17 @@ func NewNode(name string, yml NodeYAML) (*Node, error) { } onlineDeadline = *callYml.OnlineDeadline } + var maxOnlineTime uint + if callYml.MaxOnlineTime != nil { + maxOnlineTime = *callYml.MaxOnlineTime + } calls = append(calls, &Call{ Cron: expr, Nice: nice, Xx: &xx, Addr: addr, OnlineDeadline: onlineDeadline, + MaxOnlineTime: maxOnlineTime, }) } @@ -208,6 +219,7 @@ func NewNode(name string, yml NodeYAML) (*Node, error) { Calls: calls, Addrs: yml.Addrs, OnlineDeadline: defOnlineDeadline, + MaxOnlineTime: defMaxOnlineTime, } copy(node.ExchPub[:], exchPub) if len(noisePub) > 0 { diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go index a087544..03005a1 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go @@ -49,7 +49,8 @@ func main() { version = flag.Bool("version", false, "Print version information") warranty = flag.Bool("warranty", false, "Print warranty information") - onlineDeadline = flag.Int("onlinedeadline", 0, "Override onlinedeadline option") + onlineDeadline = flag.Uint("onlinedeadline", 0, "Override onlinedeadline option") + maxOnlineTime = flag.Uint("maxonlinetime", 0, "Override maxonlinetime option") ) flag.Usage = usage flag.Parse() @@ -96,6 +97,9 @@ func main() { if *onlineDeadline == 0 { onlineDeadline = &node.OnlineDeadline } + if *maxOnlineTime == 0 { + maxOnlineTime = &node.MaxOnlineTime + } var xxOnly nncp.TRxTx if *rxOnly { @@ -119,7 +123,7 @@ func main() { } } - if !ctx.CallNode(node, addrs, nice, &xxOnly, *onlineDeadline) { + if !ctx.CallNode(node, addrs, nice, &xxOnly, *onlineDeadline, *maxOnlineTime) { os.Exit(1) } } diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go index 24a1a80..bcfd6c3 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go @@ -125,7 +125,14 @@ func main() { } else { node.Busy = true node.Unlock() - ctx.CallNode(node, addrs, call.Nice, call.Xx, call.OnlineDeadline) + ctx.CallNode( + node, + addrs, + call.Nice, + call.Xx, + call.OnlineDeadline, + call.MaxOnlineTime, + ) node.Lock() node.Busy = false node.Unlock() diff --git a/src/cypherpunks.ru/nncp/node.go b/src/cypherpunks.ru/nncp/node.go index 1e1b5ba..7992f4b 100644 --- a/src/cypherpunks.ru/nncp/node.go +++ b/src/cypherpunks.ru/nncp/node.go @@ -47,7 +47,8 @@ type Node struct { Freq *string Via []*NodeId Addrs map[string]string - OnlineDeadline int + OnlineDeadline uint + MaxOnlineTime uint Calls []*Call Busy bool @@ -69,7 +70,8 @@ type Call struct { Nice uint8 Xx *TRxTx Addr *string - OnlineDeadline int + OnlineDeadline uint + MaxOnlineTime uint } func NewNodeGenerate() (*NodeOur, error) { diff --git a/src/cypherpunks.ru/nncp/sp.go b/src/cypherpunks.ru/nncp/sp.go index 82af817..4cc670f 100644 --- a/src/cypherpunks.ru/nncp/sp.go +++ b/src/cypherpunks.ru/nncp/sp.go @@ -159,7 +159,8 @@ func payloadsSplit(payloads [][]byte) [][]byte { type SPState struct { ctx *Ctx Node *Node - onlineDeadline int + onlineDeadline uint + maxOnlineTime uint nice uint8 hs *noise.HandshakeState csOur *noise.CipherState @@ -185,8 +186,14 @@ type SPState struct { } func (state *SPState) NotAlive() bool { + if state.isDead { + return true + } now := time.Now() - return state.isDead || (int(now.Sub(state.RxLastSeen).Seconds()) >= state.onlineDeadline && int(now.Sub(state.TxLastSeen).Seconds()) >= state.onlineDeadline) + if state.maxOnlineTime > 0 && state.started.Add(time.Duration(state.maxOnlineTime)*time.Second).Before(now) { + return true + } + return uint(now.Sub(state.RxLastSeen).Seconds()) >= state.onlineDeadline && uint(now.Sub(state.TxLastSeen).Seconds()) >= state.onlineDeadline } func (state *SPState) dirUnlock() { @@ -257,7 +264,7 @@ func (ctx *Ctx) infosOur(nodeId *NodeId, nice uint8, seen *map[[32]byte]struct{} return payloadsSplit(payloads) } -func (ctx *Ctx) StartI(conn net.Conn, nodeId *NodeId, nice uint8, xxOnly *TRxTx, onlineDeadline int) (*SPState, error) { +func (ctx *Ctx) StartI(conn net.Conn, nodeId *NodeId, nice uint8, xxOnly *TRxTx, onlineDeadline, maxOnlineTime uint) (*SPState, error) { err := ctx.ensureRxDir(nodeId) if err != nil { return nil, err @@ -293,6 +300,7 @@ func (ctx *Ctx) StartI(conn net.Conn, nodeId *NodeId, nice uint8, xxOnly *TRxTx, hs: noise.NewHandshakeState(conf), Node: node, onlineDeadline: onlineDeadline, + maxOnlineTime: maxOnlineTime, nice: nice, payloads: make(chan []byte), infosTheir: make(map[[32]byte]*SPInfo), @@ -402,6 +410,7 @@ func (ctx *Ctx) StartR(conn net.Conn, nice uint8, xxOnly *TRxTx) (*SPState, erro } state.Node = node state.onlineDeadline = node.OnlineDeadline + state.maxOnlineTime = node.MaxOnlineTime sds := SDS{"node": node.Id, "nice": strconv.Itoa(int(nice))} if ctx.ensureRxDir(node.Id); err != nil { -- 2.44.0