blob: f243a98e4e9cdb0e30224bfb60ecfa5ebc075c99 [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},
Davide Pesavento84eb4872021-11-27 23:01:04 -0500208 [30] = {name = "ForwardingHint" , summary = true},
Junxiao Shi5e384792016-11-24 03:59:06 +0000209 [10] = {name = "Nonce" , field = ProtoField.uint32("ndn.nonce", "Nonce", base.HEX) , value = getNonce},
Davide Pesavento91865282021-11-27 22:18:08 -0500210 [12] = {name = "InterestLifetime" , field = ProtoField.uint64("ndn.lifetime", "InterestLifetime", base.DEC) , value = getNonNegativeInteger},
211 [34] = {name = "HopLimit" , field = ProtoField.uint8("ndn.hoplimit", "HopLimit", base.DEC) , value = getHopLimit},
212 [36] = {name = "ApplicationParameters" , field = ProtoField.bytes("ndn.app_params", "ApplicationParameters")},
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700213
Junxiao Shiac4b5462018-04-17 02:26:17 +0000214 -- Data and its sub-elements in Packet Format v0.3
215 [6] = {name = "Data" , summary = true},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700216 [20] = {name = "MetaInfo" , summary = true},
Davide Pesavento91865282021-11-27 22:18:08 -0500217 [24] = {name = "ContentType" , field = ProtoField.uint64("ndn.contenttype", "ContentType", base.DEC) , value = getNonNegativeInteger},
218 [25] = {name = "FreshnessPeriod" , field = ProtoField.uint64("ndn.freshness", "FreshnessPeriod", base.DEC) , value = getNonNegativeInteger},
219 [26] = {name = "FinalBlockId" , field = ProtoField.string("ndn.finalblock", "FinalBlockId") , value = getUriFromFinalBlockId},
220 [21] = {name = "Content" , field = ProtoField.bytes("ndn.content", "Content")},
Junxiao Shiac4b5462018-04-17 02:26:17 +0000221 [22] = {name = "SignatureInfo" , summary = true},
Davide Pesavento585e18a2021-11-27 22:58:02 -0500222 [27] = {name = "SignatureType" , field = ProtoField.uint64("ndn.sigtype", "SignatureType", base.DEC) , value = getNonNegativeInteger},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700223 [28] = {name = "KeyLocator" , summary = true},
224 [29] = {name = "KeyDigest" , field = ProtoField.bytes("ndn.keydigest", "KeyDigest")},
Davide Pesavento585e18a2021-11-27 22:58:02 -0500225 [23] = {name = "SignatureValue" , field = ProtoField.bytes("ndn.sigvalue", "SignatureValue")},
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700226
Junxiao Shiac4b5462018-04-17 02:26:17 +0000227 -- NDNLPv2 headers
Zipeng Wang574eeb02016-10-05 21:46:02 -0700228 [80] = {name = "Fragment" },
Junxiao Shi89db73c2018-05-27 15:22:49 +0000229 [81] = {name = "Sequence" , field = ProtoField.uint64("ndn.sequence", "Sequence", base.DEC) , value = getNonNegativeInteger},
230 [82] = {name = "FragIndex" , field = ProtoField.uint64("ndn.fragindex", "FragIndex", base.DEC) , value = getNonNegativeInteger},
231 [83] = {name = "FragCount" , field = ProtoField.uint64("ndn.fragcount", "FragCount", base.DEC) , value = getNonNegativeInteger},
Davide Pesavento91865282021-11-27 22:18:08 -0500232 [98] = {name = "PitToken" , field = ProtoField.bytes("ndn.pit_token", "PitToken")},
Zipeng Wang574eeb02016-10-05 21:46:02 -0700233 [100] = {name = "LpPacket" , summary = true},
234 [800] = {name = "Nack" , summary = true},
Junxiao Shi89db73c2018-05-27 15:22:49 +0000235 [801] = {name = "NackReason" , field = ProtoField.string("ndn.nack_reason", "NackReason") , value = getNackReasonDetail},
Davide Pesavento38dca392021-12-20 20:50:45 -0500236 [812] = {name = "IncomingFaceId" , field = ProtoField.uint64("ndn.incoming_face", "IncomingFaceId", base.DEC) , value = getNonNegativeInteger},
Davide Pesavento585e18a2021-11-27 22:58:02 -0500237 [816] = {name = "NextHopFaceId" , field = ProtoField.uint64("ndn.nexthop_face", "NextHopFaceId", base.DEC) , value = getNonNegativeInteger},
Zipeng Wang574eeb02016-10-05 21:46:02 -0700238 [820] = {name = "CachePolicy" , summary = true},
Davide Pesavento585e18a2021-11-27 22:58:02 -0500239 [821] = {name = "CachePolicyType" , field = ProtoField.string("ndn.cache_policy", "CachePolicyType") , value = getCachePolicyDetail},
Junxiao Shi89db73c2018-05-27 15:22:49 +0000240 [832] = {name = "CongestionMark" , field = ProtoField.uint64("ndn.congestion_mark", "CongestionMark", base.DEC) , value = getNonNegativeInteger},
241 [836] = {name = "Ack" , field = ProtoField.uint64("ndn.ack", "Ack", base.DEC) , value = getNonNegativeInteger},
Davide Pesavento200014a2021-11-27 21:21:47 -0500242 [840] = {name = "TxSequence" , field = ProtoField.uint64("ndn.txseq", "TxSequence", base.DEC) , value = getNonNegativeInteger},
Zipeng Wang574eeb02016-10-05 21:46:02 -0700243}
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700244
245-- -- Add protofields in NDN protocol
Davide Pesavento200014a2021-11-27 21:21:47 -0500246ndn.fields = {}
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700247for key, value in pairs(NDN_DICT) do
248 table.insert(ndn.fields, value.field)
249end
250
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700251-----------------------------------------------------
252-----------------------------------------------------
253
254-- block
255-- .tvb
256-- .offset
257-- .type
258-- .typeLen
259-- .length
260-- .lengthLen
261-- .size = .typeLen + .lengthLen + .length
262
263function addInfo(block, root) -- may be add additional context later
264 local info = NDN_DICT[block.type]
265
266 if (info == nil) then
267 info = {}
268 info.value = getGenericBlockInfo
Zipeng Wang574eeb02016-10-05 21:46:02 -0700269 -- color
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700270 end
271
272 local treeInfo
273 if (info.value == nil) then
274
275 if (info.field ~= nil) then
276 treeInfo = root:add(info.field, block.tvb(block.offset, block.size))
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700277 else
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700278 treeInfo = root:add(block.tvb(block.offset, block.size), info.name)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700279 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700280
281 treeInfo:append_text(", Type: " .. block.type .. ", Length: " .. block.length)
282 else
283 block.value = info.value(block)
284
285 if (info.field ~= nil) then
286 treeInfo = root:add(info.field, block.tvb(block.offset, block.size), block.value)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700287 else
Zipeng Wang574eeb02016-10-05 21:46:02 -0700288 treeInfo = root:add(block.tvb(block.offset, block.size), block.value)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700289 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700290 end
291 block.root = treeInfo
292 return block.root
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700293end
294
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700295function addSummary(block)
296 if (block.elements == nil) then
297 return
298 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700299
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700300 local info = NDN_DICT[block.type]
301 if (info == nil or info.summary == nil) then
302 return
303 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700304
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700305 local summary = {}
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700306
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700307 for k, subblock in pairs(block.elements) do
308 if (subblock.value ~= nil) then
309 local info = NDN_DICT[subblock.type]
310 if (info ~= nil) then
311 table.insert(summary, info.name .. ": " .. subblock.value)
312 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700313 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700314 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700315
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700316 if (#summary > 0) then
317 block.summary = table.concat(summary, ", ")
318 if (block.value == nil) then
319 block.value = block.summary
320 end
321 block.root:append_text(", " .. block.summary)
322 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700323end
324
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700325-----------------------------------------------------
326-----------------------------------------------------
327
328function readVarNumber(tvb, offset)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000329 if offset >= tvb:len() then
330 return 0, 0
331 end
332
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700333 local firstOctet = tvb(offset, 1):uint()
334 if (firstOctet < 253) then
335 return firstOctet, 1
Junxiao Shie65c6d72016-07-24 21:53:19 +0000336 elseif (firstOctet == 253) and (offset + 3 < tvb:len()) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700337 return tvb(offset + 1, 2):uint(), 3
Junxiao Shie65c6d72016-07-24 21:53:19 +0000338 elseif (firstOctet == 254) and (offset + 5 < tvb:len()) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700339 return tvb(offset + 1, 4):uint(), 5
Junxiao Shie65c6d72016-07-24 21:53:19 +0000340 elseif (firstOctet == 255) and (offset + 9 < tvb:len()) then
341 return tvb(offset + 1, 8):uint64(), 9
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700342 end
Junxiao Shie65c6d72016-07-24 21:53:19 +0000343
344 return 0, 0
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700345end
346
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700347function getBlock(tvb, offset)
348 local block = {}
349 block.tvb = tvb
350 block.offset = offset
351
352 block.type, block.typeLen = readVarNumber(block.tvb, block.offset)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000353 if block.typeLen == 0 then
354 return nil
355 end
356
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700357 block.length, block.lengthLen = readVarNumber(block.tvb, block.offset + block.typeLen)
Junxiao Shie65c6d72016-07-24 21:53:19 +0000358 if block.lengthLen == 0 then
359 return nil
360 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700361
362 block.size = block.typeLen + block.lengthLen + block.length
363
364 return block
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700365end
366
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700367function canBeValidNdnPacket(block)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700368 if ((block.type == 5 or block.type == 6 or block.type == 100) and block.length <= 8800) then
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700369 return true
370 else
371 return false
372 end
373end
374
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700375function findNdnPacket(tvb)
376 offset = 0
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700377
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700378 while offset + 2 < tvb:len() do
379 local block = getBlock(tvb, offset)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700380
Junxiao Shie65c6d72016-07-24 21:53:19 +0000381 if (block ~= nil) and canBeValidNdnPacket(block) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700382 return block
383 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700384
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700385 offset = offset + 1
386 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700387
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700388 return nil
389end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700390
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700391function getSubBlocks(block)
392 local valueLeft = block.length
393 local subBlocks = {}
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700394
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700395 while valueLeft > 0 do
Junxiao Shie65c6d72016-07-24 21:53:19 +0000396 local offset = block.offset + block.typeLen + block.lengthLen + (block.length - valueLeft)
397 local child = getBlock(block.tvb, offset)
398 if child == nil then
399 return nil
400 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700401
402 valueLeft = valueLeft - child.size
403 table.insert(subBlocks, child)
404 end
405
406 if (valueLeft == 0) then
407 return subBlocks
408 else
409 return nil
410 end
411end
412
413-----------------------------------------------------
414-----------------------------------------------------
415
416-- NDN protocol dissector function
417function ndn.dissector(tvb, pInfo, root) -- Tvb, Pinfo, TreeItem
418
419 if (tvb:len() ~= tvb:reported_len()) then
420 return 0 -- ignore partially captured packets
421 -- this can/may be re-enabled only for unfragmented UDP packets
422 end
423
424 local ok, block = pcall(findNdnPacket, tvb)
425 if (not ok) then
426 return 0
427 end
428
429 if (block == nil or block.offset == nil) then
430 -- no valid NDN packets found
431 return 0
432 end
433
434 local nBytesLeft = tvb:len() - block.offset
435 -- print (pInfo.number .. ":: Found block: " .. block.type .. " of length " .. block.size .. " bytesLeft: " .. nBytesLeft)
436
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000437 local pktType = ""
438 local pktName = ""
439
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700440 while (block.size <= nBytesLeft) do
441 -- Create TreeItems
442 block.tree = root:add(ndn, tvb(block.offset, block.size))
443
444 local queue = {block}
445 while (#queue > 0) do
446 local block = queue[1]
447 table.remove(queue, 1)
448
449 block.elements = getSubBlocks(block)
450 local subtree = addInfo(block, block.tree)
451
452 if (block.elements ~= nil) then
453 for i, subBlock in pairs(block.elements) do
454 subBlock.tree = subtree
455 table.insert(queue, subBlock)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700456 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700457 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700458 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700459
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700460 -- Make summaries
461 local queue = {block}
462 while (#queue > 0) do
463 local block = queue[1]
464 if (block.visited ~= nil or block.elements == nil) then
465 -- try to make summary
466 table.remove(queue, 1)
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700467
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700468 addSummary(block)
469 else
470 for i, subBlock in pairs(block.elements) do
471 table.insert(queue, 1, subBlock)
472 end
473 block.visited = true
474 end
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000475
476 -- prepare information to fill info column
477 if block.type == 5 and pktType ~= "Nack" then
478 pktType = "Interest"
479 elseif block.type == 6 then
480 pktType = "Data"
481 elseif block.type == 800 then
482 pktType = "Nack"
483 end
484
Alexander Afanasyev4fb67ea2018-08-02 08:18:28 -0600485 if block.type == 7 then
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000486 pktName = getUriFromName(block)
487 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700488 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700489
490 local info = NDN_DICT[block.type]
491 if (info ~= nil) then
Zipeng Wang574eeb02016-10-05 21:46:02 -0700492 if (block.summary ~= nil) then
493 block.tree:append_text(", " .. NDN_DICT[block.type].name .. ", " .. block.summary)
494 else
495 block.tree:append_text(", " .. NDN_DICT[block.type].name)
496 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700497 end
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700498
499 nBytesLeft = nBytesLeft - block.size
500
501 if (nBytesLeft > 0) then
502 ok, block = pcall(getBlock, tvb, tvb:len() - nBytesLeft)
Zipeng Wang574eeb02016-10-05 21:46:02 -0700503 if (not ok or block == nil or not canBeValidNdnPacket(block)) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700504 break
505 end
506 end
Zipeng Wang574eeb02016-10-05 21:46:02 -0700507 end -- while(block.size <= nBytesLeft)
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700508
509 pInfo.cols.protocol = tostring(pInfo.cols.protocol) .. " (" .. ndn.name .. ")"
Junxiao Shi1fda67d2018-01-12 21:35:31 +0000510 pInfo.cols.info = pktType .. " " .. pktName
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700511
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700512 if (nBytesLeft > 0 and block ~= nil and block.size ~= nil and block.size > nBytesLeft) then
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700513 pInfo.desegment_offset = tvb:len() - nBytesLeft
514
515 -- Originally, I set desegment_len to the exact lenght, but it mysteriously didn't work for TCP
516 -- pInfo.desegment_len = block.size -- this will not work to desegment TCP streams
517 pInfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT
518 end
Alexander Afanasyev6fbb7b42015-08-10 11:53:49 -0700519end
520
Alexander Afanasyev357c2052015-08-10 21:26:52 -0700521local udpDissectorTable = DissectorTable.get("udp.port")
522udpDissectorTable:add("6363", ndn)
523udpDissectorTable:add("56363", ndn)
524
525local tcpDissectorTable = DissectorTable.get("tcp.port")
526tcpDissectorTable:add("6363", ndn)
527
528local websocketDissectorTable = DissectorTable.get("ws.port")
529-- websocketDissectorTable:add("9696", ndn)
530websocketDissectorTable:add("1-65535", ndn)
531
Alexander Afanasyev7f43c532015-08-12 15:28:51 -0700532local ethernetDissectorTable = DissectorTable.get("ethertype")
533ethernetDissectorTable:add(0x8624, ndn)
534
Alexander Afanasyev4fb67ea2018-08-02 08:18:28 -0600535local pppDissectorTable = DissectorTable.get("ppp.protocol")
536pppDissectorTable:add(0x0077, ndn)
537
Junxiao Shiac4b5462018-04-17 02:26:17 +0000538io.stderr:write("NDN dissector successfully loaded\n")