Skip to content

Commit

Permalink
Merge pull request #10 from frostyslav/decline_packet
Browse files Browse the repository at this point in the history
Add decline packet functionality
  • Loading branch information
d2g committed May 26, 2017
2 parents d47b4cd + 8ec2161 commit 8ca8fe2
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions client.go
Expand Up @@ -197,6 +197,16 @@ func (c *Client) GetAcknowledgement(requestPacket *dhcp4.Packet) (dhcp4.Packet,
}
}

/*
* Send Decline to the received acknowledgement.
*/
func (c *Client) SendDecline(acknowledgementPacket *dhcp4.Packet) (dhcp4.Packet, error) {
declinePacket := c.DeclinePacket(acknowledgementPacket)
declinePacket.PadToMinSize()

return declinePacket, c.SendPacket(declinePacket)
}

/*
* Send a DHCP Packet.
*/
Expand Down Expand Up @@ -296,6 +306,29 @@ func (c *Client) ReleasePacket(acknowledgement *dhcp4.Packet) dhcp4.Packet {
return packet
}

/*
* Create Decline Packet
*/
func (c *Client) DeclinePacket(acknowledgement *dhcp4.Packet) dhcp4.Packet {
messageid := make([]byte, 4)
if _, err := rand.Read(messageid); err != nil {
panic(err)
}

acknowledgementOptions := acknowledgement.ParseOptions()

packet := dhcp4.NewPacket(dhcp4.BootRequest)
packet.SetCHAddr(acknowledgement.CHAddr())
packet.SetXId(messageid)

packet.AddOption(dhcp4.OptionDHCPMessageType, []byte{byte(dhcp4.Decline)})
packet.AddOption(dhcp4.OptionRequestedIPAddress, (acknowledgement.YIAddr()).To4())
packet.AddOption(dhcp4.OptionServerIdentifier, acknowledgementOptions[dhcp4.OptionServerIdentifier])

//packet.PadToMinSize()
return packet
}

/*
* Lets do a Full DHCP Request.
*/
Expand Down

0 comments on commit 8ca8fe2

Please sign in to comment.