Fix Null event tuple coding

This commit is contained in:
Josh Deprez 2024-05-12 12:08:33 +10:00
parent 3a2cbdcde8
commit 3067daa264
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw

View file

@ -173,9 +173,12 @@ func (e EventTuples) WriteTo(w io.Writer) (int64, error) {
} }
func parseEventTuples(p []byte) (EventTuples, error) { func parseEventTuples(p []byte) (EventTuples, error) {
// Each event tuple is at least 4 bytes, so we need to store at most // Event tuples can be 1, 4, or 6 bytes long. But the only type of length 1
// len(p)/4 of them. // is the Null event type sent to probe whether or not the data receiver is
e := make(EventTuples, 0, len(p)/4) // still listening. If that's present there probably aren't any other
// tuples. Hence len(p)/4 (rounded up) is a reasonable estimate of max tuple
// count.
e := make(EventTuples, 0, (len(p)+3)/4)
for len(p) > 0 { for len(p) > 0 {
et, nextp, err := parseEventTuple(p) et, nextp, err := parseEventTuple(p)
if err != nil { if err != nil {
@ -198,6 +201,10 @@ type EventTuple struct {
func (et *EventTuple) WriteTo(w io.Writer) (int64, error) { func (et *EventTuple) WriteTo(w io.Writer) (int64, error) {
a := acc(w) a := acc(w)
a.write8(uint8(et.EventCode)) a.write8(uint8(et.EventCode))
if et.EventCode == EventCodeNull {
// null tuple
return a.ret()
}
a.write16(uint16(et.RangeStart)) a.write16(uint16(et.RangeStart))
if !et.Extended { if !et.Extended {
// non-extended tuple // non-extended tuple
@ -211,12 +218,18 @@ func (et *EventTuple) WriteTo(w io.Writer) (int64, error) {
} }
func parseEventTuple(p []byte) (EventTuple, []byte, error) { func parseEventTuple(p []byte) (EventTuple, []byte, error) {
if len(p) < 4 { if len(p) < 1 {
return EventTuple{}, p, fmt.Errorf("insufficient input length %d for network event tuple", len(p)) return EventTuple{}, p, fmt.Errorf("insufficient input length %d for any network event tuple", len(p))
} }
var et EventTuple var et EventTuple
et.EventCode = EventCode(p[0]) et.EventCode = EventCode(p[0])
if et.EventCode == EventCodeNull {
return et, p[1:], nil
}
if len(p) < 4 {
return EventTuple{}, p, fmt.Errorf("insufficient input length %d for non-Null network event tuple", len(p))
}
et.RangeStart = ddp.Network(binary.BigEndian.Uint16(p[1:3])) et.RangeStart = ddp.Network(binary.BigEndian.Uint16(p[1:3]))
et.RangeEnd = et.RangeStart et.RangeEnd = et.RangeStart
et.Distance = p[3] et.Distance = p[3]