]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/call.go
Various Yggdrasil related refactoring and tcpip network stack
[nncp.git] / src / call.go
index 2f635d63d0aa7d36b9fb33a87206cd173db9f4fb..c4e04cbd9d2a1a47a1e5c5f27a62410b159d0a0c 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2022 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -18,10 +18,16 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package nncp
 
 import (
+       "errors"
+       "fmt"
        "net"
+       "os"
+       "strings"
        "time"
 
+       "github.com/dustin/go-humanize"
        "github.com/gorhill/cronexpr"
+       nncpYggdrasil "go.cypherpunks.ru/nncp/v8/yggdrasil"
 )
 
 type Call struct {
@@ -35,6 +41,7 @@ type Call struct {
        MaxOnlineTime  time.Duration
        WhenTxExists   bool
        NoCK           bool
+       MCDIgnore      bool
 
        AutoToss       bool
        AutoTossDoSeen bool
@@ -42,6 +49,7 @@ type Call struct {
        AutoTossNoFreq bool
        AutoTossNoExec bool
        AutoTossNoTrns bool
+       AutoTossNoArea bool
 }
 
 func (ctx *Ctx) CallNode(
@@ -53,23 +61,44 @@ func (ctx *Ctx) CallNode(
        onlineDeadline, maxOnlineTime time.Duration,
        listOnly bool,
        noCK bool,
-       onlyPkts map[[32]byte]bool,
+       onlyPkts map[[MTHSize]byte]bool,
 ) (isGood bool) {
        for _, addr := range addrs {
                les := LEs{{"Node", node.Id}, {"Addr", addr}}
-               ctx.LogD("call", les, "dialing")
+               ctx.LogD("calling", les, func(les LEs) string {
+                       return fmt.Sprintf("Calling %s (%s)", node.Name, addr)
+               })
                var conn ConnDeadlined
                var err error
                if addr[0] == '|' {
                        conn, err = NewPipeConn(addr[1:])
+               } else if addr == UCSPITCPClient {
+                       ucspiConn := UCSPIConn{R: os.NewFile(6, "R"), W: os.NewFile(7, "W")}
+                       if ucspiConn.R == nil {
+                               err = errors.New("no 6 file descriptor")
+                       }
+                       if ucspiConn.W == nil {
+                               err = errors.New("no 7 file descriptor")
+                       }
+                       conn = ucspiConn
+                       addr = UCSPITCPRemoteAddr()
+                       if addr == "" {
+                               addr = UCSPITCPClient
+                       }
+               } else if strings.HasPrefix(addr, "yggdrasilc://") {
+                       conn, err = nncpYggdrasil.NewConn(ctx.YggdrasilAliases, addr)
                } else {
                        conn, err = net.Dial("tcp", addr)
                }
                if err != nil {
-                       ctx.LogD("call", append(les, LE{"Err", err}), "dialing")
+                       ctx.LogE("calling", les, err, func(les LEs) string {
+                               return fmt.Sprintf("Calling %s (%s)", node.Name, addr)
+                       })
                        continue
                }
-               ctx.LogD("call", les, "connected")
+               ctx.LogD("call-connected", les, func(les LEs) string {
+                       return fmt.Sprintf("Connected %s (%s)", node.Name, addr)
+               })
                state := SPState{
                        Ctx:            ctx,
                        Node:           node,
@@ -84,22 +113,38 @@ func (ctx *Ctx) CallNode(
                        onlyPkts:       onlyPkts,
                }
                if err = state.StartI(conn); err == nil {
-                       ctx.LogI("call-start", les, "connected")
+                       ctx.LogI("call-started", les, func(les LEs) string {
+                               return fmt.Sprintf("Connection to %s (%s)", node.Name, addr)
+                       })
                        state.Wait()
-                       ctx.LogI("call-finish", LEs{
-                               {"Node", state.Node.Id},
-                               {"Duration", int64(state.Duration.Seconds())},
-                               {"RxBytes", state.RxBytes},
-                               {"TxBytes", state.TxBytes},
-                               {"RxSpeed", state.RxSpeed},
-                               {"TxSpeed", state.TxSpeed},
-                       }, "")
+                       ctx.LogI("call-finished", append(
+                               les,
+                               LE{"Duration", int64(state.Duration.Seconds())},
+                               LE{"RxBytes", state.RxBytes},
+                               LE{"RxSpeed", state.RxSpeed},
+                               LE{"TxBytes", state.TxBytes},
+                               LE{"TxSpeed", state.TxSpeed},
+                       ), func(les LEs) string {
+                               return fmt.Sprintf(
+                                       "Finished call with %s (%d:%d:%d): %s received (%s/sec), %s transferred (%s/sec)",
+                                       node.Name,
+                                       int(state.Duration.Hours()),
+                                       int(state.Duration.Minutes()),
+                                       int(state.Duration.Seconds())%60,
+                                       humanize.IBytes(uint64(state.RxBytes)),
+                                       humanize.IBytes(uint64(state.RxSpeed)),
+                                       humanize.IBytes(uint64(state.TxBytes)),
+                                       humanize.IBytes(uint64(state.TxSpeed)),
+                               )
+                       })
                        isGood = true
-                       conn.Close() // #nosec G104
+                       conn.Close()
                        break
                } else {
-                       ctx.LogE("call-start", les, err, "")
-                       conn.Close() // #nosec G104
+                       ctx.LogE("call-started", les, err, func(les LEs) string {
+                               return fmt.Sprintf("Connection to %s (%s)", node.Name, addr)
+                       })
+                       conn.Close()
                }
        }
        return