]> Cypherpunks.ru repositories - nncp.git/blob - src/call.go
87fb5aeef9a76769163ee6f78be87790c39e6404
[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
37         AutoToss       bool
38         AutoTossDoSeen bool
39         AutoTossNoFile bool
40         AutoTossNoFreq bool
41         AutoTossNoExec bool
42         AutoTossNoTrns bool
43 }
44
45 func (ctx *Ctx) CallNode(
46         node *Node,
47         addrs []string,
48         nice uint8,
49         xxOnly TRxTx,
50         rxRate, txRate int,
51         onlineDeadline, maxOnlineTime time.Duration,
52         listOnly bool,
53         onlyPkts map[[32]byte]bool,
54 ) (isGood bool) {
55         for _, addr := range addrs {
56                 sds := SDS{"node": node.Id, "addr": addr}
57                 ctx.LogD("call", sds, "dialing")
58                 var conn ConnDeadlined
59                 var err error
60                 if addr[0] == '|' {
61                         conn, err = NewPipeConn(addr[1:])
62                 } else {
63                         conn, err = net.Dial("tcp", addr)
64                 }
65                 if err != nil {
66                         ctx.LogD("call", SdsAdd(sds, SDS{"err": err}), "dialing")
67                         continue
68                 }
69                 ctx.LogD("call", sds, "connected")
70                 state := SPState{
71                         Ctx:            ctx,
72                         Node:           node,
73                         Nice:           nice,
74                         onlineDeadline: onlineDeadline,
75                         maxOnlineTime:  maxOnlineTime,
76                         xxOnly:         xxOnly,
77                         rxRate:         rxRate,
78                         txRate:         txRate,
79                         listOnly:       listOnly,
80                         onlyPkts:       onlyPkts,
81                 }
82                 if err = state.StartI(conn); err == nil {
83                         ctx.LogI("call-start", sds, "connected")
84                         state.Wait()
85                         ctx.LogI("call-finish", SDS{
86                                 "node":     state.Node.Id,
87                                 "duration": int64(state.Duration.Seconds()),
88                                 "rxbytes":  state.RxBytes,
89                                 "txbytes":  state.TxBytes,
90                                 "rxspeed":  state.RxSpeed,
91                                 "txspeed":  state.TxSpeed,
92                         }, "")
93                         isGood = true
94                         conn.Close() // #nosec G104
95                         break
96                 } else {
97                         ctx.LogE("call-start", sds, err, "")
98                         conn.Close() // #nosec G104
99                 }
100         }
101         return
102 }