Skip to content

Commit a0d396b

Browse files
committed
Saving work on commsdsl2wireshark.
1 parent 8309b06 commit a0d396b

38 files changed

Lines changed: 1658 additions & 131 deletions

app/commsdsl2wireshark/src/Wireshark.cpp

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "commsdsl/gen/util.h"
2323
#include "commsdsl/gen/strings.h"
2424

25+
#include <algorithm>
2526
#include <cassert>
2627
#include <fstream>
2728
#include <type_traits>
@@ -68,12 +69,24 @@ std::string Wireshark::wiresharkExtractorsMapName(const WiresharkGenerator& gene
6869
return wiresharkProtocolObjName(generator) + "_extractors_map";
6970
}
7071

71-
std::string Wireshark::wiresharkStatusCodeStr(const WiresharkGenerator& generator, StatusCode code)
72+
std::string Wireshark::wiresharkStatusCodeStr(const WiresharkGenerator& generator, WiresharkStatusCode code)
7273
{
7374
Wireshark obj(generator);
7475
return obj.wiresharkStatusCodeNameInternal() + "." + wiresharkStatusCodeStrInternal(code);
7576
}
7677

78+
std::string Wireshark::wiresharkOptModeStr(const WiresharkGenerator& generator, WiresharkOptMode code)
79+
{
80+
Wireshark obj(generator);
81+
return obj.wiresharkOptModeNameInternal() + "." + wiresharkOptModeStrInternal(code);
82+
}
83+
84+
std::string Wireshark::wiresharkOptModeValsName(const WiresharkGenerator& generator)
85+
{
86+
Wireshark obj(generator);
87+
return obj.wiresharkOptModeNameInternal() + strings::genValsSuffixStr();
88+
}
89+
7790
std::string Wireshark::wiresharkFieldValueFuncName(const WiresharkGenerator& generator)
7891
{
7992
return wiresharkProtocolObjName(generator) + "_field_value";
@@ -96,6 +109,7 @@ bool Wireshark::wiresharkWriteInternal() const
96109
"#^#GEN_COMMENT#$#\n"
97110
"#^#PROTOCOL#$#\n"
98111
"#^#STATUS_CODE#$#\n"
112+
"#^#OPT_MODE#$#\n"
99113
"#^#FIELDS_REG#$#\n"
100114
"#^#EXTRACTORS_DECL#$#\n"
101115
"#^#FIELD_VALUE_FUNC#$#\n"
@@ -116,6 +130,7 @@ bool Wireshark::wiresharkWriteInternal() const
116130
{"FIELDS_LIST", wiresharkFieldsListName(m_wiresharkGenerator)},
117131
{"CODE", wiresharkCodeInternal()},
118132
{"STATUS_CODE", wiresharkStatusCodeDefInternal()},
133+
{"OPT_MODE", wiresharkOptionalModeDefInternal()},
119134
{"EXTRACTORS_DECL", wiresharkExtractorsDeclInternal()},
120135
{"EXTRACTORS_REG", wiresharkExtractorsRegCodeInternal()},
121136
{"FIELD_VALUE_FUNC", wiresharkFieldValueFuncInternal()},
@@ -227,7 +242,7 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
227242

228243
util::GenReplacementMap frameRepl = {
229244
{"NAME", wiresharkFrame.wiresharkDissectName()},
230-
{"SUCCESS", wiresharkStatusCodeStr(m_wiresharkGenerator, StatusCode::Success)},
245+
{"SUCCESS", wiresharkStatusCodeStr(m_wiresharkGenerator, WiresharkStatusCode::Success)},
231246
};
232247

233248
elems.push_back(util::genProcessTemplate(FrameTempl, frameRepl));
@@ -260,8 +275,8 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
260275
util:: GenReplacementMap repl = {
261276
{"NAME", wiresharkProtocolObjName(m_wiresharkGenerator)},
262277
{"FRAMES", util::genStrListToString(elems, "\n", "")},
263-
{"SUCCESS", wiresharkStatusCodeStr(m_wiresharkGenerator, StatusCode::Success)},
264-
{"NOT_ENOUGH_DATA", wiresharkStatusCodeStr(m_wiresharkGenerator, StatusCode::NotEnoughData)},
278+
{"SUCCESS", wiresharkStatusCodeStr(m_wiresharkGenerator, WiresharkStatusCode::Success)},
279+
{"NOT_ENOUGH_DATA", wiresharkStatusCodeStr(m_wiresharkGenerator, WiresharkStatusCode::NotEnoughData)},
265280
};
266281

267282
return util::genProcessTemplate(Templ, repl);
@@ -276,8 +291,8 @@ std::string Wireshark::wiresharkStatusCodeDefInternal() const
276291
{
277292
util::GenStringsList vals;
278293

279-
for (auto idx = 0U; idx < static_cast<unsigned>(StatusCode::ValuesLimit); ++idx) {
280-
vals.push_back(wiresharkStatusCodeStrInternal(static_cast<StatusCode>(idx)) + " = " + std::to_string(idx));
294+
for (auto idx = 0U; idx < static_cast<unsigned>(WiresharkStatusCode::ValuesLimit); ++idx) {
295+
vals.push_back(wiresharkStatusCodeStrInternal(static_cast<WiresharkStatusCode>(idx)) + " = " + std::to_string(idx));
281296
}
282297

283298
const std::string Templ =
@@ -294,6 +309,56 @@ std::string Wireshark::wiresharkStatusCodeDefInternal() const
294309
return util::genProcessTemplate(Templ, repl);
295310
}
296311

312+
std::string Wireshark::wiresharkOptModeNameInternal() const
313+
{
314+
return wiresharkProtocolObjName(m_wiresharkGenerator) + "_OptMode";
315+
}
316+
317+
std::string Wireshark::wiresharkOptionalModeDefInternal() const
318+
{
319+
auto& schemas = m_wiresharkGenerator.genSchemas();
320+
bool requiresOptMode =
321+
std::any_of(
322+
schemas.begin(), schemas.end(),
323+
[](auto& sPtr)
324+
{
325+
assert(sPtr);
326+
return WiresharkSchema::wiresharkCast(*sPtr).wiresharkNeedsOptionalModeDefinition();
327+
});
328+
329+
if (!requiresOptMode) {
330+
return strings::genEmptyString();
331+
}
332+
333+
util::GenStringsList vals;
334+
util::GenStringsList valNames;
335+
336+
for (auto idx = 0U; idx < static_cast<unsigned>(WiresharkOptMode::ValuesLimit); ++idx) {
337+
auto& optModeStr = wiresharkOptModeStrInternal(static_cast<WiresharkOptMode>(idx));
338+
vals.push_back(optModeStr + " = " + std::to_string(idx));
339+
valNames.push_back("[" + std::to_string(idx) + "] = \"" + optModeStr + "\"");
340+
}
341+
342+
const std::string Templ =
343+
"local #^#NAME#$# = {\n"
344+
" #^#VALS#$#\n"
345+
"}\n"
346+
"\n"
347+
"local #^#VALS_NAME#$# = {\n"
348+
" #^#VAL_NAMES#$#\n"
349+
"}\n"
350+
;
351+
352+
util::GenReplacementMap repl = {
353+
{"NAME", wiresharkOptModeNameInternal()},
354+
{"VALS", util::genStrListToString(vals, ",\n", "")},
355+
{"VALS_NAME", wiresharkOptModeValsName(m_wiresharkGenerator)},
356+
{"VAL_NAMES", util::genStrListToString(valNames, ",\n", "")},
357+
};
358+
359+
return util::genProcessTemplate(Templ, repl);
360+
}
361+
297362
std::string Wireshark::wiresharkExtractorsDeclInternal() const
298363
{
299364
const std::string Templ =
@@ -348,7 +413,7 @@ std::string Wireshark::wiresharkFieldValueFuncInternal() const
348413
return util::genProcessTemplate(Templ, repl);
349414
}
350415

351-
const std::string& Wireshark::wiresharkStatusCodeStrInternal(StatusCode code)
416+
const std::string& Wireshark::wiresharkStatusCodeStrInternal(WiresharkStatusCode code)
352417
{
353418
static const std::string Map[] = {
354419
/* Success */ "SUCCESS",
@@ -359,7 +424,22 @@ const std::string& Wireshark::wiresharkStatusCodeStrInternal(StatusCode code)
359424
/* CodegenError */ "CODEGEN_ERROR",
360425
};
361426
static const std::size_t MapSize = std::extent_v<decltype(Map)>;
362-
static_assert(MapSize == static_cast<unsigned>(StatusCode::ValuesLimit));
427+
static_assert(MapSize == static_cast<unsigned>(WiresharkStatusCode::ValuesLimit));
428+
429+
auto idx = static_cast<unsigned>(code);
430+
assert(idx < MapSize);
431+
return Map[idx];
432+
}
433+
434+
const std::string& Wireshark::wiresharkOptModeStrInternal(WiresharkOptMode code)
435+
{
436+
static const std::string Map[] = {
437+
/* Tentative */ "TENTATIVE",
438+
/* Exists */ "EXISTS",
439+
/* Missing */ "MISSING",
440+
};
441+
static const std::size_t MapSize = std::extent_v<decltype(Map)>;
442+
static_assert(MapSize == static_cast<unsigned>(WiresharkOptMode::ValuesLimit));
363443

364444
auto idx = static_cast<unsigned>(code);
365445
assert(idx < MapSize);

app/commsdsl2wireshark/src/Wireshark.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class WiresharkGenerator;
2424
class Wireshark
2525
{
2626
public:
27-
enum class StatusCode {
27+
enum class WiresharkStatusCode {
2828
Success,
2929
NotEnoughData,
3030
MalformedPacket,
@@ -34,14 +34,23 @@ class Wireshark
3434
ValuesLimit // Must be last
3535
};
3636

37+
enum class WiresharkOptMode {
38+
Tentative,
39+
Exists,
40+
Missing,
41+
ValuesLimit // Must be last
42+
};
43+
3744
static bool wiresharkWrite(const WiresharkGenerator& generator);
3845
static std::string wiresharkFileName(const WiresharkGenerator& generator);
3946
static const std::string& wiresharkProtocolObjName(const WiresharkGenerator& generator);
4047
static std::string wiresharkCreateFieldFuncName(const WiresharkGenerator& generator);
4148
static std::string wiresharkCreateExtractorFuncName(const WiresharkGenerator& generator);
4249
static std::string wiresharkFieldsListName(const WiresharkGenerator& generator);
4350
static std::string wiresharkExtractorsMapName(const WiresharkGenerator& generator);
44-
static std::string wiresharkStatusCodeStr(const WiresharkGenerator& generator, StatusCode code);
51+
static std::string wiresharkStatusCodeStr(const WiresharkGenerator& generator, WiresharkStatusCode code);
52+
static std::string wiresharkOptModeStr(const WiresharkGenerator& generator, WiresharkOptMode code);
53+
static std::string wiresharkOptModeValsName(const WiresharkGenerator& generator);
4554
static std::string wiresharkFieldValueFuncName(const WiresharkGenerator& generator);
4655

4756
private:
@@ -56,10 +65,13 @@ class Wireshark
5665
std::string wiresharkDissectFuncBodyInternal() const;
5766
std::string wiresharkStatusCodeNameInternal() const;
5867
std::string wiresharkStatusCodeDefInternal() const;
68+
std::string wiresharkOptModeNameInternal() const;
69+
std::string wiresharkOptionalModeDefInternal() const;
5970
std::string wiresharkExtractorsDeclInternal() const;
6071
std::string wiresharkExtractorsRegCodeInternal() const;
6172
std::string wiresharkFieldValueFuncInternal() const;
62-
static const std::string& wiresharkStatusCodeStrInternal(StatusCode code);
73+
static const std::string& wiresharkStatusCodeStrInternal(WiresharkStatusCode code);
74+
static const std::string& wiresharkOptModeStrInternal(WiresharkOptMode code);
6375

6476
const WiresharkGenerator& m_wiresharkGenerator;
6577
};

app/commsdsl2wireshark/src/WiresharkBitfieldField.cpp

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ std::string WiresharkBitfieldField::wiresharkDissectBodyImpl([[maybe_unused]] co
178178
static const std::string MemTempl =
179179
"#^#RESULT#$#, _ = #^#DISSECT#$#(#^#TVB#$#, #^#SUBTREE#$#, #^#OFFSET#$#, #^#OFFSET#$# + #^#LEN#$#)\n"
180180
"if #^#RESULT#$# ~= #^#SUCCESS#$# then\n"
181-
" return #^#RESULT#$#, #^#OFFSET#$#\n"
181+
" break\n"
182182
"end\n"
183183
;
184184

@@ -189,17 +189,19 @@ std::string WiresharkBitfieldField::wiresharkDissectBodyImpl([[maybe_unused]] co
189189
{"SUBTREE", wiresharkFieldSubtreeStr()},
190190
{"OFFSET", wiresharkOffsetStr()},
191191
{"LEN", std::to_string(parseObj.parseMaxLength())},
192-
{"SUCCESS", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::Success)},
192+
{"SUCCESS", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::WiresharkStatusCode::Success)},
193193
};
194194

195195
members.push_back(util::genProcessTemplate(MemTempl, memRepl));
196196
}
197197

198198
static const std::string Templ =
199-
"local #^#RANGE#$# = tvb(#^#OFFSET#$#, #^#LEN#$#)\n"
200-
"local #^#SUBTREE#$# = tree:add#^#SUFFIX#$#(field, #^#RANGE#$#)\n"
201-
"#^#MEMBERS#$#\n"
202-
"#^#NEXT_OFFSET#$# = #^#OFFSET#$# + #^#LEN#$#\n"
199+
"repeat\n"
200+
" local #^#RANGE#$# = #^#TVB#$#(#^#OFFSET#$#, #^#LEN#$#)\n"
201+
" local #^#SUBTREE#$# = #^#TREE#$#:add#^#SUFFIX#$#(#^#FIELD#$#, #^#RANGE#$#)\n"
202+
" #^#MEMBERS#$#\n"
203+
" #^#NEXT_OFFSET#$# = #^#OFFSET#$# + #^#LEN#$#\n"
204+
"until true\n"
203205
;
204206

205207
util::GenReplacementMap repl = {
@@ -209,6 +211,9 @@ std::string WiresharkBitfieldField::wiresharkDissectBodyImpl([[maybe_unused]] co
209211
{"OFFSET", wiresharkOffsetStr()},
210212
{"NEXT_OFFSET", wiresharkNextOffsetStr()},
211213
{"MEMBERS", util::genStrListToString(members, "\n", "")},
214+
{"FIELD", wiresharkFieldStr()},
215+
{"TVB", wiresharkTvbStr()},
216+
{"TREE", wiresharkTreeStr()},
212217
};
213218

214219
if (parseObj.parseEndian() == commsdsl::parse::ParseEndian_Little) {
@@ -271,6 +276,74 @@ std::string WiresharkBitfieldField::wiresharkValidFuncBodyImpl(const WiresharkFi
271276
return util::genProcessTemplate(Templ, repl);
272277
}
273278

279+
std::string WiresharkBitfieldField::wiresharkValueAccessStrImpl(const std::string& accStr, const WiresharkField* refField) const
280+
{
281+
if (accStr.empty()) {
282+
// TODO: support value override to allow proper comparison to bitfield value
283+
return WiresharkBase::wiresharkValueAccessStrImpl(accStr, refField);
284+
}
285+
286+
assert(refField == nullptr);
287+
auto memInfo = wiresharkSplitMemberAccStr(accStr, m_wiresharkFields);
288+
if (memInfo.first == nullptr) {
289+
genGenerator().genLogger().genError("BUG: Unexpected access string \"" + accStr + "\" for " + genParseObj().parseInnerRef());
290+
return strings::genUnexpectedValueStr();
291+
}
292+
293+
return memInfo.first->wiresharkValueAccessStr(memInfo.second);
294+
}
295+
296+
std::string WiresharkBitfieldField::wiresharkSizeAccessStrImpl(const std::string& accStr, const WiresharkField* refField) const
297+
{
298+
if (accStr.empty()) {
299+
[[maybe_unused]] static constexpr bool Should_not_happen = false;
300+
assert(Should_not_happen);
301+
return WiresharkBase::wiresharkSizeAccessStrImpl(accStr, refField);
302+
}
303+
304+
assert(refField == nullptr);
305+
auto memInfo = wiresharkSplitMemberAccStr(accStr, m_wiresharkFields);
306+
if (memInfo.first == nullptr) {
307+
genGenerator().genLogger().genError("BUG: Unexpected access string \"" + accStr + "\" for " + genParseObj().parseInnerRef());
308+
return strings::genUnexpectedValueStr();
309+
}
310+
311+
return memInfo.first->wiresharkSizeAccessStr(memInfo.second);
312+
}
313+
314+
std::string WiresharkBitfieldField::wiresharkExistsCheckStrImpl(const std::string& accStr) const
315+
{
316+
if (accStr.empty()) {
317+
[[maybe_unused]] static constexpr bool Should_not_happen = false;
318+
assert(Should_not_happen);
319+
return WiresharkBase::wiresharkExistsCheckStrImpl(accStr);
320+
}
321+
322+
auto memInfo = wiresharkSplitMemberAccStr(accStr, m_wiresharkFields);
323+
if (memInfo.first == nullptr) {
324+
genGenerator().genLogger().genError("BUG: Unexpected access string \"" + accStr + "\" for " + genParseObj().parseInnerRef());
325+
return strings::genUnexpectedValueStr();
326+
}
327+
328+
return memInfo.first->wiresharkExistsCheckStr(memInfo.second);
329+
}
330+
331+
std::string WiresharkBitfieldField::wiresharkDefaultAssignmentsImpl([[maybe_unused]] const WiresharkField* refField) const
332+
{
333+
assert(refField == nullptr);
334+
util::GenStringsList elems;
335+
for (auto* f : m_wiresharkFields) {
336+
auto str = f->wiresharkDefaultAssignments();
337+
if (str.empty()) {
338+
continue;
339+
}
340+
341+
elems.push_back(std::move(str));
342+
}
343+
344+
return util::genStrListToString(elems, "", "");
345+
}
346+
274347
bool WiresharkBitfieldField::wiresharkHasTrivialValidImpl() const
275348
{
276349
auto parseObj = genBitfieldFieldParseObj();
@@ -288,6 +361,11 @@ bool WiresharkBitfieldField::wiresharkHasTrivialValidImpl() const
288361
});
289362
}
290363

364+
const WiresharkBitfieldField::WiresharkFieldsList& WiresharkBitfieldField::wiresharkMemberFieldsImpl() const
365+
{
366+
return m_wiresharkFields;
367+
}
368+
291369
std::string WiresharkBitfieldField::wiresharkExtraValidCondsCodeInternal() const
292370
{
293371
auto parseObj = genBitfieldFieldParseObj();
@@ -307,7 +385,7 @@ std::string WiresharkBitfieldField::wiresharkExtraValidCondsCodeInternal() const
307385

308386
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
309387
util::GenReplacementMap repl = {
310-
{"COND", wiresharkDslCondToString(wiresharkGenerator, m_wiresharkFields, validCond)},
388+
{"COND", wiresharkDslCondToString(wiresharkGenerator, m_wiresharkFields, wiresharkInterface(), validCond)},
311389
};
312390

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

app/commsdsl2wireshark/src/WiresharkBitfieldField.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,16 @@ class WiresharkBitfieldField final : public commsdsl::gen::GenBitfieldField, pub
4646
virtual std::string wiresharkDissectBodyImpl(const WiresharkField* refField) const override;
4747
virtual std::string wiresharkExtractorsRegCodeImpl(const WiresharkField* refField) const override;
4848
virtual std::string wiresharkValidFuncBodyImpl(const WiresharkField* refField) const override;
49+
virtual std::string wiresharkValueAccessStrImpl(const std::string& accStr, const WiresharkField* refField) const override;
50+
virtual std::string wiresharkSizeAccessStrImpl(const std::string& accStr, const WiresharkField* refField) const override;
51+
virtual std::string wiresharkExistsCheckStrImpl(const std::string& accStr) const override;
52+
virtual std::string wiresharkDefaultAssignmentsImpl(const WiresharkField* refField) const override;
4953
virtual bool wiresharkHasTrivialValidImpl() const override;
54+
virtual const WiresharkFieldsList& wiresharkMemberFieldsImpl() const override;
5055

5156
private:
5257
std::string wiresharkExtraValidCondsCodeInternal() const;
58+
5359
WiresharkFieldsList m_wiresharkFields;
5460
};
5561

0 commit comments

Comments
 (0)