blob: 13b8a8cc8b35bd8f6bcb05aeb58e162b650753b4 [file] [log] [blame]
Junxiao Shi1fda67d2018-01-12 21:35:31 +00001-- Copyright (c) 2015-2018, Regents of the University of California.
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -07002--
3-- This file is part of ndn-tools (Named Data Networking Essential Tools).
4-- See AUTHORS.md for complete list of ndn-tools authors and contributors.
5--
6-- ndn-tools is free software: you can redistribute it and/or modify it under the terms
7-- of the GNU General Public License as published by the Free Software Foundation,
8-- either version 3 of the License, or (at your option) any later version.
9--
10-- ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11-- without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12-- PURPOSE. See the GNU General Public License for more details.
13--
14-- You should have received a copy of the GNU General Public License along with
15-- ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
16--
17-- @author Qi Zhao <https://www.linkedin.com/pub/qi-zhao/73/835/9a3>
18-- @author Seunghyun Yoo <http://relue2718.com/>
19-- @author Seungbae Kim <https://sites.google.com/site/sbkimcv/>
Alexander Afanasyev357c2052015-08-10 21:26:52 -070020-- @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Zipeng Wang574eeb02016-10-05 21:46:02 -070021-- @author Zipeng Wang
22-- @author Qianshan Yu
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -070023
24-- inspect.lua (https://github.com/kikito/inspect.lua) can be used for debugging.
25-- See more at http://stackoverflow.com/q/15175859/2150331
26-- local inspect = require('inspect')
27
28-- NDN protocol
Alexander Afanasyev357c2052015-08-10 21:26:52 -070029ndn = Proto("ndn", "Named Data Networking (NDN)")
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -070030
Zipeng Wang574eeb02016-10-05 21:46:02 -070031-- TODO with NDNLPv2 processing:
32-- * mark field "unknown" when the field is recognized but the relevant feature is disabled
33-- * colorize "unknown field"
34-- * for a field that appears out-of-order, display "out-of-order field " in red
35
Alexander Afanasyev357c2052015-08-10 21:26:52 -070036-----------------------------------------------------
37-----------------------------------------------------
38-- Field formatting helpers
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -070039
Alexander Afanasyev357c2052015-08-10 21:26:52 -070040-- Borrowed from http://lua-users.org/wiki/StringRecipes
41function escapeString(str)
42 if (str) then
43 str = string.gsub(str, "\n", "\r\n")
44 str = string.gsub(str, "([^%w %-%_%.%~])",
45 function (c) return string.format ("%%%02X", string.byte(c)) end)
46 str = string.gsub(str, " ", "+")
47 end
48 return str
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -070049end
50
Junxiao Shi5e384792016-11-24 03:59:06 +000051-- @return TLV-VALUE portion of a TLV block
52function getValue(b)
53 return b.tvb(b.offset + b.typeLen + b.lengthLen, b.length)
54end
55
56function getUriFromNameComponent(b)
Alexander Afanasyev357c2052015-08-10 21:26:52 -070057 -- @todo Implement proper proper URL escaping
Junxiao Shi5e384792016-11-24 03:59:06 +000058 return getValue(b):string()
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -070059end
60
Alexander Afanasyev357c2052015-08-10 21:26:52 -070061function getUriFromName(nameBlock)
62 if (nameBlock.elements == nil) then
63 return ""
64 else
65 components = {}
66 for i, block in pairs(nameBlock.elements) do
67 table.insert(components, getUriFromNameComponent(block))
68 end
69
70 return "/" .. table.concat(components, "/")
71 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -070072end
73
Junxiao Shi5e384792016-11-24 03:59:06 +000074function getUriFromExclude(block)
75 -- @todo
76 return ""
77end
78
Zipeng Wang574eeb02016-10-05 21:46:02 -070079function getNackReasonDetail(b)
80 local code = getNonNegativeInteger(b)
81 if (code == 0) then return "None"
82 elseif (code == 50) then return "Congestion"
83 elseif (code == 100) then return "Duplicate"
84 elseif (code == 150) then return "NoRoute"
85 else return "Unknown"
86 end
87end
88
89function getCachePolicyDetail(b)
90 local code = getNonNegativeInteger(b)
91 if (code == 1) then return "NoCache"
92 else return "Unknown"
93 end
94end
95
Alexander Afanasyev357c2052015-08-10 21:26:52 -070096function getNonNegativeInteger(b)
Junxiao Shi5e384792016-11-24 03:59:06 +000097 if (b.length == 1 or b.length == 2 or b.length == 4) then
98 return getValue(b):uint()
Alexander Afanasyev357c2052015-08-10 21:26:52 -070099 -- Something strange with uint64, not supporting it for now
100 -- elseif (b.length == 8) then
101 -- return b.tvb(b.offset + b.typeLen + b.lengthLen, 8):uint64()
102 else
103 return 0xFFFFFFFF;
104 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700105end
106
Junxiao Shi5e384792016-11-24 03:59:06 +0000107function getNonce(b)
108 assert(b.type == 10)
109 if (b.length ~= 4) then
110 return "invalid (should have 4 octets)"
111 end
112 return getValue(b):uint()
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700113end
114
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700115function getTrue(block)
116 return "Yes"
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700117end
118
Zipeng Wang574eeb02016-10-05 21:46:02 -0700119local AppPrivateBlock1 = 100
120local AppPrivateBlock2 = 800
121local AppPrivateBlock3 = 1000
122
123function canIgnoreTlvType(t)
124 if (t < AppPrivateBlock2 or t >= AppPrivateBlock3) then
125 return false
126 else
127 local mod = math.fmod(t, 2)
128 if (mod == 1) then
129 return true
130 else
131 return false
132 end
133 end
134end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700135
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700136function getGenericBlockInfo(block)
137 local name = ""
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700138
Zipeng Wang574eeb02016-10-05 21:46:02 -0700139 -- TODO: Properly format informational message based type value reservations
140 -- (http://named-data.net/doc/ndn-tlv/types.html#type-value-reservations)
141 if (block.type <= AppPrivateBlock1) then
142 name = "Unrecognized from the reserved range " .. 0 .. "-" .. AppPrivateBlock1 .. ""
143 elseif (AppPrivateBlock1 < block.type and block.type < AppPrivateBlock2) then
144 name = "Unrecognized from the reserved range " .. (AppPrivateBlock1 + 1) .. "-" .. (AppPrivateBlock2 - 1) .. ""
145 elseif (AppPrivateBlock2 <= block.type and block.type <= AppPrivateBlock3) then
146 if (canIgnoreTlvType(block.type)) then
147 name = "Unknown field (ignored)"
148 else
149 name = "Unknown field"
150 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700151 else
Zipeng Wang574eeb02016-10-05 21:46:02 -0700152 name = "RESERVED_3"
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700153 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700154
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700155 return name .. ", Type: " .. block.type .. ", Length: " .. block.length
156end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700157
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700158-----------------------------------------------------
159-----------------------------------------------------
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700160
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700161local NDN_DICT = {
162 -- Interest or Data packets
163 [5] = {name = "Interest" , summary = true},
164 [6] = {name = "Data" , summary = true},
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700165
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700166 -- Name field
167 [7] = {name = "Name" , field = ProtoField.string("ndn.name", "Name") , value = getUriFromName},
168 [1] = {name = "ImplicitSha256DigestComponent", field = ProtoField.string("ndn.implicitsha256", "ImplicitSha256DigestComponent"), value = getUriFromNameComponent},
169 [8] = {name = "NameComponent" , field = ProtoField.string("ndn.namecomponent", "NameComponent") , value = getUriFromNameComponent},
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700170
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700171 -- Sub-fields of Interest packet
172 [9] = {name = "Selectors" , summary = true},
Junxiao Shi5e384792016-11-24 03:59:06 +0000173 [10] = {name = "Nonce" , field = ProtoField.uint32("ndn.nonce", "Nonce", base.HEX) , value = getNonce},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700174 [12] = {name = "InterestLifetime" , field = ProtoField.uint32("ndn.interestlifetime", "InterestLifetime", base.DEC) , value = getNonNegativeInteger},
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700175
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700176 -- Sub-fields of Interest/Selector field
177 [13] = {name = "MinSuffixComponents" , field = ProtoField.uint32("ndn.minsuffix", "MinSuffixComponents") , value = getNonNegativeInteger},
178 [14] = {name = "MaxSuffixComponents" , field = ProtoField.uint32("ndn.maxsuffix", "MaxSuffixComponents") , value = getNonNegativeInteger},
179 [15] = {name = "PublisherPublicKeyLocator" , summary = true},
180 [16] = {name = "Exclude" , field = ProtoField.string("ndn.exclude", "Exclude") , value = getUriFromExclude},
181 [17] = {name = "ChildSelector" , field = ProtoField.uint32("ndn.childselector", "ChildSelector", base.DEC) , value = getNonNegativeInteger},
182 [18] = {name = "MustBeFresh" , field = ProtoField.string("ndn.mustbefresh", "MustBeFresh") , value = getTrue},
183 [19] = {name = "Any" , field = ProtoField.string("ndn.any", "Any") , value = getTrue},
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700184
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700185 -- Sub-fields of Data packet
186 [20] = {name = "MetaInfo" , summary = true},
187 [21] = {name = "Content" , field = ProtoField.string("ndn.content", "Content")},
188 [22] = {name = "SignatureInfo" , summary = true},
189 [23] = {name = "SignatureValue" , field = ProtoField.bytes("ndn.signaturevalue", "SignatureValue")},
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700190
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700191 -- Sub-fields of Data/MetaInfo field
192 [24] = {name = "ContentType" , field = ProtoField.uint32("ndn.contenttype", "Content Type", base.DEC) , value = getNonNegativeInteger},
193 [25] = {name = "FreshnessPeriod" , field = ProtoField.uint32("ndn.freshnessperiod", "FreshnessPeriod", base.DEC) , value = getNonNegativeInteger},
194 [26] = {name = "FinalBlockId" , field = ProtoField.string("ndn.finalblockid", "FinalBlockId") , value = getUriFromNameComponent},
195
196 -- Sub-fields of Data/Signature field
197 [27] = {name = "SignatureType" , field = ProtoField.uint32("ndn.signaturetype", "SignatureType", base.DEC) , value = getNonNegativeInteger},
198 [28] = {name = "KeyLocator" , summary = true},
199 [29] = {name = "KeyDigest" , field = ProtoField.bytes("ndn.keydigest", "KeyDigest")},
200
201 -- Other fields
202 [30] = {name = "LinkPreference" , field = ProtoField.uint32("ndn.link_preference", "LinkPreference", base.DEC) , value = getNonNegativeInteger},
203 [31] = {name = "LinkDelegation" , summary = true},
204 [32] = {name = "SelectedDelegation" , field = ProtoField.uint32("ndn.selected_delegation", "SelectedDelegation", base.DEC), value = getNonNegativeInteger},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700205
Zipeng Wang574eeb02016-10-05 21:46:02 -0700206 -- NDNLPv2 Packet field
207 [80] = {name = "Fragment" },
208 [81] = {name = "Sequence" , field = ProtoField.uint32("ndn.sequence", "Sequence", base.DEC), value = getNonNegativeInteger},
209 [82] = {name = "FragIndex" , field = ProtoField.uint32("ndn.fragindex", "FragIndex", base.DEC), value = getNonNegativeInteger},
210 [83] = {name = "FragCount" , field = ProtoField.uint32("ndn.fragcount", "FragCount", base.DEC), value = getNonNegativeInteger},
211 [100] = {name = "LpPacket" , summary = true},
212 [800] = {name = "Nack" , summary = true},
213 [801] = {name = "NackReason" , field = ProtoField.string("ndn.nack_reason", "NackReason"), value = getNackReasonDetail},
214 [816] = {name = "NextHopFaceId" , field = ProtoField.uint32("ndn.nexthop_faceid", "NextHopFaceId", base.DEC), value = getNonNegativeInteger},
215 [817] = {name = "IncomingFaceId" , field = ProtoField.uint32("ndn.incoming_faceid", "IncomingFaceId", base.DEC), value = getNonNegativeInteger},
216 [820] = {name = "CachePolicy" , summary = true},
217 [821] = {name = "CachePolicyType" , field = ProtoField.string("ndn.cachepolicy_type", "CachePolicyType"), value = getCachePolicyDetail},
218}
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700219
220-- -- Add protofields in NDN protocol
221ndn.fields = {
222}
223for key, value in pairs(NDN_DICT) do
224 table.insert(ndn.fields, value.field)
225end
226
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700227-----------------------------------------------------
228-----------------------------------------------------
229
230-- block
231-- .tvb
232-- .offset
233-- .type
234-- .typeLen
235-- .length
236-- .lengthLen
237-- .size = .typeLen + .lengthLen + .length
238
239function addInfo(block, root) -- may be add additional context later
240 local info = NDN_DICT[block.type]
241
242 if (info == nil) then
243 info = {}
244 info.value = getGenericBlockInfo
Zipeng Wang574eeb02016-10-05 21:46:02 -0700245 -- color
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700246 end
247
248 local treeInfo
249 if (info.value == nil) then
250
251 if (info.field ~= nil) then
252 treeInfo = root:add(info.field, block.tvb(block.offset, block.size))
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700253 else
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700254 treeInfo = root:add(block.tvb(block.offset, block.size), info.name)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700255 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700256
257 treeInfo:append_text(", Type: " .. block.type .. ", Length: " .. block.length)
258 else
259 block.value = info.value(block)
260
261 if (info.field ~= nil) then
262 treeInfo = root:add(info.field, block.tvb(block.offset, block.size), block.value)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700263 else
Zipeng Wang574eeb02016-10-05 21:46:02 -0700264 treeInfo = root:add(block.tvb(block.offset, block.size), block.value)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700265 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700266 end
267 block.root = treeInfo
268 return block.root
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700269end
270
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700271function addSummary(block)
272 if (block.elements == nil) then
273 return
274 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700275
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700276 local info = NDN_DICT[block.type]
277 if (info == nil or info.summary == nil) then
278 return
279 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700280
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700281 local summary = {}
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700282
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700283 for k, subblock in pairs(block.elements) do
284 if (subblock.value ~= nil) then
285 local info = NDN_DICT[subblock.type]
286 if (info ~= nil) then
287 table.insert(summary, info.name .. ": " .. subblock.value)
288 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700289 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700290 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700291
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700292 if (#summary > 0) then
293 block.summary = table.concat(summary, ", ")
294 if (block.value == nil) then
295 block.value = block.summary
296 end
297 block.root:append_text(", " .. block.summary)
298 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700299end
300
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700301-----------------------------------------------------
302-----------------------------------------------------
303
304function readVarNumber(tvb, offset)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000305 if offset >= tvb:len() then
306 return 0, 0
307 end
308
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700309 local firstOctet = tvb(offset, 1):uint()
310 if (firstOctet < 253) then
311 return firstOctet, 1
Junxiao Shie65c6d72016-07-24 21:53:19 +0000312 elseif (firstOctet == 253) and (offset + 3 < tvb:len()) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700313 return tvb(offset + 1, 2):uint(), 3
Junxiao Shie65c6d72016-07-24 21:53:19 +0000314 elseif (firstOctet == 254) and (offset + 5 < tvb:len()) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700315 return tvb(offset + 1, 4):uint(), 5
Junxiao Shie65c6d72016-07-24 21:53:19 +0000316 elseif (firstOctet == 255) and (offset + 9 < tvb:len()) then
317 return tvb(offset + 1, 8):uint64(), 9
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700318 end
Junxiao Shie65c6d72016-07-24 21:53:19 +0000319
320 return 0, 0
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700321end
322
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700323function getBlock(tvb, offset)
324 local block = {}
325 block.tvb = tvb
326 block.offset = offset
327
328 block.type, block.typeLen = readVarNumber(block.tvb, block.offset)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000329 if block.typeLen == 0 then
330 return nil
331 end
332
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700333 block.length, block.lengthLen = readVarNumber(block.tvb, block.offset + block.typeLen)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000334 if block.lengthLen == 0 then
335 return nil
336 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700337
338 block.size = block.typeLen + block.lengthLen + block.length
339
340 return block
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700341end
342
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700343function canBeValidNdnPacket(block)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700344 if ((block.type == 5 or block.type == 6 or block.type == 100) and block.length <= 8800) then
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700345 return true
346 else
347 return false
348 end
349end
350
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700351function findNdnPacket(tvb)
352 offset = 0
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700353
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700354 while offset + 2 < tvb:len() do
355 local block = getBlock(tvb, offset)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700356
Junxiao Shie65c6d72016-07-24 21:53:19 +0000357 if (block ~= nil) and canBeValidNdnPacket(block) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700358 return block
359 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700360
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700361 offset = offset + 1
362 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700363
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700364 return nil
365end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700366
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700367function getSubBlocks(block)
368 local valueLeft = block.length
369 local subBlocks = {}
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700370
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700371 while valueLeft > 0 do
Junxiao Shie65c6d72016-07-24 21:53:19 +0000372 local offset = block.offset + block.typeLen + block.lengthLen + (block.length - valueLeft)
373 local child = getBlock(block.tvb, offset)
374 if child == nil then
375 return nil
376 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700377
378 valueLeft = valueLeft - child.size
379 table.insert(subBlocks, child)
380 end
381
382 if (valueLeft == 0) then
383 return subBlocks
384 else
385 return nil
386 end
387end
388
389-----------------------------------------------------
390-----------------------------------------------------
391
392-- NDN protocol dissector function
393function ndn.dissector(tvb, pInfo, root) -- Tvb, Pinfo, TreeItem
394
395 if (tvb:len() ~= tvb:reported_len()) then
396 return 0 -- ignore partially captured packets
397 -- this can/may be re-enabled only for unfragmented UDP packets
398 end
399
400 local ok, block = pcall(findNdnPacket, tvb)
401 if (not ok) then
402 return 0
403 end
404
405 if (block == nil or block.offset == nil) then
406 -- no valid NDN packets found
407 return 0
408 end
409
410 local nBytesLeft = tvb:len() - block.offset
411 -- print (pInfo.number .. ":: Found block: " .. block.type .. " of length " .. block.size .. " bytesLeft: " .. nBytesLeft)
412
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000413 local pktType = ""
414 local pktName = ""
415
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700416 while (block.size <= nBytesLeft) do
417 -- Create TreeItems
418 block.tree = root:add(ndn, tvb(block.offset, block.size))
419
420 local queue = {block}
421 while (#queue > 0) do
422 local block = queue[1]
423 table.remove(queue, 1)
424
425 block.elements = getSubBlocks(block)
426 local subtree = addInfo(block, block.tree)
427
428 if (block.elements ~= nil) then
429 for i, subBlock in pairs(block.elements) do
430 subBlock.tree = subtree
431 table.insert(queue, subBlock)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700432 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700433 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700434 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700435
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700436 -- Make summaries
437 local queue = {block}
438 while (#queue > 0) do
439 local block = queue[1]
440 if (block.visited ~= nil or block.elements == nil) then
441 -- try to make summary
442 table.remove(queue, 1)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700443
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700444 addSummary(block)
445 else
446 for i, subBlock in pairs(block.elements) do
447 table.insert(queue, 1, subBlock)
448 end
449 block.visited = true
450 end
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000451
452 -- prepare information to fill info column
453 if block.type == 5 and pktType ~= "Nack" then
454 pktType = "Interest"
455 elseif block.type == 6 then
456 pktType = "Data"
457 elseif block.type == 800 then
458 pktType = "Nack"
459 end
460
461 if pktName == "" and block.type == 7 then
462 pktName = getUriFromName(block)
463 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700464 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700465
466 local info = NDN_DICT[block.type]
467 if (info ~= nil) then
Zipeng Wang574eeb02016-10-05 21:46:02 -0700468 if (block.summary ~= nil) then
469 block.tree:append_text(", " .. NDN_DICT[block.type].name .. ", " .. block.summary)
470 else
471 block.tree:append_text(", " .. NDN_DICT[block.type].name)
472 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700473 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700474
475 nBytesLeft = nBytesLeft - block.size
476
477 if (nBytesLeft > 0) then
478 ok, block = pcall(getBlock, tvb, tvb:len() - nBytesLeft)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700479 if (not ok or block == nil or not canBeValidNdnPacket(block)) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700480 break
481 end
482 end
Zipeng Wang574eeb02016-10-05 21:46:02 -0700483 end -- while(block.size <= nBytesLeft)
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700484
485 pInfo.cols.protocol = tostring(pInfo.cols.protocol) .. " (" .. ndn.name .. ")"
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000486 pInfo.cols.info = pktType .. " " .. pktName
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700487
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700488 if (nBytesLeft > 0 and block ~= nil and block.size ~= nil and block.size > nBytesLeft) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700489 pInfo.desegment_offset = tvb:len() - nBytesLeft
490
491 -- Originally, I set desegment_len to the exact lenght, but it mysteriously didn't work for TCP
492 -- pInfo.desegment_len = block.size -- this will not work to desegment TCP streams
493 pInfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT
494 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700495end
496
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700497local udpDissectorTable = DissectorTable.get("udp.port")
498udpDissectorTable:add("6363", ndn)
499udpDissectorTable:add("56363", ndn)
500
501local tcpDissectorTable = DissectorTable.get("tcp.port")
502tcpDissectorTable:add("6363", ndn)
503
504local websocketDissectorTable = DissectorTable.get("ws.port")
505-- websocketDissectorTable:add("9696", ndn)
506websocketDissectorTable:add("1-65535", ndn)
507
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700508local ethernetDissectorTable = DissectorTable.get("ethertype")
509ethernetDissectorTable:add(0x8624, ndn)
510
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700511io.stderr:write("ndn.lua is successfully loaded\n")
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000512