ParsePacket returns domain header

This commit is contained in:
Josh Deprez 2024-03-24 21:31:11 +11:00
parent ccfbfc31ba
commit 86cd7879b6
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw
2 changed files with 39 additions and 45 deletions

View file

@ -90,17 +90,17 @@ type Packet interface {
// then a particular packet type. // then a particular packet type.
// //
// (This function contains the big switch statement.) // (This function contains the big switch statement.)
func ParsePacket(p []byte) (Packet, error) { func ParsePacket(p []byte) (DomainHeader, Packet, error) {
dh, p, err := ParseDomainHeader(p) dh, p, err := ParseDomainHeader(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
if dh.Version != 1 { if dh.Version != 1 {
return nil, fmt.Errorf("unsupported domain header version %d", dh.Version) return dh, nil, fmt.Errorf("unsupported domain header version %d", dh.Version)
} }
switch dh.PacketType { switch dh.PacketType {
case PacketTypeAppleTalk: case PacketTypeAppleTalk:
return &AppleTalkPacket{ return dh, &AppleTalkPacket{
DomainHeader: dh, DomainHeader: dh,
Data: p, Data: p,
}, nil }, nil
@ -108,12 +108,12 @@ func ParsePacket(p []byte) (Packet, error) {
case PacketTypeRouting: case PacketTypeRouting:
tr, p, err := parseTrHeader(p) tr, p, err := parseTrHeader(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
tr.DomainHeader = dh tr.DomainHeader = dh
h, p, err := parseHeader(p) h, p, err := parseHeader(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
h.TrHeader = tr h.TrHeader = tr
@ -121,137 +121,137 @@ func ParsePacket(p []byte) (Packet, error) {
case CmdCodeOpenReq: case CmdCodeOpenReq:
oreq, err := parseOpenReq(p) oreq, err := parseOpenReq(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
oreq.Header = h oreq.Header = h
return oreq, nil return dh, oreq, nil
case CmdCodeOpenRsp: case CmdCodeOpenRsp:
orsp, err := parseOpenRsp(p) orsp, err := parseOpenRsp(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
orsp.Header = h orsp.Header = h
return orsp, nil return dh, orsp, nil
case CmdCodeRIReq: case CmdCodeRIReq:
return &RIReqPacket{ return dh, &RIReqPacket{
Header: h, Header: h,
}, nil }, nil
case CmdCodeRIRsp: case CmdCodeRIRsp:
rir, err := parseRIRsp(p) rir, err := parseRIRsp(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
rir.Header = h rir.Header = h
return rir, nil return dh, rir, nil
case CmdCodeRIAck: case CmdCodeRIAck:
return &RIAckPacket{ return dh, &RIAckPacket{
Header: h, Header: h,
}, nil }, nil
case CmdCodeRIUpd: case CmdCodeRIUpd:
riu, err := parseRIUpd(p) riu, err := parseRIUpd(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
riu.Header = h riu.Header = h
return riu, nil return dh, riu, nil
case CmdCodeRD: case CmdCodeRD:
rd, err := parseRD(p) rd, err := parseRD(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
rd.Header = h rd.Header = h
return rd, nil return dh, rd, nil
case CmdCodeZoneReq: case CmdCodeZoneReq:
sc, p, err := parseSubcode(p) sc, p, err := parseSubcode(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
switch sc { switch sc {
case SubcodeZoneInfoReq: case SubcodeZoneInfoReq:
zir, err := parseZIReqPacket(p) zir, err := parseZIReqPacket(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
zir.Header = h zir.Header = h
return zir, nil return dh, zir, nil
case SubcodeGetDomainZoneList: case SubcodeGetDomainZoneList:
gdzl, err := parseGDZLReqPacket(p) gdzl, err := parseGDZLReqPacket(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
gdzl.Header = h gdzl.Header = h
return gdzl, nil return dh, gdzl, nil
case SubcodeGetZonesNet: case SubcodeGetZonesNet:
gzn, err := parseGZNReqPacket(p) gzn, err := parseGZNReqPacket(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
gzn.Header = h gzn.Header = h
return gzn, nil return dh, gzn, nil
default: default:
return nil, fmt.Errorf("unknown subcode %d", sc) return dh, nil, fmt.Errorf("unknown subcode %d", sc)
} }
case CmdCodeZoneRsp: case CmdCodeZoneRsp:
sc, p, err := parseSubcode(p) sc, p, err := parseSubcode(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
switch sc { switch sc {
case SubcodeZoneInfoNonExt, SubcodeZoneInfoExt: case SubcodeZoneInfoNonExt, SubcodeZoneInfoExt:
zir, err := parseZIRspPacket(p) zir, err := parseZIRspPacket(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
zir.Header = h zir.Header = h
zir.Subcode = sc // 1 or 2, only known at this layer zir.Subcode = sc // 1 or 2, only known at this layer
return zir, nil return dh, zir, nil
case SubcodeGetDomainZoneList: case SubcodeGetDomainZoneList:
gdzl, err := parseGDZLRspPacket(p) gdzl, err := parseGDZLRspPacket(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
gdzl.Header = h gdzl.Header = h
return gdzl, nil return dh, gdzl, nil
case SubcodeGetZonesNet: case SubcodeGetZonesNet:
gzn, err := parseGZNRspPacket(p) gzn, err := parseGZNRspPacket(p)
if err != nil { if err != nil {
return nil, err return dh, nil, err
} }
gzn.Header = h gzn.Header = h
return gzn, nil return dh, gzn, nil
default: default:
return nil, fmt.Errorf("unknown subcode %d", sc) return dh, nil, fmt.Errorf("unknown subcode %d", sc)
} }
case CmdCodeTickle: case CmdCodeTickle:
return &TicklePacket{ return dh, &TicklePacket{
Header: h, Header: h,
}, nil }, nil
case CmdCodeTickleAck: case CmdCodeTickleAck:
return &TickleAckPacket{ return dh, &TickleAckPacket{
Header: h, Header: h,
}, nil }, nil
default: default:
return nil, fmt.Errorf("unknown routing packet command code %d", h.CommandCode) return dh, nil, fmt.Errorf("unknown routing packet command code %d", h.CommandCode)
} }
default: default:
return nil, fmt.Errorf("unsupported domain header packet type %d", dh.PacketType) return dh, nil, fmt.Errorf("unsupported domain header packet type %d", dh.PacketType)
} }
} }

View file

@ -98,13 +98,7 @@ func main() {
log.Printf("Received packet of length %d from %v", pktlen, raddr) log.Printf("Received packet of length %d from %v", pktlen, raddr)
dh, _, parseErr := aurp.ParseDomainHeader(pb[:pktlen]) dh, pkt, parseErr := aurp.ParsePacket(pb[:pktlen])
if parseErr != nil {
log.Printf("Failed to parse domain header: %v", err)
continue
}
pkt, parseErr := aurp.ParsePacket(pb[:pktlen])
if parseErr != nil { if parseErr != nil {
log.Printf("Failed to parse packet: %v", parseErr) log.Printf("Failed to parse packet: %v", parseErr)
continue continue