From 35d689461ead796eb13010250fea82de6ac2982d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20B=C3=B6hm?= Date: Wed, 26 Apr 2023 18:16:29 +0200 Subject: [PATCH] Fix DHCPv4 options encoding For calculating the DHCPv4 length by the "Len" method there is always a byte added for the end option. But the END option is only set when there are any options at all. This results in a missing END option in case there are no options present. This commit moves the encoding of the END option so it is always set unconditionally. --- layers/dhcpv4.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/layers/dhcpv4.go b/layers/dhcpv4.go index 67c80b7d5..4a8a528fc 100644 --- a/layers/dhcpv4.go +++ b/layers/dhcpv4.go @@ -222,8 +222,8 @@ func (d *DHCPv4) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.Serialize copy(data[108:236], d.File) binary.BigEndian.PutUint32(data[236:240], DHCPMagic) + offset := 240 if len(d.Options) > 0 { - offset := 240 for _, o := range d.Options { if err := o.encode(data[offset:]); err != nil { return err @@ -235,10 +235,10 @@ func (d *DHCPv4) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.Serialize offset += 2 + len(o.Data) } } - optend := NewDHCPOption(DHCPOptEnd, nil) - if err := optend.encode(data[offset:]); err != nil { - return err - } + } + optend := NewDHCPOption(DHCPOptEnd, nil) + if err := optend.encode(data[offset:]); err != nil { + return err } return nil }