Skip to content

Commit cf8be3b

Browse files
committed
Saving work on <id> layer of commsdsl2wireshark.
1 parent baa6e97 commit cf8be3b

13 files changed

Lines changed: 259 additions & 12 deletions

app/commsdsl2wireshark/src/Wireshark.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ std::string Wireshark::wiresharkDissectFuncInternal() const
124124
const std::string Templ =
125125
"-- Main Dissector Entry Point\n"
126126
"function #^#NAME#$#.dissector(tvb, pinfo, tree)\n"
127+
" #^#REPLACE#$#\n"
127128
" #^#BODY#$#\n"
128129
"end\n"
129130
;
@@ -134,7 +135,7 @@ std::string Wireshark::wiresharkDissectFuncInternal() const
134135

135136
util::GenReplacementMap repl = {
136137
{"NAME", wiresharkProtocolObjName(m_wiresharkGenerator)},
137-
{"BODY", std::move(replaceCode)},
138+
{"REPLACE", std::move(replaceCode)},
138139
};
139140

140141
if (!bodyReplaced) {
@@ -225,6 +226,7 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
225226
;
226227

227228
util:: GenReplacementMap repl = {
229+
{"NAME", wiresharkProtocolObjName(m_wiresharkGenerator)},
228230
{"FRAMES", util::genStrListToString(elems, "\n", "")}
229231
};
230232

app/commsdsl2wireshark/src/WiresharkEnumField.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ std::string WiresharkEnumField::wiresharkFieldRegistrationImpl(const WiresharkFi
7272
return util::genProcessTemplate(Templ, repl);
7373
}
7474

75+
std::string WiresharkEnumField::wiresharkTvbRangeAccessImpl() const
76+
{
77+
auto obj = genEnumFieldParseObj();
78+
return WiresharkIntField::wiresharkTvbRangeAccessIntegralValue(obj.parseType(), obj.parseEndian(), obj.parseMaxLength());
79+
}
80+
7581
std::string WiresharkEnumField::wiresharkValsInternal(const WiresharkField* refField) const
7682
{
7783
auto& values = genSortedRevValues();

app/commsdsl2wireshark/src/WiresharkEnumField.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class WiresharkEnumField final : public commsdsl::gen::GenEnumField, public Wire
3737
protected:
3838
virtual bool genPrepareImpl() override;
3939
virtual std::string wiresharkFieldRegistrationImpl(const WiresharkField* refField) const override;
40+
virtual std::string wiresharkTvbRangeAccessImpl() const override;
4041

4142
private:
4243
std::string wiresharkValsInternal(const WiresharkField* refField) const;

app/commsdsl2wireshark/src/WiresharkField.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ bool WiresharkField::wiresharkHasCustomNameCode(const WiresharkField* refField)
168168
return m_customCode.m_hasName;
169169
}
170170

171+
std::string WiresharkField::wiresharkTvbRangeAccess() const
172+
{
173+
return wiresharkTvbRangeAccessImpl();
174+
}
175+
171176
std::string WiresharkField::wiresharkDissectNameImpl(const WiresharkField* refField) const
172177
{
173178
const auto* genField = &m_genField;
@@ -276,6 +281,12 @@ std::string WiresharkField::wiresharkMembersDissectCodeImpl() const
276281
return std::string();
277282
}
278283

284+
std::string WiresharkField::wiresharkTvbRangeAccessImpl() const
285+
{
286+
[[maybe_unused]] static constexpr bool Should_not_be_called = false;
287+
assert(Should_not_be_called);
288+
}
289+
279290
std::string WiresharkField::wiresharkFieldRefName(const WiresharkField* refField) const
280291
{
281292
const auto* genField = &m_genField;

app/commsdsl2wireshark/src/WiresharkField.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@ class WiresharkField
5858
const std::string& wiresharkCustomNameCode(const WiresharkField* refField = nullptr) const;
5959
bool wiresharkHasCustomNameCode(const WiresharkField* refField = nullptr) const;
6060

61+
std::string wiresharkTvbRangeAccess() const;
62+
6163
protected:
6264
virtual std::string wiresharkDissectNameImpl(const WiresharkField* refField) const;
6365
virtual std::string wiresharkDissectCodeImpl(const WiresharkField* refField) const;
6466
virtual std::string wiresharkFieldObjNameImpl(const WiresharkField* refField) const;
6567
virtual std::string wiresharkFieldRegistrationImpl(const WiresharkField* refField) const;
6668
virtual std::string wiresharkMembersDissectCodeImpl() const;
69+
virtual std::string wiresharkTvbRangeAccessImpl() const;
6770

6871
std::string wiresharkFieldRefName(const WiresharkField* refField) const;
6972
bool wiresharkIsBitfieldMember() const;

app/commsdsl2wireshark/src/WiresharkFrame.cpp

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

1616
#include "WiresharkFrame.h"
1717

18+
#include "Wireshark.h"
1819
#include "WiresharkGenerator.h"
1920
#include "WiresharkInterface.h"
2021
#include "WiresharkNamespace.h"
@@ -48,6 +49,7 @@ std::string WiresharkFrame::wiresharkDissectCode() const
4849
{
4950
static const std::string Templ =
5051
"#^#LAYERS#$#\n"
52+
"#^#FUNC_LIST#$#\n"
5153
"#^#PREPEND#$#\n"
5254
"local function #^#NAME#$##^#SUFFIX#$#(tvb, tree)\n"
5355
" #^#REPLACE#$#\n"
@@ -70,6 +72,7 @@ std::string WiresharkFrame::wiresharkDissectCode() const
7072
{"REPLACE", wiresharkGenerator.genReadCodeInjectCode(replaceFileName, "Replace this function body", &replaced)},
7173
{"PREPEND", wiresharkGenerator.genReadCodeInjectCode(prependFileName, "Prepend here")},
7274
{"EXTEND", wiresharkGenerator.genReadCodeInjectCode(extendFileName, "Extend function above", &extended)},
75+
{"FUNC_LIST", wiresharkLayerFuncsListInternal()},
7376
};
7477

7578
if (!replaced) {
@@ -119,8 +122,30 @@ bool WiresharkFrame::genPrepareImpl()
119122

120123
std::string WiresharkFrame::wiresharkDissectBodyInternal() const
121124
{
122-
// TODO:
123-
return std::string();
125+
static const std::string Templ =
126+
"local result = true\n"
127+
"local offset = 0\n"
128+
"local len = tvb:len()\n"
129+
"while result and (offset < len) do\n"
130+
" local frame_subtree = tree:add(#^#PROTO_NAME#$#, tvb(offset, -1), \"#^#FRAME_NAME#$#\")\n"
131+
" local layer_func = #^#LIST_NAME#$#[1]\n"
132+
" local next_offset = 0\n"
133+
" result, next_offset = layer_func(tvb, frame_subtree, offset, len, #^#LIST_NAME#$#, 2, #^#NIL#$#)\n"
134+
" frame_subtree:set_len(next_offset - offset)\n"
135+
" offset = next_offset\n"
136+
"end\n"
137+
"return result, offset\n"
138+
;
139+
140+
auto parseObj = genParseObj();
141+
util::GenReplacementMap repl = {
142+
{"PROTO_NAME", Wireshark::wiresharkProtocolObjName(WiresharkGenerator::wiresharkCast(genGenerator()))},
143+
{"FRAME_NAME", util::genDisplayName(parseObj.parseDisplayName(), parseObj.parseName())},
144+
{"LIST_NAME", wiresharkLayerFuncsListNameInternal()},
145+
{"NIL", strings::genNilStr()},
146+
};
147+
148+
return util::genProcessTemplate(Templ, repl);
124149
}
125150

126151
std::string WiresharkFrame::wiresharkLayersDissectCodeInternal() const
@@ -147,4 +172,31 @@ const WiresharkInterface* WiresharkFrame::wiresharkInterfaceInternal() const
147172
return parentNs->wiresharkInterface();
148173
}
149174

175+
std::string WiresharkFrame::wiresharkLayerFuncsListInternal() const
176+
{
177+
static const std::string Templ =
178+
"local #^#NAME#$# = {\n"
179+
" #^#LAYERS#$#\n"
180+
"}\n"
181+
;
182+
183+
util::GenStringsList layers;
184+
for (auto* l : m_wiresharkLayers) {
185+
layers.push_back(l->wiresharkDissectName());
186+
}
187+
188+
util::GenReplacementMap repl = {
189+
{"NAME", wiresharkLayerFuncsListNameInternal()},
190+
{"LAYERS", util::genStrListToString(layers, ",\n", "")},
191+
};
192+
193+
return util::genProcessTemplate(Templ, repl);
194+
}
195+
196+
std::string WiresharkFrame::wiresharkLayerFuncsListNameInternal() const
197+
{
198+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
199+
return wiresharkGenerator.wiresharkFuncNameFor(*this, "_layers");
200+
}
201+
150202
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkFrame.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class WiresharkFrame final : public commsdsl::gen::GenFrame
5757
std::string wiresharkDissectBodyInternal() const;
5858
std::string wiresharkLayersDissectCodeInternal() const;
5959
const WiresharkInterface* wiresharkInterfaceInternal() const;
60+
std::string wiresharkLayerFuncsListInternal() const;
61+
std::string wiresharkLayerFuncsListNameInternal() const;
6062

6163
WiresharkLayersAccessList m_wiresharkLayers;
6264
bool m_validFrame = false;

app/commsdsl2wireshark/src/WiresharkIdLayer.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@
1515

1616
#include "WiresharkIdLayer.h"
1717

18+
#include "WiresharkFrame.h"
1819
#include "WiresharkGenerator.h"
20+
#include "WiresharkMessage.h"
21+
#include "WiresharkNamespace.h"
22+
23+
#include "commsdsl/gen/strings.h"
24+
#include "commsdsl/gen/util.h"
25+
26+
#include <cassert>
27+
#include <map>
28+
#include <vector>
29+
30+
namespace strings = commsdsl::gen::strings;
31+
namespace util = commsdsl::gen::util;
1932

2033
namespace commsdsl2wireshark
2134
{
@@ -26,4 +39,75 @@ WiresharkIdLayer::WiresharkIdLayer(WiresharkGenerator& generator, ParseLayer par
2639
{
2740
}
2841

42+
std::string WiresharkIdLayer::wiresharkDissectBodyImpl() const
43+
{
44+
static const std::string Templ =
45+
"#^#FIELD#$#\n"
46+
"local id = tvb(offset, next_offset - offset):#^#VALUE#$#\n"
47+
"-- TODO\n"
48+
"offset = next_offset\n"
49+
"#^#NEXT#$#\n"
50+
;
51+
52+
auto* field = wiresharkField();
53+
assert(field != nullptr);
54+
55+
util::GenReplacementMap repl = {
56+
{"FIELD", wiresharkDissectFieldCode()},
57+
{"VALUE", field->wiresharkTvbRangeAccess()},
58+
{"NEXT", wiresharkNextFuncCode()},
59+
};
60+
61+
return util::genProcessTemplate(Templ, repl);
62+
}
63+
64+
std::string WiresharkIdLayer::wiresharkExtraDissectCodeImpl() const
65+
{
66+
auto* parentFrame = genParentFrame();
67+
assert(parentFrame != nullptr);
68+
auto parentNs = parentFrame->genParentNamespace();
69+
assert(parentNs != nullptr);
70+
auto messages = parentNs->genGetAllMessagesIdSorted();
71+
72+
using DissectMap = std::map<std::uintmax_t /*id */, util::GenStringsList /*dissect_funcs*/>;
73+
74+
DissectMap map;
75+
for (auto* m : messages) {
76+
auto& wiresharkMsg = WiresharkMessage::wiresharkCast(*m);
77+
map[m->genParseObj().parseId()].push_back(wiresharkMsg.wiresharkDissectName());
78+
}
79+
80+
util::GenStringsList elems;
81+
for (auto& info : map) {
82+
static const std::string ElemTempl =
83+
"[#^#ID#$#] = {#^#LIST#$#}"
84+
;
85+
86+
util::GenReplacementMap elemRepl = {
87+
{"ID", std::to_string(info.first)},
88+
{"LIST", util::genStrListToString(info.second, ", ", "")},
89+
};
90+
91+
elems.push_back(util::genProcessTemplate(ElemTempl, elemRepl));
92+
}
93+
94+
static const std::string Templ =
95+
"local #^#NAME#$# = {\n"
96+
" #^#ELEMS#$#\n"
97+
"}\n"
98+
;
99+
100+
util::GenReplacementMap repl = {
101+
{"NAME", wiresharkMsgMapNameInternal()},
102+
{"ELEMS", util::genStrListToString(elems, ",\n", "")},
103+
};
104+
105+
return util::genProcessTemplate(Templ, repl);
106+
}
107+
108+
std::string WiresharkIdLayer::wiresharkMsgMapNameInternal() const
109+
{
110+
return WiresharkGenerator::wiresharkCast(genGenerator()).wiresharkFuncNameFor(*this, "_msg");
111+
}
112+
29113
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkIdLayer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include "commsdsl/gen/GenIdLayer.h"
2121

22+
#include <string>
23+
2224
namespace commsdsl2wireshark
2325
{
2426

@@ -33,6 +35,13 @@ class WiresharkIdLayer final : public commsdsl::gen::GenIdLayer, public Wireshar
3335
using GenElem = commsdsl::gen::GenElem;
3436

3537
WiresharkIdLayer(WiresharkGenerator& generator, ParseLayer parseObj, GenElem* parent);
38+
39+
protected:
40+
virtual std::string wiresharkDissectBodyImpl() const override;
41+
virtual std::string wiresharkExtraDissectCodeImpl() const override;
42+
43+
private:
44+
std::string wiresharkMsgMapNameInternal() const;
3645
};
3746

3847
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkIntField.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ const std::string& WiresharkIntField::wiresharkIntegralType(ParseIntField::Parse
9898
return iter->second;
9999
}
100100

101+
std::string WiresharkIntField::wiresharkTvbRangeAccessIntegralValue(ParseIntField::ParseType type, ParseEndian endian, std::size_t len)
102+
{
103+
std::string prefix;
104+
if (endian == ParseEndian::ParseEndian_Little) {
105+
prefix = "le_";
106+
}
107+
108+
if (GenIntField::genIsUnsignedType(type)) {
109+
prefix += 'u';
110+
}
111+
112+
if (len <= 4) {
113+
return prefix + "int()";
114+
}
115+
116+
return prefix + "int64()";
117+
}
118+
101119
bool WiresharkIntField::genPrepareImpl()
102120
{
103121
if ((!GenBase::genPrepareImpl()) ||
@@ -142,6 +160,12 @@ std::string WiresharkIntField::wiresharkFieldRegistrationImpl(const WiresharkFie
142160
return util::genProcessTemplate(Templ, repl);
143161
}
144162

163+
std::string WiresharkIntField::wiresharkTvbRangeAccessImpl() const
164+
{
165+
auto obj = genIntFieldParseObj();
166+
return wiresharkTvbRangeAccessIntegralValue(obj.parseType(), obj.parseEndian(), obj.parseMaxLength());
167+
}
168+
145169
std::string WiresharkIntField::wiresharkSpecialsInternal(const WiresharkField* refField) const
146170
{
147171
auto& specials = genSpecialsSortedByValue();

0 commit comments

Comments
 (0)