Skip to content

Commit

Permalink
WSDD: added protocol trace
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpevzner committed Mar 24, 2020
1 parent 6cd8806 commit ceb02a7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
53 changes: 53 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,29 @@
package main

import (
"archive/tar"
"bytes"
"fmt"
"os"
"sync"
)

// Debug enables or disables debugging
var Debug = false

// Trace enables or disables protocol trace
var Trace = true

// Trace file name
const traceName = "trace.tar"

// Trace file handle, opened on demand
var (
traceFile *tar.Writer
traceLock sync.Mutex
traceIndex int
)

// LogMessage represents a multiline log message
type LogMessage struct {
prefix string // Per-line prefix
Expand Down Expand Up @@ -54,6 +69,44 @@ func LogBegin(prefix string) *LogMessage {
}
}

// Add record to the protocol trace
func LogTrace(name string, data []byte) {
// Trace enabled?
if !Trace {
return
}

// Acquire trace lock
traceLock.Lock()
defer traceLock.Unlock()

// Open trace file on demand
if traceFile == nil {
file, err := os.OpenFile(traceName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
LogError("%s: %s", traceName, err)
return
}

traceFile = tar.NewWriter(file)
}

// Build full name
name = fmt.Sprintf("%.3d-%s.xml", traceIndex, name)
traceIndex++

// Write file header and data
hdr := &tar.Header{
Name: name,
Mode: 0644,
Size: int64(len(data)),
}

traceFile.WriteHeader(hdr)
traceFile.Write(data)
traceFile.Flush()
}

// Debug appends line to the LogMessage
func (m *LogMessage) Debug(format string, args ...interface{}) *LogMessage {
if Debug {
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const usage = `Usage:
Options are:
-d enable debug mode
-t enable protocol trace
-h print help page
`

Expand All @@ -33,6 +34,9 @@ func main() {
switch arg {
case "-d":
Debug = true
case "-t":
Debug = true
Trace = true
case "-h":
fmt.Printf(usage, os.Args[0])
os.Exit(0)
Expand Down
7 changes: 7 additions & 0 deletions wsdd.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ func getMetadata(log *LogMessage, address, xaddr string) []Endpoint {
resp, err := http.Post(xaddr, "application/soap+xml; charset=utf-8",
bytes.NewBuffer(([]byte)(msg)))

LogTrace("http-request", []byte(msg))

if err != nil {
log.Debug("HTTP: %s", err)
return nil
Expand All @@ -219,6 +221,8 @@ func getMetadata(log *LogMessage, address, xaddr string) []Endpoint {
return nil
}

LogTrace("http-response", response)

// Parse response XML
elements, err := XMLDecode(wsddNsMap, bytes.NewBuffer(response))
if err != nil {
Expand Down Expand Up @@ -386,6 +390,8 @@ func recvUDPMessages(conn *net.UDPConn, zone string, outchan chan Endpoint) {
msg := buf[:n]

LogDebug("%s: UDP message received", from)
LogTrace(fmt.Sprintf("udp-from-%s", from), msg)

log := LogBegin(fmt.Sprintf("%s", from))
handleUDPMessage(log, msg, zone, outchan)
log.Commit()
Expand Down Expand Up @@ -449,6 +455,7 @@ func WSSDDiscover(outchan chan Endpoint) {
msg := fmt.Sprintf(probeTemplate, u)
conn.WriteTo([]byte(msg), dest)
LogDebug("%s: UDP message sent", dest)
LogTrace(fmt.Sprintf("udp-to-%s", dest), []byte(msg))
}

time.Sleep(250 * time.Millisecond)
Expand Down

0 comments on commit ceb02a7

Please sign in to comment.