Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name some numeric constants #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func trimNull(d []byte) []byte {
return d
}

func (p Packet) Cookie() []byte { return p[236:240] }
func (p Packet) Cookie() []byte { return p[236:MinimalPacketSize] }
func (p Packet) Options() []byte {
if len(p) > 240 {
return p[240:]
if len(p) > MinimalPacketSize {
return p[MinimalPacketSize:]
}
return nil
}
Expand Down Expand Up @@ -131,8 +131,8 @@ func NewPacket(opCode OpCode) Packet {
p := make(Packet, 241)
p.SetOpCode(opCode)
p.SetHType(1) // Ethernet
p.SetCookie([]byte{99, 130, 83, 99})
p[240] = byte(End)
p.SetCookie(MagicCookie)
p[MinimalPacketSize] = byte(End)
return p
}

Expand All @@ -145,7 +145,7 @@ func (p *Packet) AddOption(o OptionCode, value []byte) {

// Removes all options from packet.
func (p *Packet) StripOptions() {
*p = append((*p)[:240], byte(End))
*p = append((*p)[:MinimalPacketSize], byte(End))
}

// Creates a request packet that a Client would send to a server.
Expand Down Expand Up @@ -188,14 +188,23 @@ func ReplyPacket(req Packet, mt MessageType, serverId, yIAddr net.IP, leaseDurat

// PadToMinSize pads a packet so that when sent over UDP, the entire packet,
// is 300 bytes (BOOTP min), to be compatible with really old devices.
var padder [272]byte
var padder [PaddedMinimalPacketSize]byte

func (p *Packet) PadToMinSize() {
if n := len(*p); n < 272 {
*p = append(*p, padder[:272-n]...)
if n := len(*p); n < PaddedMinimalPacketSize {
*p = append(*p, padder[:PaddedMinimalPacketSize-n]...)
}
}

// Some constants
const (
MinimalPacketSize = 240 // Minimal packet size
PaddedMinimalPacketSize = 272
)

// MagicCookie
var MagicCookie = []byte{99, 130, 83, 99}

// OpCodes
const (
BootRequest OpCode = 1 // From Client
Expand Down
3 changes: 1 addition & 2 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,11 @@ func TestReplyPacket(t *testing.T) {
// behavior does not change.
func newPacket(opCode OpCode) Packet {
const ethernetHType = 1
var cookie = []byte{99, 130, 83, 99}

p := make(Packet, 241)
p[0] = byte(opCode)
p[1] = ethernetHType
copy(p[236:240], cookie)
copy(p[236:240], MagicCookie)
p[240] = byte(End)

return p
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Serve(conn ServeConn, handler Handler) error {
if err != nil {
return err
}
if n < 240 { // Packet too small to be DHCP
if n < MinimalPacketSize { // Packet too small to be DHCP
continue
}
req := Packet(buffer[:n])
Expand Down