## Finding games: the small UDP messages for LAN discovery and the list server, kept ## apart from the game protocol (NetProtocol), which runs over ENet on the game port. ## ## Everything is little-endian and starts with u8 type and the 4 bytes "TDWD": ## ## QUERY client -> game server or list server. u16 protocol version, u8 page, ## zero padding up to QUERY_BYTES. ## INFO game server -> client. u16 protocol version, u16 game port, ## u8 people, u8 max players, u8 phase, u8 name length, name. ## REGISTER game server -> list server. u32 token, then the same fields as INFO. ## CHALLENGE list server -> game server. u32 token. ## LIST list server -> client. u8 page, u8 pages, u8 count, then per server: ## u8 address length, address (ASCII), and the same fields as INFO. ## ## No answer is ever bigger than the QUERY that asked for it, so neither kind of ## server can be used to flood a third party with more bytes than it was sent. A ## REGISTER is answered with a CHALLENGE that is smaller still, and a server is only ## listed once it echoes the token, which proves it receives at the address it ## claims to send from. Decoders return an empty Dictionary for anything malformed ## and never raise an error. class_name NetDiscovery extends RefCounted const MAGIC: String = "TDWD" const MSG_QUERY: int = 1 const MSG_INFO: int = 2 const MSG_REGISTER: int = 3 const MSG_CHALLENGE: int = 4 const MSG_LIST: int = 5 ## Every QUERY is padded to this size; every answer fits in it. const QUERY_BYTES: int = 512 ## A game server answers LAN queries on its game port plus this. const DISCOVERY_PORT_OFFSET: int = 1 ## A LAN scan asks the discovery ports of this many game ports, from the default up in ## steps of two, so a few servers on one machine are all found. const LAN_SCAN_SERVERS: int = 8 const DEFAULT_LIST_PORT: int = 24660 const SERVER_NAME_MAX_CHARS: int = 32 const SERVER_NAME_MAX_BYTES: int = 48 const DEFAULT_SERVER_NAME: String = "Tidewall server" ## Longest address text: a full IPv6 address. const ADDRESS_MAX_BYTES: int = 45 const ENTRIES_PER_PAGE: int = 4 const MAX_PAGES: int = 64 const _HEADER: int = 5 ## u16 version, u16 port, u8 people, u8 max, u8 phase, u8 name length. const _INFO_FIXED: int = 8 ## A server's description: {"version", "port", "players", "max_players", "phase", ## "name"}. Everything is clamped to what fits on the wire, and people to places. static func info(port: int, players: int, max_players: int, phase: int, server_name: String, version: int = NetProtocol.VERSION) -> Dictionary: return { "version": clampi(version, 0, 0xFFFF), "port": clampi(port, 0, 0xFFFF), "players": clampi(players, 0, clampi(max_players, 0, 255)), "max_players": clampi(max_players, 0, 255), "phase": clampi(phase, 0, 255), "name": clean_server_name(server_name), } ## A server name fit to show, cleaned like a player name but longer. static func clean_server_name(raw: String) -> String: return NetProtocol.clean_name(raw, SERVER_NAME_MAX_CHARS, SERVER_NAME_MAX_BYTES, DEFAULT_SERVER_NAME) static func encode_query(page: int = 0, version: int = NetProtocol.VERSION) -> PackedByteArray: var out: StreamPeerBuffer = _writer(MSG_QUERY) out.put_u16(version) out.put_u8(clampi(page, 0, 255)) var bytes: PackedByteArray = out.data_array bytes.resize(QUERY_BYTES) return bytes ## {"version", "page"} or empty. Short queries are refused: the padding is what keeps ## answers from being bigger than questions. static func decode_query(bytes: PackedByteArray) -> Dictionary: if bytes.size() != QUERY_BYTES or not _is(bytes, MSG_QUERY): return {} return {"version": bytes.decode_u16(5), "page": bytes[7]} static func encode_info(server: Dictionary) -> PackedByteArray: var out: StreamPeerBuffer = _writer(MSG_INFO) _put_info(out, server) return out.data_array static func decode_info(bytes: PackedByteArray) -> Dictionary: if not _is(bytes, MSG_INFO): return {} var result: Dictionary = _read_info(bytes, _HEADER) if result.is_empty() or int(result["end"]) != bytes.size(): return {} result.erase("end") return result static func encode_register(token: int, server: Dictionary) -> PackedByteArray: var out: StreamPeerBuffer = _writer(MSG_REGISTER) out.put_u32(token & 0xFFFFFFFF) _put_info(out, server) return out.data_array ## {"token", and the INFO fields} or empty. static func decode_register(bytes: PackedByteArray) -> Dictionary: if bytes.size() < _HEADER + 4 or not _is(bytes, MSG_REGISTER): return {} var result: Dictionary = _read_info(bytes, _HEADER + 4) if result.is_empty() or int(result["end"]) != bytes.size(): return {} result.erase("end") result["token"] = bytes.decode_u32(_HEADER) return result static func encode_challenge(token: int) -> PackedByteArray: var out: StreamPeerBuffer = _writer(MSG_CHALLENGE) out.put_u32(token & 0xFFFFFFFF) return out.data_array static func decode_challenge(bytes: PackedByteArray) -> Dictionary: if bytes.size() != _HEADER + 4 or not _is(bytes, MSG_CHALLENGE): return {} return {"token": bytes.decode_u32(_HEADER)} ## One page of the list. Each entry is an INFO Dictionary with "address" added. static func encode_list(page: int, pages: int, entries: Array[Dictionary]) -> PackedByteArray: var out: StreamPeerBuffer = _writer(MSG_LIST) var count: int = mini(entries.size(), ENTRIES_PER_PAGE) out.put_u8(clampi(page, 0, 255)) out.put_u8(clampi(pages, 0, 255)) out.put_u8(count) for index: int in count: var entry: Dictionary = entries[index] var address: PackedByteArray = String(entry.get("address", "")).to_ascii_buffer() if address.size() > ADDRESS_MAX_BYTES: address = PackedByteArray() out.put_u8(address.size()) out.put_data(address) _put_info(out, entry) return out.data_array ## {"page", "pages", "servers": Array[Dictionary]} or empty. static func decode_list(bytes: PackedByteArray) -> Dictionary: if bytes.size() < _HEADER + 3 or bytes.size() > QUERY_BYTES or not _is(bytes, MSG_LIST): return {} var count: int = bytes[_HEADER + 2] if count > ENTRIES_PER_PAGE: return {} var servers: Array[Dictionary] = [] var at: int = _HEADER + 3 for _index: int in count: if at >= bytes.size(): return {} var size: int = bytes[at] if size == 0 or size > ADDRESS_MAX_BYTES or at + 1 + size > bytes.size(): return {} var address: String = _address(bytes.slice(at + 1, at + 1 + size)) if address.is_empty(): return {} var entry: Dictionary = _read_info(bytes, at + 1 + size) if entry.is_empty(): return {} at = int(entry["end"]) entry.erase("end") entry["address"] = address servers.append(entry) if at != bytes.size(): return {} return {"page": bytes[_HEADER], "pages": bytes[_HEADER + 1], "servers": servers} static func _put_info(out: StreamPeerBuffer, server: Dictionary) -> void: var name_bytes: PackedByteArray = clean_server_name(String(server.get("name", ""))).to_utf8_buffer() out.put_u16(clampi(int(server.get("version", 0)), 0, 0xFFFF)) out.put_u16(clampi(int(server.get("port", 0)), 0, 0xFFFF)) out.put_u8(clampi(int(server.get("players", 0)), 0, 255)) out.put_u8(clampi(int(server.get("max_players", 0)), 0, 255)) out.put_u8(clampi(int(server.get("phase", 0)), 0, 255)) out.put_u8(name_bytes.size()) out.put_data(name_bytes) ## The INFO fields starting at `at`, with "end" the index after them, or empty. static func _read_info(bytes: PackedByteArray, at: int) -> Dictionary: if at + _INFO_FIXED > bytes.size(): return {} var name_size: int = bytes[at + 7] if name_size > SERVER_NAME_MAX_BYTES or at + _INFO_FIXED + name_size > bytes.size(): return {} var port: int = bytes.decode_u16(at + 2) var players: int = bytes[at + 4] var max_players: int = bytes[at + 5] if port == 0 or max_players == 0 or players > max_players: return {} var raw: String = NetProtocol._utf8(bytes.slice(at + _INFO_FIXED, at + _INFO_FIXED + name_size)) if name_size > 0 and raw.is_empty(): return {} return { "version": bytes.decode_u16(at), "port": port, "players": players, "max_players": max_players, "phase": bytes[at + 6], "name": clean_server_name(raw), "end": at + _INFO_FIXED + name_size, } ## Address text made only of the characters an IP address uses, or empty. static func _address(bytes: PackedByteArray) -> String: for byte: int in bytes: var digit: bool = byte >= 0x30 and byte <= 0x39 var hex: bool = (byte >= 0x61 and byte <= 0x66) or (byte >= 0x41 and byte <= 0x46) if not (digit or hex or byte == 0x2E or byte == 0x3A): return "" var text: String = bytes.get_string_from_ascii() return text if text.is_valid_ip_address() else "" static func _is(bytes: PackedByteArray, kind: int) -> bool: return bytes.size() >= _HEADER and bytes[0] == kind and bytes.slice(1, 5) == MAGIC.to_ascii_buffer() static func _writer(kind: int) -> StreamPeerBuffer: var out: StreamPeerBuffer = StreamPeerBuffer.new() out.big_endian = false out.put_u8(kind) out.put_data(MAGIC.to_ascii_buffer()) return out