diff options
author | Christopher Koch <chrisko@google.com> | 2018-12-28 17:35:48 -0800 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2019-01-09 23:54:01 +0000 |
commit | 108ed92e1c9901936541020bc3214533acce77bb (patch) | |
tree | f1b8806daa9a4a3d3f105dda78be9afeeb756012 /dhcpv4/bsdp | |
parent | 4d2cfd40eee7dd315385d320534b6c99094449e9 (diff) |
dhcpv4: export packet members; remove setters and getters.
Diffstat (limited to 'dhcpv4/bsdp')
-rw-r--r-- | dhcpv4/bsdp/bsdp.go | 30 | ||||
-rw-r--r-- | dhcpv4/bsdp/bsdp_test.go | 49 | ||||
-rw-r--r-- | dhcpv4/bsdp/client.go | 2 |
3 files changed, 38 insertions, 43 deletions
diff --git a/dhcpv4/bsdp/bsdp.go b/dhcpv4/bsdp/bsdp.go index 8d4430a..3f97602 100644 --- a/dhcpv4/bsdp/bsdp.go +++ b/dhcpv4/bsdp/bsdp.go @@ -143,12 +143,10 @@ func InformSelectForAck(ack dhcpv4.DHCPv4, replyPort uint16, selectedImage BootI if needsReplyPort(replyPort) && replyPort >= 1024 { return nil, errors.New("replyPort must be a privileged port") } - d.SetOpcode(dhcpv4.OpcodeBootRequest) - d.SetHwType(ack.HwType()) - d.SetHwAddrLen(ack.HwAddrLen()) - clientHwAddr := ack.ClientHwAddr() - d.SetClientHwAddr(clientHwAddr[:]) - d.SetTransactionID(ack.TransactionID()) + d.OpCode = dhcpv4.OpcodeBootRequest + d.HWType = ack.HWType + d.ClientHWAddr = ack.ClientHWAddr + d.TransactionID = ack.TransactionID if ack.IsBroadcast() { d.SetBroadcast() } else { @@ -209,11 +207,10 @@ func NewReplyForInformList(inform *dhcpv4.DHCPv4, config ReplyConfig) (*dhcpv4.D if err != nil { return nil, err } - reply.SetClientIPAddr(inform.ClientIPAddr()) - reply.SetYourIPAddr(net.IPv4zero) - reply.SetGatewayIPAddr(inform.GatewayIPAddr()) - reply.SetServerIPAddr(config.ServerIP) - reply.SetServerHostName([]byte(config.ServerHostname)) + reply.ClientIPAddr = inform.ClientIPAddr + reply.GatewayIPAddr = inform.GatewayIPAddr + reply.ServerIPAddr = config.ServerIP + reply.ServerHostName = config.ServerHostname reply.AddOption(&dhcpv4.OptMessageType{MessageType: dhcpv4.MessageTypeAck}) reply.AddOption(&dhcpv4.OptServerIdentifier{ServerID: config.ServerIP}) @@ -249,12 +246,11 @@ func NewReplyForInformSelect(inform *dhcpv4.DHCPv4, config ReplyConfig) (*dhcpv4 return nil, err } - reply.SetClientIPAddr(inform.ClientIPAddr()) - reply.SetYourIPAddr(net.IPv4zero) - reply.SetGatewayIPAddr(inform.GatewayIPAddr()) - reply.SetServerIPAddr(config.ServerIP) - reply.SetServerHostName([]byte(config.ServerHostname)) - reply.SetBootFileName([]byte(config.BootFileName)) + reply.ClientIPAddr = inform.ClientIPAddr + reply.GatewayIPAddr = inform.GatewayIPAddr + reply.ServerIPAddr = config.ServerIP + reply.ServerHostName = config.ServerHostname + reply.BootFileName = config.BootFileName reply.AddOption(&dhcpv4.OptMessageType{MessageType: dhcpv4.MessageTypeAck}) reply.AddOption(&dhcpv4.OptServerIdentifier{ServerID: config.ServerIP}) diff --git a/dhcpv4/bsdp/bsdp_test.go b/dhcpv4/bsdp/bsdp_test.go index 610b8d6..cb703da 100644 --- a/dhcpv4/bsdp/bsdp_test.go +++ b/dhcpv4/bsdp/bsdp_test.go @@ -103,20 +103,19 @@ func TestNewInformList_ReplyPort(t *testing.T) { require.Equal(t, replyPort, opt.(*OptReplyPort).Port) } -func newAck(hwAddr []byte, transactionID uint32) *dhcpv4.DHCPv4 { +func newAck(hwAddr net.HardwareAddr, transactionID [4]byte) *dhcpv4.DHCPv4 { ack, _ := dhcpv4.New() - ack.SetTransactionID(transactionID) - ack.SetHwType(iana.HwTypeEthernet) - ack.SetClientHwAddr(hwAddr) - ack.SetHwAddrLen(uint8(len(hwAddr))) + ack.TransactionID = transactionID + ack.HWType = iana.HwTypeEthernet + ack.ClientHWAddr = hwAddr ack.AddOption(&dhcpv4.OptMessageType{MessageType: dhcpv4.MessageTypeAck}) ack.AddOption(&dhcpv4.OptionGeneric{OptionCode: dhcpv4.OptionEnd}) return ack } func TestInformSelectForAck_Broadcast(t *testing.T) { - hwAddr := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66} - tid := uint32(22) + hwAddr := net.HardwareAddr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66} + tid := [4]byte{0x22, 0, 0, 0} serverID := net.IPv4(1, 2, 3, 4) bootImage := BootImage{ ID: BootImageID{ @@ -132,10 +131,10 @@ func TestInformSelectForAck_Broadcast(t *testing.T) { m, err := InformSelectForAck(*ack, 0, bootImage) require.NoError(t, err) - require.Equal(t, dhcpv4.OpcodeBootRequest, m.Opcode()) - require.Equal(t, ack.HwType(), m.HwType()) - require.Equal(t, ack.ClientHwAddr(), m.ClientHwAddr()) - require.Equal(t, ack.TransactionID(), m.TransactionID()) + require.Equal(t, dhcpv4.OpcodeBootRequest, m.OpCode) + require.Equal(t, ack.HWType, m.HWType) + require.Equal(t, ack.ClientHWAddr, m.ClientHWAddr) + require.Equal(t, ack.TransactionID, m.TransactionID) require.True(t, m.IsBroadcast()) // Validate options. @@ -157,8 +156,8 @@ func TestInformSelectForAck_Broadcast(t *testing.T) { } func TestInformSelectForAck_NoServerID(t *testing.T) { - hwAddr := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66} - tid := uint32(22) + hwAddr := net.HardwareAddr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66} + tid := [4]byte{0x22, 0, 0, 0} bootImage := BootImage{ ID: BootImageID{ IsInstall: true, @@ -174,8 +173,8 @@ func TestInformSelectForAck_NoServerID(t *testing.T) { } func TestInformSelectForAck_BadReplyPort(t *testing.T) { - hwAddr := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66} - tid := uint32(22) + hwAddr := net.HardwareAddr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66} + tid := [4]byte{0x22, 0, 0, 0} serverID := net.IPv4(1, 2, 3, 4) bootImage := BootImage{ ID: BootImageID{ @@ -194,8 +193,8 @@ func TestInformSelectForAck_BadReplyPort(t *testing.T) { } func TestInformSelectForAck_ReplyPort(t *testing.T) { - hwAddr := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66} - tid := uint32(22) + hwAddr := net.HardwareAddr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66} + tid := [4]byte{0x22, 0, 0, 0} serverID := net.IPv4(1, 2, 3, 4) bootImage := BootImage{ ID: BootImageID{ @@ -272,9 +271,9 @@ func TestNewReplyForInformList(t *testing.T) { } ack, err := NewReplyForInformList(inform, config) require.NoError(t, err) - require.Equal(t, net.IP{1, 2, 3, 4}, ack.ClientIPAddr()) - require.Equal(t, net.IPv4zero, ack.YourIPAddr()) - require.Equal(t, "bsdp.foo.com", ack.ServerHostName()) + require.Equal(t, net.IP{1, 2, 3, 4}, ack.ClientIPAddr) + require.Equal(t, net.IPv4zero, ack.YourIPAddr) + require.Equal(t, "bsdp.foo.com", ack.ServerHostName) // Validate options. RequireHasOption(t, ack, &dhcpv4.OptMessageType{MessageType: dhcpv4.MessageTypeAck}) @@ -283,7 +282,7 @@ func TestNewReplyForInformList(t *testing.T) { require.NotNil(t, ack.GetOneOption(dhcpv4.OptionVendorSpecificInformation)) // Ensure options terminated with End option. - require.Equal(t, &dhcpv4.OptionGeneric{OptionCode: dhcpv4.OptionEnd}, ack.Options()[len(ack.Options())-1]) + require.Equal(t, &dhcpv4.OptionGeneric{OptionCode: dhcpv4.OptionEnd}, ack.Options[len(ack.Options)-1]) // Vendor-specific options. vendorOpts := ack.GetOneOption(dhcpv4.OptionVendorSpecificInformation).(*OptVendorSpecificInformation) @@ -353,9 +352,9 @@ func TestNewReplyForInformSelect(t *testing.T) { } ack, err := NewReplyForInformSelect(inform, config) require.NoError(t, err) - require.Equal(t, net.IP{1, 2, 3, 4}, ack.ClientIPAddr()) - require.Equal(t, net.IPv4zero, ack.YourIPAddr()) - require.Equal(t, "bsdp.foo.com", ack.ServerHostName()) + require.Equal(t, net.IP{1, 2, 3, 4}, ack.ClientIPAddr) + require.Equal(t, net.IPv4zero, ack.YourIPAddr) + require.Equal(t, "bsdp.foo.com", ack.ServerHostName) // Validate options. RequireHasOption(t, ack, &dhcpv4.OptMessageType{MessageType: dhcpv4.MessageTypeAck}) @@ -365,7 +364,7 @@ func TestNewReplyForInformSelect(t *testing.T) { require.NotNil(t, ack.GetOneOption(dhcpv4.OptionVendorSpecificInformation)) // Ensure options are terminated with End option. - require.Equal(t, &dhcpv4.OptionGeneric{OptionCode: dhcpv4.OptionEnd}, ack.Options()[len(ack.Options())-1]) + require.Equal(t, &dhcpv4.OptionGeneric{OptionCode: dhcpv4.OptionEnd}, ack.Options[len(ack.Options)-1]) vendorOpts := ack.GetOneOption(dhcpv4.OptionVendorSpecificInformation).(*OptVendorSpecificInformation) RequireHasOption(t, vendorOpts, &OptMessageType{Type: MessageTypeSelect}) diff --git a/dhcpv4/bsdp/client.go b/dhcpv4/bsdp/client.go index dd9cbcd..dd4a0a0 100644 --- a/dhcpv4/bsdp/client.go +++ b/dhcpv4/bsdp/client.go @@ -19,7 +19,7 @@ func NewClient() *Client { } func castVendorOpt(ack *dhcpv4.DHCPv4) { - opts := ack.Options() + opts := ack.Options for i := 0; i < len(opts); i++ { if opts[i].Code() == dhcpv4.OptionVendorSpecificInformation { vendorOpt, err := ParseOptVendorSpecificInformation(opts[i].ToBytes()) |