diff options
-rw-r--r-- | dhcpv4/nclient4/client.go | 4 | ||||
-rw-r--r-- | dhcpv6/nclient6/client.go | 57 |
2 files changed, 58 insertions, 3 deletions
diff --git a/dhcpv4/nclient4/client.go b/dhcpv4/nclient4/client.go index 95a224c..2092e01 100644 --- a/dhcpv4/nclient4/client.go +++ b/dhcpv4/nclient4/client.go @@ -264,7 +264,7 @@ func WithTimeout(d time.Duration) ClientOpt { func WithSummaryLogger() ClientOpt { return func(c *Client) { c.logger = shortSummaryLogger{ - Logger: log.New(os.Stderr, "[dhcpv4]", log.LstdFlags), + Logger: log.New(os.Stderr, "[dhcpv4] ", log.LstdFlags), } } } @@ -273,7 +273,7 @@ func WithSummaryLogger() ClientOpt { func WithDebugLogger() ClientOpt { return func(c *Client) { c.logger = debugLogger{ - Logger: log.New(os.Stderr, "[dhcpv4]", log.LstdFlags), + Logger: log.New(os.Stderr, "[dhcpv4] ", log.LstdFlags), } } } diff --git a/dhcpv6/nclient6/client.go b/dhcpv6/nclient6/client.go index 5c18685..a39689a 100644 --- a/dhcpv6/nclient6/client.go +++ b/dhcpv6/nclient6/client.go @@ -10,6 +10,7 @@ import ( "fmt" "log" "net" + "os" "strings" "sync" "sync/atomic" @@ -51,6 +52,7 @@ type Client struct { conn net.PacketConn timeout time.Duration retry int + logger logger // bufferCap is the channel capacity for each TransactionID. bufferCap int @@ -76,6 +78,38 @@ type Client struct { pending map[dhcpv6.TransactionID]*pendingCh } +type logger interface { + Printf(format string, v ...interface{}) + PrintMessage(prefix string, message *dhcpv6.Message) +} + +type emptyLogger struct{} + +func (e emptyLogger) Printf(format string, v ...interface{}) {} +func (e emptyLogger) PrintMessage(prefix string, message *dhcpv6.Message) {} + +type shortSummaryLogger struct { + *log.Logger +} + +func (s shortSummaryLogger) Printf(format string, v ...interface{}) { + s.Logger.Printf(format, v...) +} +func (s shortSummaryLogger) PrintMessage(prefix string, message *dhcpv6.Message) { + s.Printf("%s: %s", prefix, message) +} + +type debugLogger struct { + *log.Logger +} + +func (d debugLogger) Printf(format string, v ...interface{}) { + d.Logger.Printf(format, v...) +} +func (d debugLogger) PrintMessage(prefix string, message *dhcpv6.Message) { + d.Printf("%s: %s", prefix, message.Summary()) +} + // NewIPv6UDPConn returns a UDP connection bound to both the interface and port // given based on a IPv6 DGRAM socket. func NewIPv6UDPConn(iface string, port int) (net.PacketConn, error) { @@ -109,6 +143,7 @@ func NewWithConn(conn net.PacketConn, ifaceHWAddr net.HardwareAddr, opts ...Clie serverAddr: AllDHCPRelayAgentsAndServers, bufferCap: 5, conn: conn, + logger: emptyLogger{}, done: make(chan struct{}), pending: make(map[dhcpv6.TransactionID]*pendingCh), @@ -168,7 +203,7 @@ func (c *Client) receiveLoop() { n, _, err := c.conn.ReadFrom(b) if err != nil { if !isErrClosing(err) { - log.Printf("error reading from UDP connection: %v", err) + c.logger.Printf("error reading from UDP connection: %v", err) } return } @@ -231,6 +266,24 @@ func WithBroadcastAddr(n *net.UDPAddr) ClientOpt { } } +// WithSummaryLogger logs one-line DHCPv6 message summarys when sent & received. +func WithSummaryLogger() ClientOpt { + return func(c *Client) { + c.logger = shortSummaryLogger{ + Logger: log.New(os.Stderr, "[dhcpv6] ", log.LstdFlags), + } + } +} + +// WithDebugLogger logs multi-line full DHCPv6 messages when sent & received. +func WithDebugLogger() ClientOpt { + return func(c *Client) { + c.logger = debugLogger{ + Logger: log.New(os.Stderr, "[dhcpv6] ", log.LstdFlags), + } + } +} + // Matcher matches DHCP packets. type Matcher func(*dhcpv6.Message) bool @@ -336,6 +389,7 @@ func (c *Client) SendAndRead(ctx context.Context, dest *net.UDPAddr, msg *dhcpv6 if err != nil { return err } + c.logger.PrintMessage("sent message", msg) defer rem() for { @@ -351,6 +405,7 @@ func (c *Client) SendAndRead(ctx context.Context, dest *net.UDPAddr, msg *dhcpv6 case packet := <-ch: if match == nil || match(packet) { + c.logger.PrintMessage("received message", packet) response = packet return nil } |