Skip to content

Commit

Permalink
Change EDNS_EXPIRE field to support zero length option data (Resolves #…
Browse files Browse the repository at this point in the history
…1292)

As per [RFC7134](https://datatracker.ietf.org/doc/html/rfc7314#section-2) the Expire
Option in queries should be zero-length. In the current implementation the field is
uint32 which always instatiates 4bytes for that field when packing to wire format.
For that reason we change the field to []uint8 so it can support 0-length and 4-byte
length option data.
  • Loading branch information
dmavrommatis committed Sep 6, 2021
1 parent ab67aa6 commit 3a613c4
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions edns.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,30 +576,26 @@ func (e *EDNS0_N3U) copy() EDNS0 { return &EDNS0_N3U{e.Code, e.AlgCode} }
// EDNS0_EXPIRE implements the EDNS0 option as described in RFC 7314.
type EDNS0_EXPIRE struct {
Code uint16 // Always EDNS0EXPIRE
Expire uint32
Expire []uint8
}

// Option implements the EDNS0 interface.
func (e *EDNS0_EXPIRE) Option() uint16 { return EDNS0EXPIRE }
func (e *EDNS0_EXPIRE) String() string { return strconv.FormatUint(uint64(e.Expire), 10) }
func (e *EDNS0_EXPIRE) copy() EDNS0 { return &EDNS0_EXPIRE{e.Code, e.Expire} }
func (e *EDNS0_EXPIRE) Option() uint16 { return EDNS0EXPIRE }
func (e *EDNS0_EXPIRE) copy() EDNS0 { return &EDNS0_EXPIRE{e.Code, e.Expire} }
func (e *EDNS0_EXPIRE) pack() ([]byte, error) { return e.Expire, nil }
func (e *EDNS0_EXPIRE) unpack(b []byte) error { e.Expire = b; return nil }

func (e *EDNS0_EXPIRE) pack() ([]byte, error) {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, e.Expire)
return b, nil
}

func (e *EDNS0_EXPIRE) unpack(b []byte) error {
if len(b) == 0 {
// zero-length EXPIRE query, see RFC 7314 Section 2
return nil
}
if len(b) < 4 {
return ErrBuf
func (e *EDNS0_EXPIRE) String() string {
// Re-use the hash map
s := ""
for _, alg := range e.Expire {
if a, ok := HashToString[alg]; ok {
s += " " + a
} else {
s += " " + strconv.Itoa(int(alg))
}
}
e.Expire = binary.BigEndian.Uint32(b)
return nil
return s
}

// The EDNS0_LOCAL option is used for local/experimental purposes. The option
Expand Down

0 comments on commit 3a613c4

Please sign in to comment.