Davide Pesavento | 105cd9e | 2019-04-06 18:13:44 -0400 | [diff] [blame] | 1 | -- Copyright (c) 2015-2019, 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 | |
Junxiao Shi | 5e38479 | 2016-11-24 03:59:06 +0000 | [diff] [blame] | 40 | -- @return TLV-VALUE portion of a TLV block |
| 41 | function getValue(b) |
| 42 | return b.tvb(b.offset + b.typeLen + b.lengthLen, b.length) |
| 43 | end |
| 44 | |
Junxiao Shi | 89db73c | 2018-05-27 15:22:49 +0000 | [diff] [blame] | 45 | function 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() |
| 50 | end |
| 51 | |
Davide Pesavento | 89b52d4 | 2021-11-27 22:10:42 -0500 | [diff] [blame] | 52 | function 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 Shi | c687af6 | 2018-05-06 21:58:09 +0000 | [diff] [blame] | 62 | for i = 0, (b.length - 1) do |
Davide Pesavento | 89b52d4 | 2021-11-27 22:10:42 -0500 | [diff] [blame] | 63 | local byte = b.tvb(b.offset + b.typeLen + b.lengthLen + i, 1) |
Junxiao Shi | c687af6 | 2018-05-06 21:58:09 +0000 | [diff] [blame] | 64 | s = s .. string.format("%02x", byte:uint()) |
| 65 | end |
| 66 | return s |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 67 | end |
| 68 | |
Junxiao Shi | c687af6 | 2018-05-06 21:58:09 +0000 | [diff] [blame] | 69 | function getUriFromNameComponent(b) |
Davide Pesavento | 89b52d4 | 2021-11-27 22:10:42 -0500 | [diff] [blame] | 70 | if b.type == 1 or b.type == 2 then |
| 71 | return getUriFromSha256DigestComponent(b) |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 72 | end |
Davide Pesavento | 89b52d4 | 2021-11-27 22:10:42 -0500 | [diff] [blame] | 73 | |
| 74 | local s = "" |
Junxiao Shi | c687af6 | 2018-05-06 21:58:09 +0000 | [diff] [blame] | 75 | 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 Pesavento | 89b52d4 | 2021-11-27 22:10:42 -0500 | [diff] [blame] | 80 | local byte = b.tvb(b.offset + b.typeLen + b.lengthLen + i, 1) |
| 81 | local ch = byte:uint() |
Junxiao Shi | c687af6 | 2018-05-06 21:58:09 +0000 | [diff] [blame] | 82 | 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 |
| 93 | end |
| 94 | |
| 95 | function getUriFromName(b) |
| 96 | if b.elements == nil then |
| 97 | return "/" |
| 98 | end |
Davide Pesavento | 89b52d4 | 2021-11-27 22:10:42 -0500 | [diff] [blame] | 99 | |
Junxiao Shi | c687af6 | 2018-05-06 21:58:09 +0000 | [diff] [blame] | 100 | components = {} |
| 101 | for i, comp in pairs(b.elements) do |
| 102 | table.insert(components, getUriFromNameComponent(comp)) |
| 103 | end |
| 104 | return "/" .. table.concat(components, "/") |
| 105 | end |
| 106 | |
| 107 | function getUriFromFinalBlockId(b) |
| 108 | if b.elements == nil then |
| 109 | return "/" |
| 110 | end |
| 111 | return getUriFromNameComponent(b.elements[1]) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 112 | end |
| 113 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 114 | function getNackReasonDetail(b) |
Davide Pesavento | 9186528 | 2021-11-27 22:18:08 -0500 | [diff] [blame^] | 115 | assert(b.type == 801) |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 116 | local code = getNonNegativeInteger(b) |
Davide Pesavento | 9186528 | 2021-11-27 22:18:08 -0500 | [diff] [blame^] | 117 | |
| 118 | if code == UInt64(0) then return "None" |
| 119 | elseif code == UInt64(50) then return "Congestion" |
Junxiao Shi | 89db73c | 2018-05-27 15:22:49 +0000 | [diff] [blame] | 120 | elseif code == UInt64(100) then return "Duplicate" |
| 121 | elseif code == UInt64(150) then return "NoRoute" |
| 122 | else return tostring(code) |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 123 | end |
| 124 | end |
| 125 | |
| 126 | function getCachePolicyDetail(b) |
Davide Pesavento | 9186528 | 2021-11-27 22:18:08 -0500 | [diff] [blame^] | 127 | assert(b.type == 821) |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 128 | local code = getNonNegativeInteger(b) |
Davide Pesavento | 9186528 | 2021-11-27 22:18:08 -0500 | [diff] [blame^] | 129 | |
| 130 | if code == UInt64(0) then return "None" |
| 131 | elseif code == UInt64(1) then return "NoCache" |
| 132 | else return tostring(code) .. " (unknown)" |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 133 | end |
| 134 | end |
| 135 | |
Junxiao Shi | 5e38479 | 2016-11-24 03:59:06 +0000 | [diff] [blame] | 136 | function getNonce(b) |
| 137 | assert(b.type == 10) |
Davide Pesavento | 9186528 | 2021-11-27 22:18:08 -0500 | [diff] [blame^] | 138 | if b.length ~= 4 then |
Junxiao Shi | 5e38479 | 2016-11-24 03:59:06 +0000 | [diff] [blame] | 139 | return "invalid (should have 4 octets)" |
| 140 | end |
| 141 | return getValue(b):uint() |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 142 | end |
| 143 | |
Davide Pesavento | 9186528 | 2021-11-27 22:18:08 -0500 | [diff] [blame^] | 144 | function 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() |
| 150 | end |
| 151 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 152 | function getTrue(block) |
| 153 | return "Yes" |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 154 | end |
| 155 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 156 | local AppPrivateBlock1 = 100 |
| 157 | local AppPrivateBlock2 = 800 |
| 158 | local AppPrivateBlock3 = 1000 |
| 159 | |
| 160 | function canIgnoreTlvType(t) |
| 161 | if (t < AppPrivateBlock2 or t >= AppPrivateBlock3) then |
| 162 | return false |
| 163 | else |
| 164 | local mod = math.fmod(t, 2) |
| 165 | if (mod == 1) then |
| 166 | return true |
| 167 | else |
| 168 | return false |
| 169 | end |
| 170 | end |
| 171 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 172 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 173 | function getGenericBlockInfo(block) |
| 174 | local name = "" |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 175 | |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 176 | -- TODO: Properly format informational message based type value reservations |
| 177 | -- (http://named-data.net/doc/ndn-tlv/types.html#type-value-reservations) |
| 178 | if (block.type <= AppPrivateBlock1) then |
| 179 | name = "Unrecognized from the reserved range " .. 0 .. "-" .. AppPrivateBlock1 .. "" |
| 180 | elseif (AppPrivateBlock1 < block.type and block.type < AppPrivateBlock2) then |
| 181 | name = "Unrecognized from the reserved range " .. (AppPrivateBlock1 + 1) .. "-" .. (AppPrivateBlock2 - 1) .. "" |
| 182 | elseif (AppPrivateBlock2 <= block.type and block.type <= AppPrivateBlock3) then |
| 183 | if (canIgnoreTlvType(block.type)) then |
| 184 | name = "Unknown field (ignored)" |
| 185 | else |
| 186 | name = "Unknown field" |
| 187 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 188 | else |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 189 | name = "RESERVED_3" |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 190 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 191 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 192 | return name .. ", Type: " .. block.type .. ", Length: " .. block.length |
| 193 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 194 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 195 | ----------------------------------------------------- |
| 196 | ----------------------------------------------------- |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 197 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 198 | local NDN_DICT = { |
Davide Pesavento | 105cd9e | 2019-04-06 18:13:44 -0400 | [diff] [blame] | 199 | -- Name and name components |
Davide Pesavento | 89b52d4 | 2021-11-27 22:10:42 -0500 | [diff] [blame] | 200 | [7] = {name = "Name" , field = ProtoField.string("ndn.name", "Name") , value = getUriFromName}, |
| 201 | [1] = {name = "ImplicitSha256DigestComponent" , field = ProtoField.string("ndn.implicit_sha256", "ImplicitSha256DigestComponent"), value = getUriFromNameComponent}, |
| 202 | [2] = {name = "ParametersSha256DigestComponent", field = ProtoField.string("ndn.params_sha256", "ParametersSha256DigestComponent"), value = getUriFromNameComponent}, |
| 203 | [8] = {name = "GenericNameComponent" , field = ProtoField.string("ndn.namecomponent", "GenericNameComponent") , value = getUriFromNameComponent}, |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 204 | |
Junxiao Shi | ac4b546 | 2018-04-17 02:26:17 +0000 | [diff] [blame] | 205 | -- Interest and its sub-elements in Packet Format v0.3 |
| 206 | [5] = {name = "Interest" , summary = true}, |
| 207 | [33] = {name = "CanBePrefix" , field = ProtoField.string("ndn.canbeprefix", "CanBePrefix") , value = getTrue}, |
| 208 | [18] = {name = "MustBeFresh" , field = ProtoField.string("ndn.mustbefresh", "MustBeFresh") , value = getTrue}, |
| 209 | -- [30] = {name = "ForwardingHint" , summary = true}, |
| 210 | -- ForwardingHint and LinkPreference have the same TLV-TYPE number, so we can't recognize both for now (see #4185). |
| 211 | [31] = {name = "LinkDelegation" , summary = true}, |
Davide Pesavento | 105cd9e | 2019-04-06 18:13:44 -0400 | [diff] [blame] | 212 | [30] = {name = "LinkPreference" , field = ProtoField.uint64("ndn.linkpreference", "LinkPreference", base.DEC) , value = getNonNegativeInteger}, |
Junxiao Shi | 5e38479 | 2016-11-24 03:59:06 +0000 | [diff] [blame] | 213 | [10] = {name = "Nonce" , field = ProtoField.uint32("ndn.nonce", "Nonce", base.HEX) , value = getNonce}, |
Davide Pesavento | 9186528 | 2021-11-27 22:18:08 -0500 | [diff] [blame^] | 214 | [12] = {name = "InterestLifetime" , field = ProtoField.uint64("ndn.lifetime", "InterestLifetime", base.DEC) , value = getNonNegativeInteger}, |
| 215 | [34] = {name = "HopLimit" , field = ProtoField.uint8("ndn.hoplimit", "HopLimit", base.DEC) , value = getHopLimit}, |
| 216 | [36] = {name = "ApplicationParameters" , field = ProtoField.bytes("ndn.app_params", "ApplicationParameters")}, |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 217 | |
Junxiao Shi | ac4b546 | 2018-04-17 02:26:17 +0000 | [diff] [blame] | 218 | -- Data and its sub-elements in Packet Format v0.3 |
| 219 | [6] = {name = "Data" , summary = true}, |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 220 | [20] = {name = "MetaInfo" , summary = true}, |
Davide Pesavento | 9186528 | 2021-11-27 22:18:08 -0500 | [diff] [blame^] | 221 | [24] = {name = "ContentType" , field = ProtoField.uint64("ndn.contenttype", "ContentType", base.DEC) , value = getNonNegativeInteger}, |
| 222 | [25] = {name = "FreshnessPeriod" , field = ProtoField.uint64("ndn.freshness", "FreshnessPeriod", base.DEC) , value = getNonNegativeInteger}, |
| 223 | [26] = {name = "FinalBlockId" , field = ProtoField.string("ndn.finalblock", "FinalBlockId") , value = getUriFromFinalBlockId}, |
| 224 | [21] = {name = "Content" , field = ProtoField.bytes("ndn.content", "Content")}, |
Junxiao Shi | ac4b546 | 2018-04-17 02:26:17 +0000 | [diff] [blame] | 225 | [22] = {name = "SignatureInfo" , summary = true}, |
Junxiao Shi | 89db73c | 2018-05-27 15:22:49 +0000 | [diff] [blame] | 226 | [27] = {name = "SignatureType" , field = ProtoField.uint64("ndn.signaturetype", "SignatureType", base.DEC) , value = getNonNegativeInteger}, |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 227 | [28] = {name = "KeyLocator" , summary = true}, |
| 228 | [29] = {name = "KeyDigest" , field = ProtoField.bytes("ndn.keydigest", "KeyDigest")}, |
Junxiao Shi | ac4b546 | 2018-04-17 02:26:17 +0000 | [diff] [blame] | 229 | [23] = {name = "SignatureValue" , field = ProtoField.bytes("ndn.signaturevalue", "SignatureValue")}, |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 230 | |
Junxiao Shi | ac4b546 | 2018-04-17 02:26:17 +0000 | [diff] [blame] | 231 | -- NDNLPv2 headers |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 232 | [80] = {name = "Fragment" }, |
Junxiao Shi | 89db73c | 2018-05-27 15:22:49 +0000 | [diff] [blame] | 233 | [81] = {name = "Sequence" , field = ProtoField.uint64("ndn.sequence", "Sequence", base.DEC) , value = getNonNegativeInteger}, |
| 234 | [82] = {name = "FragIndex" , field = ProtoField.uint64("ndn.fragindex", "FragIndex", base.DEC) , value = getNonNegativeInteger}, |
| 235 | [83] = {name = "FragCount" , field = ProtoField.uint64("ndn.fragcount", "FragCount", base.DEC) , value = getNonNegativeInteger}, |
Davide Pesavento | 9186528 | 2021-11-27 22:18:08 -0500 | [diff] [blame^] | 236 | [98] = {name = "PitToken" , field = ProtoField.bytes("ndn.pit_token", "PitToken")}, |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 237 | [100] = {name = "LpPacket" , summary = true}, |
| 238 | [800] = {name = "Nack" , summary = true}, |
Junxiao Shi | 89db73c | 2018-05-27 15:22:49 +0000 | [diff] [blame] | 239 | [801] = {name = "NackReason" , field = ProtoField.string("ndn.nack_reason", "NackReason") , value = getNackReasonDetail}, |
| 240 | [816] = {name = "NextHopFaceId" , field = ProtoField.uint64("ndn.nexthop_faceid", "NextHopFaceId", base.DEC) , value = getNonNegativeInteger}, |
| 241 | [817] = {name = "IncomingFaceId" , field = ProtoField.uint64("ndn.incoming_faceid", "IncomingFaceId", base.DEC) , value = getNonNegativeInteger}, |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 242 | [820] = {name = "CachePolicy" , summary = true}, |
Junxiao Shi | 89db73c | 2018-05-27 15:22:49 +0000 | [diff] [blame] | 243 | [821] = {name = "CachePolicyType" , field = ProtoField.string("ndn.cachepolicy_type", "CachePolicyType") , value = getCachePolicyDetail}, |
| 244 | [832] = {name = "CongestionMark" , field = ProtoField.uint64("ndn.congestion_mark", "CongestionMark", base.DEC) , value = getNonNegativeInteger}, |
| 245 | [836] = {name = "Ack" , field = ProtoField.uint64("ndn.ack", "Ack", base.DEC) , value = getNonNegativeInteger}, |
Davide Pesavento | 200014a | 2021-11-27 21:21:47 -0500 | [diff] [blame] | 246 | [840] = {name = "TxSequence" , field = ProtoField.uint64("ndn.txseq", "TxSequence", base.DEC) , value = getNonNegativeInteger}, |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 247 | } |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 248 | |
| 249 | -- -- Add protofields in NDN protocol |
Davide Pesavento | 200014a | 2021-11-27 21:21:47 -0500 | [diff] [blame] | 250 | ndn.fields = {} |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 251 | for key, value in pairs(NDN_DICT) do |
| 252 | table.insert(ndn.fields, value.field) |
| 253 | end |
| 254 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 255 | ----------------------------------------------------- |
| 256 | ----------------------------------------------------- |
| 257 | |
| 258 | -- block |
| 259 | -- .tvb |
| 260 | -- .offset |
| 261 | -- .type |
| 262 | -- .typeLen |
| 263 | -- .length |
| 264 | -- .lengthLen |
| 265 | -- .size = .typeLen + .lengthLen + .length |
| 266 | |
| 267 | function addInfo(block, root) -- may be add additional context later |
| 268 | local info = NDN_DICT[block.type] |
| 269 | |
| 270 | if (info == nil) then |
| 271 | info = {} |
| 272 | info.value = getGenericBlockInfo |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 273 | -- color |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 274 | end |
| 275 | |
| 276 | local treeInfo |
| 277 | if (info.value == nil) then |
| 278 | |
| 279 | if (info.field ~= nil) then |
| 280 | treeInfo = root:add(info.field, block.tvb(block.offset, block.size)) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 281 | else |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 282 | treeInfo = root:add(block.tvb(block.offset, block.size), info.name) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 283 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 284 | |
| 285 | treeInfo:append_text(", Type: " .. block.type .. ", Length: " .. block.length) |
| 286 | else |
| 287 | block.value = info.value(block) |
| 288 | |
| 289 | if (info.field ~= nil) then |
| 290 | 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] | 291 | else |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 292 | treeInfo = root:add(block.tvb(block.offset, block.size), block.value) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 293 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 294 | end |
| 295 | block.root = treeInfo |
| 296 | return block.root |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 297 | end |
| 298 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 299 | function addSummary(block) |
| 300 | if (block.elements == nil) then |
| 301 | return |
| 302 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 303 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 304 | local info = NDN_DICT[block.type] |
| 305 | if (info == nil or info.summary == nil) then |
| 306 | return |
| 307 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 308 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 309 | local summary = {} |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 310 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 311 | for k, subblock in pairs(block.elements) do |
| 312 | if (subblock.value ~= nil) then |
| 313 | local info = NDN_DICT[subblock.type] |
| 314 | if (info ~= nil) then |
| 315 | table.insert(summary, info.name .. ": " .. subblock.value) |
| 316 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 317 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 318 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 319 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 320 | if (#summary > 0) then |
| 321 | block.summary = table.concat(summary, ", ") |
| 322 | if (block.value == nil) then |
| 323 | block.value = block.summary |
| 324 | end |
| 325 | block.root:append_text(", " .. block.summary) |
| 326 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 327 | end |
| 328 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 329 | ----------------------------------------------------- |
| 330 | ----------------------------------------------------- |
| 331 | |
| 332 | function readVarNumber(tvb, offset) |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 333 | if offset >= tvb:len() then |
| 334 | return 0, 0 |
| 335 | end |
| 336 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 337 | local firstOctet = tvb(offset, 1):uint() |
| 338 | if (firstOctet < 253) then |
| 339 | return firstOctet, 1 |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 340 | elseif (firstOctet == 253) and (offset + 3 < tvb:len()) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 341 | return tvb(offset + 1, 2):uint(), 3 |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 342 | elseif (firstOctet == 254) and (offset + 5 < tvb:len()) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 343 | return tvb(offset + 1, 4):uint(), 5 |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 344 | elseif (firstOctet == 255) and (offset + 9 < tvb:len()) then |
| 345 | return tvb(offset + 1, 8):uint64(), 9 |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 346 | end |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 347 | |
| 348 | return 0, 0 |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 349 | end |
| 350 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 351 | function getBlock(tvb, offset) |
| 352 | local block = {} |
| 353 | block.tvb = tvb |
| 354 | block.offset = offset |
| 355 | |
| 356 | block.type, block.typeLen = readVarNumber(block.tvb, block.offset) |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 357 | if block.typeLen == 0 then |
| 358 | return nil |
| 359 | end |
| 360 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 361 | block.length, block.lengthLen = readVarNumber(block.tvb, block.offset + block.typeLen) |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 362 | if block.lengthLen == 0 then |
| 363 | return nil |
| 364 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 365 | |
| 366 | block.size = block.typeLen + block.lengthLen + block.length |
| 367 | |
| 368 | return block |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 369 | end |
| 370 | |
Alexander Afanasyev | 7f43c53 | 2015-08-12 15:28:51 -0700 | [diff] [blame] | 371 | function canBeValidNdnPacket(block) |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 372 | 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] | 373 | return true |
| 374 | else |
| 375 | return false |
| 376 | end |
| 377 | end |
| 378 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 379 | function findNdnPacket(tvb) |
| 380 | offset = 0 |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 381 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 382 | while offset + 2 < tvb:len() do |
| 383 | local block = getBlock(tvb, offset) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 384 | |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 385 | if (block ~= nil) and canBeValidNdnPacket(block) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 386 | return block |
| 387 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 388 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 389 | offset = offset + 1 |
| 390 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 391 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 392 | return nil |
| 393 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 394 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 395 | function getSubBlocks(block) |
| 396 | local valueLeft = block.length |
| 397 | local subBlocks = {} |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 398 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 399 | while valueLeft > 0 do |
Junxiao Shi | e65c6d7 | 2016-07-24 21:53:19 +0000 | [diff] [blame] | 400 | local offset = block.offset + block.typeLen + block.lengthLen + (block.length - valueLeft) |
| 401 | local child = getBlock(block.tvb, offset) |
| 402 | if child == nil then |
| 403 | return nil |
| 404 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 405 | |
| 406 | valueLeft = valueLeft - child.size |
| 407 | table.insert(subBlocks, child) |
| 408 | end |
| 409 | |
| 410 | if (valueLeft == 0) then |
| 411 | return subBlocks |
| 412 | else |
| 413 | return nil |
| 414 | end |
| 415 | end |
| 416 | |
| 417 | ----------------------------------------------------- |
| 418 | ----------------------------------------------------- |
| 419 | |
| 420 | -- NDN protocol dissector function |
| 421 | function ndn.dissector(tvb, pInfo, root) -- Tvb, Pinfo, TreeItem |
| 422 | |
| 423 | if (tvb:len() ~= tvb:reported_len()) then |
| 424 | return 0 -- ignore partially captured packets |
| 425 | -- this can/may be re-enabled only for unfragmented UDP packets |
| 426 | end |
| 427 | |
| 428 | local ok, block = pcall(findNdnPacket, tvb) |
| 429 | if (not ok) then |
| 430 | return 0 |
| 431 | end |
| 432 | |
| 433 | if (block == nil or block.offset == nil) then |
| 434 | -- no valid NDN packets found |
| 435 | return 0 |
| 436 | end |
| 437 | |
| 438 | local nBytesLeft = tvb:len() - block.offset |
| 439 | -- print (pInfo.number .. ":: Found block: " .. block.type .. " of length " .. block.size .. " bytesLeft: " .. nBytesLeft) |
| 440 | |
Junxiao Shi | 1fda67d | 2018-01-12 21:35:31 +0000 | [diff] [blame] | 441 | local pktType = "" |
| 442 | local pktName = "" |
| 443 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 444 | while (block.size <= nBytesLeft) do |
| 445 | -- Create TreeItems |
| 446 | block.tree = root:add(ndn, tvb(block.offset, block.size)) |
| 447 | |
| 448 | local queue = {block} |
| 449 | while (#queue > 0) do |
| 450 | local block = queue[1] |
| 451 | table.remove(queue, 1) |
| 452 | |
| 453 | block.elements = getSubBlocks(block) |
| 454 | local subtree = addInfo(block, block.tree) |
| 455 | |
| 456 | if (block.elements ~= nil) then |
| 457 | for i, subBlock in pairs(block.elements) do |
| 458 | subBlock.tree = subtree |
| 459 | table.insert(queue, subBlock) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 460 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 461 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 462 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 463 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 464 | -- Make summaries |
| 465 | local queue = {block} |
| 466 | while (#queue > 0) do |
| 467 | local block = queue[1] |
| 468 | if (block.visited ~= nil or block.elements == nil) then |
| 469 | -- try to make summary |
| 470 | table.remove(queue, 1) |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 471 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 472 | addSummary(block) |
| 473 | else |
| 474 | for i, subBlock in pairs(block.elements) do |
| 475 | table.insert(queue, 1, subBlock) |
| 476 | end |
| 477 | block.visited = true |
| 478 | end |
Junxiao Shi | 1fda67d | 2018-01-12 21:35:31 +0000 | [diff] [blame] | 479 | |
| 480 | -- prepare information to fill info column |
| 481 | if block.type == 5 and pktType ~= "Nack" then |
| 482 | pktType = "Interest" |
| 483 | elseif block.type == 6 then |
| 484 | pktType = "Data" |
| 485 | elseif block.type == 800 then |
| 486 | pktType = "Nack" |
| 487 | end |
| 488 | |
Alexander Afanasyev | 4fb67ea | 2018-08-02 08:18:28 -0600 | [diff] [blame] | 489 | if block.type == 7 then |
Junxiao Shi | 1fda67d | 2018-01-12 21:35:31 +0000 | [diff] [blame] | 490 | pktName = getUriFromName(block) |
| 491 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 492 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 493 | |
| 494 | local info = NDN_DICT[block.type] |
| 495 | if (info ~= nil) then |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 496 | if (block.summary ~= nil) then |
| 497 | block.tree:append_text(", " .. NDN_DICT[block.type].name .. ", " .. block.summary) |
| 498 | else |
| 499 | block.tree:append_text(", " .. NDN_DICT[block.type].name) |
| 500 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 501 | end |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 502 | |
| 503 | nBytesLeft = nBytesLeft - block.size |
| 504 | |
| 505 | if (nBytesLeft > 0) then |
| 506 | ok, block = pcall(getBlock, tvb, tvb:len() - nBytesLeft) |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 507 | if (not ok or block == nil or not canBeValidNdnPacket(block)) then |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 508 | break |
| 509 | end |
| 510 | end |
Zipeng Wang | 574eeb0 | 2016-10-05 21:46:02 -0700 | [diff] [blame] | 511 | end -- while(block.size <= nBytesLeft) |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 512 | |
| 513 | pInfo.cols.protocol = tostring(pInfo.cols.protocol) .. " (" .. ndn.name .. ")" |
Junxiao Shi | 1fda67d | 2018-01-12 21:35:31 +0000 | [diff] [blame] | 514 | pInfo.cols.info = pktType .. " " .. pktName |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 515 | |
Alexander Afanasyev | 7f43c53 | 2015-08-12 15:28:51 -0700 | [diff] [blame] | 516 | 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] | 517 | pInfo.desegment_offset = tvb:len() - nBytesLeft |
| 518 | |
| 519 | -- Originally, I set desegment_len to the exact lenght, but it mysteriously didn't work for TCP |
| 520 | -- pInfo.desegment_len = block.size -- this will not work to desegment TCP streams |
| 521 | pInfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT |
| 522 | end |
Alexander Afanasyev | 6fbb7b4 | 2015-08-10 11:53:49 -0700 | [diff] [blame] | 523 | end |
| 524 | |
Alexander Afanasyev | 357c205 | 2015-08-10 21:26:52 -0700 | [diff] [blame] | 525 | local udpDissectorTable = DissectorTable.get("udp.port") |
| 526 | udpDissectorTable:add("6363", ndn) |
| 527 | udpDissectorTable:add("56363", ndn) |
| 528 | |
| 529 | local tcpDissectorTable = DissectorTable.get("tcp.port") |
| 530 | tcpDissectorTable:add("6363", ndn) |
| 531 | |
| 532 | local websocketDissectorTable = DissectorTable.get("ws.port") |
| 533 | -- websocketDissectorTable:add("9696", ndn) |
| 534 | websocketDissectorTable:add("1-65535", ndn) |
| 535 | |
Alexander Afanasyev | 7f43c53 | 2015-08-12 15:28:51 -0700 | [diff] [blame] | 536 | local ethernetDissectorTable = DissectorTable.get("ethertype") |
| 537 | ethernetDissectorTable:add(0x8624, ndn) |
| 538 | |
Alexander Afanasyev | 4fb67ea | 2018-08-02 08:18:28 -0600 | [diff] [blame] | 539 | local pppDissectorTable = DissectorTable.get("ppp.protocol") |
| 540 | pppDissectorTable:add(0x0077, ndn) |
| 541 | |
Junxiao Shi | ac4b546 | 2018-04-17 02:26:17 +0000 | [diff] [blame] | 542 | io.stderr:write("NDN dissector successfully loaded\n") |