summaryrefslogtreecommitdiffhomepage
path: root/pkg/dhcp/dhcp_string.go
blob: 29ce98593bff8da4f4f787649c8225726d470418 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dhcp

import (
	"bytes"
	"fmt"

	"gvisor.googlesource.com/gvisor/pkg/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))
}