Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Commit

Permalink
New LogAll variable to enable info and debug error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
irai committed Feb 15, 2020
1 parent 490a7e0 commit 0bbe6cc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
1 change: 1 addition & 0 deletions cmd/nbnslistener/nbnslistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
log.Fatal("invalid CIDR ", err)
}

nbns.LogAll = true
handler, err := nbns.NewHandler(*network)
if err != nil {
log.Fatal("error in nbns", err)
Expand Down
8 changes: 6 additions & 2 deletions goroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ func (h *goroutinePool) new(name string) (ret *goroutinePool) {
func (h *goroutinePool) Begin(name string) *goroutine {
g := goroutine{name: name, pool: h}
atomic.AddInt32(&h.n, 1)
log.Infof("%s goroutine started", g.name)
if LogAll {
log.Infof("%s goroutine started", g.name)
}
return &g
}

Expand All @@ -76,7 +78,9 @@ func (h *goroutinePool) Stopping() bool {
func (g *goroutine) End() {
atomic.AddInt32(&g.pool.n, -1)
stopping := atomic.LoadInt32(&g.pool.stopping)
log.Infof("%s goroutine finished - remaining %d", g.name, atomic.LoadInt32(&g.pool.n))
if LogAll {
log.Infof("%s goroutine finished - remaining %d", g.name, atomic.LoadInt32(&g.pool.n))
}
if stopping != 0 {
g.pool.stoppedChannel <- g
}
Expand Down
34 changes: 27 additions & 7 deletions nbns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type Handler struct {
notification chan<- Entry
}

// LogAll tells the package to log Info and lower messages
// Default is to log only Error and Warning; set it to true to log other messages
var LogAll bool

// Entry holds a NBNS name entry
type Entry struct {
IP net.IP
Expand Down Expand Up @@ -107,7 +111,9 @@ func (h *Handler) ListenAndServe(interval time.Duration) error {

if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
log.Debug("NBNS timeout reading result", err)
if LogAll {
log.Debug("NBNS timeout reading result", err)
}
continue
}

Expand All @@ -117,12 +123,16 @@ func (h *Handler) ListenAndServe(interval time.Duration) error {
if t.Op == "dial" {
log.Error("NBNS error unknown host", err)
} else if t.Op == "read" {
log.Debug("NBNS error conn refused", err)
if LogAll {
log.Debug("NBNS error conn refused", err)
}
}

case syscall.Errno:
if t == syscall.ECONNREFUSED {
log.Debug("NBNS error connection refused", err)
if LogAll {
log.Debug("NBNS error connection refused", err)
}
}

default:
Expand All @@ -135,26 +145,36 @@ func (h *Handler) ListenAndServe(interval time.Duration) error {
packet := packet(readBuffer)
switch {
case packet.opcode() == opcodeQuery && packet.response() == 1:
log.Info("nbns received nbns nodeStatusResponse from IP ", *udpAddr)
if LogAll {
log.Info("nbns received nbns nodeStatusResponse from IP ", *udpAddr)
}
entry, err := parseNodeStatusResponsePacket(packet, udpAddr.IP)
if err != nil {
log.Error("error processing nodeStatusResponse ", err)
return err
}

if h.notification != nil {
log.Debugf("nbns send notification name %s ip %s", entry.Name, entry.IP)
if LogAll {
if LogAll {
log.Debugf("nbns send notification name %s ip %s", entry.Name, entry.IP)
}
}
h.notification <- entry
}

case packet.response() == 0:
if packet.trnID() != sequence { // ignore our own request
log.Info("nbns not implemented - recvd nbns request from IP ", *udpAddr)
if LogAll {
log.Info("nbns not implemented - recvd nbns request from IP ", *udpAddr)
}
packet.printHeader()
}

default:
log.Infof("nbns not implemented opcode=%v ", packet.opcode())
if LogAll {
log.Infof("nbns not implemented opcode=%v ", packet.opcode())
}
packet.printHeader()
}

Expand Down
40 changes: 25 additions & 15 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ func encodeNBNSName(name string) string {
// Final name - len 0x00 means no more names
buffer.Write([]byte{0x00})

log.Debugf("NBNS netbios len %v name ->%s\n", len(buffer.Bytes()), string(buffer.Bytes()))
if LogAll {
log.Debugf("NBNS netbios len %v name ->%s\n", len(buffer.Bytes()), string(buffer.Bytes()))
}

return string(buffer.Bytes())
}
Expand Down Expand Up @@ -157,16 +159,18 @@ func (p packet) arCount() uint16 { return uint16(p[10])<<8 | uint16(p[11]) }
func (p packet) payload() []byte { return p[12:] }

func (p packet) printHeader() {
fmt.Println("NBNS packet structure")
// fmt.Printf("Buffer: 0x%02q\n", p)
fmt.Printf("TrnId 0x%04x\n", p.trnID())
fmt.Printf("response %b\n", p.response())
fmt.Printf("opcode %b\n", p.opcode())
fmt.Printf("flags %016b\n", p.flags())
fmt.Printf(" broadcast %v\n", p.flagsBroadcast())
fmt.Printf("rcode %b\n", p.rcode())
fmt.Printf("QDCount 0x%04x | ANCount 0x%04x\n", p.qdCount(), p.anCount())
fmt.Printf("NSCount 0x%04x | ARCount 0x%04x\n", p.nsCount(), p.arCount())
if LogAll {
fmt.Println("NBNS packet structure")
// fmt.Printf("Buffer: 0x%02q\n", p)
fmt.Printf("TrnId 0x%04x\n", p.trnID())
fmt.Printf("response %b\n", p.response())
fmt.Printf("opcode %b\n", p.opcode())
fmt.Printf("flags %016b\n", p.flags())
fmt.Printf(" broadcast %v\n", p.flagsBroadcast())
fmt.Printf("rcode %b\n", p.rcode())
fmt.Printf("QDCount 0x%04x | ANCount 0x%04x\n", p.qdCount(), p.anCount())
fmt.Printf("NSCount 0x%04x | ARCount 0x%04x\n", p.nsCount(), p.arCount())
}
}

// nameQueryWireFormat Name Query Request
Expand Down Expand Up @@ -238,7 +242,9 @@ func query(name string, questionType uint16) (packet packet) {
// / STATISTICS (variable len) /
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
func parseNodeStatusResponsePacket(packet packet, ip net.IP) (entry Entry, err error) {
log.Debug("nbns parsing node status response packet")
if LogAll {
log.Debug("nbns parsing node status response packet")
}

// Assume no questions
if packet.qdCount() > 0 {
Expand All @@ -253,7 +259,9 @@ func parseNodeStatusResponsePacket(packet packet, ip net.IP) (entry Entry, err e
// variable len reading
buf := bytes.NewBuffer(packet.payload())
name := decodeNBNSName(buf)
log.Debugf("nbns name: |%s|\n", name)
if LogAll {
log.Debugf("nbns name: |%s|\n", name)
}

var tmp16 uint16
var numNames uint8
Expand All @@ -279,8 +287,10 @@ func parseNodeStatusResponsePacket(packet packet, ip net.IP) (entry Entry, err e
}

entry = Entry{IP: ip, Name: table[0]} // first entry
log.Debugf("nbns new entry name %s ip %s", entry.Name, entry.IP)
log.Debug("nbns node names ", table)
if LogAll {
log.Debugf("nbns new entry name %s ip %s", entry.Name, entry.IP)
log.Debug("nbns node names ", table)
}

return entry, nil
}

0 comments on commit 0bbe6cc

Please sign in to comment.