]> Cypherpunks.ru repositories - nncp.git/blob - src/call.go
Intermediate .nock packets step
[nncp.git] / src / call.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 package nncp
19
20 import (
21         "net"
22         "time"
23
24         "github.com/gorhill/cronexpr"
25 )
26
27 type Call struct {
28         Cron           *cronexpr.Expression
29         Nice           uint8
30         Xx             TRxTx
31         RxRate         int
32         TxRate         int
33         Addr           *string
34         OnlineDeadline time.Duration
35         MaxOnlineTime  time.Duration
36         WhenTxExists   bool
37         NoCK           bool
38
39         AutoToss       bool
40         AutoTossDoSeen bool
41         AutoTossNoFile bool
42         AutoTossNoFreq bool
43         AutoTossNoExec bool
44         AutoTossNoTrns bool
45 }
46
47 func (ctx *Ctx) CallNode(
48         node *Node,
49         addrs []string,
50         nice uint8,
51         xxOnly TRxTx,
52         rxRate, txRate int,
53         onlineDeadline, maxOnlineTime time.Duration,
54         listOnly bool,
55         noCK bool,
56         onlyPkts map[[32]byte]bool,
57 ) (isGood bool) {
58         for _, addr := range addrs {
59                 les := LEs{{"Node", node.Id}, {"Addr", addr}}
60                 ctx.LogD("call", les, "dialing")
61                 var conn ConnDeadlined
62                 var err error
63                 if addr[0] == '|' {
64                         conn, err = NewPipeConn(addr[1:])
65                 } else {
66                         conn, err = net.Dial("tcp", addr)
67                 }
68                 if err != nil {
69                         ctx.LogD("call", append(les, LE{"Err", err}), "dialing")
70                         continue
71                 }
72                 ctx.LogD("call", les, "connected")
73                 state := SPState{
74                         Ctx:            ctx,
75                         Node:           node,
76                         Nice:           nice,
77                         onlineDeadline: onlineDeadline,
78                         maxOnlineTime:  maxOnlineTime,
79                         xxOnly:         xxOnly,
80                         rxRate:         rxRate,
81                         txRate:         txRate,
82                         listOnly:       listOnly,
83                         NoCK:           noCK,
84                         onlyPkts:       onlyPkts,
85                 }
86                 if err = state.StartI(conn); err == nil {
87                         ctx.LogI("call-start", les, "connected")
88                         state.Wait()
89                         ctx.LogI("call-finish", LEs{
90                                 {"Node", state.Node.Id},
91                                 {"Duration", int64(state.Duration.Seconds())},
92                                 {"RxBytes", state.RxBytes},
93                                 {"TxBytes", state.TxBytes},
94                                 {"RxSpeed", state.RxSpeed},
95                                 {"TxSpeed", state.TxSpeed},
96                         }, "")
97                         isGood = true
98                         conn.Close() // #nosec G104
99                         break
100                 } else {
101                         ctx.LogE("call-start", les, err, "")
102                         conn.Close() // #nosec G104
103                 }
104         }
105         return
106 }