Skip to content

Commit a7b331e

Browse files
committed
Supporting <size> layer in commsdsl2wireshark and pseudo <value> layer to set version.
1 parent fe4d7ce commit a7b331e

8 files changed

Lines changed: 206 additions & 15 deletions

File tree

app/commsdsl2wireshark/src/Wireshark.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,26 @@ std::string Wireshark::wiresharkLocalNamespaceName(const WiresharkGenerator& gen
9797
return wiresharkProtocolObjName(generator) + "_local";
9898
}
9999

100+
std::string Wireshark::wiresharkProtVersionGetFuncName(const WiresharkGenerator& generator)
101+
{
102+
return wiresharkLocalNamespaceName(generator) + ".prot_version_get";
103+
}
104+
105+
std::string Wireshark::wiresharkProtVersionSetFuncName(const WiresharkGenerator& generator)
106+
{
107+
return wiresharkLocalNamespaceName(generator) + ".prot_version_set";
108+
}
109+
110+
std::string Wireshark::wiresharkPinfoName(const WiresharkGenerator& generator)
111+
{
112+
return wiresharkLocalNamespaceName(generator) + ".last_pinfo";
113+
}
114+
115+
std::string Wireshark::wiresharkPacketIdFuncName(const WiresharkGenerator& generator)
116+
{
117+
return wiresharkLocalNamespaceName(generator) + ".packet_id";
118+
}
119+
100120
bool Wireshark::wiresharkWriteInternal() const
101121
{
102122
auto fileName = wiresharkFileName(m_wiresharkGenerator);
@@ -114,6 +134,8 @@ bool Wireshark::wiresharkWriteInternal() const
114134
"#^#GEN_COMMENT#$#\n"
115135
"#^#PROTOCOL#$#\n"
116136
"#^#LOCAL#$#\n"
137+
"#^#PINFO#$#\n"
138+
"#^#PROT_VERSION#$#\n"
117139
"#^#STATUS_CODE#$#\n"
118140
"#^#OPT_MODE#$#\n"
119141
"#^#FIELDS_REG#$#\n"
@@ -141,6 +163,8 @@ bool Wireshark::wiresharkWriteInternal() const
141163
{"EXTRACTORS_DECL", wiresharkExtractorsDeclInternal()},
142164
{"EXTRACTORS_REG", wiresharkExtractorsRegCodeInternal()},
143165
{"FIELD_VALUE_FUNC", wiresharkFieldValueFuncInternal()},
166+
{"PROT_VERSION", wiresharkProtocolVersionDefInternal()},
167+
{"PINFO", wiresharkPinfoDefInternal()},
144168
};
145169

146170
auto str = commsdsl::gen::util::genProcessTemplate(Templ, repl, true);
@@ -278,6 +302,7 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
278302
"end\n"
279303
"\n"
280304
"pinfo.cols.protocol = #^#NAME#$#.name\n"
305+
"#^#PINFO#$# = pinfo\n"
281306
"\n"
282307
"local result = #^#SUCCESS#$#\n"
283308
"local next_offset = 0\n"
@@ -302,6 +327,7 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
302327
{"FRAMES", util::genStrListToString(elems, "\n", "")},
303328
{"SUCCESS", wiresharkStatusCodeStr(m_wiresharkGenerator, WiresharkStatusCode::Success)},
304329
{"NOT_ENOUGH_DATA", wiresharkStatusCodeStr(m_wiresharkGenerator, WiresharkStatusCode::NotEnoughData)},
330+
{"PINFO", wiresharkPinfoName(m_wiresharkGenerator)},
305331
};
306332

307333
return util::genProcessTemplate(Templ, repl);
@@ -438,6 +464,59 @@ std::string Wireshark::wiresharkFieldValueFuncInternal() const
438464
return util::genProcessTemplate(Templ, repl);
439465
}
440466

467+
std::string Wireshark::wiresharkProtocolVersionDefInternal() const
468+
{
469+
const std::string Templ =
470+
"#^#LOCAL#$#.spec_version = #^#SPEC#$#\n"
471+
"#^#LOCAL#$#.prot_version = {}\n"
472+
"\n"
473+
"function #^#GET_NAME#$#()\n"
474+
" local pkt_id = #^#PKT_ID#$#()\n"
475+
" local last_ver = #^#LOCAL#$#.prot_version[pkt_id]\n"
476+
" if last_ver ~= #^#NIL#$# then\n"
477+
" return last_ver\n"
478+
" end\n"
479+
"\n"
480+
" return #^#LOCAL#$#.spec_version\n"
481+
"end\n"
482+
"\n"
483+
"function #^#SET_NAME#$#(value)\n"
484+
" local pkt_id = #^#PKT_ID#$#()\n"
485+
" #^#LOCAL#$#.prot_version[pkt_id] = value\n"
486+
"end\n"
487+
;
488+
489+
util::GenReplacementMap repl = {
490+
{"LOCAL", wiresharkLocalNamespaceName(m_wiresharkGenerator)},
491+
{"SPEC", std::to_string(m_wiresharkGenerator.genProtocolSchema().genSchemaVersion())},
492+
{"GET_NAME", wiresharkProtVersionGetFuncName(m_wiresharkGenerator)},
493+
{"SET_NAME", wiresharkProtVersionSetFuncName(m_wiresharkGenerator)},
494+
{"PKT_ID", wiresharkPacketIdFuncName(m_wiresharkGenerator)},
495+
{"NIL", strings::genNilStr()},
496+
};
497+
498+
return util::genProcessTemplate(Templ, repl);
499+
}
500+
501+
std::string Wireshark::wiresharkPinfoDefInternal() const
502+
{
503+
const std::string Templ =
504+
"#^#NAME#$# = #^#NIL#$#\n"
505+
"\n"
506+
"function #^#PKT_ID#$#()\n"
507+
" return tostring(#^#NAME#$#.src) .. \":\" .. tostring(#^#NAME#$#.src_port) .. \" -> \" .. tostring(#^#NAME#$#.dst) .. \":\" .. tostring(#^#NAME#$#.dst_port)\n"
508+
"end\n"
509+
;
510+
511+
util::GenReplacementMap repl = {
512+
{"NAME", wiresharkPinfoName(m_wiresharkGenerator)},
513+
{"NIL", strings::genNilStr()},
514+
{"PKT_ID", wiresharkPacketIdFuncName(m_wiresharkGenerator)},
515+
};
516+
517+
return util::genProcessTemplate(Templ, repl);
518+
}
519+
441520
const std::string& Wireshark::wiresharkStatusCodeStrInternal(WiresharkStatusCode code)
442521
{
443522
static const std::string Map[] = {

app/commsdsl2wireshark/src/Wireshark.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class Wireshark
5353
static std::string wiresharkOptModeValsName(const WiresharkGenerator& generator);
5454
static std::string wiresharkFieldValueFuncName(const WiresharkGenerator& generator);
5555
static std::string wiresharkLocalNamespaceName(const WiresharkGenerator& generator);
56+
static std::string wiresharkProtVersionGetFuncName(const WiresharkGenerator& generator);
57+
static std::string wiresharkProtVersionSetFuncName(const WiresharkGenerator& generator);
58+
static std::string wiresharkPinfoName(const WiresharkGenerator& generator);
59+
static std::string wiresharkPacketIdFuncName(const WiresharkGenerator& generator);
5660

5761
private:
5862
explicit Wireshark(const WiresharkGenerator& generator) : m_wiresharkGenerator(generator) {}
@@ -72,6 +76,8 @@ class Wireshark
7276
std::string wiresharkExtractorsDeclInternal() const;
7377
std::string wiresharkExtractorsRegCodeInternal() const;
7478
std::string wiresharkFieldValueFuncInternal() const;
79+
std::string wiresharkProtocolVersionDefInternal() const;
80+
std::string wiresharkPinfoDefInternal() const;
7581
static const std::string& wiresharkStatusCodeStrInternal(WiresharkStatusCode code);
7682
static const std::string& wiresharkOptModeStrInternal(WiresharkOptMode code);
7783

app/commsdsl2wireshark/src/WiresharkIntField.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,22 @@ std::string WiresharkIntField::wiresharkDefaultAssignmentsImpl(const WiresharkFi
361361
"#^#TREE#$#:add(#^#FIELD#$#, #^#TVB#$#(#^#OFFSET#$#, 0), #^#VAL#$#):set_hidden(true)\n"
362362
;
363363

364-
auto val = parseObj.parseDefaultValue();
365-
if ((val == 0) &&
366-
(parseObj.parseSemanticType() == commsdsl::parse::ParseField::ParseSemanticType::Version)) {
367-
val = genGenerator().genSchemaOf(*this).genSchemaVersion();
368-
}
364+
std::string valStr;
365+
do {
366+
auto val = parseObj.parseDefaultValue();
367+
if ((val == 0) &&
368+
(parseObj.parseSemanticType() == commsdsl::parse::ParseField::ParseSemanticType::Version)) {
369+
370+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
371+
valStr = Wireshark::wiresharkProtVersionGetFuncName(wiresharkGenerator) + "()";
372+
break;
373+
}
369374

370-
auto valStr = std::to_string(val);
371-
if ((val < 0) && genIsUnsignedType()) {
372-
valStr = std::to_string(static_cast<std::uintmax_t>(val));
373-
}
375+
valStr = std::to_string(val);
376+
if ((val < 0) && genIsUnsignedType()) {
377+
valStr = std::to_string(static_cast<std::uintmax_t>(val));
378+
}
379+
} while (false);
374380

375381
util::GenReplacementMap repl = {
376382
{"TREE", wiresharkTreeStr()},

app/commsdsl2wireshark/src/WiresharkSizeLayer.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@
1515

1616
#include "WiresharkSizeLayer.h"
1717

18+
#include "Wireshark.h"
19+
#include "WiresharkField.h"
1820
#include "WiresharkGenerator.h"
1921

22+
#include "commsdsl/gen/util.h"
23+
24+
#include <cassert>
25+
26+
namespace util = commsdsl::gen::util;
27+
2028
namespace commsdsl2wireshark
2129
{
2230

@@ -26,4 +34,40 @@ WiresharkSizeLayer::WiresharkSizeLayer(WiresharkGenerator& generator, ParseLayer
2634
{
2735
}
2836

37+
std::string WiresharkSizeLayer::wiresharkDissectBodyImpl() const
38+
{
39+
static const std::string Templ =
40+
"#^#FIELD#$#\n"
41+
"local next_limit = #^#NEXT_OFFSET#$# + #^#VALUE_FUNC#$#()\n"
42+
"if #^#LIMIT#$# < next_limit then\n"
43+
" return #^#NOT_ENOUGH_DATA#$#, #^#OFFSET#$#\n"
44+
"end\n"
45+
"\n"
46+
"#^#OFFSET#$# = #^#NEXT_OFFSET#$#\n"
47+
"#^#LIMIT#$# = next_limit\n"
48+
"#^#NEXT#$#\n"
49+
"if #^#RESULT#$# == #^#NOT_ENOUGH_DATA#$# then\n"
50+
" return #^#MALFORMED#$#, #^#LIMIT#$#\n"
51+
"end\n"
52+
;
53+
54+
auto* field = wiresharkField();
55+
assert(field != nullptr);
56+
57+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
58+
util::GenReplacementMap repl = {
59+
{"FIELD", wiresharkDissectFieldCode()},
60+
{"NEXT", wiresharkNextFuncCode()},
61+
{"OFFSET", WiresharkField::wiresharkOffsetStr()},
62+
{"NEXT_OFFSET", WiresharkField::wiresharkNextOffsetStr()},
63+
{"VALUE_FUNC", field->wiresharkValueFuncName()},
64+
{"NOT_ENOUGH_DATA", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::WiresharkStatusCode::NotEnoughData)},
65+
{"MALFORMED", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::WiresharkStatusCode::MalformedPacket)},
66+
{"RESULT", WiresharkField::wiresharkResultStr()},
67+
{"LIMIT", WiresharkField::wiresharkOffsetLimitStr()},
68+
};
69+
70+
return util::genProcessTemplate(Templ, repl);
71+
}
72+
2973
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkSizeLayer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class WiresharkSizeLayer final : public commsdsl::gen::GenSizeLayer, public Wire
3333
using GenElem = commsdsl::gen::GenElem;
3434

3535
WiresharkSizeLayer(WiresharkGenerator& generator, ParseLayer parseObj, GenElem* parent);
36+
37+
protected:
38+
virtual std::string wiresharkDissectBodyImpl() const override;
3639
};
3740

3841
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkValueLayer.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,24 @@ WiresharkValueLayer::WiresharkValueLayer(WiresharkGenerator& generator, ParseLay
4141

4242
std::string WiresharkValueLayer::wiresharkDissectBodyImpl() const
4343
{
44+
auto parseObj = genValueLayerParseObj();
45+
if (parseObj.parsePseudo()) {
46+
return wiresharkNextFuncCode();
47+
}
48+
4449
static const std::string Templ =
4550
"#^#FIELD#$#\n"
4651
"#^#INTERFACE_READ#$#\n"
47-
"offset = next_offset\n"
52+
"#^#OFFSET#$# = #^#NEXT_OFFSET#$#\n"
4853
"#^#NEXT#$#\n"
4954
;
5055

5156
util::GenReplacementMap repl = {
5257
{"FIELD", wiresharkDissectFieldCode()},
5358
{"INTERFACE_READ", wiresharkInterfaceReadCodeInternal()},
5459
{"NEXT", wiresharkNextFuncCode()},
60+
{"OFFSET", WiresharkField::wiresharkOffsetStr()},
61+
{"NEXT_OFFSET", WiresharkField::wiresharkNextOffsetStr()},
5562
};
5663

5764
return util::genProcessTemplate(Templ, repl);
@@ -64,11 +71,6 @@ bool WiresharkValueLayer::wiresharkIsInterfaceSupportedImpl(const WiresharkInter
6471

6572
std::string WiresharkValueLayer::wiresharkInterfaceReadCodeInternal() const
6673
{
67-
auto parseObj = genValueLayerParseObj();
68-
if (parseObj.parsePseudo()) {
69-
return strings::genEmptyString();
70-
}
71-
7274
static const std::string Templ =
7375
"local interface_tree = #^#TREE#$#:add(#^#PROTO#$#, #^#TVB#$#(#^#OFFSET#$#, #^#LIMIT#$# - #^#OFFSET#$#))\n"
7476
"interface_tree:set_hidden(true)\n"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
function test35_local.message_Msg1_read(tvb, tree, offset, offset_limit)
3+
local result, next_offset = test35_local.message_Msg1_readOrig(tvb, tree, offset, offset_limit)
4+
if result == test35_local.StatusCode.SUCCESS then
5+
test35_local.prot_version_set(test35_local.field_Version_value())
6+
end
7+
8+
return result, next_offset
9+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
3+
from commsdsl_pcap_gen import *
4+
5+
def do_frame(size, id, payload):
6+
prefix = struct.pack('>BB', size, id)
7+
return prefix + payload
8+
9+
def pcap1(f):
10+
seq = 1000
11+
msg1_payload1 = struct.pack('>B', 1)
12+
msg1_1 = do_frame(len(msg1_payload1) + 1, 1, msg1_payload1)
13+
msg2_payload1 = struct.pack('')
14+
msg2_1 = do_frame(len(msg2_payload1) + 1, 2, msg2_payload1)
15+
msg1_payload2 = struct.pack('>B', 2)
16+
msg1_2 = do_frame(len(msg1_payload2) + 1, 1, msg1_payload2)
17+
msg2_payload2 = struct.pack('>B', 0xff)
18+
msg2_2 = do_frame(len(msg2_payload2) + 1, 2, msg2_payload2)
19+
header = commsdsl_create_ethernet_ip_tcp_headers(len(msg1_1) + len(msg2_1) + len(msg1_2) + len(msg2_2), seq)
20+
commsdsl_write_packet(f, header + msg1_1 + msg2_1 + msg1_2 + msg2_2, time.time())
21+
22+
def pcap2(f):
23+
seq = 2000
24+
msg1_payload = struct.pack('>B', 1)
25+
msg1 = do_frame(len(msg1_payload) + 1, 1, msg1_payload)
26+
msg2_payload = struct.pack('')
27+
msg2 = do_frame(len(msg2_payload) + 1, 2, msg2_payload)
28+
header = commsdsl_create_ethernet_ip_tcp_headers(len(msg1) + len(msg2), seq)
29+
commsdsl_write_packet(f, header + msg1 + msg2, time.time())
30+
31+
seq = 3000
32+
header = commsdsl_create_ethernet_ip_tcp_headers(len(msg2) + len(msg2), seq)
33+
commsdsl_write_packet(f, header + msg2 + msg2, time.time())
34+
35+
def main():
36+
with open(sys.argv[1], 'wb') as f:
37+
commsdsl_write_pcap_header(f)
38+
pcap1(f)
39+
pcap2(f)
40+
41+
if __name__ == '__main__':
42+
main()

0 commit comments

Comments
 (0)