]> Cypherpunks.ru repositories - nncp.git/commitdiff
-maxonlinetime option
authorSergey Matveev <stargrave@stargrave.org>
Sun, 15 Jan 2017 14:06:52 +0000 (17:06 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 15 Jan 2017 14:06:52 +0000 (17:06 +0300)
doc/call.texi
doc/cfg.texi
doc/cmds.texi
src/cypherpunks.ru/nncp/call.go
src/cypherpunks.ru/nncp/cfg.go
src/cypherpunks.ru/nncp/cmd/nncp-call/main.go
src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go
src/cypherpunks.ru/nncp/node.go
src/cypherpunks.ru/nncp/sp.go

index a0fe0332e55900718188ecff1f193634869a7c40..509da59e213c7c1c582cb4053a8a54bb99b63358 100644 (file)
@@ -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
index acfdbf7a574bb7e2d067e0abfe2e69cfb6d470ad..3160d73d96d8e51a23271f6f86342c9c4e9f9f73 100644 (file)
@@ -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
index b71b202955cebf8bff2fec05760a732fac750513..1061cc273352224029ed19eb223e76a8c4ce3a00 100644 (file)
@@ -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
index fa1aff7ccc8f666b87dea4f767d77c23aa08f292..5b8e0584a4f334599f702d0273d8fdd5d5fb0ecf 100644 (file)
@@ -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()
index 10dd46b917a430174b17ac3bb8801d705917bd4f..0c7de2f0f5e03270de8cca9366c8fc17ae93d1cb 100644 (file)
@@ -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 {
index a0875440114adfe04d779d6a29fdd15d6e0d7157..03005a1ac66bc8fe2adba112ba7e7113032756bf 100644 (file)
@@ -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)
        }
 }
index 24a1a8033788969154a28762f464cc1b31d30206..bcfd6c3b001e87777bedfc6c813521bf01850435 100644 (file)
@@ -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()
index 1e1b5bab3b3e96da17bd4aad8784f188707b348e..7992f4bc8f1ce19d4c6a2b3e7d13f7cd937f1b78 100644 (file)
@@ -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) {
index 82af81779cda1e3119438920c38e6c420406c046..4cc670f42c7b955887a908c05ec7ddb3cc2da9a4 100644 (file)
@@ -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 {