blob: 0ec0bcc04fa43a9d032c79f7ca1e9b90b4f5603f [file] [log] [blame]
Davide Pesavento105cd9e2019-04-06 18:13:44 -04001-- Copyright (c) 2015-2019, 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
Junxiao Shi5e384792016-11-24 03:59:06 +000040-- @return TLV-VALUE portion of a TLV block
41function getValue(b)
42 return b.tvb(b.offset + b.typeLen + b.lengthLen, b.length)
43end
44
Junxiao Shi89db73c2018-05-27 15:22:49 +000045function getNonNegativeInteger(b)
46 if b.length == 1 or b.length == 2 or b.length == 4 or b.length == 8 then
47 return getValue(b):uint64()
48 end
49 return UInt64.max()
50end
51
Davide Pesavento89b52d42021-11-27 22:10:42 -050052function getUriFromSha256DigestComponent(b)
53 local s = ""
54 if b.type == 1 then
55 s = "sha256digest="
56 elseif b.type == 2 then
57 s = "params-sha256="
58 else
59 assert(false)
60 end
61
Junxiao Shic687af62018-05-06 21:58:09 +000062 for i = 0, (b.length - 1) do
Davide Pesavento89b52d42021-11-27 22:10:42 -050063 local byte = b.tvb(b.offset + b.typeLen + b.lengthLen + i, 1)
Junxiao Shic687af62018-05-06 21:58:09 +000064 s = s .. string.format("%02x", byte:uint())
65 end
66 return s
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -070067end
68
Junxiao Shic687af62018-05-06 21:58:09 +000069function getUriFromNameComponent(b)
Davide Pesavento89b52d42021-11-27 22:10:42 -050070 if b.type == 1 or b.type == 2 then
71 return getUriFromSha256DigestComponent(b)
Alexander Afanasyev357c2052015-08-10 21:26:52 -070072 end
Davide Pesavento89b52d42021-11-27 22:10:42 -050073
74 local s = ""
Junxiao Shic687af62018-05-06 21:58:09 +000075 if b.type ~= 8 then
76 s = string.format("%d=", b.type)
77 end
78 hasNonPeriod = false
79 for i = 0, (b.length - 1) do
Davide Pesavento89b52d42021-11-27 22:10:42 -050080 local byte = b.tvb(b.offset + b.typeLen + b.lengthLen + i, 1)
81 local ch = byte:uint()
Junxiao Shic687af62018-05-06 21:58:09 +000082 hasNonPeriod = hasNonPeriod or ch ~= 0x2E
83 if (ch >= 0x41 and ch <= 0x5A) or (ch >= 0x61 and ch <= 0x7A) or (ch >= 0x30 and ch <= 0x39) or ch == 0x2D or ch == 0x2E or ch == 0x5F or ch == 0x7E then
84 s = s .. byte:string()
85 else
86 s = s .. string.format("%%%02X", ch)
87 end
88 end
89 if not hasNonPeriod then
90 s = s .. "..."
91 end
92 return s
93end
94
95function getUriFromName(b)
96 if b.elements == nil then
97 return "/"
98 end
Davide Pesavento89b52d42021-11-27 22:10:42 -050099
Junxiao Shic687af62018-05-06 21:58:09 +0000100 components = {}
101 for i, comp in pairs(b.elements) do
102 table.insert(components, getUriFromNameComponent(comp))
103 end
104 return "/" .. table.concat(components, "/")
105end
106
107function getUriFromFinalBlockId(b)
108 if b.elements == nil then
109 return "/"
110 end
111 return getUriFromNameComponent(b.elements[1])
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700112end
113
Zipeng Wang574eeb02016-10-05 21:46:02 -0700114function getNackReasonDetail(b)
Davide Pesavento91865282021-11-27 22:18:08 -0500115 assert(b.type == 801)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700116 local code = getNonNegativeInteger(b)
Davide Pesavento91865282021-11-27 22:18:08 -0500117
118 if code == UInt64(0) then return "None"
119 elseif code == UInt64(50) then return "Congestion"
Junxiao Shi89db73c2018-05-27 15:22:49 +0000120 elseif code == UInt64(100) then return "Duplicate"
121 elseif code == UInt64(150) then return "NoRoute"
122 else return tostring(code)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700123 end
124end
125
126function getCachePolicyDetail(b)
Davide Pesavento91865282021-11-27 22:18:08 -0500127 assert(b.type == 821)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700128 local code = getNonNegativeInteger(b)
Davide Pesavento91865282021-11-27 22:18:08 -0500129
130 if code == UInt64(0) then return "None"
131 elseif code == UInt64(1) then return "NoCache"
132 else return tostring(code) .. " (unknown)"
Zipeng Wang574eeb02016-10-05 21:46:02 -0700133 end
134end
135
Junxiao Shi5e384792016-11-24 03:59:06 +0000136function getNonce(b)
137 assert(b.type == 10)
Davide Pesavento91865282021-11-27 22:18:08 -0500138 if b.length ~= 4 then
Junxiao Shi5e384792016-11-24 03:59:06 +0000139 return "invalid (should have 4 octets)"
140 end
141 return getValue(b):uint()
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700142end
143
Davide Pesavento91865282021-11-27 22:18:08 -0500144function getHopLimit(b)
145 assert(b.type == 34)
146 if b.length ~= 1 then
147 return "invalid (should have 1 octet)"
148 end
149 return getValue(b):uint()
150end
151
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700152function getTrue(block)
153 return "Yes"
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700154end
155
Zipeng Wang574eeb02016-10-05 21:46:02 -0700156local AppPrivateBlock1 = 100
157local AppPrivateBlock2 = 800
158local AppPrivateBlock3 = 1000
159
160function canIgnoreTlvType(t)
Davide Pesavento585e18a2021-11-27 22:58:02 -0500161 if t < AppPrivateBlock2 or t >= AppPrivateBlock3 then
Zipeng Wang574eeb02016-10-05 21:46:02 -0700162 return false
163 else
Davide Pesavento585e18a2021-11-27 22:58:02 -0500164 if math.fmod(t, 2) == 1 then
Zipeng Wang574eeb02016-10-05 21:46:02 -0700165 return true
166 else
167 return false
168 end
169 end
170end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700171
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700172function getGenericBlockInfo(block)
173 local name = ""
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700174
Zipeng Wang574eeb02016-10-05 21:46:02 -0700175 -- TODO: Properly format informational message based type value reservations
Davide Pesavento585e18a2021-11-27 22:58:02 -0500176 -- (https://named-data.net/doc/NDN-packet-spec/current/types.html#tlv-type-number-reservations)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700177 if (block.type <= AppPrivateBlock1) then
178 name = "Unrecognized from the reserved range " .. 0 .. "-" .. AppPrivateBlock1 .. ""
179 elseif (AppPrivateBlock1 < block.type and block.type < AppPrivateBlock2) then
180 name = "Unrecognized from the reserved range " .. (AppPrivateBlock1 + 1) .. "-" .. (AppPrivateBlock2 - 1) .. ""
181 elseif (AppPrivateBlock2 <= block.type and block.type <= AppPrivateBlock3) then
Davide Pesavento585e18a2021-11-27 22:58:02 -0500182 if canIgnoreTlvType(block.type) then
Zipeng Wang574eeb02016-10-05 21:46:02 -0700183 name = "Unknown field (ignored)"
184 else
Davide Pesavento585e18a2021-11-27 22:58:02 -0500185 name = "Unknown field"
Zipeng Wang574eeb02016-10-05 21:46:02 -0700186 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700187 else
Zipeng Wang574eeb02016-10-05 21:46:02 -0700188 name = "RESERVED_3"
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700189 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700190
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700191 return name .. ", Type: " .. block.type .. ", Length: " .. block.length
192end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700193
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700194-----------------------------------------------------
195-----------------------------------------------------
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700196
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700197local NDN_DICT = {
Davide Pesavento105cd9e2019-04-06 18:13:44 -0400198 -- Name and name components
Davide Pesavento89b52d42021-11-27 22:10:42 -0500199 [7] = {name = "Name" , field = ProtoField.string("ndn.name", "Name") , value = getUriFromName},
200 [1] = {name = "ImplicitSha256DigestComponent" , field = ProtoField.string("ndn.implicit_sha256", "ImplicitSha256DigestComponent"), value = getUriFromNameComponent},
201 [2] = {name = "ParametersSha256DigestComponent", field = ProtoField.string("ndn.params_sha256", "ParametersSha256DigestComponent"), value = getUriFromNameComponent},
202 [8] = {name = "GenericNameComponent" , field = ProtoField.string("ndn.namecomponent", "GenericNameComponent") , value = getUriFromNameComponent},
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700203
Junxiao Shiac4b5462018-04-17 02:26:17 +0000204 -- Interest and its sub-elements in Packet Format v0.3
205 [5] = {name = "Interest" , summary = true},
206 [33] = {name = "CanBePrefix" , field = ProtoField.string("ndn.canbeprefix", "CanBePrefix") , value = getTrue},
207 [18] = {name = "MustBeFresh" , field = ProtoField.string("ndn.mustbefresh", "MustBeFresh") , value = getTrue},
208 -- [30] = {name = "ForwardingHint" , summary = true},
209 -- ForwardingHint and LinkPreference have the same TLV-TYPE number, so we can't recognize both for now (see #4185).
210 [31] = {name = "LinkDelegation" , summary = true},
Davide Pesavento105cd9e2019-04-06 18:13:44 -0400211 [30] = {name = "LinkPreference" , field = ProtoField.uint64("ndn.linkpreference", "LinkPreference", base.DEC) , value = getNonNegativeInteger},
Junxiao Shi5e384792016-11-24 03:59:06 +0000212 [10] = {name = "Nonce" , field = ProtoField.uint32("ndn.nonce", "Nonce", base.HEX) , value = getNonce},
Davide Pesavento91865282021-11-27 22:18:08 -0500213 [12] = {name = "InterestLifetime" , field = ProtoField.uint64("ndn.lifetime", "InterestLifetime", base.DEC) , value = getNonNegativeInteger},
214 [34] = {name = "HopLimit" , field = ProtoField.uint8("ndn.hoplimit", "HopLimit", base.DEC) , value = getHopLimit},
215 [36] = {name = "ApplicationParameters" , field = ProtoField.bytes("ndn.app_params", "ApplicationParameters")},
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700216
Junxiao Shiac4b5462018-04-17 02:26:17 +0000217 -- Data and its sub-elements in Packet Format v0.3
218 [6] = {name = "Data" , summary = true},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700219 [20] = {name = "MetaInfo" , summary = true},
Davide Pesavento91865282021-11-27 22:18:08 -0500220 [24] = {name = "ContentType" , field = ProtoField.uint64("ndn.contenttype", "ContentType", base.DEC) , value = getNonNegativeInteger},
221 [25] = {name = "FreshnessPeriod" , field = ProtoField.uint64("ndn.freshness", "FreshnessPeriod", base.DEC) , value = getNonNegativeInteger},
222 [26] = {name = "FinalBlockId" , field = ProtoField.string("ndn.finalblock", "FinalBlockId") , value = getUriFromFinalBlockId},
223 [21] = {name = "Content" , field = ProtoField.bytes("ndn.content", "Content")},
Junxiao Shiac4b5462018-04-17 02:26:17 +0000224 [22] = {name = "SignatureInfo" , summary = true},
Davide Pesavento585e18a2021-11-27 22:58:02 -0500225 [27] = {name = "SignatureType" , field = ProtoField.uint64("ndn.sigtype", "SignatureType", base.DEC) , value = getNonNegativeInteger},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700226 [28] = {name = "KeyLocator" , summary = true},
227 [29] = {name = "KeyDigest" , field = ProtoField.bytes("ndn.keydigest", "KeyDigest")},
Davide Pesavento585e18a2021-11-27 22:58:02 -0500228 [23] = {name = "SignatureValue" , field = ProtoField.bytes("ndn.sigvalue", "SignatureValue")},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700229
Junxiao Shiac4b5462018-04-17 02:26:17 +0000230 -- NDNLPv2 headers
Zipeng Wang574eeb02016-10-05 21:46:02 -0700231 [80] = {name = "Fragment" },
Junxiao Shi89db73c2018-05-27 15:22:49 +0000232 [81] = {name = "Sequence" , field = ProtoField.uint64("ndn.sequence", "Sequence", base.DEC) , value = getNonNegativeInteger},
233 [82] = {name = "FragIndex" , field = ProtoField.uint64("ndn.fragindex", "FragIndex", base.DEC) , value = getNonNegativeInteger},
234 [83] = {name = "FragCount" , field = ProtoField.uint64("ndn.fragcount", "FragCount", base.DEC) , value = getNonNegativeInteger},
Davide Pesavento91865282021-11-27 22:18:08 -0500235 [98] = {name = "PitToken" , field = ProtoField.bytes("ndn.pit_token", "PitToken")},
Zipeng Wang574eeb02016-10-05 21:46:02 -0700236 [100] = {name = "LpPacket" , summary = true},
237 [800] = {name = "Nack" , summary = true},
Junxiao Shi89db73c2018-05-27 15:22:49 +0000238 [801] = {name = "NackReason" , field = ProtoField.string("ndn.nack_reason", "NackReason") , value = getNackReasonDetail},
Davide Pesavento38dca392021-12-20 20:50:45 -0500239 [812] = {name = "IncomingFaceId" , field = ProtoField.uint64("ndn.incoming_face", "IncomingFaceId", base.DEC) , value = getNonNegativeInteger},
Davide Pesavento585e18a2021-11-27 22:58:02 -0500240 [816] = {name = "NextHopFaceId" , field = ProtoField.uint64("ndn.nexthop_face", "NextHopFaceId", base.DEC) , value = getNonNegativeInteger},
Zipeng Wang574eeb02016-10-05 21:46:02 -0700241 [820] = {name = "CachePolicy" , summary = true},
Davide Pesavento585e18a2021-11-27 22:58:02 -0500242 [821] = {name = "CachePolicyType" , field = ProtoField.string("ndn.cache_policy", "CachePolicyType") , value = getCachePolicyDetail},
Junxiao Shi89db73c2018-05-27 15:22:49 +0000243 [832] = {name = "CongestionMark" , field = ProtoField.uint64("ndn.congestion_mark", "CongestionMark", base.DEC) , value = getNonNegativeInteger},
244 [836] = {name = "Ack" , field = ProtoField.uint64("ndn.ack", "Ack", base.DEC) , value = getNonNegativeInteger},
Davide Pesavento200014a2021-11-27 21:21:47 -0500245 [840] = {name = "TxSequence" , field = ProtoField.uint64("ndn.txseq", "TxSequence", base.DEC) , value = getNonNegativeInteger},
Zipeng Wang574eeb02016-10-05 21:46:02 -0700246}
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700247
248-- -- Add protofields in NDN protocol
Davide Pesavento200014a2021-11-27 21:21:47 -0500249ndn.fields = {}
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700250for key, value in pairs(NDN_DICT) do
251 table.insert(ndn.fields, value.field)
252end
253
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700254-----------------------------------------------------
255-----------------------------------------------------
256
257-- block
258-- .tvb
259-- .offset
260-- .type
261-- .typeLen
262-- .length
263-- .lengthLen
264-- .size = .typeLen + .lengthLen + .length
265
266function addInfo(block, root) -- may be add additional context later
267 local info = NDN_DICT[block.type]
268
269 if (info == nil) then
270 info = {}
271 info.value = getGenericBlockInfo
Zipeng Wang574eeb02016-10-05 21:46:02 -0700272 -- color
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700273 end
274
275 local treeInfo
276 if (info.value == nil) then
277
278 if (info.field ~= nil) then
279 treeInfo = root:add(info.field, block.tvb(block.offset, block.size))
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700280 else
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700281 treeInfo = root:add(block.tvb(block.offset, block.size), info.name)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700282 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700283
284 treeInfo:append_text(", Type: " .. block.type .. ", Length: " .. block.length)
285 else
286 block.value = info.value(block)
287
288 if (info.field ~= nil) then
289 treeInfo = root:add(info.field, block.tvb(block.offset, block.size), block.value)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700290 else
Zipeng Wang574eeb02016-10-05 21:46:02 -0700291 treeInfo = root:add(block.tvb(block.offset, block.size), block.value)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700292 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700293 end
294 block.root = treeInfo
295 return block.root
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700296end
297
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700298function addSummary(block)
299 if (block.elements == nil) then
300 return
301 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700302
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700303 local info = NDN_DICT[block.type]
304 if (info == nil or info.summary == nil) then
305 return
306 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700307
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700308 local summary = {}
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700309
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700310 for k, subblock in pairs(block.elements) do
311 if (subblock.value ~= nil) then
312 local info = NDN_DICT[subblock.type]
313 if (info ~= nil) then
314 table.insert(summary, info.name .. ": " .. subblock.value)
315 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700316 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700317 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700318
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700319 if (#summary > 0) then
320 block.summary = table.concat(summary, ", ")
321 if (block.value == nil) then
322 block.value = block.summary
323 end
324 block.root:append_text(", " .. block.summary)
325 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700326end
327
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700328-----------------------------------------------------
329-----------------------------------------------------
330
331function readVarNumber(tvb, offset)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000332 if offset >= tvb:len() then
333 return 0, 0
334 end
335
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700336 local firstOctet = tvb(offset, 1):uint()
337 if (firstOctet < 253) then
338 return firstOctet, 1
Junxiao Shie65c6d72016-07-24 21:53:19 +0000339 elseif (firstOctet == 253) and (offset + 3 < tvb:len()) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700340 return tvb(offset + 1, 2):uint(), 3
Junxiao Shie65c6d72016-07-24 21:53:19 +0000341 elseif (firstOctet == 254) and (offset + 5 < tvb:len()) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700342 return tvb(offset + 1, 4):uint(), 5
Junxiao Shie65c6d72016-07-24 21:53:19 +0000343 elseif (firstOctet == 255) and (offset + 9 < tvb:len()) then
344 return tvb(offset + 1, 8):uint64(), 9
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700345 end
Junxiao Shie65c6d72016-07-24 21:53:19 +0000346
347 return 0, 0
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700348end
349
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700350function getBlock(tvb, offset)
351 local block = {}
352 block.tvb = tvb
353 block.offset = offset
354
355 block.type, block.typeLen = readVarNumber(block.tvb, block.offset)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000356 if block.typeLen == 0 then
357 return nil
358 end
359
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700360 block.length, block.lengthLen = readVarNumber(block.tvb, block.offset + block.typeLen)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000361 if block.lengthLen == 0 then
362 return nil
363 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700364
365 block.size = block.typeLen + block.lengthLen + block.length
366
367 return block
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700368end
369
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700370function canBeValidNdnPacket(block)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700371 if ((block.type == 5 or block.type == 6 or block.type == 100) and block.length <= 8800) then
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700372 return true
373 else
374 return false
375 end
376end
377
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700378function findNdnPacket(tvb)
379 offset = 0
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700380
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700381 while offset + 2 < tvb:len() do
382 local block = getBlock(tvb, offset)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700383
Junxiao Shie65c6d72016-07-24 21:53:19 +0000384 if (block ~= nil) and canBeValidNdnPacket(block) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700385 return block
386 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700387
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700388 offset = offset + 1
389 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700390
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700391 return nil
392end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700393
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700394function getSubBlocks(block)
395 local valueLeft = block.length
396 local subBlocks = {}
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700397
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700398 while valueLeft > 0 do
Junxiao Shie65c6d72016-07-24 21:53:19 +0000399 local offset = block.offset + block.typeLen + block.lengthLen + (block.length - valueLeft)
400 local child = getBlock(block.tvb, offset)
401 if child == nil then
402 return nil
403 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700404
405 valueLeft = valueLeft - child.size
406 table.insert(subBlocks, child)
407 end
408
409 if (valueLeft == 0) then
410 return subBlocks
411 else
412 return nil
413 end
414end
415
416-----------------------------------------------------
417-----------------------------------------------------
418
419-- NDN protocol dissector function
420function ndn.dissector(tvb, pInfo, root) -- Tvb, Pinfo, TreeItem
421
422 if (tvb:len() ~= tvb:reported_len()) then
423 return 0 -- ignore partially captured packets
424 -- this can/may be re-enabled only for unfragmented UDP packets
425 end
426
427 local ok, block = pcall(findNdnPacket, tvb)
428 if (not ok) then
429 return 0
430 end
431
432 if (block == nil or block.offset == nil) then
433 -- no valid NDN packets found
434 return 0
435 end
436
437 local nBytesLeft = tvb:len() - block.offset
438 -- print (pInfo.number .. ":: Found block: " .. block.type .. " of length " .. block.size .. " bytesLeft: " .. nBytesLeft)
439
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000440 local pktType = ""
441 local pktName = ""
442
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700443 while (block.size <= nBytesLeft) do
444 -- Create TreeItems
445 block.tree = root:add(ndn, tvb(block.offset, block.size))
446
447 local queue = {block}
448 while (#queue > 0) do
449 local block = queue[1]
450 table.remove(queue, 1)
451
452 block.elements = getSubBlocks(block)
453 local subtree = addInfo(block, block.tree)
454
455 if (block.elements ~= nil) then
456 for i, subBlock in pairs(block.elements) do
457 subBlock.tree = subtree
458 table.insert(queue, subBlock)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700459 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700460 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700461 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700462
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700463 -- Make summaries
464 local queue = {block}
465 while (#queue > 0) do
466 local block = queue[1]
467 if (block.visited ~= nil or block.elements == nil) then
468 -- try to make summary
469 table.remove(queue, 1)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700470
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700471 addSummary(block)
472 else
473 for i, subBlock in pairs(block.elements) do
474 table.insert(queue, 1, subBlock)
475 end
476 block.visited = true
477 end
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000478
479 -- prepare information to fill info column
480 if block.type == 5 and pktType ~= "Nack" then
481 pktType = "Interest"
482 elseif block.type == 6 then
483 pktType = "Data"
484 elseif block.type == 800 then
485 pktType = "Nack"
486 end
487
Alexander Afanasyev4fb67ea2018-08-02 08:18:28 -0600488 if block.type == 7 then
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000489 pktName = getUriFromName(block)
490 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700491 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700492
493 local info = NDN_DICT[block.type]
494 if (info ~= nil) then
Zipeng Wang574eeb02016-10-05 21:46:02 -0700495 if (block.summary ~= nil) then
496 block.tree:append_text(", " .. NDN_DICT[block.type].name .. ", " .. block.summary)
497 else
498 block.tree:append_text(", " .. NDN_DICT[block.type].name)
499 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700500 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700501
502 nBytesLeft = nBytesLeft - block.size
503
504 if (nBytesLeft > 0) then
505 ok, block = pcall(getBlock, tvb, tvb:len() - nBytesLeft)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700506 if (not ok or block == nil or not canBeValidNdnPacket(block)) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700507 break
508 end
509 end
Zipeng Wang574eeb02016-10-05 21:46:02 -0700510 end -- while(block.size <= nBytesLeft)
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700511
512 pInfo.cols.protocol = tostring(pInfo.cols.protocol) .. " (" .. ndn.name .. ")"
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000513 pInfo.cols.info = pktType .. " " .. pktName
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700514
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700515 if (nBytesLeft > 0 and block ~= nil and block.size ~= nil and block.size > nBytesLeft) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700516 pInfo.desegment_offset = tvb:len() - nBytesLeft
517
518 -- Originally, I set desegment_len to the exact lenght, but it mysteriously didn't work for TCP
519 -- pInfo.desegment_len = block.size -- this will not work to desegment TCP streams
520 pInfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT
521 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700522end
523
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700524local udpDissectorTable = DissectorTable.get("udp.port")
525udpDissectorTable:add("6363", ndn)
526udpDissectorTable:add("56363", ndn)
527
528local tcpDissectorTable = DissectorTable.get("tcp.port")
529tcpDissectorTable:add("6363", ndn)
530
531local websocketDissectorTable = DissectorTable.get("ws.port")
532-- websocketDissectorTable:add("9696", ndn)
533websocketDissectorTable:add("1-65535", ndn)
534
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700535local ethernetDissectorTable = DissectorTable.get("ethertype")
536ethernetDissectorTable:add(0x8624, ndn)
537
Alexander Afanasyev4fb67ea2018-08-02 08:18:28 -0600538local pppDissectorTable = DissectorTable.get("ppp.protocol")
539pppDissectorTable:add(0x0077, ndn)
540
Junxiao Shiac4b5462018-04-17 02:26:17 +0000541io.stderr:write("NDN dissector successfully loaded\n")