decodeAddress static method

({InternetAddress address, int port})? decodeAddress(
  1. Uint8List data
)

Decodes an address attribute (MAPPED-ADDRESS, RESPONSE-ORIGIN, OTHER-ADDRESS)

Implementation

static ({InternetAddress address, int port})? decodeAddress(Uint8List data) {
  if (data.length < 8) return null;

  final buffer = ByteData.view(data.buffer, data.offsetInBytes);

  // Skip first byte (reserved) and get address family
  final family = buffer.getUint8(1);
  final port = buffer.getUint16(2);

  // Get IP address
  final addressBytes = data.sublist(4, 4 + (family == 1 ? 4 : 16));

  return (
    address: InternetAddress.fromRawAddress(addressBytes),
    port: port,
  );
}