Junxiao Shi | 1fda67d | 2018-01-12 21:35:31 +0000 | [diff] [blame] | 1 | -- Copyright (c) 2015-2018, Regents of the University of California. |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 2 | -- |
| 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 Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 20 | -- @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 21 | -- @author Zipeng Wang |
| 22 | -- @author Qianshan Yu |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 23 | |
| 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 Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 29 | ndn = Proto("ndn", "Named Data Networking (NDN)") |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 30 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 31 | -- 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 Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 36 | ----------------------------------------------------- |
| 37 | ----------------------------------------------------- |
| 38 | -- Field formatting helpers |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 40 | -- Borrowed from http://lua-users.org/wiki/StringRecipes |
| 41 | function 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 49 | end |
| 50 | |
Junxiao Shi | 5e38479 | 2016-11-24 03:59:06 +0000 | [diff] [blame] | 51 | -- @return TLV-VALUE portion of a TLV block |
| 52 | function getValue(b) |
| 53 | return b.tvb(b.offset + b.typeLen + b.lengthLen, b.length) |
| 54 | end |
| 55 | |
| 56 | function getUriFromNameComponent(b) |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 57 | -- @todo Implement proper proper URL escaping |
Junxiao Shi | 5e38479 | 2016-11-24 03:59:06 +0000 | [diff] [blame] | 58 | return getValue(b):string() |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 59 | end |
| 60 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 61 | function 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 72 | end |
| 73 | |
Junxiao Shi | 5e38479 | 2016-11-24 03:59:06 +0000 | [diff] [blame] | 74 | function getUriFromExclude(block) |
| 75 | -- @todo |
| 76 | return "" |
| 77 | end |
| 78 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 79 | function 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 |
| 87 | end |
| 88 | |
| 89 | function getCachePolicyDetail(b) |
| 90 | local code = getNonNegativeInteger(b) |
| 91 | if (code == 1) then return "NoCache" |
| 92 | else return "Unknown" |
| 93 | end |
| 94 | end |
| 95 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 96 | function getNonNegativeInteger(b) |
Junxiao Shi | 5e38479 | 2016-11-24 03:59:06 +0000 | [diff] [blame] | 97 | if (b.length == 1 or b.length == 2 or b.length == 4) then |
| 98 | return getValue(b):uint() |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 99 | -- 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 105 | end |
| 106 | |
Junxiao Shi | 5e38479 | 2016-11-24 03:59:06 +0000 | [diff] [blame] | 107 | function 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 113 | end |
| 114 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 115 | function getTrue(block) |
| 116 | return "Yes" |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 117 | end |
| 118 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 119 | local AppPrivateBlock1 = 100 |
| 120 | local AppPrivateBlock2 = 800 |
| 121 | local AppPrivateBlock3 = 1000 |
| 122 | |
| 123 | function 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 |
| 134 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 136 | function getGenericBlockInfo(block) |
| 137 | local name = "" |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 138 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 139 | -- 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 Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 151 | else |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 152 | name = "RESERVED_3" |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 153 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 154 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 155 | return name .. ", Type: " .. block.type .. ", Length: " .. block.length |
| 156 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 157 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 158 | ----------------------------------------------------- |
| 159 | ----------------------------------------------------- |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 160 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 161 | local NDN_DICT = { |
| 162 | -- Interest or Data packets |
| 163 | [5] = {name = "Interest" , summary = true}, |
| 164 | [6] = {name = "Data" , summary = true}, |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 166 | -- 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 170 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 171 | -- Sub-fields of Interest packet |
| 172 | [9] = {name = "Selectors" , summary = true}, |
Junxiao Shi | 5e38479 | 2016-11-24 03:59:06 +0000 | [diff] [blame] | 173 | [10] = {name = "Nonce" , field = ProtoField.uint32("ndn.nonce", "Nonce", base.HEX) , value = getNonce}, |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 174 | [12] = {name = "InterestLifetime" , field = ProtoField.uint32("ndn.interestlifetime", "InterestLifetime", base.DEC) , value = getNonNegativeInteger}, |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 175 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 176 | -- 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 184 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 185 | -- 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 190 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 191 | -- 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 Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 205 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 206 | -- 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 Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 219 | |
| 220 | -- -- Add protofields in NDN protocol |
| 221 | ndn.fields = { |
| 222 | } |
| 223 | for key, value in pairs(NDN_DICT) do |
| 224 | table.insert(ndn.fields, value.field) |
| 225 | end |
| 226 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 227 | ----------------------------------------------------- |
| 228 | ----------------------------------------------------- |
| 229 | |
| 230 | -- block |
| 231 | -- .tvb |
| 232 | -- .offset |
| 233 | -- .type |
| 234 | -- .typeLen |
| 235 | -- .length |
| 236 | -- .lengthLen |
| 237 | -- .size = .typeLen + .lengthLen + .length |
| 238 | |
| 239 | function 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 Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 245 | -- color |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 246 | 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 253 | else |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 254 | treeInfo = root:add(block.tvb(block.offset, block.size), info.name) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 255 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 256 | |
| 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 263 | else |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 264 | treeInfo = root:add(block.tvb(block.offset, block.size), block.value) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 265 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 266 | end |
| 267 | block.root = treeInfo |
| 268 | return block.root |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 269 | end |
| 270 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 271 | function addSummary(block) |
| 272 | if (block.elements == nil) then |
| 273 | return |
| 274 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 275 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 276 | local info = NDN_DICT[block.type] |
| 277 | if (info == nil or info.summary == nil) then |
| 278 | return |
| 279 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 280 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 281 | local summary = {} |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 282 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 283 | 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 289 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 290 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 291 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 292 | 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 299 | end |
| 300 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 301 | ----------------------------------------------------- |
| 302 | ----------------------------------------------------- |
| 303 | |
| 304 | function readVarNumber(tvb, offset) |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 305 | if offset >= tvb:len() then |
| 306 | return 0, 0 |
| 307 | end |
| 308 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 309 | local firstOctet = tvb(offset, 1):uint() |
| 310 | if (firstOctet < 253) then |
| 311 | return firstOctet, 1 |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 312 | elseif (firstOctet == 253) and (offset + 3 < tvb:len()) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 313 | return tvb(offset + 1, 2):uint(), 3 |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 314 | elseif (firstOctet == 254) and (offset + 5 < tvb:len()) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 315 | return tvb(offset + 1, 4):uint(), 5 |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 316 | elseif (firstOctet == 255) and (offset + 9 < tvb:len()) then |
| 317 | return tvb(offset + 1, 8):uint64(), 9 |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 318 | end |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 319 | |
| 320 | return 0, 0 |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 321 | end |
| 322 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 323 | function 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 Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 329 | if block.typeLen == 0 then |
| 330 | return nil |
| 331 | end |
| 332 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 333 | block.length, block.lengthLen = readVarNumber(block.tvb, block.offset + block.typeLen) |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 334 | if block.lengthLen == 0 then |
| 335 | return nil |
| 336 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 337 | |
| 338 | block.size = block.typeLen + block.lengthLen + block.length |
| 339 | |
| 340 | return block |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 341 | end |
| 342 | |
Alexander Afanasyev | 7f43c53 | 2015-08-12 15:28:51 -0700 | [diff] [blame] | 343 | function canBeValidNdnPacket(block) |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 344 | if ((block.type == 5 or block.type == 6 or block.type == 100) and block.length <= 8800) then |
Alexander Afanasyev | 7f43c53 | 2015-08-12 15:28:51 -0700 | [diff] [blame] | 345 | return true |
| 346 | else |
| 347 | return false |
| 348 | end |
| 349 | end |
| 350 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 351 | function findNdnPacket(tvb) |
| 352 | offset = 0 |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 353 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 354 | while offset + 2 < tvb:len() do |
| 355 | local block = getBlock(tvb, offset) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 356 | |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 357 | if (block ~= nil) and canBeValidNdnPacket(block) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 358 | return block |
| 359 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 360 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 361 | offset = offset + 1 |
| 362 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 363 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 364 | return nil |
| 365 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 366 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 367 | function getSubBlocks(block) |
| 368 | local valueLeft = block.length |
| 369 | local subBlocks = {} |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 370 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 371 | while valueLeft > 0 do |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 372 | 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 Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 377 | |
| 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 |
| 387 | end |
| 388 | |
| 389 | ----------------------------------------------------- |
| 390 | ----------------------------------------------------- |
| 391 | |
| 392 | -- NDN protocol dissector function |
| 393 | function 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 Shi | 1fda67d | 2018-01-12 21:35:31 +0000 | [diff] [blame] | 413 | local pktType = "" |
| 414 | local pktName = "" |
| 415 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 416 | 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 432 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 433 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 434 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 435 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 436 | -- 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 443 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 444 | 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 Shi | 1fda67d | 2018-01-12 21:35:31 +0000 | [diff] [blame] | 451 | |
| 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 464 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 465 | |
| 466 | local info = NDN_DICT[block.type] |
| 467 | if (info ~= nil) then |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 468 | 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 473 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 474 | |
| 475 | nBytesLeft = nBytesLeft - block.size |
| 476 | |
| 477 | if (nBytesLeft > 0) then |
| 478 | ok, block = pcall(getBlock, tvb, tvb:len() - nBytesLeft) |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 479 | if (not ok or block == nil or not canBeValidNdnPacket(block)) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 480 | break |
| 481 | end |
| 482 | end |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 483 | end -- while(block.size <= nBytesLeft) |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 484 | |
| 485 | pInfo.cols.protocol = tostring(pInfo.cols.protocol) .. " (" .. ndn.name .. ")" |
Junxiao Shi | 1fda67d | 2018-01-12 21:35:31 +0000 | [diff] [blame] | 486 | pInfo.cols.info = pktType .. " " .. pktName |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 487 | |
Alexander Afanasyev | 7f43c53 | 2015-08-12 15:28:51 -0700 | [diff] [blame] | 488 | if (nBytesLeft > 0 and block ~= nil and block.size ~= nil and block.size > nBytesLeft) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 489 | 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 Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 495 | end |
| 496 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 497 | local udpDissectorTable = DissectorTable.get("udp.port") |
| 498 | udpDissectorTable:add("6363", ndn) |
| 499 | udpDissectorTable:add("56363", ndn) |
| 500 | |
| 501 | local tcpDissectorTable = DissectorTable.get("tcp.port") |
| 502 | tcpDissectorTable:add("6363", ndn) |
| 503 | |
| 504 | local websocketDissectorTable = DissectorTable.get("ws.port") |
| 505 | -- websocketDissectorTable:add("9696", ndn) |
| 506 | websocketDissectorTable:add("1-65535", ndn) |
| 507 | |
Alexander Afanasyev | 7f43c53 | 2015-08-12 15:28:51 -0700 | [diff] [blame] | 508 | local ethernetDissectorTable = DissectorTable.get("ethertype") |
| 509 | ethernetDissectorTable:add(0x8624, ndn) |
| 510 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 511 | io.stderr:write("ndn.lua is successfully loaded\n") |
Junxiao Shi | 1fda67d | 2018-01-12 21:35:31 +0000 | [diff] [blame] | 512 | |