diff options
author | Chris Koch <chrisko@google.com> | 2020-07-10 17:09:56 -0700 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2020-07-11 01:17:33 +0100 |
commit | e1b69ee5fb3318ca17418526efa5bc41c98ff29b (patch) | |
tree | d351b344a5955ffb7eca520b2dcd3b3351335e39 /dhcpv6 | |
parent | f80356b40e79a2c07bf20ebbb328e3c9df14d0bf (diff) |
nclient6: optional dropped packet logging
Signed-off-by: Chris Koch <chrisko@google.com>
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/nclient6/client.go | 19 |
1 files changed, 19 insertions, 0 deletions
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. |