blob: 68b23a48d96cfbb0dda2b826a9150a0be8a03f7d [file] [log] [blame]
package dhcp
import (
"bytes"
"fmt"
"github.com/google/netstack/tcpip"
)
func (h header) String() string {
opts, err := h.options()
var msgtype dhcpMsgType
if err == nil {
msgtype, err = opts.dhcpMsgType()
}
if !h.isValid() || err != nil {
return fmt.Sprintf("DHCP invalid, %v %v h[1:4]=%x cookie=%x len=%d (%v)", h.op(), h.xid(), []byte(h[1:4]), []byte(h[236:240]), len(h), err)
}
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "%v %v len=%d\n", msgtype, h.xid(), len(h))
fmt.Fprintf(buf, "\tciaddr:%v yiaddr:%v siaddr:%v giaddr:%v\n",
tcpip.Address(h.ciaddr()),
tcpip.Address(h.yiaddr()),
tcpip.Address(h.siaddr()),
tcpip.Address(h.giaddr()))
fmt.Fprintf(buf, "\tchaddr:%x", h.chaddr())
for _, opt := range opts {
fmt.Fprintf(buf, "\n\t%v", opt)
}
return buf.String()
}
func (opt option) String() string {
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "%v: ", opt.code)
fmt.Fprintf(buf, "%x", opt.body)
return buf.String()
}
func (code optionCode) String() string {
switch code {
case optSubnetMask:
return "option(subnet-mask)"
case optDefaultGateway:
return "option(default-gateway)"
case optDomainNameServer:
return "option(dns)"
case optDomainName:
return "option(domain-name)"
case optReqIPAddr:
return "option(request-ip-address)"
case optLeaseTime:
return "option(lease-time)"
case optDHCPMsgType:
return "option(message-type)"
case optDHCPServer:
return "option(server)"
case optParamReq:
return "option(parameter-request)"
case optMessage:
return "option(message)"
case optClientID:
return "option(client-id)"
default:
return fmt.Sprintf("option(%d)", code)
}
}
func (o op) String() string {
switch o {
case opRequest:
return "op(request)"
case opReply:
return "op(reply)"
}
return fmt.Sprintf("op(UNKNOWN:%d)", int(o))
}
func (t dhcpMsgType) String() string {
switch t {
case dhcpDISCOVER:
return "DHCPDISCOVER"
case dhcpOFFER:
return "DHCPOFFER"
case dhcpREQUEST:
return "DHCPREQUEST"
case dhcpDECLINE:
return "DHCPDECLINE"
case dhcpACK:
return "DHCPACK"
case dhcpNAK:
return "DHCPNAK"
case dhcpRELEASE:
return "DHCPRELEASE"
}
return fmt.Sprintf("DHCP(%d)", int(t))
}
func (v xid) String() string {
return fmt.Sprintf("xid:%x", uint32(v))
}