Skip to content

Commit

Permalink
dhcp: set both BootFile options
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu committed Jan 7, 2025
1 parent be97101 commit ab07b94
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
63 changes: 60 additions & 3 deletions pkg/roles/dhcp/dhcp_handler4_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"testing"
"time"

"beryju.io/gravity/api"
"beryju.io/gravity/pkg/instance"
"beryju.io/gravity/pkg/roles/dhcp"
"beryju.io/gravity/pkg/roles/dhcp/types"
"beryju.io/gravity/pkg/tests"
"github.com/gorilla/securecookie"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -378,6 +378,63 @@ func TestDHCP_Parallel(t *testing.T) {
wg.Wait()
}

func generateHW() net.HardwareAddr {
return net.HardwareAddr(securecookie.GenerateRandomKey(6))
func TestDHCPRequest_Options(t *testing.T) {
defer tests.Setup(t)()
rootInst := instance.New()
ctx := tests.Context()
inst := rootInst.ForRole("dhcp", ctx)
role := dhcp.New(inst)

tests.PanicIfError(inst.KV().Put(
ctx,
inst.KV().Key(
types.KeyRole,
types.KeyScopes,
"test",
).String(),
tests.MustJSON(dhcp.Scope{
SubnetCIDR: "10.100.0.0/24",
Default: true,
TTL: 86400,
Options: []*types.DHCPOption{
{
TagName: types.TagNameRouter,
Value: api.PtrString("1.2.3.4"),
},
{
TagName: types.TagNameBootfile,
Value: api.PtrString("foo"),
},
{
TagName: "",
Value: api.PtrString("1.2.3.4"),
},
{
TagName: "foo",
Value: api.PtrString("1.2.3.4"),
},
},
IPAM: map[string]string{
"type": "internal",
"range_start": "10.100.0.100",
"range_end": "10.100.0.250",
},
}),
))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

req, err := dhcpv4.FromBytes(DHCPRequestPayload)
assert.NoError(t, err)
req4 := role.NewRequest4(req)
res := role.Handler4(req4)
assert.NotNil(t, res)
assert.Equal(t, "10.100.0.100", res.YourIPAddr.String())
assert.Equal(t, "ffffff00", res.SubnetMask().String())
assert.Equal(t, "44:90:bb:66:32:04", res.ClientHWAddr.String())
assert.Equal(t, "1.2.3.4", res.Router()[0].String())
assert.Equal(t, "foo", res.BootFileName)
assert.Equal(t, "foo", res.BootFileNameOption())
assert.Equal(t, "foo", res.TFTPServerName())
assert.Equal(t, 86400*time.Second, res.IPAddressLeaseTime(1*time.Second))
}
5 changes: 4 additions & 1 deletion pkg/roles/dhcp/leases.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (l *Lease) createReply(req *Request4) *dhcpv4.DHCPv4 {

for _, opt := range l.scope.Options {
finalVal := make([]byte, 0)
if opt.Tag == nil && opt.TagName == "" {
if opt == nil || opt.Tag == nil && opt.TagName == "" {
continue
}
if opt.TagName != "" {
Expand Down Expand Up @@ -299,6 +299,9 @@ func (l *Lease) createReply(req *Request4) *dhcpv4.DHCPv4 {
}
dopt := dhcpv4.OptGeneric(dhcpv4.GenericOptionCode(*opt.Tag), finalVal)
rep.UpdateOption(dopt)
if dopt.Code.Code() == uint8(dhcpv4.OptionBootfileName) {
rep.BootFileName = dhcpv4.GetString(dopt.Code, rep.Options)
}
}
return rep
}
5 changes: 5 additions & 0 deletions pkg/roles/dhcp/role_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package dhcp_test

import (
"net"
"testing"

"beryju.io/gravity/pkg/instance"
"beryju.io/gravity/pkg/roles/dhcp"
"beryju.io/gravity/pkg/tests"
"github.com/gorilla/securecookie"
"github.com/stretchr/testify/assert"
)

func generateHW() net.HardwareAddr {
return net.HardwareAddr(securecookie.GenerateRandomKey(6))
}
func RoleConfig() []byte {
return []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 0,
Expand Down
4 changes: 2 additions & 2 deletions pkg/roles/dhcp/types/dhcp_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
TagNameNameServer = "name_server"
TagNameDomainName = "domain_name"
TagNameBootfile = "bootfile"
TagNameTFTPserver = "tftp_server"
TagNameTFTPServer = "tftp_server"
)

// https://datatracker.ietf.org/doc/html/rfc2131
Expand All @@ -23,7 +23,7 @@ var TagMap map[OptionTagName]uint8 = map[OptionTagName]uint8{
TagNameNameServer: dhcpv4.OptionDomainNameServer.Code(),
TagNameDomainName: dhcpv4.OptionDomainName.Code(),
TagNameBootfile: dhcpv4.OptionBootfileName.Code(),
TagNameTFTPserver: dhcpv4.OptionTFTPServerName.Code(),
TagNameTFTPServer: dhcpv4.OptionTFTPServerName.Code(),
}

var IPTags = map[uint8]bool{
Expand Down

0 comments on commit ab07b94

Please sign in to comment.