forked from u-root/u-root
/
dhcp4.go
90 lines (78 loc) · 2.02 KB
/
dhcp4.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
// Copyright 2017-2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dhclient
import (
"net"
"net/url"
"github.com/u-root/dhcp4"
"github.com/u-root/dhcp4/dhcp4opts"
)
// Packet4 implements convenience functions for DHCPv4 packets.
type Packet4 struct {
P *dhcp4.Packet
}
// NewPacket4 wraps a DHCPv4 packet with some convenience methods.
func NewPacket4(p *dhcp4.Packet) *Packet4 {
return &Packet4{
P: p,
}
}
// Lease returns the IPNet assigned.
func (p *Packet4) Lease() *net.IPNet {
netmask := dhcp4opts.GetSubnetMask(p.P.Options)
if netmask == nil {
// If they did not offer a subnet mask, we choose the most
// restrictive option.
netmask = []byte{255, 255, 255, 255}
}
return &net.IPNet{
IP: p.P.YIAddr,
Mask: net.IPMask(netmask),
}
}
// Gateway returns the gateway IP assigned.
//
// OptionRouter is used as opposed to GIAddr, which seems unused by most DHCP
// servers?
func (p *Packet4) Gateway() net.IP {
gw := dhcp4opts.GetRouters(p.P.Options)
if gw == nil {
return nil
}
return gw[0]
}
// DNS returns DNS IPs assigned.
func (p *Packet4) DNS() []net.IP {
ips := dhcp4opts.GetDomainNameServers(p.P.Options)
if ips == nil {
return nil
}
return []net.IP(ips)
}
// Boot returns the boot file assigned.
func (p *Packet4) Boot() (url.URL, error) {
// TODO: This is not 100% right -- if a certain option is set, this
// stuff is encoded in options instead of in the packet's BootFile and
// ServerName fields.
// While the default is tftp, servers may specify HTTP or FTP URIs.
u, err := url.Parse(p.P.BootFile)
if err != nil {
return url.URL{}, err
}
if len(u.Scheme) == 0 {
// Defaults to tftp is not specified.
u.Scheme = "tftp"
u.Path = p.P.BootFile
if len(p.P.ServerName) == 0 {
server := dhcp4opts.GetServerIdentifier(p.P.Options)
if server == nil {
return url.URL{}, err
}
u.Host = net.IP(server).String()
} else {
u.Host = p.P.ServerName
}
}
return *u, nil
}