This commit is contained in:
Josh Deprez 2024-03-15 16:38:02 +11:00
parent 5bb3435dc9
commit 78c19bd166
No known key found for this signature in database
2 changed files with 47 additions and 21 deletions

View file

@ -259,8 +259,10 @@ func (ot *OptionTuple) WriteTo(w io.Writer) (int64, error) {
} }
a := acc(w) a := acc(w)
a.write8(uint8(len(ot.Data) + 1)) a.write([]byte{
a.write8(uint8(ot.Type)) byte(len(ot.Data) + 1),
byte(ot.Type),
})
a.write(ot.Data) a.write(ot.Data)
return a.ret() return a.ret()
} }
@ -371,6 +373,36 @@ func parseOpenReq(p []byte) (*OpenReqPacket, error) {
}, nil }, nil
} }
// OpenRsp is used to respond to Open-Req.
type OpenRspPacket struct {
*Header
RateOrErrCode int16
Options Options
}
func (p *OpenRspPacket) WriteTo(w io.Writer) (int64, error) {
a := acc(w)
a.writeTo(p.Header)
a.write16(uint16(p.RateOrErrCode))
a.writeTo(p.Options)
return a.ret()
}
func parseOpenRsp(p []byte) (*OpenRspPacket, error) {
if len(p) < 3 {
return nil, fmt.Errorf("insufficient input length %d for Open-Rsp packet", len(p))
}
opts, err := parseOptions(p[2:])
if err != nil {
return nil, err
}
return &OpenRspPacket{
RateOrErrCode: int16(binary.BigEndian.Uint16(p[:2])),
Options: opts,
}, nil
}
// ParsePacket parses the body of a UDP packet for a domain header, and then // ParsePacket parses the body of a UDP packet for a domain header, and then
// based on the packet type, an AURP-Tr header, an AURP routing header, and // based on the packet type, an AURP-Tr header, an AURP routing header, and
// then a particular packet type. // then a particular packet type.
@ -419,6 +451,9 @@ func ParsePacket(p []byte) (Packet, error) {
} }
orsp.Header = h orsp.Header = h
return orsp, nil return orsp, nil
default:
return nil, fmt.Errorf("unknown routing packet command code %d", h.CommandCode)
} }
default: default:

View file

@ -1,13 +1,12 @@
package aurp package aurp
import ( import (
"encoding/binary"
"io" "io"
) )
// wtacc is a helper for io.WriterTo implementations. // wtacc is a helper for io.WriterTo implementations.
// It sacrifices early returns for a shorter syntax. However, it refuses to // It sacrifices early returns for a shorter syntax. However, it won't continue
// continue writing to the destination writer after detecting an error. // writing to the destination writer after detecting an error.
type wtacc struct { type wtacc struct {
w io.Writer w io.Writer
n int64 n int64
@ -21,25 +20,17 @@ func (a *wtacc) ret() (int64, error) {
} }
func (a *wtacc) write8(x uint8) { func (a *wtacc) write8(x uint8) {
if a.err != nil { a.write([]byte{x})
return
}
_, a.err = a.w.Write([]byte{x})
if a.err != nil {
return
}
a.n++
} }
func (a *wtacc) write16(x uint16) { func (a *wtacc) write16(x uint16) {
if a.err != nil { // Could do this with:
return // binary.Write(a.w, binary.BigEndian, x)
} // - but don't wanna
a.err = binary.Write(a.w, binary.BigEndian, x) a.write([]byte{
if a.err != nil { byte(x >> 8),
return byte(x & 0xff),
} })
a.n += 2
} }
func (a *wtacc) write(b []byte) { func (a *wtacc) write(b []byte) {