fromBytes static method

APLResponseRecord fromBytes({
  1. required String name,
  2. required int ttl,
  3. required Uint8List bytes,
  4. required int offset,
  5. required int length,
})

Implementation

static APLResponseRecord fromBytes({
    required String name,
    required int ttl,
    required Uint8List bytes,
    required int offset,
    required int length }) {

    final List<APLPrefix> prefixes = [ ];
    int cursor = offset;

    while (cursor < offset + length) {
        if (cursor + 4 > bytes.length) {
            throw FormatException('Truncated APL entry at offset $cursor');
        }

        final int family = (bytes[cursor] << 8) | bytes[cursor + 1];
        final int prefix = bytes[cursor + 2];
        final int lengthAndNegation = bytes[cursor + 3];
        final bool negation = (lengthAndNegation & 0x80) != 0;
        final int afdLength = lengthAndNegation & 0x7F;
        cursor += 4;

        if (cursor + afdLength > bytes.length) {
            throw FormatException('Invalid AFD length at offset $cursor');
        }

        final Uint8List afdBytes = bytes.sublist(cursor, cursor + afdLength);
        cursor += afdLength;

        final InternetAddress ip = InternetAddress.fromRawAddress(
            _padAddress(afdBytes, family),
            type: family == 1
                ? InternetAddressType.IPv4
                : InternetAddressType.IPv6,
        );

        prefixes.add(APLPrefix(
            family: family,
            prefix: prefix,
            negation: negation,
            ip: ip,
        ));
    }

    return APLResponseRecord(
        name: name,
        ttl: ttl,
        prefixes: prefixes);
}