-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
92 lines (76 loc) · 1.99 KB
/
options.go
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
package main
import "net"
type DHCPMessageType uint8
const (
_ DHCPMessageType = iota
DHCPDISCOVER
DHCPOFFER
DHCPREQUEST
DHCPDECLINE
DHCPACK
DHCPNAK
DHCPRELEASE
DHCPINFORM
)
const (
Opts_NetMask uint8 = 1
Opts_RouterIP uint8 = 3
Opts_HostName uint8 = 12
Opts_RequestedIP uint8 = 50
Opts_IPLeaseTime uint8 = 51
Opts_OptionOverload uint8 = 52
Opts_MessageType uint8 = 53
Opts_ServerIdentifier uint8 = 54
Opts_ParameterRequestList uint8 = 55
Opts_Message uint8 = 56
Opts_MaximumMessageSize uint8 = 57
Opts_ClientIdentifier uint8 = 61
Opts_End uint8 = 255
)
type DhcpOpt struct {
Type uint8
Length uint8
Data []uint8
}
func NewDhcpOpt(t uint8, l uint8, d ...uint8) DhcpOpt {
return DhcpOpt{
Type: t,
Length: l,
Data: d,
}
}
type DhcpOpts []DhcpOpt
func (opts *DhcpOpts) AddLeaseTime(secs uint32) {
*opts = append(*opts, NewDhcpOpt(Opts_IPLeaseTime, 4, byte(secs>>24), byte(secs>>16), byte(secs>>8), byte(secs)))
}
func (opts *DhcpOpts) AddMessageType(mt DHCPMessageType) {
*opts = append(*opts, NewDhcpOpt(Opts_MessageType, 1, uint8(mt)))
}
func (opts *DhcpOpts) AddServerIP(ip net.IP) {
*opts = append(*opts, NewDhcpOpt(Opts_ServerIdentifier, 4, ip.To4()...))
}
func (opts *DhcpOpts) AddEnd() {
*opts = append(*opts, NewDhcpOpt(Opts_End, 0))
}
func (opts *DhcpOpts) AddNetmask(netmask net.IPMask) {
*opts = append(*opts, NewDhcpOpt(Opts_NetMask, 4, netmask...))
}
func (opts *DhcpOpts) AddRouter(ip net.IP) {
*opts = append(*opts, NewDhcpOpt(Opts_RouterIP, 4, ip.To4()...))
}
func (opts *DhcpOpts) GetHostName() string {
for _, opt := range *opts {
if opt.Type == Opts_HostName {
return string(opt.Data)
}
}
return ""
}
func (opts *DhcpOpts) GetLeaseTime() uint32 {
for _, opt := range *opts {
if opt.Type == Opts_IPLeaseTime {
return uint32(opt.Data[0])<<24 | uint32(opt.Data[1])<<16 | uint32(opt.Data[2])<<8 | uint32(opt.Data[3])
}
}
return 0
}