Skip to content

Commit fab6723

Browse files
committed
Saving work on commsdsl2wireshark.
1 parent 900c3ab commit fab6723

11 files changed

Lines changed: 258 additions & 44 deletions

app/commsdsl2wireshark/src/Wireshark.cpp

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424

2525
#include <cassert>
2626
#include <fstream>
27+
#include <type_traits>
2728

2829
namespace util = commsdsl::gen::util;
2930
namespace strings = commsdsl::gen::strings;
3031

3132
namespace commsdsl2wireshark
3233
{
3334

34-
bool Wireshark::wiresharkWrite(WiresharkGenerator& generator)
35+
bool Wireshark::wiresharkWrite(const WiresharkGenerator& generator)
3536
{
3637
Wireshark obj(generator);
3738
return obj.wiresharkWriteInternal();
@@ -57,6 +58,12 @@ std::string Wireshark::wiresharkFieldsListName(const WiresharkGenerator& generat
5758
return wiresharkProtocolObjName(generator) + "_fields_list";
5859
}
5960

61+
std::string Wireshark::wiresharkStatusCodeStr(const WiresharkGenerator& generator, StatusCode code)
62+
{
63+
Wireshark obj(generator);
64+
return obj.wiresharkStatusCodeNameInternal() + "." + wiresharkStatusCodeStrInternal(code);
65+
}
66+
6067
bool Wireshark::wiresharkWriteInternal() const
6168
{
6269
auto fileName = wiresharkFileName(m_wiresharkGenerator);
@@ -73,6 +80,7 @@ bool Wireshark::wiresharkWriteInternal() const
7380
const std::string Templ =
7481
"#^#GEN_COMMENT#$#\n"
7582
"#^#PROTOCOL#$#\n"
83+
"#^#STATUS_CODE#$#\n"
7684
"#^#FIELDS_REG#$#\n"
7785
"#^#CODE#$#\n"
7886
"#^#DISSECT_FUNC#$#\n"
@@ -89,6 +97,7 @@ bool Wireshark::wiresharkWriteInternal() const
8997
{"NAME", wiresharkProtocolObjName(m_wiresharkGenerator)},
9098
{"FIELDS_LIST", wiresharkFieldsListName(m_wiresharkGenerator)},
9199
{"CODE", wiresharkCodeInternal()},
100+
{"STATUS_CODE", wiresharkStatusCodeDefInternal()},
92101
};
93102

94103
auto str = commsdsl::gen::util::genProcessTemplate(Templ, repl, true);
@@ -209,13 +218,14 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
209218
"\n"
210219
"pinfo.cols.protocol = #^#NAME#$#.name\n"
211220
"\n"
212-
"local result = false\n"
221+
"local result = #^#SUCCESS#$#\n"
213222
"local next_offset = 0\n"
214223
"repeat\n"
215224
" #^#FRAMES#$#\n"
216225
"until true\n"
217226
"\n"
218-
"if not result then\n"
227+
"if (result ~= #^#NOT_ENOUGH_DATA#$#) and (result ~= #^#SUCCESS#$#) then\n"
228+
" -- Consume everything\n"
219229
" return\n"
220230
"end\n"
221231
"\n"
@@ -227,11 +237,58 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
227237

228238
util:: GenReplacementMap repl = {
229239
{"NAME", wiresharkProtocolObjName(m_wiresharkGenerator)},
230-
{"FRAMES", util::genStrListToString(elems, "\n", "")}
240+
{"FRAMES", util::genStrListToString(elems, "\n", "")},
241+
{"SUCCESS", wiresharkStatusCodeStr(m_wiresharkGenerator, StatusCode::Success)},
242+
{"NOT_ENOUGH_DATA", wiresharkStatusCodeStr(m_wiresharkGenerator, StatusCode::NotEnoughData)},
231243
};
232244

233245
return util::genProcessTemplate(Templ, repl);
234246
}
235247

248+
std::string Wireshark::wiresharkStatusCodeNameInternal() const
249+
{
250+
return wiresharkProtocolObjName(m_wiresharkGenerator) + "_StatusCode";
251+
}
252+
253+
std::string Wireshark::wiresharkStatusCodeDefInternal() const
254+
{
255+
util::GenStringsList vals;
256+
257+
for (auto idx = 0U; idx < static_cast<unsigned>(StatusCode::ValuesLimit); ++idx) {
258+
vals.push_back(wiresharkStatusCodeStrInternal(static_cast<StatusCode>(idx)) + " = " + std::to_string(idx));
259+
}
260+
261+
const std::string Templ =
262+
"local #^#NAME#$# = {\n"
263+
" #^#VALS#$#\n"
264+
"}\n"
265+
;
266+
267+
util::GenReplacementMap repl = {
268+
{"NAME", wiresharkStatusCodeNameInternal()},
269+
{"VALS", util::genStrListToString(vals, ",\n", "")},
270+
};
271+
272+
return util::genProcessTemplate(Templ, repl);
273+
}
274+
275+
const std::string& Wireshark::wiresharkStatusCodeStrInternal(StatusCode code)
276+
{
277+
static const std::string Map[] = {
278+
/* Success */ "SUCCESS",
279+
/* NotEnoughData */ "NOT_ENOUGH_DATA",
280+
/* MalformedData */ "MALFORMED_PACKET",
281+
/* InvalidMsgId */ "INVALID_MSG_ID",
282+
/* InvalidMsgData */ "INVALID_MSG_DATA",
283+
/* CodegenError */ "CODEGEN_ERROR",
284+
};
285+
static const std::size_t MapSize = std::extent_v<decltype(Map)>;
286+
static_assert(MapSize == static_cast<unsigned>(StatusCode::ValuesLimit));
287+
288+
auto idx = static_cast<unsigned>(code);
289+
assert(idx < MapSize);
290+
return Map[idx];
291+
}
292+
236293
} // namespace commsdsl2wireshark
237294

app/commsdsl2wireshark/src/Wireshark.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,25 @@ class WiresharkGenerator;
2424
class Wireshark
2525
{
2626
public:
27-
static bool wiresharkWrite(WiresharkGenerator& generator);
27+
enum class StatusCode {
28+
Success,
29+
NotEnoughData,
30+
MalformedPacket,
31+
InvalidMsgId,
32+
InvalidMsgData,
33+
CodegenError,
34+
ValuesLimit // Must be last
35+
};
36+
37+
static bool wiresharkWrite(const WiresharkGenerator& generator);
2838
static std::string wiresharkFileName(const WiresharkGenerator& generator);
2939
static const std::string& wiresharkProtocolObjName(const WiresharkGenerator& generator);
3040
static std::string wiresharkCreateFieldFuncName(const WiresharkGenerator& generator);
3141
static std::string wiresharkFieldsListName(const WiresharkGenerator& generator);
42+
static std::string wiresharkStatusCodeStr(const WiresharkGenerator& generator, StatusCode code);
3243

3344
private:
34-
explicit Wireshark(WiresharkGenerator& generator) : m_wiresharkGenerator(generator) {}
45+
explicit Wireshark(const WiresharkGenerator& generator) : m_wiresharkGenerator(generator) {}
3546

3647
private:
3748
bool wiresharkWriteInternal() const;
@@ -40,8 +51,11 @@ class Wireshark
4051
std::string wiresharkFieldsRegistrationInternal() const;
4152
std::string wiresharkCodeInternal() const;
4253
std::string wiresharkDissectFuncBodyInternal() const;
54+
std::string wiresharkStatusCodeNameInternal() const;
55+
std::string wiresharkStatusCodeDefInternal() const;
56+
static const std::string& wiresharkStatusCodeStrInternal(StatusCode code);
4357

44-
WiresharkGenerator& m_wiresharkGenerator;
58+
const WiresharkGenerator& m_wiresharkGenerator;
4559
};
4660

4761
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkField.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ std::string WiresharkField::wiresharkDissectCodeImpl(const WiresharkField* refFi
208208
"#^#NAME_VAR#$#\n"
209209
"#^#REG#$#\n"
210210
"#^#PREPEND#$#\n"
211-
"local function #^#NAME#$##^#SUFFIX#$#(tvb, tree, offset, offsetLimit)\n"
211+
"local function #^#NAME#$##^#SUFFIX#$#(#^#SIG#$#)\n"
212212
" #^#REPLACE#$#\n"
213213
" #^#BODY#$#\n"
214214
"end\n"
@@ -230,14 +230,15 @@ std::string WiresharkField::wiresharkDissectCodeImpl(const WiresharkField* refFi
230230
{"PREPEND", wiresharkGenerator.genReadCodeInjectCode(prependFileName, "Prepend here")},
231231
{"EXTEND", wiresharkGenerator.genReadCodeInjectCode(extendFileName, "Extend function above", &extended)},
232232
{"NAME_VAR", wiresharkNameDefInternal(refField)},
233+
{"SIG", wiresharkDissectSignature()},
233234
};
234235

235236
if (refField == nullptr) {
236237
repl["MEMBERS"] = wiresharkMembersDissectCodeImpl();
237238
}
238239

239240
if (!replaced) {
240-
repl["BODY"] = wiresharkDissectBodyInternal();
241+
repl["BODY"] = wiresharkDissectBodyInternal(refField);
241242
}
242243

243244
if (extended) {
@@ -288,6 +289,11 @@ std::string WiresharkField::wiresharkTvbRangeAccessImpl() const
288289
return strings::genEmptyString();
289290
}
290291

292+
std::string WiresharkField::wiresharkDissectBodyImpl() const
293+
{
294+
return strings::genEmptyString();
295+
}
296+
291297
std::string WiresharkField::wiresharkFieldRefName(const WiresharkField* refField) const
292298
{
293299
const auto* genField = &m_genField;
@@ -419,6 +425,12 @@ bool WiresharkField::wiresharkHasOverrideCode() const
419425
m_customCode.m_hasName;
420426
}
421427

428+
const std::string& WiresharkField::wiresharkDissectSignature()
429+
{
430+
static const std::string Str = "tvb, tree, offset, offset_limit, field";
431+
return Str;
432+
}
433+
422434
bool WiresharkField::wiresharkCopyCodeFromInternal()
423435
{
424436
auto obj = m_genField.genParseObj();
@@ -504,10 +516,24 @@ std::string WiresharkField::wiresharkNameDefInternal(const WiresharkField* refFi
504516
return util::genProcessTemplate(Templ, repl);
505517
}
506518

507-
std::string WiresharkField::wiresharkDissectBodyInternal() const
519+
std::string WiresharkField::wiresharkDissectBodyInternal(const WiresharkField* refField) const
508520
{
509-
// TODO:
510-
return std::string();
521+
static const std::string Templ =
522+
"field = field or #^#FIELD#$#\n"
523+
"local result = #^#ERROR#$#\n"
524+
"local next_offset = offset\n"
525+
"#^#REST#$#\n"
526+
"return result, next_offset\n"
527+
;
528+
529+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(m_genField.genGenerator());
530+
util::GenReplacementMap repl = {
531+
{"FIELD", wiresharkFieldObjName(refField)},
532+
{"ERROR", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::InvalidMsgData)},
533+
{"REST", wiresharkDissectBodyImpl()},
534+
};
535+
536+
return util::genProcessTemplate(Templ, repl);
511537
}
512538

513539
std::string WiresharkField::wiresharkCustomReadCodeInternal(bool& hasRealCode) const

app/commsdsl2wireshark/src/WiresharkField.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class WiresharkField
6767
virtual std::string wiresharkFieldRegistrationImpl(const WiresharkField* refField) const;
6868
virtual std::string wiresharkMembersDissectCodeImpl() const;
6969
virtual std::string wiresharkTvbRangeAccessImpl() const;
70+
virtual std::string wiresharkDissectBodyImpl() const;
7071

7172
std::string wiresharkFieldRefName(const WiresharkField* refField) const;
7273
bool wiresharkIsBitfieldMember() const;
@@ -78,6 +79,7 @@ class WiresharkField
7879
std::string wiresharkFieldDisplayNameStr(const WiresharkField* refField) const;
7980
std::string wiresharkFieldNameVarNameStr(const WiresharkField* refField) const;
8081
bool wiresharkHasOverrideCode() const;
82+
static const std::string& wiresharkDissectSignature();
8183

8284
private:
8385
using WiresharkCustomCodeFunc = std::string (WiresharkField::*)(bool& hasCode) const;
@@ -102,7 +104,7 @@ class WiresharkField
102104
bool& hasCode);
103105

104106
std::string wiresharkNameDefInternal(const WiresharkField* refField) const;
105-
std::string wiresharkDissectBodyInternal() const;
107+
std::string wiresharkDissectBodyInternal(const WiresharkField* refField) const;
106108
std::string wiresharkCustomReadCodeInternal(bool& hasRealCode) const;
107109
std::string wiresharkCustomValidCodeInternal(bool& hasRealCode) const;
108110
std::string wiresharkCustomNameCodeInternal(bool& hasRealCode) const;

app/commsdsl2wireshark/src/WiresharkFrame.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,39 @@ bool WiresharkFrame::genPrepareImpl()
123123
std::string WiresharkFrame::wiresharkDissectBodyInternal() const
124124
{
125125
static const std::string Templ =
126-
"local result = true\n"
126+
"local result = #^#SUCCESS#$#\n"
127127
"local offset = 0\n"
128128
"local len = tvb:len()\n"
129-
"while result and (offset < len) do\n"
129+
"while (result == #^#SUCCESS#$#) and (offset < len) do\n"
130130
" local frame_subtree = tree:add(#^#PROTO_NAME#$#, tvb(offset, -1), \"#^#FRAME_NAME#$#\")\n"
131131
" local layer_func = #^#LIST_NAME#$#[1]\n"
132-
" local next_offset = 0\n"
132+
" local next_offset = offset\n"
133133
" result, next_offset = layer_func(tvb, frame_subtree, offset, len, #^#LIST_NAME#$#, 2, #^#NIL#$#)\n"
134+
" if result == #^#NOT_ENOUGH_DATA#$# then\n"
135+
" frame_subtree:set_hidden(true)\n"
136+
" return result, offset\n"
137+
" end\n"
138+
"\n"
139+
" if result ~= #^#SUCCESS#$# then\n"
140+
" frame_subtree:set_hidden(true)\n"
141+
" return result, next_offset\n"
142+
" end\n"
143+
"\n"
134144
" frame_subtree:set_len(next_offset - offset)\n"
135145
" offset = next_offset\n"
136146
"end\n"
137147
"return result, offset\n"
138148
;
139149

150+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
140151
auto parseObj = genParseObj();
141152
util::GenReplacementMap repl = {
142-
{"PROTO_NAME", Wireshark::wiresharkProtocolObjName(WiresharkGenerator::wiresharkCast(genGenerator()))},
153+
{"PROTO_NAME", Wireshark::wiresharkProtocolObjName(wiresharkGenerator)},
143154
{"FRAME_NAME", util::genDisplayName(parseObj.parseDisplayName(), parseObj.parseName())},
144155
{"LIST_NAME", wiresharkLayerFuncsListNameInternal()},
145156
{"NIL", strings::genNilStr()},
157+
{"SUCCESS", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::Success)},
158+
{"NOT_ENOUGH_DATA", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::NotEnoughData)},
146159
};
147160

148161
return util::genProcessTemplate(Templ, repl);

app/commsdsl2wireshark/src/WiresharkLayer.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "WiresharkLayer.h"
1717

18+
#include "Wireshark.h"
1819
#include "WiresharkField.h"
1920
#include "WiresharkGenerator.h"
2021

@@ -66,9 +67,9 @@ std::string WiresharkLayer::wiresharkDissectCode() const
6667
"#^#FIELD#$#\n"
6768
"#^#EXTRA#$#\n"
6869
"#^#PREPEND#$#\n"
69-
"local function #^#NAME#$##^#SUFFIX#$#(tvb, tree, offset, offsetLimit, funcs, next_idx, msg)\n"
70+
"local function #^#NAME#$##^#SUFFIX#$#(tvb, tree, offset, offset-limit, funcs, next_idx, msg)\n"
7071
" #^#REPLACE#$#\n"
71-
" local result = true\n"
72+
" local result = #^#SUCCESS#$#\n"
7273
" local next_offset = offset\n"
7374
" #^#BODY#$#\n"
7475
" return result, next_offset\n"
@@ -91,6 +92,7 @@ std::string WiresharkLayer::wiresharkDissectCode() const
9192
{"PREPEND", wiresharkGenerator.genReadCodeInjectCode(prependFileName, "Prepend here")},
9293
{"EXTEND", wiresharkGenerator.genReadCodeInjectCode(extendFileName, "Extend function above", &extended)},
9394
{"EXTRA", wiresharkExtraDissectCodeImpl()},
95+
{"SUCCESS", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::Success)},
9496
};
9597

9698
if (!replaced) {
@@ -133,14 +135,16 @@ std::string WiresharkLayer::wiresharkDissectFieldCode() const
133135
}
134136

135137
static const std::string Templ =
136-
"result, next_offset = #^#NAME#$#(tvb, tree, offset, offsetLimit)\n"
137-
"if not result then\n"
138+
"result, next_offset = #^#NAME#$#(tvb, tree, offset, offset_limit)\n"
139+
"if result ~= #^#SUCCESS#$# then\n"
138140
" return result, next_offset\n"
139141
"end\n"
140142
;
141143

144+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(m_genLayer.genGenerator());
142145
util::GenReplacementMap repl = {
143146
{"NAME", field->wiresharkDissectName()},
147+
{"SUCCESS", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::Success)},
144148
};
145149

146150
return util::genProcessTemplate(Templ, repl);
@@ -151,16 +155,15 @@ std::string WiresharkLayer::wiresharkNextFuncCode() const
151155
static const std::string Templ =
152156
"local next_func = func[next_idx]\n"
153157
"if next_func == #^#NIL#$# then\n"
154-
" return false, offset\n"
155-
"end\n"
156-
"result, next_offset = next_func(tvb, tree, offset, offsetLimit, funcs, next_idx + 1, msg)\n"
157-
"if not result then\n"
158-
" return result, next_offset\n"
158+
" return #^#ERROR#$#, offset\n"
159159
"end\n"
160+
"result, next_offset = next_func(tvb, tree, offset, offset_limit, funcs, next_idx + 1, msg)\n"
160161
;
161162

163+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(m_genLayer.genGenerator());
162164
util::GenReplacementMap repl = {
163165
{"NIL", strings::genNilStr()},
166+
{"ERROR", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::CodegenError)},
164167
};
165168

166169
return util::genProcessTemplate(Templ, repl);

0 commit comments

Comments
 (0)