Skip to content

Commit

Permalink
added server netprint function
Browse files Browse the repository at this point in the history
  • Loading branch information
samiemostafavi committed Jan 7, 2024
1 parent 2a351e1 commit 5430db2
Show file tree
Hide file tree
Showing 7 changed files with 1,568 additions and 5 deletions.
14 changes: 14 additions & 0 deletions irtt_server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nlmt

import (
"fmt"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -37,6 +38,7 @@ func serverUsage() {
printf(" (default %s, see Duration units below)", DefaultMinInterval)
printf("-l length max packet length (default %d), or 0 for no maximum", DefaultMaxLength)
printf(" numbers too small will cause test packets to be dropped")
printf("-n net print print logs to a server e.g. \"0.0.0.0:50009\" (default disabled)")
printf("--hmac=key add HMAC with key (0x for hex) to all packets, provides:")
printf(" dropping of all packets without a correct HMAC")
printf(" protection for server against unauthorized discovery and use")
Expand Down Expand Up @@ -95,6 +97,7 @@ func runServerCLI(args []string) {
var maxDuration = fs.DurationP("d", "d", DefaultMaxDuration, "max duration")
var minInterval = fs.DurationP("i", "i", DefaultMinInterval, "min interval")
var maxLength = fs.IntP("l", "l", DefaultMaxLength, "max length")
var netprintAddr = fs.StringP("n", "n", "", "netprint address")
var allowTimestampStr = fs.String("tstamp", DefaultAllowStamp.String(), "allow timestamp")
var hmacStr = fs.String("hmac", defaultHMACKey, "HMAC key")
var syslogStr *string
Expand Down Expand Up @@ -188,6 +191,17 @@ func runServerCLI(args []string) {
cfg.OutputJSONAddr = *outputStr
}

// connect to the print server if set
if *netprintAddr != "" {
// Create a TCP connection to the server
cfg.netprintp, err = NewNetPrint(*netprintAddr)
if err != nil {
fmt.Println("Error connecting to the netprint server: ", err)
exitOnError(err, exitCodeBadCommandLine)
}
defer cfg.netprintp.CloseConnection()
}

// create server
s := NewServer(cfg)

Expand Down
41 changes: 41 additions & 0 deletions netprint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nlmt

import (
"net"
"sync"
)

// NetPrint represents a connection manager
type NetPrint struct {
conn net.Conn
mu sync.Mutex // Mutex for synchronization
}

// NewConnectionManager creates a new NetPrint and establishes a connection to the given address
func NewNetPrint(destAddr string) (*NetPrint, error) {
conn, err := net.Dial("tcp", destAddr)
if err != nil {
return nil, err
}

return &NetPrint{conn: conn}, nil
}

// WriteToConnection writes the provided message to the connection managed by NetPrint
func (cm *NetPrint) WriteToConnection(message string) error {
// Lock the mutex before writing to the connection
cm.mu.Lock()
defer cm.mu.Unlock()

_, err := cm.conn.Write([]byte(message))
return err
}

// CloseConnection closes the connection managed by NetPrint
func (cm *NetPrint) CloseConnection() error {
// Lock the mutex before closing the connection
cm.mu.Lock()
defer cm.mu.Unlock()

return cm.conn.Close()
}
Binary file modified nlmt
Binary file not shown.
1,494 changes: 1,494 additions & 0 deletions res.nlmt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions sconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ServerConfig struct {
OutputJSON bool
OutputJSONAddr string
OutputDir string
netprintp *NetPrint
}

// NewServerConfig returns a new ServerConfig with the default settings.
Expand All @@ -51,5 +52,6 @@ func NewServerConfig() *ServerConfig {
OutputJSON: DefaultOutputJSON,
OutputJSONAddr: DefaultOutputJSONAddr,
OutputDir: DefaultOutputDir,
netprintp: nil,
}
}
20 changes: 16 additions & 4 deletions sconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func accept(l *listener, p *packet) (sc *sconn, err error) {
}

// add recorder handler
sc.rHandler = &sconnHandler{p.raddr, sc.listener.ServerConfig.Quiet, sc.listener.ServerConfig.ReallyQuiet}
sc.rHandler = &sconnHandler{p.raddr, sc.listener.ServerConfig.netprintp, sc.listener.ServerConfig.Quiet, sc.listener.ServerConfig.ReallyQuiet}

// create recorder if oneway
if params.TripMode == TMOneWay {
Expand Down Expand Up @@ -571,6 +571,7 @@ func writeOneWayResultJSON(r *OneWayResult, output string, cancelled bool) error

type sconnHandler struct {
raddr *net.UDPAddr
netprintp *NetPrint
quiet bool
reallyQuiet bool
}
Expand All @@ -597,15 +598,26 @@ func (sc *sconnHandler) OneWayOnReceived(seqno Seqno, owtd *OneWayTripData,
if owtd.SendDelay() != InvalidDuration {
sd = fmt.Sprintf(" sd=%s", rdur(owtd.SendDelay()))
st = fmt.Sprintf(" st=%d", owtd.Client.Send.Wall)
rt = fmt.Sprintf(" st=%d", owtd.Server.Receive.Wall)
rt = fmt.Sprintf(" rt=%d", owtd.Server.Receive.Wall)
}
sl := ""
if late {
sl = " (LATE)"
}

printf("[%s] seq=%d %s%s%s ipdv=%s%s", sc.raddr, seqno,
sd, st, rt, ipdv, sl)
if sc.netprintp == nil {
printf("[%s] seq=%d %s%s%s ipdv=%s%s", sc.raddr, seqno,
sd, st, rt, ipdv, sl)
} else {
message := fmt.Sprintf("[%s] seq=%d %s%s%s ipdv=%s%s\n", sc.raddr, seqno,
sd, st, rt, ipdv, sl)
// Send the formatted string to the TCP socket
err := sc.netprintp.WriteToConnection(message)
if err != nil {
fmt.Println("Error net printing result:", err)
return
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nlmt

// Version is the NLMT version number (replaced during build).
var Version = "0.10.2"
var Version = "0.10.3"

// ProtocolVersion is the protocol version number, which must match between client
// and server.
Expand Down

0 comments on commit 5430db2

Please sign in to comment.