Optionally log unknown packet data (for debugging)

This commit is contained in:
Jakob Borg 2014-09-21 10:57:19 +02:00
parent cbf73ef29e
commit b9f83c7780

View File

@ -7,7 +7,6 @@ package main
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"encoding/hex"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -47,6 +46,7 @@ func main() {
var timestamp bool var timestamp bool
var statsIntv int var statsIntv int
var statsFile string var statsFile string
var unknownFile string
var dbDir string var dbDir string
flag.StringVar(&listen, "listen", ":22026", "Listen address") flag.StringVar(&listen, "listen", ":22026", "Listen address")
@ -54,6 +54,7 @@ func main() {
flag.BoolVar(&timestamp, "timestamp", true, "Timestamp the log output") flag.BoolVar(&timestamp, "timestamp", true, "Timestamp the log output")
flag.IntVar(&statsIntv, "stats-intv", 0, "Statistics output interval (s)") flag.IntVar(&statsIntv, "stats-intv", 0, "Statistics output interval (s)")
flag.StringVar(&statsFile, "stats-file", "/var/discosrv/stats", "Statistics file name") flag.StringVar(&statsFile, "stats-file", "/var/discosrv/stats", "Statistics file name")
flag.StringVar(&unknownFile, "unknown-file", "", "Unknown packet log file name")
flag.IntVar(&lruSize, "limit-cache", lruSize, "Limiter cache entries") flag.IntVar(&lruSize, "limit-cache", lruSize, "Limiter cache entries")
flag.IntVar(&limitAvg, "limit-avg", limitAvg, "Allowed average package rate, per 10 s") flag.IntVar(&limitAvg, "limit-avg", limitAvg, "Allowed average package rate, per 10 s")
flag.IntVar(&limitBurst, "limit-burst", limitBurst, "Allowed burst size, packets") flag.IntVar(&limitBurst, "limit-burst", limitBurst, "Allowed burst size, packets")
@ -91,6 +92,14 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
var unknownLog io.Writer
if unknownFile != "" {
unknownLog, err = os.OpenFile(unknownFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
}
if statsIntv > 0 { if statsIntv > 0 {
go logStats(statsLog, statsIntv) go logStats(statsLog, statsIntv)
} }
@ -121,15 +130,24 @@ func main() {
switch magic { switch magic {
case discover.AnnouncementMagic: case discover.AnnouncementMagic:
handleAnnounceV2(db, addr, buf) err := handleAnnounceV2(db, addr, buf)
if err != nil && unknownLog != nil {
fmt.Fprintf(unknownLog, "AE %d %v %x\n", time.Now().Unix(), addr, buf)
}
case discover.QueryMagic: case discover.QueryMagic:
handleQueryV2(db, conn, addr, buf) err := handleQueryV2(db, conn, addr, buf)
if err != nil && unknownLog != nil {
fmt.Fprintf(unknownLog, "QE %d %v %x\n", time.Now().Unix(), addr, buf)
}
default: default:
lock.Lock() lock.Lock()
unknowns++ unknowns++
lock.Unlock() lock.Unlock()
if unknownLog != nil {
fmt.Fprintf(unknownLog, "UN %d %v %x\n", time.Now().Unix(), addr, buf)
}
} }
} }
} }
@ -162,13 +180,11 @@ func limit(addr *net.UDPAddr) bool {
return false return false
} }
func handleAnnounceV2(db *leveldb.DB, addr *net.UDPAddr, buf []byte) { func handleAnnounceV2(db *leveldb.DB, addr *net.UDPAddr, buf []byte) error {
var pkt discover.Announce var pkt discover.Announce
err := pkt.UnmarshalXDR(buf) err := pkt.UnmarshalXDR(buf)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
log.Println("AnnounceV2 Unmarshal:", err) return err
log.Println(hex.Dump(buf))
return
} }
if debug { if debug {
log.Printf("<- %v %#v", addr, pkt) log.Printf("<- %v %#v", addr, pkt)
@ -202,19 +218,21 @@ func handleAnnounceV2(db *leveldb.DB, addr *net.UDPAddr, buf []byte) {
// Raw node ID // Raw node ID
copy(id[:], pkt.This.ID) copy(id[:], pkt.This.ID)
} else { } else {
id.UnmarshalText(pkt.This.ID) err = id.UnmarshalText(pkt.This.ID)
if err != nil {
return err
}
} }
update(db, id, addrs) update(db, id, addrs)
return nil
} }
func handleQueryV2(db *leveldb.DB, conn *net.UDPConn, addr *net.UDPAddr, buf []byte) { func handleQueryV2(db *leveldb.DB, conn *net.UDPConn, addr *net.UDPAddr, buf []byte) error {
var pkt discover.Query var pkt discover.Query
err := pkt.UnmarshalXDR(buf) err := pkt.UnmarshalXDR(buf)
if err != nil { if err != nil {
log.Println("QueryV2 Unmarshal:", err) return err
log.Println(hex.Dump(buf))
return
} }
if debug { if debug {
log.Printf("<- %v %#v", addr, pkt) log.Printf("<- %v %#v", addr, pkt)
@ -225,7 +243,10 @@ func handleQueryV2(db *leveldb.DB, conn *net.UDPConn, addr *net.UDPAddr, buf []b
// Raw node ID // Raw node ID
copy(id[:], pkt.NodeID) copy(id[:], pkt.NodeID)
} else { } else {
id.UnmarshalText(pkt.NodeID) err = id.UnmarshalText(pkt.NodeID)
if err != nil {
return err
}
} }
lock.Lock() lock.Lock()
@ -253,19 +274,21 @@ func handleQueryV2(db *leveldb.DB, conn *net.UDPConn, addr *net.UDPAddr, buf []b
} }
if len(ann.This.Addresses) == 0 { if len(ann.This.Addresses) == 0 {
return return nil
} }
tb := ann.MarshalXDR() tb := ann.MarshalXDR()
_, _, err = conn.WriteMsgUDP(tb, nil, addr) _, _, err = conn.WriteMsgUDP(tb, nil, addr)
if err != nil { if err != nil {
log.Println("QueryV2 response write:", err) log.Println("QueryV2 response write:", err)
return nil
} }
lock.Lock() lock.Lock()
answered++ answered++
lock.Unlock() lock.Unlock()
} }
return nil
} }
func next(intv int) time.Time { func next(intv int) time.Time {