blob: f3463e26463cee619e0c7992a05788a42b0118e1 [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 = {
Junxiao Shiac4b5462018-04-17 02:26:17 +0000162 -- Name and NameComponent
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700163 [7] = {name = "Name" , field = ProtoField.string("ndn.name", "Name") , value = getUriFromName},
164 [1] = {name = "ImplicitSha256DigestComponent", field = ProtoField.string("ndn.implicitsha256", "ImplicitSha256DigestComponent"), value = getUriFromNameComponent},
Junxiao Shiac4b5462018-04-17 02:26:17 +0000165 [8] = {name = "GenericNameComponent" , field = ProtoField.string("ndn.namecomponent", "NameComponent") , value = getUriFromNameComponent},
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700166
Junxiao Shiac4b5462018-04-17 02:26:17 +0000167 -- Interest and its sub-elements in Packet Format v0.3
168 [5] = {name = "Interest" , summary = true},
169 [33] = {name = "CanBePrefix" , field = ProtoField.string("ndn.canbeprefix", "CanBePrefix") , value = getTrue},
170 [18] = {name = "MustBeFresh" , field = ProtoField.string("ndn.mustbefresh", "MustBeFresh") , value = getTrue},
171 -- [30] = {name = "ForwardingHint" , summary = true},
172 -- ForwardingHint and LinkPreference have the same TLV-TYPE number, so we can't recognize both for now (see #4185).
173 [31] = {name = "LinkDelegation" , summary = true},
174 [30] = {name = "LinkPreference" , field = ProtoField.uint32("ndn.link_preference", "LinkPreference", base.DEC) , value = getNonNegativeInteger},
Junxiao Shi5e384792016-11-24 03:59:06 +0000175 [10] = {name = "Nonce" , field = ProtoField.uint32("ndn.nonce", "Nonce", base.HEX) , value = getNonce},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700176 [12] = {name = "InterestLifetime" , field = ProtoField.uint32("ndn.interestlifetime", "InterestLifetime", base.DEC) , value = getNonNegativeInteger},
Junxiao Shiac4b5462018-04-17 02:26:17 +0000177 [34] = {name = "HopLimit" , field = ProtoField.uint32("ndn.hoplimit", "HopLimit", base.DEC) , value = getNonNegativeInteger},
178 [35] = {name = "Parameters" , field = ProtoField.string("ndn.parameters", "Parameters")},
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700179
Junxiao Shiac4b5462018-04-17 02:26:17 +0000180 -- Data and its sub-elements in Packet Format v0.3
181 [6] = {name = "Data" , summary = true},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700182 [20] = {name = "MetaInfo" , summary = true},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700183 [24] = {name = "ContentType" , field = ProtoField.uint32("ndn.contenttype", "Content Type", base.DEC) , value = getNonNegativeInteger},
184 [25] = {name = "FreshnessPeriod" , field = ProtoField.uint32("ndn.freshnessperiod", "FreshnessPeriod", base.DEC) , value = getNonNegativeInteger},
185 [26] = {name = "FinalBlockId" , field = ProtoField.string("ndn.finalblockid", "FinalBlockId") , value = getUriFromNameComponent},
Junxiao Shiac4b5462018-04-17 02:26:17 +0000186 [21] = {name = "Content" , field = ProtoField.string("ndn.content", "Content")},
187 [22] = {name = "SignatureInfo" , summary = true},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700188 [27] = {name = "SignatureType" , field = ProtoField.uint32("ndn.signaturetype", "SignatureType", base.DEC) , value = getNonNegativeInteger},
189 [28] = {name = "KeyLocator" , summary = true},
190 [29] = {name = "KeyDigest" , field = ProtoField.bytes("ndn.keydigest", "KeyDigest")},
Junxiao Shiac4b5462018-04-17 02:26:17 +0000191 [23] = {name = "SignatureValue" , field = ProtoField.bytes("ndn.signaturevalue", "SignatureValue")},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700192
Junxiao Shiac4b5462018-04-17 02:26:17 +0000193 -- NDNLPv2 headers
Zipeng Wang574eeb02016-10-05 21:46:02 -0700194 [80] = {name = "Fragment" },
195 [81] = {name = "Sequence" , field = ProtoField.uint32("ndn.sequence", "Sequence", base.DEC), value = getNonNegativeInteger},
196 [82] = {name = "FragIndex" , field = ProtoField.uint32("ndn.fragindex", "FragIndex", base.DEC), value = getNonNegativeInteger},
197 [83] = {name = "FragCount" , field = ProtoField.uint32("ndn.fragcount", "FragCount", base.DEC), value = getNonNegativeInteger},
198 [100] = {name = "LpPacket" , summary = true},
199 [800] = {name = "Nack" , summary = true},
200 [801] = {name = "NackReason" , field = ProtoField.string("ndn.nack_reason", "NackReason"), value = getNackReasonDetail},
201 [816] = {name = "NextHopFaceId" , field = ProtoField.uint32("ndn.nexthop_faceid", "NextHopFaceId", base.DEC), value = getNonNegativeInteger},
202 [817] = {name = "IncomingFaceId" , field = ProtoField.uint32("ndn.incoming_faceid", "IncomingFaceId", base.DEC), value = getNonNegativeInteger},
203 [820] = {name = "CachePolicy" , summary = true},
204 [821] = {name = "CachePolicyType" , field = ProtoField.string("ndn.cachepolicy_type", "CachePolicyType"), value = getCachePolicyDetail},
Junxiao Shiac4b5462018-04-17 02:26:17 +0000205
206 -- Deprecated elements
207 [9] = {name = "Selectors" , summary = true},
208 [13] = {name = "MinSuffixComponents" , field = ProtoField.uint32("ndn.minsuffix", "MinSuffixComponents") , value = getNonNegativeInteger},
209 [14] = {name = "MaxSuffixComponents" , field = ProtoField.uint32("ndn.maxsuffix", "MaxSuffixComponents") , value = getNonNegativeInteger},
210 [15] = {name = "PublisherPublicKeyLocator" , summary = true},
211 [16] = {name = "Exclude" , field = ProtoField.string("ndn.exclude", "Exclude") , value = getUriFromExclude},
212 [17] = {name = "ChildSelector" , field = ProtoField.uint32("ndn.childselector", "ChildSelector", base.DEC) , value = getNonNegativeInteger},
213 [19] = {name = "Any" , field = ProtoField.string("ndn.any", "Any") , value = getTrue},
214 [32] = {name = "SelectedDelegation" , field = ProtoField.uint32("ndn.selected_delegation", "SelectedDelegation", base.DEC), value = getNonNegativeInteger},
Zipeng Wang574eeb02016-10-05 21:46:02 -0700215}
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700216
217-- -- Add protofields in NDN protocol
218ndn.fields = {
219}
220for key, value in pairs(NDN_DICT) do
221 table.insert(ndn.fields, value.field)
222end
223
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700224-----------------------------------------------------
225-----------------------------------------------------
226
227-- block
228-- .tvb
229-- .offset
230-- .type
231-- .typeLen
232-- .length
233-- .lengthLen
234-- .size = .typeLen + .lengthLen + .length
235
236function addInfo(block, root) -- may be add additional context later
237 local info = NDN_DICT[block.type]
238
239 if (info == nil) then
240 info = {}
241 info.value = getGenericBlockInfo
Zipeng Wang574eeb02016-10-05 21:46:02 -0700242 -- color
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700243 end
244
245 local treeInfo
246 if (info.value == nil) then
247
248 if (info.field ~= nil) then
249 treeInfo = root:add(info.field, block.tvb(block.offset, block.size))
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700250 else
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700251 treeInfo = root:add(block.tvb(block.offset, block.size), info.name)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700252 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700253
254 treeInfo:append_text(", Type: " .. block.type .. ", Length: " .. block.length)
255 else
256 block.value = info.value(block)
257
258 if (info.field ~= nil) then
259 treeInfo = root:add(info.field, block.tvb(block.offset, block.size), block.value)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700260 else
Zipeng Wang574eeb02016-10-05 21:46:02 -0700261 treeInfo = root:add(block.tvb(block.offset, block.size), block.value)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700262 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700263 end
264 block.root = treeInfo
265 return block.root
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700266end
267
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700268function addSummary(block)
269 if (block.elements == nil) then
270 return
271 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700272
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700273 local info = NDN_DICT[block.type]
274 if (info == nil or info.summary == nil) then
275 return
276 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700277
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700278 local summary = {}
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700279
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700280 for k, subblock in pairs(block.elements) do
281 if (subblock.value ~= nil) then
282 local info = NDN_DICT[subblock.type]
283 if (info ~= nil) then
284 table.insert(summary, info.name .. ": " .. subblock.value)
285 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700286 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700287 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700288
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700289 if (#summary > 0) then
290 block.summary = table.concat(summary, ", ")
291 if (block.value == nil) then
292 block.value = block.summary
293 end
294 block.root:append_text(", " .. block.summary)
295 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700296end
297
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700298-----------------------------------------------------
299-----------------------------------------------------
300
301function readVarNumber(tvb, offset)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000302 if offset >= tvb:len() then
303 return 0, 0
304 end
305
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700306 local firstOctet = tvb(offset, 1):uint()
307 if (firstOctet < 253) then
308 return firstOctet, 1
Junxiao Shie65c6d72016-07-24 21:53:19 +0000309 elseif (firstOctet == 253) and (offset + 3 < tvb:len()) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700310 return tvb(offset + 1, 2):uint(), 3
Junxiao Shie65c6d72016-07-24 21:53:19 +0000311 elseif (firstOctet == 254) and (offset + 5 < tvb:len()) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700312 return tvb(offset + 1, 4):uint(), 5
Junxiao Shie65c6d72016-07-24 21:53:19 +0000313 elseif (firstOctet == 255) and (offset + 9 < tvb:len()) then
314 return tvb(offset + 1, 8):uint64(), 9
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700315 end
Junxiao Shie65c6d72016-07-24 21:53:19 +0000316
317 return 0, 0
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700318end
319
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700320function getBlock(tvb, offset)
321 local block = {}
322 block.tvb = tvb
323 block.offset = offset
324
325 block.type, block.typeLen = readVarNumber(block.tvb, block.offset)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000326 if block.typeLen == 0 then
327 return nil
328 end
329
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700330 block.length, block.lengthLen = readVarNumber(block.tvb, block.offset + block.typeLen)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000331 if block.lengthLen == 0 then
332 return nil
333 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700334
335 block.size = block.typeLen + block.lengthLen + block.length
336
337 return block
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700338end
339
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700340function canBeValidNdnPacket(block)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700341 if ((block.type == 5 or block.type == 6 or block.type == 100) and block.length <= 8800) then
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700342 return true
343 else
344 return false
345 end
346end
347
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700348function findNdnPacket(tvb)
349 offset = 0
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700350
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700351 while offset + 2 < tvb:len() do
352 local block = getBlock(tvb, offset)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700353
Junxiao Shie65c6d72016-07-24 21:53:19 +0000354 if (block ~= nil) and canBeValidNdnPacket(block) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700355 return block
356 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700357
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700358 offset = offset + 1
359 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700360
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700361 return nil
362end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700363
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700364function getSubBlocks(block)
365 local valueLeft = block.length
366 local subBlocks = {}
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700367
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700368 while valueLeft > 0 do
Junxiao Shie65c6d72016-07-24 21:53:19 +0000369 local offset = block.offset + block.typeLen + block.lengthLen + (block.length - valueLeft)
370 local child = getBlock(block.tvb, offset)
371 if child == nil then
372 return nil
373 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700374
375 valueLeft = valueLeft - child.size
376 table.insert(subBlocks, child)
377 end
378
379 if (valueLeft == 0) then
380 return subBlocks
381 else
382 return nil
383 end
384end
385
386-----------------------------------------------------
387-----------------------------------------------------
388
389-- NDN protocol dissector function
390function ndn.dissector(tvb, pInfo, root) -- Tvb, Pinfo, TreeItem
391
392 if (tvb:len() ~= tvb:reported_len()) then
393 return 0 -- ignore partially captured packets
394 -- this can/may be re-enabled only for unfragmented UDP packets
395 end
396
397 local ok, block = pcall(findNdnPacket, tvb)
398 if (not ok) then
399 return 0
400 end
401
402 if (block == nil or block.offset == nil) then
403 -- no valid NDN packets found
404 return 0
405 end
406
407 local nBytesLeft = tvb:len() - block.offset
408 -- print (pInfo.number .. ":: Found block: " .. block.type .. " of length " .. block.size .. " bytesLeft: " .. nBytesLeft)
409
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000410 local pktType = ""
411 local pktName = ""
412
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700413 while (block.size <= nBytesLeft) do
414 -- Create TreeItems
415 block.tree = root:add(ndn, tvb(block.offset, block.size))
416
417 local queue = {block}
418 while (#queue > 0) do
419 local block = queue[1]
420 table.remove(queue, 1)
421
422 block.elements = getSubBlocks(block)
423 local subtree = addInfo(block, block.tree)
424
425 if (block.elements ~= nil) then
426 for i, subBlock in pairs(block.elements) do
427 subBlock.tree = subtree
428 table.insert(queue, subBlock)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700429 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700430 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700431 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700432
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700433 -- Make summaries
434 local queue = {block}
435 while (#queue > 0) do
436 local block = queue[1]
437 if (block.visited ~= nil or block.elements == nil) then
438 -- try to make summary
439 table.remove(queue, 1)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700440
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700441 addSummary(block)
442 else
443 for i, subBlock in pairs(block.elements) do
444 table.insert(queue, 1, subBlock)
445 end
446 block.visited = true
447 end
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000448
449 -- prepare information to fill info column
450 if block.type == 5 and pktType ~= "Nack" then
451 pktType = "Interest"
452 elseif block.type == 6 then
453 pktType = "Data"
454 elseif block.type == 800 then
455 pktType = "Nack"
456 end
457
458 if pktName == "" and block.type == 7 then
459 pktName = getUriFromName(block)
460 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700461 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700462
463 local info = NDN_DICT[block.type]
464 if (info ~= nil) then
Zipeng Wang574eeb02016-10-05 21:46:02 -0700465 if (block.summary ~= nil) then
466 block.tree:append_text(", " .. NDN_DICT[block.type].name .. ", " .. block.summary)
467 else
468 block.tree:append_text(", " .. NDN_DICT[block.type].name)
469 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700470 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700471
472 nBytesLeft = nBytesLeft - block.size
473
474 if (nBytesLeft > 0) then
475 ok, block = pcall(getBlock, tvb, tvb:len() - nBytesLeft)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700476 if (not ok or block == nil or not canBeValidNdnPacket(block)) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700477 break
478 end
479 end
Zipeng Wang574eeb02016-10-05 21:46:02 -0700480 end -- while(block.size <= nBytesLeft)
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700481
482 pInfo.cols.protocol = tostring(pInfo.cols.protocol) .. " (" .. ndn.name .. ")"
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000483 pInfo.cols.info = pktType .. " " .. pktName
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700484
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700485 if (nBytesLeft > 0 and block ~= nil and block.size ~= nil and block.size > nBytesLeft) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700486 pInfo.desegment_offset = tvb:len() - nBytesLeft
487
488 -- Originally, I set desegment_len to the exact lenght, but it mysteriously didn't work for TCP
489 -- pInfo.desegment_len = block.size -- this will not work to desegment TCP streams
490 pInfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT
491 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700492end
493
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700494local udpDissectorTable = DissectorTable.get("udp.port")
495udpDissectorTable:add("6363", ndn)
496udpDissectorTable:add("56363", ndn)
497
498local tcpDissectorTable = DissectorTable.get("tcp.port")
499tcpDissectorTable:add("6363", ndn)
500
501local websocketDissectorTable = DissectorTable.get("ws.port")
502-- websocketDissectorTable:add("9696", ndn)
503websocketDissectorTable:add("1-65535", ndn)
504
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700505local ethernetDissectorTable = DissectorTable.get("ethertype")
506ethernetDissectorTable:add(0x8624, ndn)
507
Junxiao Shiac4b5462018-04-17 02:26:17 +0000508io.stderr:write("NDN dissector successfully loaded\n")