Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 1 | -- Copyright (c) 2015-2016, 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 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 51 | function getUriFromNameComponent(block) |
| 52 | -- @todo Implement proper proper URL escaping |
| 53 | return block.tvb(block.offset + block.typeLen + block.lengthLen, block.length):string() |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 54 | end |
| 55 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 56 | function getUriFromName(nameBlock) |
| 57 | if (nameBlock.elements == nil) then |
| 58 | return "" |
| 59 | else |
| 60 | components = {} |
| 61 | for i, block in pairs(nameBlock.elements) do |
| 62 | table.insert(components, getUriFromNameComponent(block)) |
| 63 | end |
| 64 | |
| 65 | return "/" .. table.concat(components, "/") |
| 66 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 67 | end |
| 68 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 69 | function getNackReasonDetail(b) |
| 70 | local code = getNonNegativeInteger(b) |
| 71 | if (code == 0) then return "None" |
| 72 | elseif (code == 50) then return "Congestion" |
| 73 | elseif (code == 100) then return "Duplicate" |
| 74 | elseif (code == 150) then return "NoRoute" |
| 75 | else return "Unknown" |
| 76 | end |
| 77 | end |
| 78 | |
| 79 | function getCachePolicyDetail(b) |
| 80 | local code = getNonNegativeInteger(b) |
| 81 | if (code == 1) then return "NoCache" |
| 82 | else return "Unknown" |
| 83 | end |
| 84 | end |
| 85 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 86 | function getNonNegativeInteger(b) |
| 87 | if (b.length == 1) then |
| 88 | return b.tvb(b.offset + b.typeLen + b.lengthLen, 1):uint() |
| 89 | elseif (b.length == 2) then |
| 90 | return b.tvb(b.offset + b.typeLen + b.lengthLen, 2):uint() |
| 91 | elseif (b.length == 4) then |
| 92 | return b.tvb(b.offset + b.typeLen + b.lengthLen, 4):uint() |
| 93 | -- Something strange with uint64, not supporting it for now |
| 94 | -- elseif (b.length == 8) then |
| 95 | -- return b.tvb(b.offset + b.typeLen + b.lengthLen, 8):uint64() |
| 96 | else |
| 97 | return 0xFFFFFFFF; |
| 98 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 99 | end |
| 100 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 101 | function getUriFromExclude(block) |
| 102 | -- @todo |
| 103 | return "" |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 104 | end |
| 105 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 106 | function getTrue(block) |
| 107 | return "Yes" |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 108 | end |
| 109 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 110 | local AppPrivateBlock1 = 100 |
| 111 | local AppPrivateBlock2 = 800 |
| 112 | local AppPrivateBlock3 = 1000 |
| 113 | |
| 114 | function canIgnoreTlvType(t) |
| 115 | if (t < AppPrivateBlock2 or t >= AppPrivateBlock3) then |
| 116 | return false |
| 117 | else |
| 118 | local mod = math.fmod(t, 2) |
| 119 | if (mod == 1) then |
| 120 | return true |
| 121 | else |
| 122 | return false |
| 123 | end |
| 124 | end |
| 125 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 127 | function getGenericBlockInfo(block) |
| 128 | local name = "" |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 129 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 130 | -- TODO: Properly format informational message based type value reservations |
| 131 | -- (http://named-data.net/doc/ndn-tlv/types.html#type-value-reservations) |
| 132 | if (block.type <= AppPrivateBlock1) then |
| 133 | name = "Unrecognized from the reserved range " .. 0 .. "-" .. AppPrivateBlock1 .. "" |
| 134 | elseif (AppPrivateBlock1 < block.type and block.type < AppPrivateBlock2) then |
| 135 | name = "Unrecognized from the reserved range " .. (AppPrivateBlock1 + 1) .. "-" .. (AppPrivateBlock2 - 1) .. "" |
| 136 | elseif (AppPrivateBlock2 <= block.type and block.type <= AppPrivateBlock3) then |
| 137 | if (canIgnoreTlvType(block.type)) then |
| 138 | name = "Unknown field (ignored)" |
| 139 | else |
| 140 | name = "Unknown field" |
| 141 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 142 | else |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 143 | name = "RESERVED_3" |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 144 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 145 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 146 | return name .. ", Type: " .. block.type .. ", Length: " .. block.length |
| 147 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 148 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 149 | ----------------------------------------------------- |
| 150 | ----------------------------------------------------- |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 151 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 152 | local NDN_DICT = { |
| 153 | -- Interest or Data packets |
| 154 | [5] = {name = "Interest" , summary = true}, |
| 155 | [6] = {name = "Data" , summary = true}, |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 156 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 157 | -- Name field |
| 158 | [7] = {name = "Name" , field = ProtoField.string("ndn.name", "Name") , value = getUriFromName}, |
| 159 | [1] = {name = "ImplicitSha256DigestComponent", field = ProtoField.string("ndn.implicitsha256", "ImplicitSha256DigestComponent"), value = getUriFromNameComponent}, |
| 160 | [8] = {name = "NameComponent" , field = ProtoField.string("ndn.namecomponent", "NameComponent") , value = getUriFromNameComponent}, |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 162 | -- Sub-fields of Interest packet |
| 163 | [9] = {name = "Selectors" , summary = true}, |
| 164 | [10] = {name = "Nonce" , field = ProtoField.bytes("ndn.nonce", "Nonce")}, |
| 165 | [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] | 166 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 167 | -- Sub-fields of Interest/Selector field |
| 168 | [13] = {name = "MinSuffixComponents" , field = ProtoField.uint32("ndn.minsuffix", "MinSuffixComponents") , value = getNonNegativeInteger}, |
| 169 | [14] = {name = "MaxSuffixComponents" , field = ProtoField.uint32("ndn.maxsuffix", "MaxSuffixComponents") , value = getNonNegativeInteger}, |
| 170 | [15] = {name = "PublisherPublicKeyLocator" , summary = true}, |
| 171 | [16] = {name = "Exclude" , field = ProtoField.string("ndn.exclude", "Exclude") , value = getUriFromExclude}, |
| 172 | [17] = {name = "ChildSelector" , field = ProtoField.uint32("ndn.childselector", "ChildSelector", base.DEC) , value = getNonNegativeInteger}, |
| 173 | [18] = {name = "MustBeFresh" , field = ProtoField.string("ndn.mustbefresh", "MustBeFresh") , value = getTrue}, |
| 174 | [19] = {name = "Any" , field = ProtoField.string("ndn.any", "Any") , value = getTrue}, |
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 Data packet |
| 177 | [20] = {name = "MetaInfo" , summary = true}, |
| 178 | [21] = {name = "Content" , field = ProtoField.string("ndn.content", "Content")}, |
| 179 | [22] = {name = "SignatureInfo" , summary = true}, |
| 180 | [23] = {name = "SignatureValue" , field = ProtoField.bytes("ndn.signaturevalue", "SignatureValue")}, |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 181 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 182 | -- Sub-fields of Data/MetaInfo field |
| 183 | [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}, |
| 186 | |
| 187 | -- Sub-fields of Data/Signature field |
| 188 | [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")}, |
| 191 | |
| 192 | -- Other fields |
| 193 | [30] = {name = "LinkPreference" , field = ProtoField.uint32("ndn.link_preference", "LinkPreference", base.DEC) , value = getNonNegativeInteger}, |
| 194 | [31] = {name = "LinkDelegation" , summary = true}, |
| 195 | [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] | 196 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 197 | -- NDNLPv2 Packet field |
| 198 | [80] = {name = "Fragment" }, |
| 199 | [81] = {name = "Sequence" , field = ProtoField.uint32("ndn.sequence", "Sequence", base.DEC), value = getNonNegativeInteger}, |
| 200 | [82] = {name = "FragIndex" , field = ProtoField.uint32("ndn.fragindex", "FragIndex", base.DEC), value = getNonNegativeInteger}, |
| 201 | [83] = {name = "FragCount" , field = ProtoField.uint32("ndn.fragcount", "FragCount", base.DEC), value = getNonNegativeInteger}, |
| 202 | [100] = {name = "LpPacket" , summary = true}, |
| 203 | [800] = {name = "Nack" , summary = true}, |
| 204 | [801] = {name = "NackReason" , field = ProtoField.string("ndn.nack_reason", "NackReason"), value = getNackReasonDetail}, |
| 205 | [816] = {name = "NextHopFaceId" , field = ProtoField.uint32("ndn.nexthop_faceid", "NextHopFaceId", base.DEC), value = getNonNegativeInteger}, |
| 206 | [817] = {name = "IncomingFaceId" , field = ProtoField.uint32("ndn.incoming_faceid", "IncomingFaceId", base.DEC), value = getNonNegativeInteger}, |
| 207 | [820] = {name = "CachePolicy" , summary = true}, |
| 208 | [821] = {name = "CachePolicyType" , field = ProtoField.string("ndn.cachepolicy_type", "CachePolicyType"), value = getCachePolicyDetail}, |
| 209 | } |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 210 | |
| 211 | -- -- Add protofields in NDN protocol |
| 212 | ndn.fields = { |
| 213 | } |
| 214 | for key, value in pairs(NDN_DICT) do |
| 215 | table.insert(ndn.fields, value.field) |
| 216 | end |
| 217 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 218 | ----------------------------------------------------- |
| 219 | ----------------------------------------------------- |
| 220 | |
| 221 | -- block |
| 222 | -- .tvb |
| 223 | -- .offset |
| 224 | -- .type |
| 225 | -- .typeLen |
| 226 | -- .length |
| 227 | -- .lengthLen |
| 228 | -- .size = .typeLen + .lengthLen + .length |
| 229 | |
| 230 | function addInfo(block, root) -- may be add additional context later |
| 231 | local info = NDN_DICT[block.type] |
| 232 | |
| 233 | if (info == nil) then |
| 234 | info = {} |
| 235 | info.value = getGenericBlockInfo |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 236 | -- color |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 237 | end |
| 238 | |
| 239 | local treeInfo |
| 240 | if (info.value == nil) then |
| 241 | |
| 242 | if (info.field ~= nil) then |
| 243 | treeInfo = root:add(info.field, block.tvb(block.offset, block.size)) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 244 | else |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 245 | treeInfo = root:add(block.tvb(block.offset, block.size), info.name) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 246 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 247 | |
| 248 | treeInfo:append_text(", Type: " .. block.type .. ", Length: " .. block.length) |
| 249 | else |
| 250 | block.value = info.value(block) |
| 251 | |
| 252 | if (info.field ~= nil) then |
| 253 | 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] | 254 | else |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 255 | treeInfo = root:add(block.tvb(block.offset, block.size), block.value) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 256 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 257 | end |
| 258 | block.root = treeInfo |
| 259 | return block.root |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 260 | end |
| 261 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 262 | function addSummary(block) |
| 263 | if (block.elements == nil) then |
| 264 | return |
| 265 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 266 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 267 | local info = NDN_DICT[block.type] |
| 268 | if (info == nil or info.summary == nil) then |
| 269 | return |
| 270 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 271 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 272 | local summary = {} |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 273 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 274 | for k, subblock in pairs(block.elements) do |
| 275 | if (subblock.value ~= nil) then |
| 276 | local info = NDN_DICT[subblock.type] |
| 277 | if (info ~= nil) then |
| 278 | table.insert(summary, info.name .. ": " .. subblock.value) |
| 279 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 280 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 281 | end |
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 | if (#summary > 0) then |
| 284 | block.summary = table.concat(summary, ", ") |
| 285 | if (block.value == nil) then |
| 286 | block.value = block.summary |
| 287 | end |
| 288 | block.root:append_text(", " .. block.summary) |
| 289 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 290 | end |
| 291 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 292 | ----------------------------------------------------- |
| 293 | ----------------------------------------------------- |
| 294 | |
| 295 | function readVarNumber(tvb, offset) |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 296 | if offset >= tvb:len() then |
| 297 | return 0, 0 |
| 298 | end |
| 299 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 300 | local firstOctet = tvb(offset, 1):uint() |
| 301 | if (firstOctet < 253) then |
| 302 | return firstOctet, 1 |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 303 | elseif (firstOctet == 253) and (offset + 3 < tvb:len()) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 304 | return tvb(offset + 1, 2):uint(), 3 |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 305 | elseif (firstOctet == 254) and (offset + 5 < tvb:len()) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 306 | return tvb(offset + 1, 4):uint(), 5 |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 307 | elseif (firstOctet == 255) and (offset + 9 < tvb:len()) then |
| 308 | return tvb(offset + 1, 8):uint64(), 9 |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 309 | end |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 310 | |
| 311 | return 0, 0 |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 312 | end |
| 313 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 314 | function getBlock(tvb, offset) |
| 315 | local block = {} |
| 316 | block.tvb = tvb |
| 317 | block.offset = offset |
| 318 | |
| 319 | block.type, block.typeLen = readVarNumber(block.tvb, block.offset) |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 320 | if block.typeLen == 0 then |
| 321 | return nil |
| 322 | end |
| 323 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 324 | block.length, block.lengthLen = readVarNumber(block.tvb, block.offset + block.typeLen) |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 325 | if block.lengthLen == 0 then |
| 326 | return nil |
| 327 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 328 | |
| 329 | block.size = block.typeLen + block.lengthLen + block.length |
| 330 | |
| 331 | return block |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 332 | end |
| 333 | |
Alexander Afanasyev | 7f43c53 | 2015-08-12 15:28:51 -0700 | [diff] [blame] | 334 | function canBeValidNdnPacket(block) |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 335 | 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] | 336 | return true |
| 337 | else |
| 338 | return false |
| 339 | end |
| 340 | end |
| 341 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 342 | function findNdnPacket(tvb) |
| 343 | offset = 0 |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 344 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 345 | while offset + 2 < tvb:len() do |
| 346 | local block = getBlock(tvb, offset) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 347 | |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 348 | if (block ~= nil) and canBeValidNdnPacket(block) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 349 | return block |
| 350 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 351 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 352 | offset = offset + 1 |
| 353 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 354 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 355 | return nil |
| 356 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 357 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 358 | function getSubBlocks(block) |
| 359 | local valueLeft = block.length |
| 360 | local subBlocks = {} |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 361 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 362 | while valueLeft > 0 do |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 363 | local offset = block.offset + block.typeLen + block.lengthLen + (block.length - valueLeft) |
| 364 | local child = getBlock(block.tvb, offset) |
| 365 | if child == nil then |
| 366 | return nil |
| 367 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 368 | |
| 369 | valueLeft = valueLeft - child.size |
| 370 | table.insert(subBlocks, child) |
| 371 | end |
| 372 | |
| 373 | if (valueLeft == 0) then |
| 374 | return subBlocks |
| 375 | else |
| 376 | return nil |
| 377 | end |
| 378 | end |
| 379 | |
| 380 | ----------------------------------------------------- |
| 381 | ----------------------------------------------------- |
| 382 | |
| 383 | -- NDN protocol dissector function |
| 384 | function ndn.dissector(tvb, pInfo, root) -- Tvb, Pinfo, TreeItem |
| 385 | |
| 386 | if (tvb:len() ~= tvb:reported_len()) then |
| 387 | return 0 -- ignore partially captured packets |
| 388 | -- this can/may be re-enabled only for unfragmented UDP packets |
| 389 | end |
| 390 | |
| 391 | local ok, block = pcall(findNdnPacket, tvb) |
| 392 | if (not ok) then |
| 393 | return 0 |
| 394 | end |
| 395 | |
| 396 | if (block == nil or block.offset == nil) then |
| 397 | -- no valid NDN packets found |
| 398 | return 0 |
| 399 | end |
| 400 | |
| 401 | local nBytesLeft = tvb:len() - block.offset |
| 402 | -- print (pInfo.number .. ":: Found block: " .. block.type .. " of length " .. block.size .. " bytesLeft: " .. nBytesLeft) |
| 403 | |
| 404 | while (block.size <= nBytesLeft) do |
| 405 | -- Create TreeItems |
| 406 | block.tree = root:add(ndn, tvb(block.offset, block.size)) |
| 407 | |
| 408 | local queue = {block} |
| 409 | while (#queue > 0) do |
| 410 | local block = queue[1] |
| 411 | table.remove(queue, 1) |
| 412 | |
| 413 | block.elements = getSubBlocks(block) |
| 414 | local subtree = addInfo(block, block.tree) |
| 415 | |
| 416 | if (block.elements ~= nil) then |
| 417 | for i, subBlock in pairs(block.elements) do |
| 418 | subBlock.tree = subtree |
| 419 | table.insert(queue, subBlock) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 420 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 421 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 422 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 423 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 424 | -- Make summaries |
| 425 | local queue = {block} |
| 426 | while (#queue > 0) do |
| 427 | local block = queue[1] |
| 428 | if (block.visited ~= nil or block.elements == nil) then |
| 429 | -- try to make summary |
| 430 | table.remove(queue, 1) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 431 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 432 | addSummary(block) |
| 433 | else |
| 434 | for i, subBlock in pairs(block.elements) do |
| 435 | table.insert(queue, 1, subBlock) |
| 436 | end |
| 437 | block.visited = true |
| 438 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 439 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 440 | |
| 441 | local info = NDN_DICT[block.type] |
| 442 | if (info ~= nil) then |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 443 | if (block.summary ~= nil) then |
| 444 | block.tree:append_text(", " .. NDN_DICT[block.type].name .. ", " .. block.summary) |
| 445 | else |
| 446 | block.tree:append_text(", " .. NDN_DICT[block.type].name) |
| 447 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 448 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 449 | |
| 450 | nBytesLeft = nBytesLeft - block.size |
| 451 | |
| 452 | if (nBytesLeft > 0) then |
| 453 | ok, block = pcall(getBlock, tvb, tvb:len() - nBytesLeft) |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 454 | if (not ok or block == nil or not canBeValidNdnPacket(block)) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 455 | break |
| 456 | end |
| 457 | end |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 458 | end -- while(block.size <= nBytesLeft) |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 459 | |
| 460 | pInfo.cols.protocol = tostring(pInfo.cols.protocol) .. " (" .. ndn.name .. ")" |
| 461 | |
Alexander Afanasyev | 7f43c53 | 2015-08-12 15:28:51 -0700 | [diff] [blame] | 462 | 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] | 463 | pInfo.desegment_offset = tvb:len() - nBytesLeft |
| 464 | |
| 465 | -- Originally, I set desegment_len to the exact lenght, but it mysteriously didn't work for TCP |
| 466 | -- pInfo.desegment_len = block.size -- this will not work to desegment TCP streams |
| 467 | pInfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT |
| 468 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 469 | end |
| 470 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 471 | local udpDissectorTable = DissectorTable.get("udp.port") |
| 472 | udpDissectorTable:add("6363", ndn) |
| 473 | udpDissectorTable:add("56363", ndn) |
| 474 | |
| 475 | local tcpDissectorTable = DissectorTable.get("tcp.port") |
| 476 | tcpDissectorTable:add("6363", ndn) |
| 477 | |
| 478 | local websocketDissectorTable = DissectorTable.get("ws.port") |
| 479 | -- websocketDissectorTable:add("9696", ndn) |
| 480 | websocketDissectorTable:add("1-65535", ndn) |
| 481 | |
Alexander Afanasyev | 7f43c53 | 2015-08-12 15:28:51 -0700 | [diff] [blame] | 482 | local ethernetDissectorTable = DissectorTable.get("ethertype") |
| 483 | ethernetDissectorTable:add(0x8624, ndn) |
| 484 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 485 | io.stderr:write("ndn.lua is successfully loaded\n") |