ASTERIX

ASTERIX (All Purpose STructured EUROCONTROL SuRveillance Information EXchange, EUROCONTROL-SPEC-0149) is the exchange format of air traffic surveillance data: radar plots and tracks, ADS-B reports, system tracks, service and status messages between sensors, trackers and their users. It travels in UDP datagrams, usually multicast, and it is the first protocol of the crate with neither a magic nor a port: no IANA assignment (Wireshark proposes 8600, the maintainer's radars use 8611 and 8612, the samples of Croatia Control 21111 to 22135 and 10001), and nothing in the first bytes names the protocol. What it has is structure, and the crate recognizes it by that alone: a blind probe of the dispatch table, rule(ApplicationProtocol::Asterix, Guard::Udp, ProbeId::Asterix), placed before DNS, whose datagram form is less constrained.

Eleven categories are decoded, eight of them since 11.6.0 (#119, defense lot #125). On a real surveillance network they mix in the same datagrams, radar plots next to the service messages of the same radar, system tracks next to the status of the tracker that computes them: up to 18 data blocks in one datagram of the corpus. Module: packet_parser::parse::application::protocols::asterix, with one submodule catNNN per category, uap for the tables and fields for the types several categories share.

CATContentUAP editionFRNs (FSPEC octets)Typed decoder
001monoradar plots and tracks, the generation before CAT 0481.4, two UAPs21 for plots (3), 22 for tracks (4)cat001::TargetReport
002monoradar service messages, the generation before CAT 0341.214 (2)cat002::ServiceMessage
008monoradar derived weather information1.314 (2)cat008::WeatherMessage
021ADS-B target reports, editions 2.x2.749 (7)cat021::TargetReport
023CNS/ATM ground station and service status1.314 (2)cat023::StatusReport
034monoradar service messages1.2914 (2)cat034::ServiceMessage
048monoradar target reports1.3228 (4)cat048::TargetReport
062SDPS system tracks1.2135 (5)cat062::SystemTrack
063sensor status1.714 (2)cat063::SensorStatus
065SDPS service status1.614 (2)cat065::ServiceStatus
247version number exchange1.37 (1)cat247::VersionReport

The editions are the ones tshark 4.6.6 uses by default. The tables of the eight new categories are derived from the machine-readable definitions of zoranbosnjak/asterix-specs (BSD-3) and cross-checked with tshark on the corpus. A UAP is stable from one edition to the next, apart from the number of extents of a few extended items (I021/090, I062/080), and the table keeps the latest edition, which accepts the earlier ones. uap::uap_for(category) returns the UAP of a category, uap::SUPPORTED_CATEGORIES lists the eleven.

The datagram: data blocks

The 66-byte UDP payload of frame 3 of cat_034_048.pcap: a CAT 048 data block of 55 bytes then a CAT 034 block of 11, each with its category byte, two length bytes, FSPEC and items; the two lengths add up to the payload exactly and the flow is labelled ASTERIX; a block of category 62, a length one byte too long or a trailing byte each fail the whole datagram

A datagram is one or more data blocks back to back. Each starts with one byte of category (CAT) and two bytes of length (LEN, header included), then holds one or more records. AsterixPacket::try_from reads blocks until the payload is consumed, and the lengths must add up to the payload exactly: a block shorter than 5 bytes or longer than what is left is AsterixError::InvalidBlockLength, fewer than 3 bytes left after a block is Truncated, and a category without a UAP is UnsupportedCategory. The datagram drawn above is frame 3 of cat_034_048.pcap: a CAT 048 track report followed by the CAT 034 sector-crossing message of the same radar (SAC 25, SIC 13), 55 + 11 = 66 bytes.

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct AsterixPacket<'a> {
    pub blocks: Vec<AsterixDataBlock<'a>>,
}

#[non_exhaustive]
pub struct AsterixDataBlock<'a> {
    pub category: u8,
    pub length: u16,
    pub records: Vec<AsterixRecord<'a>>,
}
}

The record: FSPEC, then the UAP

The 52-byte record of the first data block: the FSPEC ff ff 02 expanded bit by bit, each bit a Field Reference Number, then the fifteen items in FRN order with their format and the values cat048::TargetReport decodes

A record starts with its FSPEC, a run of octets in which each of the seven high bits announces one data item and the low bit, FX, announces one more FSPEC octet. Counting the octets from 0 and the seven bits of each from 1, high bit first, bit n of octet k is Field Reference Number 7k + n. Which item an FRN names, and in what format, is fixed by the UAP of the category (uap.rs), and the number of its FRNs bounds the FSPEC: 28 FRNs for CAT 048, so at most 4 FSPEC octets (the table above gives the others). An FSPEC longer than the UAP allows, or one whose FX never drops, is FspecOverflow; an FSPEC that announces no item, only FX bits or none at all, is EmptyRecord. The FRNs the specification leaves undefined are None in the table, and a bit set on one of them is UnknownFrn: FRNs 16 to 19 of the CAT 001 plots, 12 of CAT 002 and of CAT 063, 43 to 47 of CAT 021, 10 to 12 of CAT 023, 2 and 29 to 33 of CAT 062, 8 to 12 of CAT 065, 5 of CAT 247.

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct Uap {
    pub category: u8,
    pub items: &'static [Option<ItemSpec>],   // items[frn - 1], None for a reserved FRN
    pub selector: Option<UapSelector>,        // when the category has several UAPs
}

#[non_exhaustive]
pub struct ItemSpec {
    pub id: &'static str,   // "I048/010"
    pub format: ItemFormat,
}
}

Uap::item(frn) and Uap::item_by_id("I048/240") look an item up, and Uap::max_fspec_octets() is the bound above. The items are then cut in FRN order, each one a zero-copy slice:

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct AsterixRecord<'a> {
    pub category: u8,
    pub fspec: &'a [u8],
    pub items: Vec<AsterixItem<'a>>,
}

#[non_exhaustive]
pub struct AsterixItem<'a> {
    pub frn: u8,
    pub id: &'static str,   // "I048/240"; "I048/SP", "I048/RE", "I001/RFS"
    pub data: &'a [u8],
}
}

record.item("I048/240") returns the bytes of an item, record.has(..) says whether it is there, and record.uap() returns the UAP that cut the record. A record ends where its last item ends, and the next record starts right there, until the block is consumed: the record of the drawing is the whole 52-byte body of its block.

CAT 001: two UAPs, chosen by the record

CAT 001 has one UAP for plots and one for tracks. They agree on FRNs 1 and 2 and part from FRN 3: I001/040 for a plot, I001/161 for a track. uap::CAT001, the UAP uap_for(1) returns, is the plot one, and the only UAP of the crate with a selector:

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub enum UapSelector {
    Cat001TargetType,   // TYP, bit 8 of the first octet of I001/020: 0 plot, 1 track
}
}

The selector reads TYP in I001/020 (FRN 2), right after the FSPEC, or after the two bytes of I001/010 when FRN 1 is set. TYP = 1 selects uap::CAT001_TRACK, whose selector is None; without I001/020 the record is a plot, as for tshark. The bound on the FSPEC follows the UAP chosen, 3 octets for a plot and 4 for a track, and record.uap() tells which one cut the record (tested):

#![allow(unused)]
fn main() {
    #[test]
    fn a_cat001_record_picks_its_uap_from_its_own_bytes() {
        // Frame 772 of wireshark_8579_radardata.pcap: CAT 001, 002 and 001
        // blocks in one datagram.
        let raw = hex::decode(RADARDATA_FRAME_772_HEX).unwrap();
        let flow = parse(LinkType::ETHERNET, &raw).unwrap();
        assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "ASTERIX");
        let packet =
            AsterixPacket::try_from(flow.transport.as_ref().unwrap().payload.unwrap()).unwrap();
        let categories: Vec<u8> = packet.blocks.iter().map(|block| block.category).collect();
        assert_eq!(categories, [1, 2, 1]);

        // TYP = 0 in I001/020: a plot, cut with the plot UAP, the one whose
        // selector would have switched to the track UAP.
        let plot = &packet.blocks[0].records[0];
        assert_eq!(plot.uap(), Some(&uap::CAT001));
        assert_eq!(uap::CAT001.selector, Some(UapSelector::Cat001TargetType));
        let report = cat001::TargetReport::from_record(plot).unwrap();
        let descriptor = report.descriptor.unwrap();
        assert!(!descriptor.track);
        assert_eq!(descriptor.detection, cat001::RadarDetection::Combined);
        let position = report.polar_position.unwrap(); // LSB 1/128 NM in CAT 001
        assert_eq!((position.range_nm(), position.azimuth_deg()), (57.25, 239.150_390_625));
        assert_eq!(report.mode_3a.unwrap().to_string(), "0304");
        assert_eq!(report.flight_level.unwrap().flight_level(), 350.0);
        assert_eq!(report.truncated_time_of_day_seconds(), Some(440.484_375));
        assert!(!report.mode_2.unwrap().validated); // military IFF, not validated
        assert!(report.track_number.is_none()); // a track item

        // The CAT 002 block in between: the north marker of the same radar.
        let marker = cat002::ServiceMessage::from_record(&packet.blocks[1].records[0]).unwrap();
        assert_eq!(marker.message_type, Some(cat002::MessageType::NorthMarker));
        assert_eq!(marker.antenna_rotation_period_seconds(), Some(9.304_687_5));
        let counts: Vec<(u8, u16)> = marker
            .plot_counts
            .iter()
            .map(|count| (count.identification, count.counter))
            .collect();
        assert_eq!(counts, [(1, 29), (2, 19), (3, 35)]); // primary, SSR, combined

        // The last block: a simulated test target, I001/020 in two extents.
        let test_target = &packet.blocks[2].records[0];
        assert_eq!(test_target.item("I001/020"), Some(&[0x51, 0x80][..]));
        assert_eq!(uap::CAT001.item(2).unwrap().format, ItemFormat::Extended(&[1, 1]));
    }
}

The corpus has no CAT 001 track: all its CAT 001 records are plots, and the track UAP is covered by the crate's unit tests, on synthetic bytes.

Eight item formats

The UAP names the format, and the format says where the item ends. The crate cuts along these rules and nothing else: at this level an item is bytes, not a value.

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub enum ItemFormat {
    Fixed(usize),
    Variable,
    Repetitive(usize),
    Compound(&'static [Subfield]),
    Explicit,
    Extended(&'static [usize]),
    RepetitiveFx(usize),
    Rfs,
}
}

The drawing shows five of them on real bytes:

Five item formats on real bytes: a fixed item whose length comes from the UAP, a variable item that extends while the FX bit is set, a repetitive item that starts with a count, a compound item whose primary subfield says which subfields follow, and an explicit item that starts with its own length

  • Fixed(n): n bytes, from the UAP.
  • Variable: one-byte extents chained by their FX bit, like the FSPEC, as many as the record sets: I048/030, I001/130, I002/050. Every I002/050 of the corpus takes two octets, 0b 02 in frame 30453 of wireshark_8579_radardata.pcap, where tshark 4.6.6 stops after one (see the oracle).
  • Extended(extents): the bounded form, the extended items of the specification. Each extent has a known size and ends with its FX bit, and the FX of the last defined extent must be 0: a record that sets it announces more than the UAP describes, ExtentOverflow. I048/020 has six extents of one byte: one octet in the drawing of the record (a0, FX 0), two in the 39 short datagrams of the anonymized capture (01 60, frame 5 for one). I062/080 has seven, I023/101 a first extent of two bytes then one of one, I008/100 a single extent of three. The extended items of CAT 021 and 048 (I021/040, I021/090, I021/271, I048/020, I048/170, and the TIS subfield of I021/110) were unbounded Variable items before 11.6.0, and the drawings of this page, drawn before 11.6.0, still label I048/020 and I048/170 that way; the bytes and the lengths they show are unchanged.
  • Repetitive(n): a one-byte count REP, then REP × n bytes. I048/250 carries the Mode S BDS registers, 8 bytes each.
  • RepetitiveFx(n): units of n bytes, the FX bit ending the last byte of each unit, as many as the record sets: I062/510, three bytes per unit. Variable is the case n = 1.
  • Compound: a primary subfield, itself FX-chained, whose bits say which subfields follow. Each subfield is a Subfield: Fixed, Variable, Repetitive, Extended or RepetitiveFx, and a bit set on a Subfield::Spare, or beyond the list, is UnknownSubfield. I048/130 here announces its first three subfields, one byte each; in frame 1 of the anonymized capture the same item is a single 00, a compound with no subfield at all, as in 50 of its 203 records (the other 153 carry 08 df, the PAM subfield alone). record.subfield("I048/130", 1) returns the bytes of a subfield by its number in the specification, from 1, and None when the item is absent, not compound, or without that subfield.
  • Explicit: the first byte is the length, itself included; zero is InvalidExplicitLength. The RE and SP items of every category are explicit, which is how a record can carry data the UAP does not describe.
  • Rfs: Random Field Sequencing, next section.

An item that needs more bytes than its block holds is TruncatedItem, with the item's id in the error.

Random Field Sequencing

CAT 001, 002 and 008 reserve an FRN, 21 in both UAPs of CAT 001 and 14 in CAT 002 and 008, for a Random Field Sequencing field (Part 1, edition 1.0 of 1997, §4.3.5.3; the current edition 3.1 no longer describes it): one byte N, then N data items, each introduced by its own FRN on one byte, in any order. The crate cuts the field as one item, I001/RFS, I002/RFS or I008/RFS, whose bytes start with N, and checks the items it carries against the same UAP: N = 0 is EmptyRfs; an FRN that names no item, or the RFS itself, is InvalidRfsFrn; an item the FSPEC already announces, or that the field carries twice, is DuplicateRfsFrn, since a record carries each data item once. record.rfs_items() returns the items of the field in wire order, each with its FRN and id, and record.item(id) looks through the field: an item is found wherever the record put it. No capture of the corpus carries an RFS field. The crate's unit tests cover it on synthetic bytes, and the fuzz target checks that every RFS item it decodes is the one record.item returns.

Typed values, on demand

The cut is the contract; the decoding is separate, per category, and only runs when asked for. Each catNNN module has a type whose from_record turns the usual items of a record into typed fields, every one an Option, or a Vec for the entries of I002/070 and I247/550, and returns None for a record of another category:

DecoderItems it reads
cat001::TargetReport010, 020 (TargetReportDescriptor: plot or track, simulated, RadarDetection...), 040, 070, 090, 141 (truncated time of day), 050 (Mode 2); for a track, 161, 042, 200 and 170
cat002::ServiceMessage010, 000 (MessageType), 020 (sector), 030, 041 (antenna rotation period), 070 (PlotCounts), 090 (collimation error), 100 (dynamic window)
cat008::WeatherMessage010, 000, 020 (vector qualifier), the vector counts of 034, 036 and 038, 040 (contour identifier), 050 (number of contour points), 090, 100 (processing status), 110, 120
cat021::TargetReport010, 040, 161, 015, 071, 130, 131, 080, 073, 075, 077, 140, 210, 070, 145, 160, 170, 020, 132
cat023::StatusReport010, 000 (ReportType), 015 (service type and identification), 070, 100 (ground station status), 101 (service configuration), 200 (operational range), 110 (service status), 120 (number of statistics)
cat034::ServiceMessage010, 000 (MessageType), 030, 020 (sector), 041
cat048::TargetReport010, 140, 020, 040, 070, 090, 220, 240, 161, 042, 200, 170, 110, 250 (number of BDS registers), and since 11.6.0 050 and 055, the military IFF Mode 2 and Mode 1 codes
cat062::SystemTrack010, 015, 070, 105 (WGS-84), 100, 185, 210, 060, 245, the ADR, ID and MHG subfields of 380, 040, 080, 200, 136, 130, 135, 220, 120 (Mode 2), and the sensor, position, Mode C and Mode 3/A of the last measurement, 340
cat063::SensorStatus010, 015, 030, 050 (the sensor), 060 (connection and NOGO per technology), 070 (time stamping bias), 080, 081, 090, 091, 092 (range gains and biases, azimuth and elevation biases)
cat065::ServiceStatus010, 000 (MessageType), 015, 030, 020 (batch number), 040 (SDPS configuration and status), 050
cat247::VersionReport010, 015, 140, 550 (a CategoryVersion per category announced)

The values keep what the wire carries and give their conversion beside it, never in its place: TimeOfDay::seconds() (LSB 1/128 s), FlightLevel::flight_level() (LSB 1/4 FL), range_nm() and azimuth_deg() on the polar positions (LSB 1/256 NM in CAT 048, 1/128 NM in CAT 001), latitude_deg() and longitude_deg() on the WGS-84 positions. Mode3ACode, and since 11.6.0 Mode2Code and Mode1Code, display in octal, AircraftAddress in hexadecimal, AircraftIdentification from its 6-bit ICAO characters (a code outside the alphabet is rendered ?). On the datagram drawn above, the CAT 048 record and the CAT 034 one (tested):

#![allow(unused)]
fn main() {
    #[test]
    fn typed_values_are_decoded_on_demand() {
        // Frame 3 of cat_034_048.pcap: a CAT 048 block, then a CAT 034 block.
        let raw = hex::decode(CAT_034_048_FRAME_3_HEX).unwrap();
        let flow = parse(LinkType::ETHERNET, &raw).unwrap();
        assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "ASTERIX");
        let payload = flow.transport.as_ref().unwrap().payload.unwrap();
        let packet = AsterixPacket::try_from(payload).unwrap();
        let shape: Vec<(u8, u16, usize)> = packet
            .blocks
            .iter()
            .map(|block| (block.category, block.length, block.records.len()))
            .collect();
        assert_eq!(shape, [(48, 55, 1), (34, 11, 1)]);

        let record = &packet.blocks[0].records[0];
        let report = cat048::TargetReport::from_record(record).expect("a CAT 048 record");
        let position = report.polar_position.unwrap();
        let velocity = report.polar_velocity.unwrap();
        let line = format!(
            "{} ({}) at {:.2} NM / {:.2}°, FL {}, {:.0} kt heading {:.1}°",
            report.aircraft_identification.unwrap().trimmed(),
            report.aircraft_address.unwrap(),
            position.range_nm(),
            position.azimuth_deg(),
            report.flight_level.unwrap().flight_level(),
            velocity.ground_speed_kt(),
            velocity.heading_deg()
        );
        assert_eq!(line, "THY9TX (4BAACD) at 194.82 NM / 128.76°, FL 360, 457 kt heading 263.6°");

        // Each decoder reads its own category only.
        assert!(cat034::ServiceMessage::from_record(record).is_none());
        let service = cat034::ServiceMessage::from_record(&packet.blocks[1].records[0]).unwrap();
        assert_eq!(service.message_type, Some(cat034::MessageType::SectorCrossing));
        assert_eq!(service.sector_azimuth_deg(), Some(135.0));

        // I048/020 is extended: one octet here (a0, FX = 0), six at most.
        assert_eq!(record.item("I048/020"), Some(&[0xa0][..]));
        let spec = record.uap().unwrap().item_by_id("I048/020").unwrap();
        assert_eq!(spec.format, ItemFormat::Extended(&[1, 1, 1, 1, 1, 1]));
    }
}

A system track of CAT 062 and the end of batch that follows it in the same datagram (tested):

#![allow(unused)]
fn main() {
    #[test]
    fn a_system_track_and_the_subfields_its_decoder_skips() {
        // Frame 1 of cat_062_065.pcap: two CAT 062 tracks, then a CAT 065
        // end of batch.
        let raw = hex::decode(CAT_062_065_FRAME_1_HEX).unwrap();
        let flow = parse(LinkType::ETHERNET, &raw).unwrap();
        assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "ASTERIX");
        let packet =
            AsterixPacket::try_from(flow.transport.as_ref().unwrap().payload.unwrap()).unwrap();
        let shape: Vec<(u8, usize)> = packet
            .blocks
            .iter()
            .map(|block| (block.category, block.records.len()))
            .collect();
        assert_eq!(shape, [(62, 2), (65, 1)]);

        let record = &packet.blocks[0].records[0];
        let track = cat062::SystemTrack::from_record(record).unwrap();
        assert_eq!(track.track_number, Some(4713));
        assert_eq!(track.aircraft_identification.unwrap().trimmed(), "RYR174C"); // I062/380 ID
        assert_eq!(track.aircraft_address.unwrap().to_string(), "4CA7A8"); // I062/380 ADR
        let position = track.wgs84_position.unwrap();
        assert_eq!(
            (position.latitude_deg(), position.longitude_deg()),
            (41.167_123_317_718_506, 15.708_866_715_431_213)
        );
        assert_eq!(track.barometric_altitude.unwrap().flight_level(), 390.0);
        let measured = track.measured_information.unwrap(); // I062/340, the last sensor
        assert_eq!(measured.sensor.map(|sensor| (sensor.sac, sensor.sic)), Some((25, 12)));
        assert_eq!(measured.range_nm(), Some(147.726_562_5));

        // I062/080 is extended: four of its seven extents here.
        assert_eq!(record.item("I062/080"), Some(&[0x19, 0x03, 0x01, 0x08][..]));
        // A compound item, by subfield number: PSR age, 23 x 1/4 s; no TRK age.
        assert_eq!(record.subfield("I062/290", 2), Some(&[0x17][..]));
        assert_eq!(record.subfield("I062/290", 1), None);

        let end = cat065::ServiceStatus::from_record(&packet.blocks[1].records[0]).unwrap();
        assert_eq!(end.message_type, Some(cat065::MessageType::EndOfBatch));
        assert_eq!(end.batch_number, Some(1));
    }
}

And the status categories: an ADS-B ground station (CAT 023) and the editions it announces (CAT 247), a sensor seen by the tracker (CAT 063), the start of a weather picture (CAT 008) (tested):

#![allow(unused)]
fn main() {
    #[test]
    fn status_of_a_ground_station_a_sensor_and_a_weather_picture() {
        /// The UDP payload of an Ethernet frame the pipeline labels ASTERIX.
        fn datagram(raw: &[u8]) -> &[u8] {
            let flow = parse(LinkType::ETHERNET, raw).unwrap();
            assert_eq!(flow.application.unwrap().protocol.label(), "ASTERIX");
            flow.transport.unwrap().payload.unwrap()
        }

        // Frame 1 of wireshark_9953_cat021023.pcap: CAT 023, an ADS-B service.
        let raw = hex::decode(CAT021023_FRAME_1_HEX).unwrap();
        let packet = AsterixPacket::try_from(datagram(&raw)).unwrap();
        let report = cat023::StatusReport::from_record(&packet.blocks[0].records[0]).unwrap();
        assert_eq!(report.report_type, Some(cat023::ReportType::ServiceStatus));
        let service_type = report.service.map(|service| service.service_type);
        assert_eq!(service_type, Some(2)); // ADS-B Ext Squitter
        assert_eq!(report.service_status, Some(4)); // normal
        assert_eq!(report.operational_range_nm, Some(255));

        // Frame 428 of the same stream: CAT 247, the editions it sends.
        let raw = hex::decode(CAT021023_FRAME_428_HEX).unwrap();
        let packet = AsterixPacket::try_from(datagram(&raw)).unwrap();
        let versions = cat247::VersionReport::from_record(&packet.blocks[0].records[0]).unwrap();
        let editions: Vec<(u8, u8, u8)> = versions
            .category_versions
            .iter()
            .map(|version| (version.category, version.main_version, version.sub_version))
            .collect();
        assert_eq!(editions, [(21, 0, 26), (23, 1, 20)]);

        // Frame 1105 of wireshark_8579_radardata.pcap: CAT 063, a sensor
        // operational but NOGO on every technology.
        let raw = hex::decode(RADARDATA_FRAME_1105_HEX).unwrap();
        let packet = AsterixPacket::try_from(datagram(&raw)).unwrap();
        let status = cat063::SensorStatus::from_record(&packet.blocks[0].records[0]).unwrap();
        assert_eq!(status.sensor.map(|sensor| (sensor.sac, sensor.sic)), Some((34, 26)));
        let configuration = status.configuration.unwrap();
        assert_eq!(configuration.connection, cat063::ConnectionStatus::Operational);
        assert!(configuration.psr_nogo && configuration.ssr_nogo && configuration.mode_s_nogo);
        assert!(configuration.ads_nogo && configuration.mlt_nogo);
        assert_eq!(status.time_stamping_bias_ms, Some(75));

        // Frame 1 of wireshark_9239_cat008only-2.pcap: CAT 008, the start of
        // a weather picture.
        let raw = hex::decode(CAT008ONLY_2_FRAME_1_HEX).unwrap();
        let packet = AsterixPacket::try_from(datagram(&raw)).unwrap();
        let weather = cat008::WeatherMessage::from_record(&packet.blocks[0].records[0]).unwrap();
        assert_eq!(weather.message_type, Some(cat008::MessageType::StartOfPicture));
        assert_eq!(weather.processing_status.map(|status| status.scaling_factor), Some(6));
    }
}

What a typed decoder does not read stays reachable through record.item(..), and record.subfield(..) for a compound item: the BDS registers of I048/250, the subfields of I048/130 or I062/290, the vectors of a CAT 008 picture, the RE of a CAT 021 report.

The probe: all or nothing

ProbeId::Asterix is AsterixPacket::try_from(payload).is_ok(): the label is given only when the whole datagram cuts cleanly, every block of a decoded category, every record filling its block to the last byte. CAT + LEN alone would match far too many datagrams; the structure of a block of an unknown category cannot be checked, so one such block refuses the whole datagram rather than labelling half of it.

The drawing of the datagram shows three refusals. The CAT 034 block relabelled CAT 062 is still refused, but since 11.6.0 by the CAT 062 UAP, which reads its FSPEC f0 and finds FRN 2 reserved (UnknownFrn), not as the UnsupportedCategory(62) the drawing still shows; relabelled CAT 010, a category without a UAP, it is UnsupportedCategory. A length one byte too long makes the extra byte open a record whose first item is truncated; one trailing byte is Truncated, 3 bytes needed. Zero padding inside a block fails the same way: a record of 00 is EmptyRecord. A refused datagram goes on to the next rules of the table and usually comes out "Unknown", with nothing in corrupted. All five, on the bytes of the drawing (tested):

#![allow(unused)]
fn main() {
    #[test]
    fn one_failure_anywhere_and_the_datagram_is_not_asterix() {
        // The 66-byte payload of frame 3 of cat_034_048.pcap, after Ethernet,
        // IPv4 and UDP; its second block, CAT 034, starts at byte 55.
        let raw = hex::decode(CAT_034_048_FRAME_3_HEX).unwrap();
        let datagram = &raw[14 + 20 + 8..];
        assert!(AsterixPacket::try_from(datagram).is_ok());
        let second = 55;

        // Relabelled CAT 062, a decoded category: its UAP reads the FSPEC f0,
        // and FRN 2 is reserved in CAT 062.
        let mut relabelled = datagram.to_vec();
        relabelled[second] = 62;
        assert_eq!(
            AsterixPacket::try_from(relabelled.as_slice()),
            Err(AsterixError::UnknownFrn { category: 62, frn: 2 })
        );
        // Relabelled CAT 010, which has no UAP in the crate.
        relabelled[second] = 10;
        assert_eq!(
            AsterixPacket::try_from(relabelled.as_slice()),
            Err(AsterixError::UnsupportedCategory(10))
        );
        // LEN 56 for the first block: the extra byte opens a record.
        let mut longer = datagram.to_vec();
        longer[2] = 56;
        assert_eq!(
            AsterixPacket::try_from(longer.as_slice()),
            Err(AsterixError::TruncatedItem { item: "I048/020", needed: 1, actual: 0 })
        );
        // One trailing byte cannot hold a block header.
        let mut trailing = datagram.to_vec();
        trailing.push(0);
        assert_eq!(
            AsterixPacket::try_from(trailing.as_slice()),
            Err(AsterixError::Truncated { needed: 3, actual: 1 })
        );
        // Zero padding inside a block: a record of 00 announces no item.
        let mut padded = datagram.to_vec();
        padded[second + 2] += 1;
        padded.push(0);
        assert_eq!(
            AsterixPacket::try_from(padded.as_slice()),
            Err(AsterixError::EmptyRecord { category: 34 })
        );

        // In the frame, the refused datagram goes on to the next rules.
        let mut frame = raw.clone();
        frame[14 + 20 + 8 + second] = 10;
        let flow = parse(LinkType::ETHERNET, &frame).unwrap();
        assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "Unknown");
        assert!(flow.corrupted.is_none());
    }
}

Since 11.7.0 the probe reads the whole datagram (#184). Until 11.6.0 it saw only the first PROBE_CAP bytes, 18 KiB. Blocks that ended exactly at byte 18 432 were then enough, whatever came after, and a valid datagram with a block across that bound stayed "Unknown". The label now vouches for the datagram that AsterixPacket::try_from decodes. The cost stays linear: decoding 65 505 bytes of ASTERIX records takes 2.7 ms in release, 3.8 times the cost of the 18 432 bytes of the old cap. Surveillance datagrams are far smaller, 1 393 bytes at most in the corpus.

Verified against tshark, record by record

tests/asterix_golden.rs replays the nine files of pcaps_exemple/protocols/asterix/ (see its SOURCE.md):

  • the maintainer's anonymized CAT 048 capture, 203 datagrams on ports 8611 and 8612;
  • three samples of Croatia Control: a CAT 034/048 capture (100 datagrams, 20 of them with two blocks), a CAT 062/065 datagram, and a raw stream of two CAT 021 blocks (cat21_re.ast);
  • five attachments of the Wireshark tracker: wireshark_8579_radardata.pcap, 50 000 frames of a control network with 41 928 ASTERIX datagrams on 18 multicast streams; wireshark_9953_cat021023.pcap, 1 588 datagrams of CAT 021, 023 and 247; wireshark_9624_cat62_re.pcap, 668 datagrams of CAT 062 with their Reserved Expansion Field, some followed by a CAT 065 block; and the two CAT 008 captures of issue 9239, 9 and 10 datagrams.

For every record of every frame, the sequence of items and the length of the record are compared with an oracle produced from tshark 4.6.6 -V by tools/asterix_oracle.py, frames of the same shape grouped (tests/data/asterix_tshark_oracle.tsv): 44 411 frames in parity, which carry

CAT001002008021023034048062063065247
data blocks29 52019 512671 555307 9137 4847 7593071 5263
records65 14619 677671 555307 91333 66516 4343311 5263

The 96 other frames of radardata.pcap are those where tshark stumbles (tests/data/asterix_tshark_limits.tsv). Each carries a CAT 002 record whose I002/050 takes two octets: tshark reads one, then takes the other for a parasite record read beyond the block (26 frames) or declares the frame malformed (70). The crate follows the specification and decodes these records whole; the test expects the shape tshark gives without the parasite record, I002/050 counting for two octets. On frame 30453 (tested):

#![allow(unused)]
fn main() {
    #[test]
    fn where_tshark_stops_the_specification_goes_on() {
        // Frame 30453 of wireshark_8579_radardata.pcap: a CAT 002 north marker
        // whose I002/050 takes two octets. tshark 4.6.6 reads one and calls
        // the frame malformed.
        let raw = hex::decode(RADARDATA_FRAME_30453_HEX).unwrap();
        let flow = parse(LinkType::ETHERNET, &raw).unwrap();
        assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "ASTERIX");
        let packet =
            AsterixPacket::try_from(flow.transport.as_ref().unwrap().payload.unwrap()).unwrap();
        let record = &packet.blocks[0].records[0];
        let ids: Vec<&str> = record.items.iter().map(|item| item.id).collect();
        assert_eq!(ids, ["I002/010", "I002/000", "I002/030", "I002/050"]);
        assert_eq!(record.item("I002/050"), Some(&[0x0b, 0x02][..])); // FX 1, then FX 0
        assert_eq!(uap::CAT002.item(6).unwrap().format, ItemFormat::Variable);
    }
}

The typed values are cross-checked with the -V output of tshark on real frames, in the unit tests of the catNNN modules and, for CAT 048 and 034, in the golden test. The golden histogram of pcaps_exemple/protocols/ (tests/golden_pcaps.rs) counts 44 507 ASTERIX datagrams, exactly the 44 411 and the 96 of the two oracle files: no frame of another folder comes out ASTERIX. The eight new categories moved that count from 13 739, and "Unknown" from 40 177 to 9 409: the 30 047 datagrams of radardata.pcap that carry one of them, and 721 in the other captures (668 of cat62_re, 33 CAT 023/247 datagrams of cat021023, 19 of the two CAT 008 captures, 1 of cat_062_065).

The fuzz target parse_asterix covers the eleven categories and both UAPs of CAT 001. On whatever it decodes, it asserts that the block lengths add up to the input, that every record has an item and a UAP, that every item and every RFS item is non-empty and lies inside the buffer, that subfields 1 to 4 of an item, when present, lie inside it too, that the FSPECs and items of a block cover its length exactly, and it runs every typed decoder on every record.

Validations

All of them are AsterixError, in packet_parser::errors::application::asterix; the last four are new in 11.6.0:

ErrorWhen
Truncated { needed, actual }fewer than 3 bytes where a data block must start
InvalidBlockLength { category, declared, available }a LEN below 5, or beyond the bytes left
UnsupportedCategory(u8)a category without a UAP
EmptyRecord { category }an FSPEC that announces no item
FspecOverflow { category }an FSPEC longer than the UAP allows, or whose FX never drops
UnknownFrn { category, frn }an FSPEC bit on a reserved FRN, or beyond the UAP
UnknownSubfield { item, subfield }a primary subfield bit on a spare subfield, or beyond the item
InvalidExplicitLength { item }an explicit item (RE, SP) of length 0
TruncatedItem { item, needed, actual }an item, a subfield or an RFS item that overflows its block
BlockNotConsumed { category }declared, never returned (see below)
ExtentOverflow { item }an extended item whose last defined extent sets FX
EmptyRfs { category }an RFS field that announces no item
InvalidRfsFrn { category, frn }an RFS item whose FRN names no item of the UAP, or the RFS itself
DuplicateRfsFrn { category, frn }an RFS item the FSPEC already announces, or that the field carries twice

What is left out

The limits every protocol shares, reassembly and checksums among them, are on the Limits page. The ones below are this protocol's own.

  • Other categories. A category outside the eleven, CAT 010, 019, 020 or 240 for instance, is UnsupportedCategory and refuses its datagram; the corpus has no capture of them.
  • Other editions. One UAP per category, from the latest edition, which accepts the earlier ones. The CAT 021 editions 0.2x, whose UAP is entirely different from 2.x, are not described: nothing in the bytes tells the editions apart, and the crate decodes 2.x. The CAT 247 reports of cat021023.pcap announce CAT 021 edition 0.26, yet its 1 555 CAT 021 records cut cleanly along the 2.x UAP, for the crate as for tshark.
  • Item contents beyond the typed subset: RE and SP stay opaque, BDS registers and weather vectors are raw bytes, the subfields of a compound item are cut and reachable by number, most of them not interpreted.
  • CAT 001 tracks and RFS fields occur in no capture of the corpus: only the crate's unit tests, on synthetic bytes, decode them.
  • AsterixError::BlockNotConsumed is declared but never returned: its one return, for a block without a record, cannot be reached, since a block of at least 5 bytes always yields a record or a more precise error.