-- -- slippi.lua - Wireshark dissector for Slippi console mirroring traffic -- -- Slippi ("Project Slippi") mirroring lets a Slippi Nintendont console (Wii) -- stream live game data to a desktop client (Slippi Launcher / Dolphin / -- slippi-js / libmelee). The client opens a TCP connection to the console -- (default port 51441; legacy 666; relay instances 53741+) and sends a -- handshake; the console then streams length-prefixed UBJSON messages, each -- wrapping either connection metadata or a chunk of the .slp event byte stream. -- -- Wire format (per message, "normal" mode): -- -- +----------------+---------------------------------------------+ -- | u32be length N | N bytes of UBJSON | -- +----------------+---------------------------------------------+ -- -- The UBJSON object is: { "type": , "payload": { ... } } -- -- type 1 HANDSHAKE payload: nick, nintendontVersion, clientToken[4], -- pos[8] (console -> client) -- payload: cursor[8], clientToken[4], isRealtime -- (client -> console handshake) -- type 2 REPLAY payload: pos[8], nextPos[8], forcePos(bool), -- data[] <- concatenated raw .slp events -- type 3 KEEPALIVE payload: (empty) -- type 4 MENU payload: data[] <- menu snapshot event bytes -- -- "legacy" mode (old Nintendont / relay): no length prefix, the TCP stream is -- just the raw concatenated .slp event byte stream. Detected heuristically. -- -- The .slp event stream is a sequence of events, each: . -- The payload length per command is announced once per game by the Event -- Payloads (0x35) event. When we join mid-game we won't have seen it, so a -- built-in default table (current Slippi replay spec, verified against live -- console traffic) is used until a real 0x35 is observed on that TCP stream. -- -- References: -- https://github.com/project-slippi/slippi-wiki (SPEC.md, SPECTATOR_PROTOCOL.md) -- https://github.com/project-slippi/slippi-js src/node/console/* -- https://github.com/altf4/libmelee melee/slippstream.py -- -- Install: drop this file in your Wireshark "Personal Lua Plugins" directory -- (Help > About Wireshark > Folders), or run: -- tshark -X lua_script:slippi.lua -d tcp.port==51441,slippi ... -- local p_slippi = Proto("slippi", "Slippi Console Mirroring") ---------------------------------------------------------------------- -- Constants / lookup tables ---------------------------------------------------------------------- local COMM_TYPE = { [1] = "HANDSHAKE", [2] = "REPLAY", [3] = "KEEPALIVE", [4] = "MENU", } local SLP_CMD = { [0x10] = "Message Splitter", [0x35] = "Event Payloads", [0x36] = "Game Start", [0x37] = "Pre-Frame Update", [0x38] = "Post-Frame Update", [0x39] = "Game End", [0x3A] = "Frame Start", [0x3B] = "Item Update", [0x3C] = "Frame Bookend", [0x3D] = "Gecko List", } -- short tags for the Info column summary local SLP_CMD_SHORT = { [0x10] = "SPLIT", [0x35] = "PAYLOADS", [0x36] = "START", [0x37] = "PRE", [0x38] = "POST", [0x39] = "END", [0x3A] = "FS", [0x3B] = "ITEM", [0x3C] = "BE", [0x3D] = "GECKO", } -- Default payload sizes (bytes AFTER the command byte). Used only until a real -- Event Payloads (0x35) event is seen on the stream. Values match the current -- Slippi replay spec and were verified byte-for-byte against a live Slippi -- Nintendont console (Frame Start = 12, Pre = 0x42, Post = 0x54, Bookend = 8). local SLP_DEFAULT_SIZE = { [0x10] = 516, -- Message Splitter: 512 data + 2 size + 1 idx + 1 cmd(of split) [0x36] = 760, -- Game Start (>= 3.14.0); real size comes from 0x35 anyway [0x37] = 0x42, -- Pre-Frame Update (66) [0x38] = 0x54, -- Post-Frame Update (84) [0x39] = 6, -- Game End (method + LRAS initiator + 4 placements) [0x3A] = 12, -- Frame Start (frame + seed + scene frame counter) [0x3B] = 44, -- Item Update [0x3C] = 8, -- Frame Bookend (frame + latest finalized frame) -- 0x3D Gecko List is variable-length; deliberately absent so it is only -- ever split using a real 0x35 table (it always follows Game Start, which -- always carries the 0x35 event). } local STAGE = { [2]="Fountain of Dreams", [3]="Pokemon Stadium", [4]="Princess Peach's Castle", [5]="Kongo Jungle", [6]="Brinstar", [7]="Corneria", [8]="Yoshi's Story", [9]="Onett", [10]="Mute City", [11]="Rainbow Cruise", [12]="Jungle Japes", [13]="Great Bay", [14]="Hyrule Temple", [15]="Brinstar Depths", [16]="Yoshi's Island", [17]="Green Greens", [18]="Fourside", [19]="Mushroom Kingdom I", [20]="Mushroom Kingdom II", [22]="Venom", [23]="Poke Floats", [24]="Big Blue", [25]="Icicle Mountain", [26]="Icetop", [27]="Flat Zone", [28]="Dream Land N64", [29]="Yoshi's Island N64", [30]="Kongo Jungle N64", [31]="Battlefield", [32]="Final Destination", } local CHAR_EXT = { [0]="Captain Falcon", [1]="Donkey Kong", [2]="Fox", [3]="Mr. Game & Watch", [4]="Kirby", [5]="Bowser", [6]="Link", [7]="Luigi", [8]="Mario", [9]="Marth", [10]="Mewtwo", [11]="Ness", [12]="Peach", [13]="Pikachu", [14]="Ice Climbers", [15]="Jigglypuff", [16]="Samus", [17]="Yoshi", [18]="Zelda", [19]="Sheik", [20]="Falco", [21]="Young Link", [22]="Dr. Mario", [23]="Roy", [24]="Pichu", [25]="Ganondorf", [26]="Master Hand", [27]="Wireframe (M)", [28]="Wireframe (F)", [29]="Giga Bowser", [30]="Crazy Hand", [31]="Sandbag", [32]="Popo", } local PLAYER_TYPE = { [0]="Human", [1]="CPU", [2]="Demo", [3]="Empty" } local GAME_END_METHOD = { [0]="Unresolved", [1]="TIME!", [2]="GAME!", [3]="Resolved", [7]="No Contest (LRAS)", } ---------------------------------------------------------------------- -- Protocol fields ---------------------------------------------------------------------- local f = p_slippi.fields f.msg_len = ProtoField.uint32("slippi.msg_len", "Message Length", base.DEC) f.msg_type = ProtoField.uint8 ("slippi.type", "Message Type", base.DEC, COMM_TYPE) f.direction = ProtoField.string("slippi.direction", "Direction") f.legacy = ProtoField.bool ("slippi.legacy", "Legacy raw stream (no length prefix)") -- UBJSON generic rendering f.ub_key = ProtoField.string("slippi.ubjson.key", "Key") f.ub_num = ProtoField.int64 ("slippi.ubjson.num", "Number") f.ub_str = ProtoField.string("slippi.ubjson.str", "String") f.ub_bool = ProtoField.bool ("slippi.ubjson.bool", "Bool") f.ub_bytes = ProtoField.bytes ("slippi.ubjson.bytes", "Bytes") -- Named payload fields (handshake / replay) f.nick = ProtoField.string("slippi.nick", "Console Nickname") f.version = ProtoField.string("slippi.nintendont_version", "Nintendont Version") f.client_tok = ProtoField.bytes ("slippi.client_token", "Client Token") f.pos = ProtoField.bytes ("slippi.pos", "Cursor / Position") f.next_pos = ProtoField.bytes ("slippi.next_pos", "Next Position") f.force_pos = ProtoField.bool ("slippi.force_pos", "Force Position (overflow)") f.cursor_req = ProtoField.bytes ("slippi.cursor", "Requested Cursor") f.is_realtime = ProtoField.bool ("slippi.is_realtime", "Is Realtime") f.data_len = ProtoField.uint32("slippi.data_len", "SLP Data Length", base.DEC) -- SLP event stream f.slp = ProtoField.bytes ("slippi.slp", "SLP Event Stream") f.slp_event = ProtoField.bytes ("slippi.slp.event", "SLP Event") f.slp_cmd = ProtoField.uint8 ("slippi.slp.command", "Command", base.HEX, SLP_CMD) f.slp_size = ProtoField.uint16("slippi.slp.payload_size", "Payload Size", base.DEC) f.slp_raw = ProtoField.bytes ("slippi.slp.raw", "Undissected bytes") f.slp_frame = ProtoField.int32 ("slippi.slp.frame", "Frame", base.DEC) f.slp_final_frame= ProtoField.int32 ("slippi.slp.latest_finalized_frame", "Latest Finalized Frame", base.DEC) f.slp_scene_fc = ProtoField.uint32("slippi.slp.scene_frame_counter", "Scene Frame Counter", base.DEC) f.slp_seed = ProtoField.uint32("slippi.slp.rng_seed", "RNG Seed", base.HEX) f.slp_pidx = ProtoField.uint8 ("slippi.slp.player_index", "Player Index", base.DEC) f.slp_follower = ProtoField.uint8 ("slippi.slp.is_follower", "Is Follower", base.DEC) f.slp_action = ProtoField.uint16("slippi.slp.action_state", "Action State ID", base.DEC) f.slp_intchar = ProtoField.uint8 ("slippi.slp.internal_char", "Internal Character ID", base.DEC) f.slp_posx = ProtoField.float ("slippi.slp.pos_x", "Position X") f.slp_posy = ProtoField.float ("slippi.slp.pos_y", "Position Y") f.slp_facing = ProtoField.float ("slippi.slp.facing", "Facing") f.slp_percent = ProtoField.float ("slippi.slp.percent", "Percent") f.slp_shield = ProtoField.float ("slippi.slp.shield", "Shield Size") f.slp_stocks = ProtoField.uint8 ("slippi.slp.stocks", "Stocks Remaining", base.DEC) f.slp_version = ProtoField.string("slippi.slp.replay_version", "Replay Format Version") f.slp_stage = ProtoField.uint16("slippi.slp.stage", "Stage ID", base.DEC, STAGE) f.slp_ext_char = ProtoField.uint8 ("slippi.slp.external_char", "External Character ID", base.DEC, CHAR_EXT) f.slp_ptype = ProtoField.uint8 ("slippi.slp.player_type", "Player Type", base.DEC, PLAYER_TYPE) f.slp_disp_name = ProtoField.string("slippi.slp.display_name", "Display Name") f.slp_conn_code = ProtoField.string("slippi.slp.connect_code", "Connect Code") f.slp_end_method = ProtoField.uint8("slippi.slp.game_end_method", "Game End Method", base.DEC, GAME_END_METHOD) f.slp_lras = ProtoField.int8 ("slippi.slp.lras_initiator", "LRAS Initiator", base.DEC) f.slp_item_type = ProtoField.uint16("slippi.slp.item_type", "Item Type ID", base.DEC) f.slp_item_id = ProtoField.uint32("slippi.slp.item_spawn_id", "Item Spawn ID", base.DEC) f.slp_placement = ProtoField.int8 ("slippi.slp.placement", "Placement", base.DEC) f.slp_split_cmd = ProtoField.uint8("slippi.slp.split_command", "Wrapped Command", base.HEX, SLP_CMD) f.slp_split_size = ProtoField.uint16("slippi.slp.split_size", "Chunk Data Size", base.DEC) f.slp_split_last = ProtoField.bool ("slippi.slp.split_last", "Last Chunk") ---------------------------------------------------------------------- -- Per-TCP-stream state: learned Event Payloads (0x35) size tables ---------------------------------------------------------------------- local tcp_stream_f = Field.new("tcp.stream") local learned_sizes = {} -- [stream_id] = { [cmd] = payload_size } local legacy_stream = {} -- [stream_id] = true (no length prefix) local function stream_id() local s = tcp_stream_f() if s == nil then return -1 end return tonumber(s.value) or -1 end local function size_for(sid, cmd) local t = learned_sizes[sid] if t and t[cmd] ~= nil then return t[cmd], true end return SLP_DEFAULT_SIZE[cmd], false end ---------------------------------------------------------------------- -- UBJSON reader ---------------------------------------------------------------------- -- Read an integer whose type marker is at `off`. Returns value, total_size. -- Handles i U I l L. Returns nil if the marker is not an int marker. local function ub_read_int(tvb, off) local m = tvb(off, 1):uint() if m == 0x69 then return tvb(off+1,1):int(), 2 -- 'i' int8 elseif m == 0x55 then return tvb(off+1,1):uint(), 2 -- 'U' uint8 elseif m == 0x49 then return tvb(off+1,2):int(), 3 -- 'I' int16 elseif m == 0x6C then return tvb(off+1,4):int(), 5 -- 'l' int32 elseif m == 0x4C then return tvb(off+1,8):int64(), 9 -- 'L' int64 end return nil end -- forward decls local ub_parse_value, ub_parse_array, ub_parse_array_bytes, ub_array_byte_span -- Parse an object (marker '{' already consumed at caller). `off` points at the -- first member key marker (or '}'). Returns next_offset. -- `on_member(key, value_marker, value_off, tree)` is called per member and may -- return true to indicate it fully handled (dissected) that member's value. local function ub_parse_object(tvb, off, tree, on_member) while true do local m = tvb(off, 1):uint() if m == 0x7D then -- '}' return off + 1 end -- object key: bare int-length + bytes (no 'S' marker) local klen, ksz = ub_read_int(tvb, off) if klen == nil then tree:add_expert_info(PI_MALFORMED, PI_ERROR, "bad UBJSON key marker") return off + 1 end klen = tonumber(klen) local key = tvb(off + ksz, klen):string() local koff = off off = off + ksz + klen local vmarker = tvb(off, 1):uint() local handled = false if on_member then handled = on_member(key, vmarker, off, tree, koff, ksz + klen) end if handled then off = handled -- callback returns next offset else local kt = tree:add(f.ub_key, tvb(koff, ksz + klen), key) off = ub_parse_value(tvb, off, kt) end end end -- Parse a value whose marker is at `off`. Returns next_offset. ub_parse_value = function(tvb, off, tree) local m = tvb(off, 1):uint() if m == 0x5A then tree:append_text(" = null"); return off + 1 -- 'Z' elseif m == 0x4E then return off + 1 -- 'N' no-op elseif m == 0x54 then tree:add(f.ub_bool, tvb(off,1), true); return off + 1 -- 'T' elseif m == 0x46 then tree:add(f.ub_bool, tvb(off,1), false); return off + 1 -- 'F' elseif m == 0x69 or m == 0x55 or m == 0x49 or m == 0x6C or m == 0x4C then local v, sz = ub_read_int(tvb, off) tree:add(f.ub_num, tvb(off, sz), tonumber(v) or 0) return off + sz elseif m == 0x64 then -- 'd' float32 tree:add(f.ub_num, tvb(off+1, 4), 0):set_text(string.format("float32: %g", tvb(off+1,4):float())) return off + 5 elseif m == 0x44 then -- 'D' float64 tree:add(f.ub_num, tvb(off+1, 8), 0):set_text(string.format("float64: %g", tvb(off+1,8):float())) return off + 9 elseif m == 0x43 then -- 'C' char tree:add(f.ub_str, tvb(off+1, 1)) return off + 2 elseif m == 0x53 or m == 0x48 then -- 'S' string / 'H' high-prec local slen, ssz = ub_read_int(tvb, off + 1) slen = tonumber(slen) tree:add(f.ub_str, tvb(off + 1 + ssz, slen)) return off + 1 + ssz + slen elseif m == 0x5B then -- '[' array return ub_parse_array(tvb, off + 1, tree) elseif m == 0x7B then -- '{' object return ub_parse_object(tvb, off + 1, tree, nil) end tree:add_expert_info(PI_UNDECODED, PI_WARN, string.format("unknown UBJSON marker 0x%02X", m)) return off + 1 end -- Parse an array body (marker '[' already consumed). Handles the optimized -- form [ $ # . Returns next_offset. ub_parse_array = function(tvb, off, tree) if tvb(off, 1):uint() == 0x24 then -- '$' typed local etype = tvb(off + 1, 1):uint() -- next must be '#' local count, csz = ub_read_int(tvb, off + 3) -- off+2 is '#' count = tonumber(count) or 0 local body = off + 3 + csz if etype == 0x55 or etype == 0x69 then -- byte array if count > 0 then tree:add(f.ub_bytes, tvb(body, count)) else tree:append_text(" = ") end return body + count end -- other typed arrays: element size lookup local esz = ({ [0x49]=2, [0x6C]=4, [0x4C]=8, [0x64]=4, [0x44]=8 })[etype] or 1 tree:add(f.ub_bytes, tvb(body, count * esz)):append_text( string.format(" (%d x type 0x%02X)", count, etype)) return body + count * esz end -- unoptimized: values until ']' while tvb(off, 1):uint() ~= 0x5D do off = ub_parse_value(tvb, off, tree) end return off + 1 end ---------------------------------------------------------------------- -- SLP event stream dissection ---------------------------------------------------------------------- local function add_replay_version(tree, tvb, off) local s = string.format("%d.%d.%d.%d", tvb(off,1):uint(), tvb(off+1,1):uint(), tvb(off+2,1):uint(), tvb(off+3,1):uint()) tree:add(f.slp_version, tvb(off, 4), s) return s end -- Dissect a single SLP event's payload. `off` points at the command byte. local function dissect_slp_event(tvb, off, cmd, evlen, tree) local plen = evlen - 1 local function has(n) return plen >= n end if cmd == 0x35 then -- Event Payloads: byte 1 = size (3n+1); then n entries of {u8 cmd, u16 size} local n = math.floor((tvb(off+1,1):uint() - 1) / 3) for i = 0, n - 1 do local e = off + 2 + i * 3 local ec = tvb(e, 1):uint() local es = tvb(e + 1, 2):uint() tree:add(f.slp_cmd, tvb(e, 1), ec) tree:add(f.slp_size, tvb(e + 1, 2), es):append_text( SLP_CMD[ec] and (" (" .. SLP_CMD[ec] .. ")") or "") end elseif cmd == 0x36 then local v = add_replay_version(tree, tvb, off + 1) if has(0x14) then tree:add(f.slp_stage, tvb(off + 0x13, 2)) end for p = 0, 3 do local cbase = off + 0x65 + 0x24 * p if has(0x66 + 0x24 * p) then local pt = tree:add(p_slippi, tvb(cbase, 2), "Player " .. p) pt:add(f.slp_ext_char, tvb(cbase, 1)) pt:add(f.slp_ptype, tvb(cbase + 1, 1)) end end -- Display names (3.9.0+): 0x1A5 + 31*p, connect codes: 0x221 + 10*p for p = 0, 3 do local no = off + 1 + 0x1A5 + 31 * p if has(0x1A5 + 31 * p + 31) then tree:add(f.slp_disp_name, tvb(no, 31)) end local co = off + 1 + 0x221 + 10 * p if has(0x221 + 10 * p + 10) then tree:add(f.slp_conn_code, tvb(co, 10)) end end elseif cmd == 0x37 then -- Pre-Frame Update tree:add(f.slp_frame, tvb(off + 1, 4)) tree:add(f.slp_pidx, tvb(off + 5, 1)) tree:add(f.slp_follower,tvb(off + 6, 1)) tree:add(f.slp_seed, tvb(off + 7, 4)) if has(13) then tree:add(f.slp_action, tvb(off + 11, 2)) end if has(17) then tree:add(f.slp_posx, tvb(off + 13, 4)) end if has(21) then tree:add(f.slp_posy, tvb(off + 17, 4)) end if has(25) then tree:add(f.slp_facing, tvb(off + 21, 4)) end if has(64) then tree:add(f.slp_percent,tvb(off + 60, 4)) end elseif cmd == 0x38 then -- Post-Frame Update tree:add(f.slp_frame, tvb(off + 1, 4)) tree:add(f.slp_pidx, tvb(off + 5, 1)) tree:add(f.slp_follower,tvb(off + 6, 1)) tree:add(f.slp_intchar, tvb(off + 7, 1)) tree:add(f.slp_action, tvb(off + 8, 2)) if has(14) then tree:add(f.slp_posx, tvb(off + 10, 4)) end if has(18) then tree:add(f.slp_posy, tvb(off + 14, 4)) end if has(22) then tree:add(f.slp_facing, tvb(off + 18, 4)) end if has(26) then tree:add(f.slp_percent,tvb(off + 22, 4)) end if has(30) then tree:add(f.slp_shield, tvb(off + 26, 4)) end if has(34) then tree:add(f.slp_stocks, tvb(off + 33, 1)) end elseif cmd == 0x10 then -- Message Splitter -- payload: data[512] | u16 chunk size | u8 wrapped command | u8 last if has(516) then tree:add(f.slp_split_size, tvb(off + 513, 2)) tree:add(f.slp_split_cmd, tvb(off + 515, 1)) tree:add(f.slp_split_last, tvb(off + 516, 1)) end elseif cmd == 0x39 then -- Game End tree:add(f.slp_end_method, tvb(off + 1, 1)) if has(2) then tree:add(f.slp_lras, tvb(off + 2, 1)) end if has(6) then for p = 0, 3 do tree:add(f.slp_placement, tvb(off + 3 + p, 1)):append_text(" (player " .. p .. ")") end end elseif cmd == 0x3A then -- Frame Start tree:add(f.slp_frame, tvb(off + 1, 4)) tree:add(f.slp_seed, tvb(off + 5, 4)) if has(12) then tree:add(f.slp_scene_fc, tvb(off + 9, 4)) end elseif cmd == 0x3B then -- Item Update tree:add(f.slp_frame, tvb(off + 1, 4)) tree:add(f.slp_item_type, tvb(off + 5, 2)) if has(37) then tree:add(f.slp_item_id, tvb(off + 33, 4)) end elseif cmd == 0x3C then -- Frame Bookend tree:add(f.slp_frame, tvb(off + 1, 4)) tree:add(f.slp_final_frame, tvb(off + 5, 4)) end end -- Walk a concatenated SLP event byte range [off, off+len). Learns the 0x35 -- size table into learned_sizes[sid]. Returns an array of short tags and the -- min/max frame numbers seen (or nil). local function dissect_slp_stream(tvb, off, len, tree, sid, pinfo) local st = tree:add(f.slp, tvb(off, len)) local pos, endp = off, off + len local tags, fmin, fmax = {}, nil, nil while pos < endp do local cmd = tvb(pos, 1):uint() local psize, learned if cmd == 0x35 then local total = tvb(pos + 1, 1):uint() -- 3n+1 psize = total -- learn the table before we render, so later events in this same -- blob use the right sizes local n = math.floor((total - 1) / 3) learned_sizes[sid] = learned_sizes[sid] or {} for i = 0, n - 1 do local e = pos + 2 + i * 3 learned_sizes[sid][tvb(e, 1):uint()] = tvb(e + 1, 2):uint() end else psize, learned = size_for(sid, cmd) end if psize == nil then st:add(f.slp_raw, tvb(pos, endp - pos)):append_text( string.format(" (unknown command 0x%02X, stopped)", cmd)) st:add_expert_info(PI_UNDECODED, PI_NOTE, string.format("unknown SLP command 0x%02X; no size table yet", cmd)) break end local evlen = 1 + psize if pos + evlen > endp then st:add(f.slp_raw, tvb(pos, endp - pos)):append_text( string.format(" (truncated %s event, want %d have %d)", SLP_CMD[cmd] or "?", evlen, endp - pos)) break end local ev = st:add(f.slp_event, tvb(pos, evlen)) ev:set_text(string.format("%s (0x%02X) - %d bytes%s", SLP_CMD[cmd] or "Unknown", cmd, evlen, (not learned and cmd ~= 0x35) and " [default size]" or "")) ev:add(f.slp_cmd, tvb(pos, 1)) dissect_slp_event(tvb, pos, cmd, evlen, ev) tags[#tags + 1] = SLP_CMD_SHORT[cmd] or string.format("%02X", cmd) if cmd == 0x37 or cmd == 0x38 or cmd == 0x3A or cmd == 0x3C or cmd == 0x3B then local fr = tvb(pos + 1, 4):int() if fmin == nil or fr < fmin then fmin = fr end if fmax == nil or fr > fmax then fmax = fr end end pos = pos + evlen end return tags, fmin, fmax end ---------------------------------------------------------------------- -- One length-prefixed message ---------------------------------------------------------------------- -- The message body is dissected by one function that has `tvb` captured in -- scope, so the per-member callbacks below can read string/array values. local function dissect_message(tvb, pinfo, root, off, total, to_console) local sid = stream_id() local mt = root:add(p_slippi, tvb(off, total)) mt:add(f.msg_len, tvb(off, 4)) local body = off + 4 local blen = total - 4 mt:add(f.direction, tvb(off, 1), to_console and "client -> console" or "console -> client"):set_generated(true) if tvb(body, 1):uint() ~= 0x7B then mt:add(f.slp_raw, tvb(body, blen)):append_text(" (not a UBJSON object)") return "?" end -- We know the shape: { "type": U, "payload": {...} }. Parse it with the -- generic object walker but intercept the members we care about. local msg_type_val = nil local summary = {} local on_payload_member local function on_top(key, vm, voff, tree) if key == "type" and (vm == 0x55 or vm == 0x69) then local v, sz = ub_read_int(tvb, voff) msg_type_val = tonumber(v) tree:add(f.msg_type, tvb(voff, sz), msg_type_val) return voff + sz elseif key == "payload" and vm == 0x7B then local pt = tree:add(p_slippi, tvb(voff, 1), "Payload") local noff = ub_parse_object(tvb, voff + 1, pt, on_payload_member(pt)) pt:set_len(noff - voff) return noff end return false -- generic end -- payload member handler (needs tvb + summary + sid in scope) on_payload_member = function(pt) return function(key, vm, voff, tree, koff, kfull) local kt = tree:add(f.ub_key, tvb(koff, kfull), key) if key == "nick" and vm == 0x53 then local slen, ssz = ub_read_int(tvb, voff + 1) slen = tonumber(slen) kt:add(f.nick, tvb(voff + 1 + ssz, slen)) summary[#summary+1] = 'nick="' .. tvb(voff + 1 + ssz, slen):string() .. '"' return voff + 1 + ssz + slen elseif key == "nintendontVersion" and vm == 0x53 then local slen, ssz = ub_read_int(tvb, voff + 1) slen = tonumber(slen) kt:add(f.version, tvb(voff + 1 + ssz, slen)) summary[#summary+1] = "v" .. tvb(voff + 1 + ssz, slen):string() return voff + 1 + ssz + slen elseif (key == "clientToken") and vm == 0x5B then local noff = ub_parse_array_bytes(tvb, voff + 1, kt, f.client_tok) return noff elseif key == "pos" and vm == 0x5B then local noff = ub_parse_array_bytes(tvb, voff + 1, kt, f.pos) return noff elseif key == "cursor" and vm == 0x5B then return ub_parse_array_bytes(tvb, voff + 1, kt, f.cursor_req) elseif key == "nextPos" and vm == 0x5B then return ub_parse_array_bytes(tvb, voff + 1, kt, f.next_pos) elseif key == "forcePos" and (vm == 0x54 or vm == 0x46) then kt:add(f.force_pos, tvb(voff, 1), vm == 0x54) if vm == 0x54 then summary[#summary+1] = "forcePos" end return voff + 1 elseif key == "isRealtime" and (vm == 0x54 or vm == 0x46) then kt:add(f.is_realtime, tvb(voff, 1), vm == 0x54) summary[#summary+1] = "realtime=" .. tostring(vm == 0x54) return voff + 1 elseif key == "data" and vm == 0x5B then -- [ $ U # local dstart, dlen, noff = ub_array_byte_span(tvb, voff + 1) kt:add(f.data_len, tvb(voff, dstart - voff), dlen):set_generated(true) if dlen > 0 then local tags, fmin, fmax = dissect_slp_stream(tvb, dstart, dlen, kt, sid, pinfo) if #tags > 0 then if fmin ~= nil then summary[#summary+1] = (fmin == fmax) and ("frame " .. fmin) or string.format("frames %d..%d", fmin, fmax) end summary[#summary+1] = "[" .. table.concat(tags, " ") .. "]" end end return noff end return false -- generic rendering end end local noff = ub_parse_object(tvb, body + 1, mt, on_top) mt:set_len(math.max(total, noff - off)) local tname = COMM_TYPE[msg_type_val] or ("type " .. tostring(msg_type_val)) mt:append_text(": " .. tname) return tname, summary end -- Helpers that need to be visible to the closures above ------------- -- Parse [ $ U # starting at `off` (just after '['), add the -- bytes as `field` under `tree`. Returns next offset. ub_parse_array_bytes = function(tvb, off, tree, field) local dstart, dlen, noff = ub_array_byte_span(tvb, off) if dstart == nil then -- not the optimized byte form; fall back to generic return ub_parse_array(tvb, off, tree) end if dlen > 0 then tree:add(field, tvb(dstart, dlen)) end return noff end -- For $ U # at `off` (just after '['): return -- data_start, data_len, next_offset (or nil if not that shape). ub_array_byte_span = function(tvb, off) if tvb(off, 1):uint() ~= 0x24 then return nil end -- '$' local etype = tvb(off + 1, 1):uint() if tvb(off + 2, 1):uint() ~= 0x23 then return nil end -- '#' local count, csz = ub_read_int(tvb, off + 3) count = tonumber(count) or 0 local esz = (etype == 0x55 or etype == 0x69) and 1 or ({ [0x49]=2, [0x6C]=4, [0x4C]=8, [0x64]=4, [0x44]=8 })[etype] or 1 local body = off + 3 + csz return body, count * esz, body + count * esz end ---------------------------------------------------------------------- -- Legacy raw stream (no length prefix): whole TCP payload is .slp events ---------------------------------------------------------------------- local function dissect_legacy(tvb, pinfo, root) local sid = stream_id() local mt = root:add(p_slippi, tvb(), "Slippi legacy raw SLP stream") mt:add(f.legacy, tvb(0,1), true):set_generated(true) local tags, fmin, fmax = dissect_slp_stream(tvb, 0, tvb:len(), mt, sid, pinfo) local parts = { "SLP" } if fmin ~= nil then parts[#parts+1] = (fmin == fmax) and ("frame " .. fmin) or string.format("frames %d..%d", fmin, fmax) end if #tags > 0 then parts[#parts+1] = "[" .. table.concat(tags, " ") .. "]" end pinfo.cols.info:set(table.concat(parts, " ")) end ---------------------------------------------------------------------- -- Framing / desegmentation ---------------------------------------------------------------------- local SANE_MAX = 8 * 1024 * 1024 local function looks_like_prefixed(tvb, off) if tvb:len() - off < 6 then return nil end -- need len + '{' + 'i' local n = tvb(off, 4):uint() if n < 2 or n > SANE_MAX then return false end -- UBJSON object starting with an "i"-length key ("type") return tvb(off + 4, 1):uint() == 0x7B and tvb(off + 5, 1):uint() == 0x69 end function p_slippi.dissector(tvb, pinfo, tree) local n = tvb:len() if n == 0 then return 0 end pinfo.cols.protocol = "Slippi" local sid = stream_id() local to_console = (pinfo.dst_port == 51441 or pinfo.dst_port == 666 or (pinfo.dst_port >= 53741 and pinfo.dst_port <= 53750)) -- Legacy stream? decide once per TCP stream, from the first bytes we see. if legacy_stream[sid] then dissect_legacy(tvb, pinfo, tree) return n end local lp = looks_like_prefixed(tvb, 0) if lp == nil then -- not enough bytes to tell; ask for more pinfo.desegment_offset = 0 pinfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT return n end if lp == false then -- Heuristic: a raw SLP stream typically begins with Event Payloads -- (0x35) or, mid-join, some known command byte. Treat as legacy. if not to_console and SLP_CMD[tvb(0,1):uint()] ~= nil then legacy_stream[sid] = true dissect_legacy(tvb, pinfo, tree) return n end -- otherwise: not our traffic return 0 end local off = 0 local summaries = {} local last_type = nil while off < n do if n - off < 4 then pinfo.desegment_offset = off pinfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT return n end local msglen = tvb(off, 4):uint() local total = 4 + msglen if msglen < 2 or msglen > SANE_MAX then -- framing lost; bail rather than loop tree:add(p_slippi, tvb(off, n - off), "Slippi (unframed / desync)") return n end if n - off < total then pinfo.desegment_offset = off pinfo.desegment_len = total - (n - off) return n end local tname, summary = dissect_message(tvb, pinfo, tree, off, total, to_console) last_type = tname if summary and #summary > 0 then summaries[#summaries + 1] = table.concat(summary, " ") end off = off + total end local info = (to_console and "-> " or "") .. tostring(last_type) if #summaries > 0 then info = info .. " " .. table.concat(summaries, " | ") end pinfo.cols.info:set(info) return n end ---------------------------------------------------------------------- -- Registration ---------------------------------------------------------------------- local tcp_port = DissectorTable.get("tcp.port") tcp_port:add(51441, p_slippi) -- default Wii direct tcp_port:add(666, p_slippi) -- legacy Nintendont for p = 53741, 53744 do tcp_port:add(p, p_slippi) end -- relay instances -- Heuristic: catch Slippi on any TCP port (e.g. custom relay). local function heur(tvb, pinfo, tree) if tvb:len() < 13 then return false end local n = tvb(0, 4):uint() if n < 8 or n > SANE_MAX then return false end -- { i 04 "type" U ... if tvb(4, 1):uint() ~= 0x7B then return false end if tvb(5, 1):uint() ~= 0x69 then return false end if tvb(6, 1):uint() ~= 0x04 then return false end if tvb(7, 4):string() ~= "type" then return false end pinfo.conversation = p_slippi p_slippi.dissector(tvb, pinfo, tree) return true end p_slippi:register_heuristic("tcp", heur)