data: PTR/SRV target name, SRV * ['target', 'port'], TXT [key => value], A / AAAA the address */ public static function records(string $packet): array { if (strlen($packet) < 12) { return []; } $h = unpack('nid/nflags/nqd/nan/nns/nar', substr($packet, 0, 12)); if ($h === false || ($h['flags'] & 0x8000) === 0) { return []; // a question, not an answer } $i = 12; try { for ($q = 0; $q < $h['qd']; $q++) { self::readName($packet, $i); $i += 4; } $out = []; $count = $h['an'] + $h['ns'] + $h['ar']; for ($r = 0; $r < $count && $i < strlen($packet); $r++) { $name = self::readName($packet, $i); if ($i + 10 > strlen($packet)) { break; } $rr = unpack('ntype/nclass/Nttl/nlen', substr($packet, $i, 10)); $i += 10; $start = $i; $end = $i + $rr['len']; if ($end > strlen($packet)) { break; } $data = match ($rr['type']) { self::PTR => self::readName($packet, $start), self::SRV => self::srv($packet, $start), self::TXT => self::txt(substr($packet, $start, $rr['len'])), self::A => $rr['len'] === 4 ? (string)inet_ntop(substr($packet, $start, 4)) : '', self::AAAA => $rr['len'] === 16 ? (string)inet_ntop(substr($packet, $start, 16)) : '', default => null, }; $out[] = ['name' => strtolower($name), 'type' => $rr['type'], 'ttl' => $rr['ttl'], 'data' => $data]; $i = $end; } return $out; } catch (\RuntimeException) { return []; } } /** * The Cast devices a packet describes. $from: the address it came from (when no A record is given). * @return list */ public static function castDevices(string $packet, string $from = ''): array { $records = self::records($packet); $instances = []; $srv = []; $txt = []; $addr = []; foreach ($records as $r) { if ($r['type'] === self::PTR && $r['name'] === strtolower(self::CAST_SERVICE)) { $instances[strtolower((string)$r['data'])] = true; } elseif ($r['type'] === self::SRV && is_array($r['data'])) { $srv[$r['name']] = $r['data']; } elseif ($r['type'] === self::TXT && is_array($r['data'])) { $txt[$r['name']] = $r['data']; } elseif ($r['type'] === self::A && $r['data'] !== '') { $addr[$r['name']] = (string)$r['data']; } } // an answer may carry the SRV / TXT without the PTR (a direct question) foreach (array_keys($srv + $txt) as $name) { if (str_ends_with($name, '.' . strtolower(self::CAST_SERVICE))) { $instances[$name] = true; } } $out = []; foreach (array_keys($instances) as $instance) { $instance = strtolower((string)$instance); $t = $txt[$instance] ?? []; $s = $srv[$instance] ?? null; $host = $s !== null ? ($addr[strtolower($s['target'])] ?? $from) : $from; if ($host === '') { continue; } $label = (string)strstr($instance, '.' . strtolower(self::CAST_SERVICE), true); $out[] = [ 'id' => (string)($t['id'] ?? $label), 'name' => (string)($t['fn'] ?? $label), 'model' => (string)($t['md'] ?? 'Chromecast'), 'host' => $host, 'port' => (int)($s['port'] ?? 8009), ]; } return $out; } /** a name, with the pointers of DNS compression (at most 32 jumps: a loop is refused) */ private static function readName(string $p, int &$i): string { $labels = []; $pos = $i; $jumped = false; for ($guard = 0; $guard < 64; $guard++) { if ($pos >= strlen($p)) { throw new \RuntimeException('name out of the packet'); } $len = ord($p[$pos]); if ($len === 0) { if (!$jumped) { $i = $pos + 1; } return implode('.', $labels); } if (($len & 0xC0) === 0xC0) { if ($pos + 1 >= strlen($p)) { throw new \RuntimeException('name out of the packet'); } $target = (($len & 0x3F) << 8) | ord($p[$pos + 1]); if (!$jumped) { $i = $pos + 2; } $jumped = true; $pos = $target; continue; } $labels[] = substr($p, $pos + 1, $len); $pos += 1 + $len; } throw new \RuntimeException('name loop'); } /** @return array{target: string, port: int} */ private static function srv(string $p, int $i): array { $v = unpack('npriority/nweight/nport', substr($p, $i, 6)); $i += 6; return ['target' => self::readName($p, $i), 'port' => (int)($v['port'] ?? 0)]; } /** @return array */ private static function txt(string $data): array { $out = []; $i = 0; while ($i < strlen($data)) { $len = ord($data[$i]); $entry = substr($data, $i + 1, $len); $i += 1 + $len; $eq = strpos($entry, '='); if ($eq !== false) { $out[strtolower(substr($entry, 0, $eq))] = substr($entry, $eq + 1); } elseif ($entry !== '') { $out[strtolower($entry)] = ''; } } return $out; } /** Tests and fakes: an answer as a Cast device sends it (PTR + SRV + TXT + A, names compressed or not). */ public static function answer(string $instance, string $fn, string $model, string $id, string $ip, int $port = 8009): string { $service = self::name(self::CAST_SERVICE); $inst = self::name($instance . '.' . self::CAST_SERVICE); $target = self::name($id . '.local'); $rr = fn (string $name, int $type, int $class, string $data) => $name . pack('nnNn', $type, $class, 120, strlen($data)) . $data; $txt = ''; foreach (['id' => $id, 'md' => $model, 'fn' => $fn, 'ca' => '4101', 'st' => '0'] as $k => $v) { $txt .= chr(strlen("$k=$v")) . "$k=$v"; } $records = [ $rr($service, self::PTR, 1, $inst), $rr($inst, self::TXT, 0x8001, $txt), $rr($inst, self::SRV, 0x8001, pack('nnn', 0, 0, $port) . $target), $rr($target, self::A, 0x8001, (string)inet_pton($ip)), ]; return pack('nnnnnn', 0, 0x8400, 0, 1, 0, 3) . implode('', $records); } }