minetest.lua 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. -- minetest.lua
  2. -- Packet dissector for the UDP-based Minetest protocol
  3. -- Copy this to $HOME/.wireshark/plugins/
  4. --
  5. -- Minetest-c55
  6. -- Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
  7. --
  8. -- This program is free software; you can redistribute it and/or modify
  9. -- it under the terms of the GNU General Public License as published by
  10. -- the Free Software Foundation; either version 2 of the License, or
  11. -- (at your option) any later version.
  12. --
  13. -- This program is distributed in the hope that it will be useful,
  14. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. -- GNU General Public License for more details.
  17. --
  18. -- You should have received a copy of the GNU General Public License along
  19. -- with this program; if not, write to the Free Software Foundation, Inc.,
  20. -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. --
  22. -- Table of Contents:
  23. -- Part 1: Client command dissectors (TOSERVER_*)
  24. -- Part 2: Server command dissectors (TOCLIENT_*)
  25. -- Part 3: Wrapper protocol subdissectors
  26. -- Part 4: Wrapper protocol main dissector
  27. -- Part 5: Utility functions
  28. --------------------------------------------
  29. -- Part 1 --
  30. -- Client command dissectors (TOSERVER_*) --
  31. --------------------------------------------
  32. minetest_client_commands = {}
  33. minetest_client_obsolete = {}
  34. -- TOSERVER_INIT
  35. do
  36. local f_ser_fmt = ProtoField.uint8("minetest.client.init_ser_version",
  37. "Maximum serialization format version", base.DEC)
  38. local f_player_name = ProtoField.stringz("minetest.client.init_player_name", "Player Name")
  39. local f_password = ProtoField.stringz("minetest.client.init_password", "Password")
  40. local f_version = ProtoField.uint16("minetest.client.init_version", "Version", base.DEC)
  41. minetest_client_commands[0x10] = {
  42. "INIT", -- Command name
  43. 53, -- Minimum message length including code
  44. { f_ser_fmt, -- List of fields [optional]
  45. f_player_name,
  46. f_password,
  47. f_version },
  48. function(buffer, pinfo, tree, t) -- Dissector function [optional]
  49. t:add(f_ser_fmt, buffer(2,1))
  50. t:add(f_player_name, buffer(3,20))
  51. t:add(f_password, buffer(23,28))
  52. t:add(f_version, buffer(51,2))
  53. end
  54. }
  55. end
  56. -- TOSERVER_INIT2
  57. minetest_client_commands[0x11] = { "INIT2", 2 }
  58. -- TOSERVER_GETBLOCK (obsolete)
  59. minetest_client_commands[0x20] = { "GETBLOCK", 2 }
  60. minetest_client_obsolete[0x20] = true
  61. -- TOSERVER_ADDNODE (obsolete)
  62. minetest_client_commands[0x21] = { "ADDNODE", 2 }
  63. minetest_client_obsolete[0x21] = true
  64. -- TOSERVER_REMOVENODE (obsolete)
  65. minetest_client_commands[0x22] = { "REMOVENODE", 2 }
  66. minetest_client_obsolete[0x22] = true
  67. -- TOSERVER_PLAYERPOS
  68. do
  69. local f_x = ProtoField.int32("minetest.client.playerpos_x", "Position X", base.DEC)
  70. local f_y = ProtoField.int32("minetest.client.playerpos_y", "Position Y", base.DEC)
  71. local f_z = ProtoField.int32("minetest.client.playerpos_z", "Position Z", base.DEC)
  72. local f_speed_x = ProtoField.int32("minetest.client.playerpos_speed_x", "Speed X", base.DEC)
  73. local f_speed_y = ProtoField.int32("minetest.client.playerpos_speed_y", "Speed Y", base.DEC)
  74. local f_speed_z = ProtoField.int32("minetest.client.playerpos_speed_z", "Speed Z", base.DEC)
  75. local f_pitch = ProtoField.int32("minetest.client.playerpos_pitch", "Pitch", base.DEC)
  76. local f_yaw = ProtoField.int32("minetest.client.playerpos_yaw", "Yaw", base.DEC)
  77. minetest_client_commands[0x23] = {
  78. "PLAYERPOS", 34,
  79. { f_x, f_y, f_z, f_speed_x, f_speed_y, f_speed_z, f_pitch, f_yaw },
  80. function(buffer, pinfo, tree, t)
  81. t:add(f_x, buffer(2,4))
  82. t:add(f_y, buffer(6,4))
  83. t:add(f_z, buffer(10,4))
  84. t:add(f_speed_x, buffer(14,4))
  85. t:add(f_speed_y, buffer(18,4))
  86. t:add(f_speed_z, buffer(22,4))
  87. t:add(f_pitch, buffer(26,4))
  88. t:add(f_yaw, buffer(30,4))
  89. end
  90. }
  91. end
  92. -- TOSERVER_GOTBLOCKS
  93. do
  94. local f_count = ProtoField.uint8("minetest.client.gotblocks_count", "Count", base.DEC)
  95. local f_block = ProtoField.bytes("minetset.client.gotblocks_block", "Block", base.DEC)
  96. local f_x = ProtoField.int16("minetest.client.gotblocks_x", "Block position X", base.DEC)
  97. local f_y = ProtoField.int16("minetest.client.gotblocks_y", "Block position Y", base.DEC)
  98. local f_z = ProtoField.int16("minetest.client.gotblocks_z", "Block position Z", base.DEC)
  99. minetest_client_commands[0x24] = {
  100. "GOTBLOCKS", 3,
  101. { f_count, f_block, f_x, f_y, f_z },
  102. function(buffer, pinfo, tree, t)
  103. t:add(f_count, buffer(2,1))
  104. local count = buffer(2,1):uint()
  105. if minetest_check_length(buffer, 3 + 6*count, t) then
  106. pinfo.cols.info:append(" * " .. count)
  107. local index
  108. for index = 0, count - 1 do
  109. local pos = 3 + 6*index
  110. local t2 = t:add(f_block, buffer(pos, 6))
  111. t2:set_text("Block, X: " .. buffer(pos, 2):int()
  112. .. ", Y: " .. buffer(pos + 2, 2):int()
  113. .. ", Z: " .. buffer(pos + 4, 2):int())
  114. t2:add(f_x, buffer(pos, 2))
  115. t2:add(f_y, buffer(pos + 2, 2))
  116. t2:add(f_z, buffer(pos + 4, 2))
  117. end
  118. end
  119. end
  120. }
  121. end
  122. -- TOSERVER_DELETEDBLOCKS
  123. -- TODO: Test this
  124. do
  125. local f_count = ProtoField.uint8("minetest.client.deletedblocks_count", "Count", base.DEC)
  126. local f_block = ProtoField.bytes("minetset.client.deletedblocks_block", "Block", base.DEC)
  127. local f_x = ProtoField.int16("minetest.client.deletedblocks_x", "Block position X", base.DEC)
  128. local f_y = ProtoField.int16("minetest.client.deletedblocks_y", "Block position Y", base.DEC)
  129. local f_z = ProtoField.int16("minetest.client.deletedblocks_z", "Block position Z", base.DEC)
  130. minetest_client_commands[0x25] = {
  131. "DELETEDBLOCKS", 3,
  132. { f_count, f_block, f_x, f_y, f_z },
  133. function(buffer, pinfo, tree, t)
  134. t:add(f_count, buffer(2,1))
  135. local count = buffer(2,1):uint()
  136. if minetest_check_length(buffer, 3 + 6*count, t) then
  137. pinfo.cols.info:append(" * " .. count)
  138. local index
  139. for index = 0, count - 1 do
  140. local pos = 3 + 6*index
  141. local t2 = t:add(f_block, buffer(pos, 6))
  142. t2:set_text("Block, X: " .. buffer(pos, 2):int()
  143. .. ", Y: " .. buffer(pos + 2, 2):int()
  144. .. ", Z: " .. buffer(pos + 4, 2):int())
  145. t2:add(f_x, buffer(pos, 2))
  146. t2:add(f_y, buffer(pos + 2, 2))
  147. t2:add(f_z, buffer(pos + 4, 2))
  148. end
  149. end
  150. end
  151. }
  152. end
  153. -- TOSERVER_ADDNODE_FROM_INVENTORY (obsolete)
  154. minetest_client_commands[0x26] = { "ADDNODE_FROM_INVENTORY", 2 }
  155. minetest_client_obsolete[0x26] = true
  156. -- TOSERVER_CLICK_OBJECT
  157. -- TODO: Test this
  158. do
  159. local vs_button = {
  160. [0] = "left",
  161. [1] = "right"
  162. }
  163. local f_button = ProtoField.uint8("minetest.client.click_object_button", "Button", base.DEC, vs_button)
  164. local f_blockpos_x = ProtoField.int16("minetest.client.click_object_blockpos_x", "Block position X", base.DEC)
  165. local f_blockpos_y = ProtoField.int16("minetest.client.click_object_blockpos_y", "Block position Y", base.DEC)
  166. local f_blockpos_z = ProtoField.int16("minetest.client.click_object_blockpos_z", "Block position Z", base.DEC)
  167. local f_id = ProtoField.int16("minetest.client.click_object_id", "ID", base.DEC)
  168. local f_item = ProtoField.uint16("minetest.client.click_object_item", "Item", base.DEC)
  169. minetest_client_commands[0x27] = {
  170. "CLICK_OBJECT", 13,
  171. { f_button, f_blockpos_x, f_blockpos_y, f_blockpos_z, f_id, f_item },
  172. function(buffer, pinfo, tree, t)
  173. t:add(f_button, buffer(2,1))
  174. t:add(f_blockpos_x, buffer(3,2))
  175. t:add(f_blockpos_y, buffer(5,2))
  176. t:add(f_blockpos_z, buffer(7,2))
  177. t:add(f_id, buffer(9,2))
  178. t:add(f_item, buffer(11,2))
  179. end
  180. }
  181. end
  182. -- TOSERVER_GROUND_ACTION
  183. do
  184. local vs_action = {
  185. [0] = "Start digging",
  186. [1] = "Place block",
  187. [2] = "Stop digging",
  188. [3] = "Digging completed"
  189. }
  190. local f_action = ProtoField.uint8("minetest.client.ground_action", "Action", base.DEC, vs_action)
  191. local f_nodepos_undersurface_x = ProtoField.int16(
  192. "minetest.client.ground_action_nodepos_undersurface_x",
  193. "Node position (under surface) X")
  194. local f_nodepos_undersurface_y = ProtoField.int16(
  195. "minetest.client.ground_action_nodepos_undersurface_y",
  196. "Node position (under surface) Y")
  197. local f_nodepos_undersurface_z = ProtoField.int16(
  198. "minetest.client.ground_action_nodepos_undersurface_z",
  199. "Node position (under surface) Z")
  200. local f_nodepos_abovesurface_x = ProtoField.int16(
  201. "minetest.client.ground_action_nodepos_abovesurface_x",
  202. "Node position (above surface) X")
  203. local f_nodepos_abovesurface_y = ProtoField.int16(
  204. "minetest.client.ground_action_nodepos_abovesurface_y",
  205. "Node position (above surface) Y")
  206. local f_nodepos_abovesurface_z = ProtoField.int16(
  207. "minetest.client.ground_action_nodepos_abovesurface_z",
  208. "Node position (above surface) Z")
  209. local f_item = ProtoField.uint16("minetest.client.ground_action_item", "Item")
  210. minetest_client_commands[0x28] = {
  211. "GROUND_ACTION", 17,
  212. { f_action,
  213. f_nodepos_undersurface_x,
  214. f_nodepos_undersurface_y,
  215. f_nodepos_undersurface_z,
  216. f_nodepos_abovesurface_x,
  217. f_nodepos_abovesurface_y,
  218. f_nodepos_abovesurface_z,
  219. f_item },
  220. function(buffer, pinfo, tree, t)
  221. t:add(f_action, buffer(2,1))
  222. t:add(f_nodepos_undersurface_x, buffer(3,2))
  223. t:add(f_nodepos_undersurface_y, buffer(5,2))
  224. t:add(f_nodepos_undersurface_z, buffer(7,2))
  225. t:add(f_nodepos_abovesurface_x, buffer(9,2))
  226. t:add(f_nodepos_abovesurface_y, buffer(11,2))
  227. t:add(f_nodepos_abovesurface_z, buffer(13,2))
  228. t:add(f_item, buffer(15,2))
  229. end
  230. }
  231. end
  232. -- TOSERVER_RELEASE (obsolete)
  233. minetest_client_commands[0x29] = { "RELEASE", 2 }
  234. minetest_client_obsolete[0x29] = true
  235. -- TOSERVER_SIGNTEXT (old signs)
  236. -- TODO: Test this or mark obsolete
  237. do
  238. local f_blockpos_x = ProtoField.int16("minetest.client.signtext_blockpos_x", "Block position X", base.DEC)
  239. local f_blockpos_y = ProtoField.int16("minetest.client.signtext_blockpos_y", "Block position Y", base.DEC)
  240. local f_blockpos_z = ProtoField.int16("minetest.client.signtext_blockpos_z", "Block position Z", base.DEC)
  241. local f_id = ProtoField.int16("minetest.client.signtext_id", "ID", base.DEC)
  242. local f_textlen = ProtoField.uint16("minetest.client.signtext_textlen", "Text length", base.DEC)
  243. local f_text = ProtoField.string("minetest.client.signtext_text", "Text")
  244. minetest_client_commands[0x30] = {
  245. "SIGNTEXT", 12,
  246. { f_blockpos_x, f_blockpos_y, f_blockpos_z, f_id, f_textlen, f_text },
  247. function(buffer, pinfo, tree, t)
  248. t:add(f_blockpos_x, buffer(2,2))
  249. t:add(f_blockpos_y, buffer(4,2))
  250. t:add(f_blockpos_z, buffer(6,2))
  251. t:add(f_id, buffer(8,2))
  252. t:add(f_textlen, buffer(10,2))
  253. local textlen = buffer(10,2):uint()
  254. if minetest_check_length(buffer, 12 + textlen, t) then
  255. t:add(f_text, buffer, buffer(12,textlen))
  256. end
  257. end
  258. }
  259. end
  260. -- TOSERVER_INVENTORY_ACTION
  261. do
  262. local f_action = ProtoField.string("minetest.client.inventory_action", "Action")
  263. minetest_client_commands[0x31] = {
  264. "INVENTORY_ACTION", 2,
  265. { f_action },
  266. function(buffer, pinfo, tree, t)
  267. t:add(f_action, buffer(2, buffer:len() - 2))
  268. end
  269. }
  270. end
  271. -- TOSERVER_CHAT_MESSAGE
  272. do
  273. local f_length = ProtoField.uint16("minetest.client.chat_message_length", "Length", base.DEC)
  274. local f_message = ProtoField.string("minetest.client.chat_message", "Message")
  275. minetest_client_commands[0x32] = {
  276. "CHAT_MESSAGE", 4,
  277. { f_length, f_message },
  278. function(buffer, pinfo, tree, t)
  279. t:add(f_length, buffer(2,2))
  280. local textlen = buffer(2,2):uint()
  281. if minetest_check_length(buffer, 4 + textlen*2, t) then
  282. t:add(f_message, minetest_convert_utf16(buffer(4, textlen*2), "Converted chat message"))
  283. end
  284. end
  285. }
  286. end
  287. -- TOSERVER_SIGNNODETEXT
  288. do
  289. local f_pos_x = ProtoField.int16("minetest.client.signnodetext_pos_x", "Block position X", base.DEC)
  290. local f_pos_y = ProtoField.int16("minetest.client.signnodetext_pos_y", "Block position Y", base.DEC)
  291. local f_pos_z = ProtoField.int16("minetest.client.signnodetext_pos_z", "Block position Z", base.DEC)
  292. local f_textlen = ProtoField.uint16("minetest.client.signnodetext_textlen", "Text length", base.DEC)
  293. local f_text = ProtoField.string("minetest.client.signnodetext_text", "Text")
  294. minetest_client_commands[0x33] = {
  295. "SIGNNODETEXT", 10,
  296. { f_pos_x, f_pos_y, f_pos_z, f_textlen, f_text },
  297. function(buffer, pinfo, tree, t)
  298. t:add(f_pos_x, buffer(2,2))
  299. t:add(f_pos_y, buffer(4,2))
  300. t:add(f_pos_z, buffer(6,2))
  301. t:add(f_textlen, buffer(8,2))
  302. local textlen = buffer(8,2):uint()
  303. if minetest_check_length(buffer, 10 + textlen, t) then
  304. t:add(f_text, buffer(10, textlen))
  305. end
  306. end
  307. }
  308. end
  309. -- TOSERVER_CLICK_ACTIVEOBJECT
  310. do
  311. local vs_button = {
  312. [0] = "left",
  313. [1] = "right"
  314. }
  315. local f_button = ProtoField.uint8("minetest.client.click_activeobject_button", "Button", base.DEC, vs_button)
  316. local f_id = ProtoField.uint16("minetest.client.click_activeobject_id", "ID", base.DEC)
  317. local f_item = ProtoField.uint16("minetest.client.click_activeobject_item", "Item", base.DEC)
  318. minetest_client_commands[0x34] = {
  319. "CLICK_ACTIVEOBJECT", 7,
  320. { f_button, f_id, f_item },
  321. function(buffer, pinfo, tree, t)
  322. t:add(f_button, buffer(2,1))
  323. t:add(f_id, buffer(3,2))
  324. t:add(f_item, buffer(5,2))
  325. end
  326. }
  327. end
  328. -- TOSERVER_DAMAGE
  329. do
  330. local f_amount = ProtoField.uint8("minetest.client.damage_amount", "Amount", base.DEC)
  331. minetest_client_commands[0x35] = {
  332. "DAMAGE", 3,
  333. { f_amount },
  334. function(buffer, pinfo, tree, t)
  335. t:add(f_amount, buffer(2,1))
  336. end
  337. }
  338. end
  339. -- TOSERVER_PASSWORD
  340. do
  341. local f_old_password = ProtoField.string("minetest.client.password_old", "Old password")
  342. local f_new_password = ProtoField.string("minetest.client.password_new", "New password")
  343. minetest_client_commands[0x36] = {
  344. "PASSWORD", 58,
  345. { f_old_password, f_new_password },
  346. function(buffer, pinfo, tree, t)
  347. t:add(f_old_password, buffer(2,28))
  348. t:add(f_new_password, buffer(30,28))
  349. end
  350. }
  351. end
  352. -- TOSERVER_PLAYERITEM
  353. do
  354. local f_item = ProtoField.uint16("minetest.client.playeritem_item", "Wielded item")
  355. minetest_client_commands[0x37] = {
  356. "PLAYERITEM", 4,
  357. { f_item },
  358. function(buffer, pinfo, tree, t)
  359. t:add(f_item, buffer(2,2))
  360. end
  361. }
  362. end
  363. --------------------------------------------
  364. -- Part 2 --
  365. -- Server command dissectors (TOCLIENT_*) --
  366. --------------------------------------------
  367. minetest_server_commands = {}
  368. minetest_server_obsolete = {}
  369. -- TOCLIENT_INIT
  370. do
  371. local f_version = ProtoField.uint8("minetest.server.init_version", "Deployed version", base.DEC)
  372. local f_pos_x = ProtoField.int16("minetest.server.init_pos_x", "Position X", base.DEC)
  373. local f_pos_y = ProtoField.int16("minetest.server.init_pos_y", "Position Y", base.DEC)
  374. local f_pos_z = ProtoField.int16("minetest.server.init_pos_x", "Position Z", base.DEC)
  375. local f_map_seed = ProtoField.uint64("minetest.server.init_map_seed", "Map seed", base.DEC)
  376. minetest_server_commands[0x10] = {
  377. "INIT", 17,
  378. { f_version, f_pos_x, f_pos_y, f_pos_z, f_map_seed },
  379. function(buffer, pinfo, tree, t)
  380. t:add(f_version, buffer(2,1))
  381. t:add(f_pos_x, buffer(3,2))
  382. t:add(f_pos_y, buffer(5,2))
  383. t:add(f_pos_z, buffer(7,2))
  384. t:add(f_map_seed, buffer(9,8))
  385. end
  386. }
  387. end
  388. -- TOCLIENT_BLOCKDATA
  389. do
  390. local f_x = ProtoField.int16("minetest.server.blockdata_x", "Block position X", base.DEC)
  391. local f_y = ProtoField.int16("minetest.server.blockdata_y", "Block position Y", base.DEC)
  392. local f_z = ProtoField.int16("minetest.server.blockdata_z", "Block position Z", base.DEC)
  393. local f_data = ProtoField.bytes("minetest.server.blockdata_block", "Serialized MapBlock")
  394. minetest_server_commands[0x20] = {
  395. "BLOCKDATA", 8,
  396. { f_x, f_y, f_z, f_data },
  397. function(buffer, pinfo, tree, t)
  398. t:add(f_x, buffer(2,2))
  399. t:add(f_y, buffer(4,2))
  400. t:add(f_z, buffer(6,2))
  401. t:add(f_data, buffer(8, buffer:len() - 8))
  402. end
  403. }
  404. end
  405. -- TOCLIENT_ADDNODE
  406. do
  407. local f_x = ProtoField.int16("minetest.server.addnode_x", "Position X", base.DEC)
  408. local f_y = ProtoField.int16("minetest.server.addnode_y", "Position Y", base.DEC)
  409. local f_z = ProtoField.int16("minetest.server.addnode_z", "Position Z", base.DEC)
  410. local f_data = ProtoField.bytes("minetest.server.addnode_node", "Serialized MapNode")
  411. minetest_server_commands[0x21] = {
  412. "ADDNODE", 8,
  413. { f_x, f_y, f_z, f_data },
  414. function(buffer, pinfo, tree, t)
  415. t:add(f_x, buffer(2,2))
  416. t:add(f_y, buffer(4,2))
  417. t:add(f_z, buffer(6,2))
  418. t:add(f_data, buffer(8, buffer:len() - 8))
  419. end
  420. }
  421. end
  422. -- TOCLIENT_REMOVENODE
  423. do
  424. local f_x = ProtoField.int16("minetest.server.removenode_x", "Position X", base.DEC)
  425. local f_y = ProtoField.int16("minetest.server.removenode_y", "Position Y", base.DEC)
  426. local f_z = ProtoField.int16("minetest.server.removenode_z", "Position Z", base.DEC)
  427. minetest_server_commands[0x22] = {
  428. "REMOVENODE", 8,
  429. { f_x, f_y, f_z },
  430. function(buffer, pinfo, tree, t)
  431. t:add(f_x, buffer(2,2))
  432. t:add(f_y, buffer(4,2))
  433. t:add(f_z, buffer(6,2))
  434. end
  435. }
  436. end
  437. -- TOCLIENT_PLAYERPOS (obsolete)
  438. minetest_server_commands[0x23] = { "PLAYERPOS", 2 }
  439. minetest_server_obsolete[0x23] = true
  440. -- TOCLIENT_PLAYERINFO
  441. do
  442. local f_count = ProtoField.uint16("minetest.server.playerinfo_count", "Count", base.DEC)
  443. local f_player = ProtoField.bytes("minetest.server.playerinfo_player", "Player", base.DEC)
  444. local f_peer_id = ProtoField.uint16("minetest.server.playerinfo_peer_id", "Peer ID", base.DEC)
  445. local f_name = ProtoField.string("minetest.server.playerinfo_name", "Name")
  446. minetest_server_commands[0x24] = {
  447. "PLAYERINFO", 2,
  448. { f_count, f_player, f_peer_id, f_name },
  449. function(buffer, pinfo, tree, t)
  450. local count = 0
  451. local pos, index
  452. for pos = 2, buffer:len() - 22, 22 do -- does lua have integer division?
  453. count = count + 1
  454. end
  455. t:add(f_count, count):set_generated()
  456. t:set_len(2 + 22 * count)
  457. pinfo.cols.info:append(" * " .. count)
  458. for index = 0, count - 1 do
  459. local pos = 2 + 22 * index
  460. local t2 = t:add(f_player, buffer(pos, 22))
  461. t2:set_text("Player, ID: " .. buffer(pos, 2):uint()
  462. .. ", Name: " .. buffer(pos + 2, 20):string())
  463. t2:add(f_peer_id, buffer(pos, 2))
  464. t2:add(f_name, buffer(pos + 2, 20))
  465. end
  466. end
  467. }
  468. end
  469. -- TOCLIENT_OPT_BLOCK_NOT_FOUND (obsolete)
  470. minetest_server_commands[0x25] = { "OPT_BLOCK_NOT_FOUND", 2 }
  471. minetest_server_obsolete[0x25] = true
  472. -- TOCLIENT_SECTORMETA (obsolete)
  473. minetest_server_commands[0x26] = { "SECTORMETA", 2 }
  474. minetest_server_obsolete[0x26] = true
  475. -- TOCLIENT_INVENTORY
  476. do
  477. local f_inventory = ProtoField.string("minetest.server.inventory", "Inventory")
  478. minetest_server_commands[0x27] = {
  479. "INVENTORY", 2,
  480. { f_inventory },
  481. function(buffer, pinfo, tree, t)
  482. t:add(f_inventory, buffer(2, buffer:len() - 2))
  483. end
  484. }
  485. end
  486. -- TOCLIENT_OBJECTDATA
  487. do
  488. local f_player_count = ProtoField.uint16("minetest.server.objectdata_player_count",
  489. "Count of player positions", base.DEC)
  490. local f_player = ProtoField.bytes("minetest.server.objectdata_player", "Player position")
  491. local f_peer_id = ProtoField.uint16("minetest.server.objectdata_player_peer_id", "Peer ID")
  492. local f_x = ProtoField.int32("minetest.server.objectdata_player_x", "Position X", base.DEC)
  493. local f_y = ProtoField.int32("minetest.server.objectdata_player_y", "Position Y", base.DEC)
  494. local f_z = ProtoField.int32("minetest.server.objectdata_player_z", "Position Z", base.DEC)
  495. local f_speed_x = ProtoField.int32("minetest.server.objectdata_player_speed_x", "Speed X", base.DEC)
  496. local f_speed_y = ProtoField.int32("minetest.server.objectdata_player_speed_y", "Speed Y", base.DEC)
  497. local f_speed_z = ProtoField.int32("minetest.server.objectdata_player_speed_z", "Speed Z", base.DEC)
  498. local f_pitch = ProtoField.int32("minetest.server.objectdata_player_pitch", "Pitch", base.DEC)
  499. local f_yaw = ProtoField.int32("minetest.server.objectdata_player_yaw", "Yaw", base.DEC)
  500. local f_block_count = ProtoField.uint16("minetest.server.objectdata_block_count",
  501. "Count of blocks", base.DEC)
  502. minetest_server_commands[0x28] = {
  503. "OBJECTDATA", 6,
  504. { f_player_count, f_player, f_peer_id, f_x, f_y, f_z,
  505. f_speed_x, f_speed_y, f_speed_z,f_pitch, f_yaw,
  506. f_block_count },
  507. function(buffer, pinfo, tree, t)
  508. local t2, index, pos
  509. local player_count_pos = 2
  510. local player_count = buffer(player_count_pos, 2):uint()
  511. t:add(f_player_count, buffer(player_count_pos, 2))
  512. local block_count_pos = player_count_pos + 2 + 34 * player_count
  513. if not minetest_check_length(buffer, block_count_pos + 2, t) then
  514. return
  515. end
  516. for index = 0, player_count - 1 do
  517. pos = player_count_pos + 2 + 34 * index
  518. t2 = t:add(f_player, buffer(pos, 34))
  519. t2:set_text("Player position, ID: " .. buffer(pos, 2):uint())
  520. t2:add(f_peer_id, buffer(pos, 2))
  521. t2:add(f_x, buffer(pos + 2, 4))
  522. t2:add(f_y, buffer(pos + 6, 4))
  523. t2:add(f_z, buffer(pos + 10, 4))
  524. t2:add(f_speed_x, buffer(pos + 14, 4))
  525. t2:add(f_speed_y, buffer(pos + 18, 4))
  526. t2:add(f_speed_z, buffer(pos + 22, 4))
  527. t2:add(f_pitch, buffer(pos + 26, 4))
  528. t2:add(f_yaw, buffer(pos + 30, 4))
  529. end
  530. local block_count = buffer(block_count_pos, 2):uint()
  531. t:add(f_block_count, buffer(block_count_pos, 2))
  532. -- TODO: dissect blocks.
  533. -- NOTE: block_count > 0 is obsolete. (?)
  534. pinfo.cols.info:append(" * " .. (player_count + block_count))
  535. end
  536. }
  537. end
  538. -- TOCLIENT_TIME_OF_DAY
  539. do
  540. local f_time = ProtoField.uint16("minetest.server.time_of_day", "Time", base.DEC)
  541. minetest_server_commands[0x29] = {
  542. "TIME_OF_DAY", 4,
  543. { f_time },
  544. function(buffer, pinfo, tree, t)
  545. t:add(f_time, buffer(2,2))
  546. end
  547. }
  548. end
  549. -- TOCLIENT_CHAT_MESSAGE
  550. do
  551. local f_length = ProtoField.uint16("minetest.server.chat_message_length", "Length", base.DEC)
  552. local f_message = ProtoField.string("minetest.server.chat_message", "Message")
  553. minetest_server_commands[0x30] = {
  554. "CHAT_MESSAGE", 4,
  555. { f_length, f_message },
  556. function(buffer, pinfo, tree, t)
  557. t:add(f_length, buffer(2,2))
  558. local textlen = buffer(2,2):uint()
  559. if minetest_check_length(buffer, 4 + textlen*2, t) then
  560. t:add(f_message, minetest_convert_utf16(buffer(4, textlen*2), "Converted chat message"))
  561. end
  562. end
  563. }
  564. end
  565. -- TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD
  566. do
  567. local f_removed_count = ProtoField.uint16(
  568. "minetest.server.active_object_remove_add_removed_count",
  569. "Count of removed objects", base.DEC)
  570. local f_removed = ProtoField.bytes(
  571. "minetest.server.active_object_remove_add_removed",
  572. "Removed object")
  573. local f_removed_id = ProtoField.uint16(
  574. "minetest.server.active_object_remove_add_removed_id",
  575. "ID", base.DEC)
  576. local f_added_count = ProtoField.uint16(
  577. "minetest.server.active_object_remove_add_added_count",
  578. "Count of added objects", base.DEC)
  579. local f_added = ProtoField.bytes(
  580. "minetest.server.active_object_remove_add_added",
  581. "Added object")
  582. local f_added_id = ProtoField.uint16(
  583. "minetest.server.active_object_remove_add_added_id",
  584. "ID", base.DEC)
  585. local f_added_type = ProtoField.uint8(
  586. "minetest.server.active_object_remove_add_added_type",
  587. "Type", base.DEC)
  588. local f_added_init_length = ProtoField.uint32(
  589. "minetest.server.active_object_remove_add_added_init_length",
  590. "Initialization data length", base.DEC)
  591. local f_added_init_data = ProtoField.bytes(
  592. "minetest.server.active_object_remove_add_added_init_data",
  593. "Initialization data")
  594. minetest_server_commands[0x31] = {
  595. "ACTIVE_OBJECT_REMOVE_ADD", 6,
  596. { f_removed_count, f_removed, f_removed_id,
  597. f_added_count, f_added, f_added_id,
  598. f_added_type, f_added_init_length, f_added_init_data },
  599. function(buffer, pinfo, tree, t)
  600. local t2, index, pos
  601. local removed_count_pos = 2
  602. local removed_count = buffer(removed_count_pos, 2):uint()
  603. t:add(f_removed_count, buffer(removed_count_pos, 2))
  604. local added_count_pos = removed_count_pos + 2 + 2 * removed_count
  605. if not minetest_check_length(buffer, added_count_pos + 2, t) then
  606. return
  607. end
  608. -- Loop through removed active objects
  609. for index = 0, removed_count - 1 do
  610. pos = removed_count_pos + 2 + 2 * index
  611. t2 = t:add(f_removed, buffer(pos, 2))
  612. t2:set_text("Removed object, ID = " .. buffer(pos, 2):uint())
  613. t2:add(f_removed_id, buffer(pos, 2))
  614. end
  615. local added_count = buffer(added_count_pos, 2):uint()
  616. t:add(f_added_count, buffer(added_count_pos, 2))
  617. -- Loop through added active objects
  618. pos = added_count_pos + 2
  619. for index = 0, added_count - 1 do
  620. if not minetest_check_length(buffer, pos + 7, t) then
  621. return
  622. end
  623. local init_length = buffer(pos + 3, 4):uint()
  624. if not minetest_check_length(buffer, pos + 7 + init_length, t) then
  625. return
  626. end
  627. t2 = t:add(f_added, buffer(pos, 7 + init_length))
  628. t2:set_text("Added object, ID = " .. buffer(pos, 2):uint())
  629. t2:add(f_added_id, buffer(pos, 2))
  630. t2:add(f_added_type, buffer(pos + 2, 1))
  631. t2:add(f_added_init_length, buffer(pos + 3, 4))
  632. t2:add(f_added_init_data, buffer(pos + 7, init_length))
  633. pos = pos + 7 + init_length
  634. end
  635. pinfo.cols.info:append(" * " .. (removed_count + added_count))
  636. end
  637. }
  638. end
  639. -- TOCLIENT_ACTIVE_OBJECT_MESSAGES
  640. do
  641. local f_object_count = ProtoField.uint16(
  642. "minetest.server.active_object_messages_object_count",
  643. "Count of objects", base.DEC)
  644. local f_object = ProtoField.bytes(
  645. "minetest.server.active_object_messages_object",
  646. "Object")
  647. local f_object_id = ProtoField.uint16(
  648. "minetest.server.active_object_messages_id",
  649. "ID", base.DEC)
  650. local f_message_length = ProtoField.uint16(
  651. "minetest.server.active_object_messages_message_length",
  652. "Message length", base.DEC)
  653. local f_message = ProtoField.bytes(
  654. "minetest.server.active_object_messages_message",
  655. "Message")
  656. minetest_server_commands[0x32] = {
  657. "ACTIVE_OBJECT_MESSAGES", 2,
  658. { f_object_count, f_object, f_object_id, f_message_length, f_message },
  659. function(buffer, pinfo, tree, t)
  660. local t2, count, pos, message_length
  661. count = 0
  662. pos = 2
  663. while pos < buffer:len() do
  664. if not minetest_check_length(buffer, pos + 4, t) then
  665. return
  666. end
  667. message_length = buffer(pos + 2, 2):uint()
  668. if not minetest_check_length(buffer, pos + 4 + message_length, t) then
  669. return
  670. end
  671. count = count + 1
  672. pos = pos + 4 + message_length
  673. end
  674. pinfo.cols.info:append(" * " .. count)
  675. t:add(f_object_count, count):set_generated()
  676. pos = 2
  677. while pos < buffer:len() do
  678. message_length = buffer(pos + 2, 2):uint()
  679. t2 = t:add(f_object, buffer(pos, 4 + message_length))
  680. t2:set_text("Object, ID = " .. buffer(pos, 2):uint())
  681. t2:add(f_object_id, buffer(pos, 2))
  682. t2:add(f_message_length, buffer(pos + 2, 2))
  683. t2:add(f_message, buffer(pos + 4, message_length))
  684. pos = pos + 4 + message_length
  685. end
  686. end
  687. }
  688. end
  689. -- TOCLIENT_HP
  690. do
  691. local f_hp = ProtoField.uint8("minetest.server.hp", "Hitpoints", base.DEC)
  692. minetest_server_commands[0x33] = {
  693. "HP", 3,
  694. { f_hp },
  695. function(buffer, pinfo, tree, t)
  696. t:add(f_hp, buffer(2,1))
  697. end
  698. }
  699. end
  700. -- TOCLIENT_MOVE_PLAYER
  701. do
  702. local f_x = ProtoField.int32("minetest.server.move_player_x", "Position X", base.DEC)
  703. local f_y = ProtoField.int32("minetest.server.move_player_y", "Position Y", base.DEC)
  704. local f_z = ProtoField.int32("minetest.server.move_player_z", "Position Z", base.DEC)
  705. local f_pitch = ProtoField.int32("minetest.server.move_player_pitch", "Pitch", base.DEC)
  706. local f_yaw = ProtoField.int32("minetest.server.move_player_yaw", "Yaw", base.DEC)
  707. local f_garbage = ProtoField.bytes("minetest.server.move_player_garbage", "Garbage")
  708. minetest_server_commands[0x34] = {
  709. "MOVE_PLAYER", 18, -- actually 22, but see below
  710. { f_x, f_y, f_z, f_pitch, f_yaw, f_garbage },
  711. function(buffer, pinfo, tree, t)
  712. t:add(f_x, buffer(2, 4))
  713. t:add(f_y, buffer(6, 4))
  714. t:add(f_z, buffer(10, 4))
  715. -- Compatibility note:
  716. -- Up to 2011-08-23, there was a bug in Minetest that
  717. -- caused the server to serialize the pitch and yaw
  718. -- with 2 bytes each instead of 4, creating a
  719. -- malformed message.
  720. if buffer:len() >= 22 then
  721. t:add(f_pitch, buffer(14, 4))
  722. t:add(f_yaw, buffer(18, 4))
  723. else
  724. t:add(f_garbage, buffer(14, 4))
  725. t:add_expert_info(PI_MALFORMED, PI_WARN, "Malformed pitch and yaw, possibly caused by a serialization bug in Minetest")
  726. end
  727. end
  728. }
  729. end
  730. -- TOCLIENT_ACCESS_DENIED
  731. do
  732. local f_reason_length = ProtoField.uint16("minetest.server.access_denied_reason_length", "Reason length", base.DEC)
  733. local f_reason = ProtoField.string("minetest.server.access_denied_reason", "Reason")
  734. minetest_server_commands[0x35] = {
  735. "ACCESS_DENIED", 4,
  736. { f_reason_length, f_reason },
  737. function(buffer, pinfo, tree, t)
  738. t:add(f_reason_length, buffer(2,2))
  739. local reason_length = buffer(2,2):uint()
  740. if minetest_check_length(buffer, 4 + reason_length * 2, t) then
  741. t:add(f_reason, minetest_convert_utf16(buffer(4, reason_length * 2), "Converted reason message"))
  742. end
  743. end
  744. }
  745. end
  746. -- TOCLIENT_PLAYERITEM
  747. do
  748. local f_count = ProtoField.uint16(
  749. "minetest.server.playeritem_count",
  750. "Count of players", base.DEC)
  751. local f_player = ProtoField.bytes(
  752. "minetest.server.playeritem_player",
  753. "Player")
  754. local f_peer_id = ProtoField.uint16(
  755. "minetest.server.playeritem_peer_id",
  756. "Peer ID", base.DEC)
  757. local f_item_length = ProtoField.uint16(
  758. "minetest.server.playeritem_item_length",
  759. "Item information length", base.DEC)
  760. local f_item = ProtoField.string(
  761. "minetest.server.playeritem_item",
  762. "Item information")
  763. minetest_server_commands[0x36] = {
  764. "PLAYERITEM", 4,
  765. { f_count, f_player, f_peer_id, f_item_length, f_item },
  766. function(buffer, pinfo, tree, t)
  767. local count, index, pos, item_length
  768. count = buffer(2,2):uint()
  769. pinfo.cols.info:append(" * " .. count)
  770. t:add(f_count, buffer(2,2))
  771. pos = 4
  772. for index = 0, count - 1 do
  773. if not minetest_check_length(buffer, pos + 4, t) then
  774. return
  775. end
  776. item_length = buffer(pos + 2, 2):uint()
  777. if not minetest_check_length(buffer, pos + 4 + item_length, t) then
  778. return
  779. end
  780. local t2 = t:add(f_player, buffer(pos, 4 + item_length))
  781. t2:set_text("Player, ID: " .. buffer(pos, 2):uint())
  782. t2:add(f_peer_id, buffer(pos, 2))
  783. t2:add(f_item_length, buffer(pos + 2, 2))
  784. t2:add(f_item, buffer(pos + 4, item_length))
  785. pos = pos + 4 + item_length
  786. end
  787. end
  788. }
  789. end
  790. ------------------------------------
  791. -- Part 3 --
  792. -- Wrapper protocol subdissectors --
  793. ------------------------------------
  794. -- minetest.control dissector
  795. do
  796. local p_control = Proto("minetest.control", "Minetest Control")
  797. local vs_control_type = {
  798. [0] = "Ack",
  799. [1] = "Set Peer ID",
  800. [2] = "Ping",
  801. [3] = "Disco"
  802. }
  803. local f_control_type = ProtoField.uint8("minetest.control.type", "Control Type", base.DEC, vs_control_type)
  804. local f_control_ack = ProtoField.uint16("minetest.control.ack", "ACK sequence number", base.DEC)
  805. local f_control_peerid = ProtoField.uint8("minetest.control.peerid", "New peer ID", base.DEC)
  806. p_control.fields = { f_control_type, f_control_ack, f_control_peerid }
  807. local data_dissector = Dissector.get("data")
  808. function p_control.dissector(buffer, pinfo, tree)
  809. local t = tree:add(p_control, buffer(0,1))
  810. t:add(f_control_type, buffer(0,1))
  811. pinfo.cols.info = "Control message"
  812. local pos = 1
  813. if buffer(0,1):uint() == 0 then
  814. pos = 3
  815. t:set_len(3)
  816. t:add(f_control_ack, buffer(1,2))
  817. pinfo.cols.info = "Ack " .. buffer(1,2):uint()
  818. elseif buffer(0,1):uint() == 1 then
  819. pos = 3
  820. t:set_len(3)
  821. t:add(f_control_peerid, buffer(1,2))
  822. pinfo.cols.info = "Set peer ID " .. buffer(1,2):uint()
  823. elseif buffer(0,1):uint() == 2 then
  824. pinfo.cols.info = "Ping"
  825. elseif buffer(0,1):uint() == 3 then
  826. pinfo.cols.info = "Disco"
  827. end
  828. data_dissector:call(buffer(pos):tvb(), pinfo, tree)
  829. end
  830. end
  831. -- minetest.client dissector
  832. -- minetest.server dissector
  833. -- Defines the minetest.client or minetest.server Proto. These two protocols
  834. -- are created by the same function because they are so similar.
  835. -- Parameter: proto: the Proto object
  836. -- Parameter: this_peer: "Client" or "Server"
  837. -- Parameter: other_peer: "Server" or "Client"
  838. -- Parameter: commands: table of command information, built above
  839. -- Parameter: obsolete: table of obsolete commands, built above
  840. function minetest_define_client_or_server_proto(is_client)
  841. -- Differences between minetest.client and minetest.server
  842. local proto_name, this_peer, other_peer, empty_message_info
  843. local commands, obsolete
  844. if is_client then
  845. proto_name = "minetest.client"
  846. this_peer = "Client"
  847. other_peer = "Server"
  848. empty_message_info = "Empty message / Connect"
  849. commands = minetest_client_commands -- defined in Part 1
  850. obsolete = minetest_client_obsolete -- defined in Part 1
  851. else
  852. proto_name = "minetest.server"
  853. this_peer = "Server"
  854. other_peer = "Client"
  855. empty_message_info = "Empty message"
  856. commands = minetest_server_commands -- defined in Part 2
  857. obsolete = minetest_server_obsolete -- defined in Part 2
  858. end
  859. -- Create the protocol object.
  860. local proto = Proto(proto_name, "Minetest " .. this_peer .. " to " .. other_peer)
  861. -- Create a table vs_command that maps command codes to command names.
  862. local vs_command = {}
  863. local code, command_info
  864. for code, command_info in pairs(commands) do
  865. local command_name = command_info[1]
  866. vs_command[code] = "TO" .. other_peer:upper() .. "_" .. command_name
  867. end
  868. -- Field definitions
  869. local f_command = ProtoField.uint16(proto_name .. ".command", "Command", base.HEX, vs_command)
  870. local f_empty = ProtoField.bool(proto_name .. ".empty", "Is empty", BASE_NONE)
  871. proto.fields = { f_command, f_empty }
  872. -- Add command-specific fields to the protocol
  873. for code, command_info in pairs(commands) do
  874. local command_fields = command_info[3]
  875. if command_fields ~= nil then
  876. local index, field
  877. for index, field in ipairs(command_fields) do
  878. table.insert(proto.fields, field)
  879. end
  880. end
  881. end
  882. -- minetest.client or minetest.server dissector function
  883. function proto.dissector(buffer, pinfo, tree)
  884. local t = tree:add(proto, buffer)
  885. pinfo.cols.info = this_peer
  886. if buffer:len() == 0 then
  887. -- Empty message.
  888. t:add(f_empty, 1):set_generated()
  889. pinfo.cols.info:append(": " .. empty_message_info)
  890. elseif minetest_check_length(buffer, 2, t) then
  891. -- Get the command code.
  892. t:add(f_command, buffer(0,2))
  893. local code = buffer(0,2):uint()
  894. local command_info = commands[code]
  895. if command_info == nil then
  896. -- Error: Unknown command.
  897. pinfo.cols.info:append(": Unknown command")
  898. t:add_expert_info(PI_UNDECODED, PI_WARN, "Unknown " .. this_peer .. " to " .. other_peer .. " command")
  899. else
  900. -- Process a known command
  901. local command_name = command_info[1]
  902. local command_min_length = command_info[2]
  903. local command_fields = command_info[3]
  904. local command_dissector = command_info[4]
  905. if minetest_check_length(buffer, command_min_length, t) then
  906. pinfo.cols.info:append(": " .. command_name)
  907. if command_dissector ~= nil then
  908. command_dissector(buffer, pinfo, tree, t)
  909. end
  910. end
  911. if obsolete[code] then
  912. t:add_expert_info(PI_REQUEST_CODE, PI_WARN, "Obsolete command.")
  913. end
  914. end
  915. end
  916. end
  917. end
  918. minetest_define_client_or_server_proto(true) -- minetest.client
  919. minetest_define_client_or_server_proto(false) -- minetest.server
  920. -- minetest.split dissector
  921. do
  922. local p_split = Proto("minetest.split", "Minetest Split Message")
  923. local f_split_seq = ProtoField.uint16("minetest.split.seq", "Sequence number", base.DEC)
  924. local f_split_chunkcount = ProtoField.uint16("minetest.split.chunkcount", "Chunk count", base.DEC)
  925. local f_split_chunknum = ProtoField.uint16("minetest.split.chunknum", "Chunk number", base.DEC)
  926. local f_split_data = ProtoField.bytes("minetest.split.data", "Split message data")
  927. p_split.fields = { f_split_seq, f_split_chunkcount, f_split_chunknum, f_split_data }
  928. function p_split.dissector(buffer, pinfo, tree)
  929. local t = tree:add(p_split, buffer(0,6))
  930. t:add(f_split_seq, buffer(0,2))
  931. t:add(f_split_chunkcount, buffer(2,2))
  932. t:add(f_split_chunknum, buffer(4,2))
  933. t:add(f_split_data, buffer(6))
  934. pinfo.cols.info:append(" " .. buffer(0,2):uint() .. " chunk " .. buffer(4,2):uint() .. "/" .. buffer(2,2):uint())
  935. end
  936. end
  937. -------------------------------------
  938. -- Part 4 --
  939. -- Wrapper protocol main dissector --
  940. -------------------------------------
  941. -- minetest dissector
  942. do
  943. local p_minetest = Proto("minetest", "Minetest")
  944. local minetest_id = 0x4f457403
  945. local vs_id = {
  946. [minetest_id] = "Valid"
  947. }
  948. local vs_peer = {
  949. [0] = "Inexistent",
  950. [1] = "Server"
  951. }
  952. local vs_type = {
  953. [0] = "Control",
  954. [1] = "Original",
  955. [2] = "Split",
  956. [3] = "Reliable"
  957. }
  958. local f_id = ProtoField.uint32("minetest.id", "ID", base.HEX, vs_id)
  959. local f_peer = ProtoField.uint16("minetest.peer", "Peer", base.DEC, vs_peer)
  960. local f_channel = ProtoField.uint8("minetest.channel", "Channel", base.DEC)
  961. local f_type = ProtoField.uint8("minetest.type", "Type", base.DEC, vs_type)
  962. local f_seq = ProtoField.uint16("minetest.seq", "Sequence number", base.DEC)
  963. local f_subtype = ProtoField.uint8("minetest.subtype", "Subtype", base.DEC, vs_type)
  964. p_minetest.fields = { f_id, f_peer, f_channel, f_type, f_seq, f_subtype }
  965. local data_dissector = Dissector.get("data")
  966. local control_dissector = Dissector.get("minetest.control")
  967. local client_dissector = Dissector.get("minetest.client")
  968. local server_dissector = Dissector.get("minetest.server")
  969. local split_dissector = Dissector.get("minetest.split")
  970. function p_minetest.dissector(buffer, pinfo, tree)
  971. -- Add Minetest tree item and verify the ID
  972. local t = tree:add(p_minetest, buffer(0,8))
  973. t:add(f_id, buffer(0,4))
  974. if buffer(0,4):uint() ~= minetest_id then
  975. t:add_expert_info(PI_UNDECODED, PI_WARN, "Invalid ID, this is not a Minetest packet")
  976. return
  977. end
  978. -- ID is valid, so replace packet's shown protocol
  979. pinfo.cols.protocol = "Minetest"
  980. pinfo.cols.info = "Minetest"
  981. -- Set the other header fields
  982. t:add(f_peer, buffer(4,2))
  983. t:add(f_channel, buffer(6,1))
  984. t:add(f_type, buffer(7,1))
  985. t:set_text("Minetest, Peer: " .. buffer(4,2):uint() .. ", Channel: " .. buffer(6,1):uint())
  986. local reliability_info
  987. if buffer(7,1):uint() == 3 then
  988. -- Reliable message
  989. reliability_info = "Seq=" .. buffer(8,2):uint()
  990. t:set_len(11)
  991. t:add(f_seq, buffer(8,2))
  992. t:add(f_subtype, buffer(10,1))
  993. pos = 10
  994. else
  995. -- Unreliable message
  996. reliability_info = "Unrel"
  997. pos = 7
  998. end
  999. if buffer(pos,1):uint() == 0 then
  1000. -- Control message, possibly reliable
  1001. control_dissector:call(buffer(pos+1):tvb(), pinfo, tree)
  1002. elseif buffer(pos,1):uint() == 1 then
  1003. -- Original message, possibly reliable
  1004. if buffer(4,2):uint() == 1 then
  1005. server_dissector:call(buffer(pos+1):tvb(), pinfo, tree)
  1006. else
  1007. client_dissector:call(buffer(pos+1):tvb(), pinfo, tree)
  1008. end
  1009. elseif buffer(pos,1):uint() == 2 then
  1010. -- Split message, possibly reliable
  1011. if buffer(4,2):uint() == 1 then
  1012. pinfo.cols.info = "Server: Split message"
  1013. else
  1014. pinfo.cols.info = "Client: Split message"
  1015. end
  1016. split_dissector:call(buffer(pos+1):tvb(), pinfo, tree)
  1017. elseif buffer(pos,1):uint() == 3 then
  1018. -- Doubly reliable message??
  1019. t:add_expert_info(PI_MALFORMED, PI_ERROR, "Reliable message wrapped in reliable message")
  1020. else
  1021. data_dissector:call(buffer(pos+1):tvb(), pinfo, tree)
  1022. end
  1023. pinfo.cols.info:append(" (" .. reliability_info .. ")")
  1024. end
  1025. -- FIXME Is there a way to let the dissector table check if the first payload bytes are 0x4f457403?
  1026. DissectorTable.get("udp.port"):add(30000, p_minetest)
  1027. DissectorTable.get("udp.port"):add(30001, p_minetest)
  1028. end
  1029. -----------------------
  1030. -- Part 5 --
  1031. -- Utility functions --
  1032. -----------------------
  1033. -- Checks if a (sub-)Tvb is long enough to be further dissected.
  1034. -- If it is long enough, sets the dissector tree item length to min_len
  1035. -- and returns true. If it is not long enough, adds expert info to the
  1036. -- dissector tree and returns false.
  1037. -- Parameter: tvb: the Tvb
  1038. -- Parameter: min_len: required minimum length
  1039. -- Parameter: t: dissector tree item
  1040. -- Returns: true if tvb:len() >= min_len, false otherwise
  1041. function minetest_check_length(tvb, min_len, t)
  1042. if tvb:len() >= min_len then
  1043. t:set_len(min_len)
  1044. return true
  1045. -- Tvb:reported_length_remaining() has been added in August 2011
  1046. -- and is not yet widely available, disable for the time being
  1047. -- TODO: uncomment at a later date
  1048. -- TODO: when uncommenting this, also re-check if other parts of
  1049. -- the dissector could benefit from reported_length_remaining
  1050. --elseif tvb:reported_length_remaining() >= min_len then
  1051. -- t:add_expert_info(PI_UNDECODED, PI_INFO, "Only part of this packet was captured, unable to decode.")
  1052. -- return false
  1053. else
  1054. t:add_expert_info(PI_MALFORMED, PI_ERROR, "Message is too short")
  1055. return false
  1056. end
  1057. end
  1058. -- Takes a Tvb or TvbRange (i.e. part of a packet) that
  1059. -- contains a UTF-16 string and returns a TvbRange containing
  1060. -- string converted to ASCII. Any characters outside the range
  1061. -- 0x20 to 0x7e are replaced by a question mark.
  1062. -- Parameter: tvb: Tvb or TvbRange that contains the UTF-16 data
  1063. -- Parameter: name: will be the name of the newly created Tvb.
  1064. -- Returns: New TvbRange containing the ASCII string.
  1065. -- TODO: Handle surrogates (should only produce one question mark)
  1066. -- TODO: Remove this when Wireshark supports UTF-16 strings natively.
  1067. function minetest_convert_utf16(tvb, name)
  1068. local hex, pos, char
  1069. hex = ""
  1070. for pos = 0, tvb:len() - 2, 2 do
  1071. char = tvb(pos, 2):uint()
  1072. if (char >= 0x20) and (char <= 0x7e) then
  1073. hex = hex .. string.format(" %02x", char)
  1074. else
  1075. hex = hex .. " 3F"
  1076. end
  1077. end
  1078. return ByteArray.new(hex):tvb(name):range()
  1079. end