From e1b69ee5fb3318ca17418526efa5bc41c98ff29b Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Fri, 10 Jul 2020 17:09:56 -0700 Subject: nclient6: optional dropped packet logging Signed-off-by: Chris Koch --- dhcpv6/nclient6/client.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'dhcpv6') diff --git a/dhcpv6/nclient6/client.go b/dhcpv6/nclient6/client.go index 74cdf5c..c40e6c8 100644 --- a/dhcpv6/nclient6/client.go +++ b/dhcpv6/nclient6/client.go @@ -71,6 +71,9 @@ type Client struct { // wg protects the receiveLoop. wg sync.WaitGroup + // printDropped logs dropped packets to logger if true. + printDropped bool + pendingMu sync.Mutex // pending stores the distribution channels for each pending // TransactionID. receiveLoop uses this map to determine which channel @@ -217,6 +220,12 @@ func (c *Client) receiveLoop() { msg, err := dhcpv6.MessageFromBytes(b[:n]) if err != nil { // Not a valid DHCP packet; keep listening. + if c.printDropped { + if len(b) > 12 { + b = b[:12] + } + c.logger.Printf("Invalid DHCPv6 message received (len %d bytes), first 12 bytes: %#x", n, b) + } continue } @@ -231,6 +240,9 @@ func (c *Client) receiveLoop() { // This send may block. case p.ch <- msg: } + } else if c.printDropped { + // The Stringer will print the transaction ID. + c.logger.Printf("No client waiting for msg with this XID: %s", msg) } c.pendingMu.Unlock() } @@ -249,6 +261,13 @@ func WithTimeout(d time.Duration) ClientOpt { } } +// WithLogDroppedPackets logs a short message for dropped packets. +func WithLogDroppedPackets() ClientOpt { + return func(c *Client) { + c.printDropped = true + } +} + // WithRetry configures the number of retransmissions to attempt. // // Default is 3. -- cgit v1.2.3