fromBytes static method

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

Implementation

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

    final int end = offset + length;
    if (end > bytes.length) {
        throw FormatException('HIP record overflow');
    }

    final int hitLength = bytes[offset];
    final int algorithm = bytes[offset + 1];
    final int pkLength = (bytes[offset + 2] << 8) | bytes[offset + 3];

    final int posHit = offset + 4;
    final int posPK = posHit + hitLength;
    final int posRendezvous = posPK + pkLength;

    if (posRendezvous > end) {
        throw FormatException('HIP record truncated');
    }

    final hit = bytes.sublist(posHit, posPK);
    final publicKey = bytes.sublist(posPK, posRendezvous);

    // Parse rendezvous servers
    final List<String> servers = [];
    int ptr = posRendezvous;
    while (ptr < end) {
        late String server;
        (ptr, server) = DNSHelper.parseDomainName(bytes, ptr);
        servers.add(server);
    }

    return HIPResponseRecord(
        name: name,
        ttl: ttl,
        hitLength: hitLength,
        algorithm: algorithm,
        pkLength: pkLength,
        hit: hit,
        publicKey: publicKey,
        rendezvousServers: servers);
}