Skip to content

Commit baa6e97

Browse files
committed
Saving work on commsdsl2wireshark dissector.
1 parent 6011aba commit baa6e97

8 files changed

Lines changed: 109 additions & 11 deletions

File tree

app/commsdsl2wireshark/src/Wireshark.cpp

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

1616
#include "Wireshark.h"
1717

18+
#include "WiresharkFrame.h"
1819
#include "WiresharkGenerator.h"
1920
#include "WiresharkSchema.h"
2021

@@ -123,13 +124,6 @@ std::string Wireshark::wiresharkDissectFuncInternal() const
123124
const std::string Templ =
124125
"-- Main Dissector Entry Point\n"
125126
"function #^#NAME#$#.dissector(tvb, pinfo, tree)\n"
126-
" #^#REPLACE#$#\n"
127-
" if tvb:len() == 0 then\n"
128-
" return\n"
129-
" end\n"
130-
"\n"
131-
" pinfo.cols.protocol = #^#NAME#$#.name\n"
132-
"\n"
133127
" #^#BODY#$#\n"
134128
"end\n"
135129
;
@@ -140,11 +134,11 @@ std::string Wireshark::wiresharkDissectFuncInternal() const
140134

141135
util::GenReplacementMap repl = {
142136
{"NAME", wiresharkProtocolObjName(m_wiresharkGenerator)},
143-
{"REPLACE", std::move(replaceCode)},
137+
{"BODY", std::move(replaceCode)},
144138
};
145139

146140
if (!bodyReplaced) {
147-
// TODO: Add frame function
141+
repl["BODY"] = wiresharkDissectFuncBodyInternal();
148142
}
149143

150144
return util::genProcessTemplate(Templ, repl);
@@ -186,5 +180,56 @@ std::string Wireshark::wiresharkCodeInternal() const
186180
return util::genStrListToString(elems, "\n", "");
187181
}
188182

183+
std::string Wireshark::wiresharkDissectFuncBodyInternal() const
184+
{
185+
util::GenStringsList elems;
186+
auto frames = m_wiresharkGenerator.genProtocolSchema().genGetAllFrames();
187+
for (auto* f : frames) {
188+
assert(f != nullptr);
189+
auto& wiresharkFrame = WiresharkFrame::wiresharkCast(*f);
190+
static const std::string FrameTempl =
191+
"result, next_offset = #^#NAME#$#(tvb, tree)\n"
192+
"if result then\n"
193+
" break\n"
194+
"end\n"
195+
;
196+
197+
util::GenReplacementMap frameRepl = {
198+
{"NAME", wiresharkFrame.wiresharkDissectName()}
199+
};
200+
201+
elems.push_back(util::genProcessTemplate(FrameTempl, frameRepl));
202+
}
203+
204+
const std::string Templ =
205+
"if tvb:len() == 0 then\n"
206+
" return\n"
207+
"end\n"
208+
"\n"
209+
"pinfo.cols.protocol = #^#NAME#$#.name\n"
210+
"\n"
211+
"local result = false\n"
212+
"local next_offset = 0\n"
213+
"repeat\n"
214+
" #^#FRAMES#$#\n"
215+
"until true\n"
216+
"\n"
217+
"if not result then\n"
218+
" return\n"
219+
"end\n"
220+
"\n"
221+
"if next_offset < tvb:len() then\n"
222+
" pinfo.desegment_offset = next_offset\n"
223+
" pinfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT\n"
224+
"end\n"
225+
;
226+
227+
util:: GenReplacementMap repl = {
228+
{"FRAMES", util::genStrListToString(elems, "\n", "")}
229+
};
230+
231+
return util::genProcessTemplate(Templ, repl);
232+
}
233+
189234
} // namespace commsdsl2wireshark
190235

app/commsdsl2wireshark/src/Wireshark.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Wireshark
3939
std::string wiresharkDissectFuncInternal() const;
4040
std::string wiresharkFieldsRegistrationInternal() const;
4141
std::string wiresharkCodeInternal() const;
42+
std::string wiresharkDissectFuncBodyInternal() const;
4243

4344
WiresharkGenerator& m_wiresharkGenerator;
4445
};

app/commsdsl2wireshark/src/WiresharkFrame.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616
#include "WiresharkFrame.h"
1717

1818
#include "WiresharkGenerator.h"
19+
#include "WiresharkInterface.h"
20+
#include "WiresharkNamespace.h"
1921

2022
#include "commsdsl/gen/strings.h"
2123
#include "commsdsl/gen/util.h"
2224

25+
#include <algorithm>
26+
#include <cassert>
27+
2328
namespace strings = commsdsl::gen::strings;
2429
namespace util = commsdsl::gen::util;
2530

@@ -94,8 +99,21 @@ bool WiresharkFrame::genPrepareImpl()
9499
m_wiresharkLayers.push_back(WiresharkLayer::wiresharkCast(l));
95100
}
96101

97-
// TODO: validate frame
98-
m_validFrame = true;
102+
auto* iFace = wiresharkInterfaceInternal();
103+
do {
104+
if (iFace == nullptr) {
105+
genGenerator().genLogger().genDebug("No valid interface for frame " + genName());
106+
break;
107+
}
108+
109+
m_validFrame =
110+
std::all_of(
111+
m_wiresharkLayers.begin(), m_wiresharkLayers.end(),
112+
[iFace](auto* l)
113+
{
114+
return l->wiresharkIsInterfaceSupported(*iFace);
115+
});
116+
} while (false);
99117
return true;
100118
}
101119

@@ -120,4 +138,13 @@ std::string WiresharkFrame::wiresharkLayersDissectCodeInternal() const
120138
return util::genStrListToString(layers, "\n", "\n");
121139
}
122140

141+
const WiresharkInterface* WiresharkFrame::wiresharkInterfaceInternal() const
142+
{
143+
auto* parent = genGetParent();
144+
assert(parent != nullptr);
145+
assert(parent->genElemType() == commsdsl::gen::GenElem::GenType_Namespace);
146+
auto* parentNs = WiresharkNamespace::wiresharkCast(static_cast<const commsdsl::gen::GenNamespace*>(parent));
147+
return parentNs->wiresharkInterface();
148+
}
149+
123150
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkFrame.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace commsdsl2wireshark
2323
{
2424

2525
class WiresharkGenerator;
26+
class WiresharkInterface;
2627
class WiresharkFrame final : public commsdsl::gen::GenFrame
2728
{
2829
using GenBase = commsdsl::gen::GenFrame;
@@ -55,6 +56,7 @@ class WiresharkFrame final : public commsdsl::gen::GenFrame
5556
private:
5657
std::string wiresharkDissectBodyInternal() const;
5758
std::string wiresharkLayersDissectCodeInternal() const;
59+
const WiresharkInterface* wiresharkInterfaceInternal() const;
5860

5961
WiresharkLayersAccessList m_wiresharkLayers;
6062
bool m_validFrame = false;

app/commsdsl2wireshark/src/WiresharkLayer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ std::string WiresharkLayer::wiresharkDissectCode() const
101101
return util::genProcessTemplate(Templ, repl);
102102
}
103103

104+
bool WiresharkLayer::wiresharkIsInterfaceSupported(const WiresharkInterface& iFace) const
105+
{
106+
return wiresharkIsInterfaceSupportedImpl(iFace);
107+
}
108+
104109
std::string WiresharkLayer::wiresharkDissectBodyImpl() const
105110
{
106111
auto* field = WiresharkField::wiresharkCast(m_genLayer.genMemberField());
@@ -123,6 +128,11 @@ std::string WiresharkLayer::wiresharkDissectBodyImpl() const
123128
return util::genProcessTemplate(Templ, repl);
124129
}
125130

131+
bool WiresharkLayer::wiresharkIsInterfaceSupportedImpl([[maybe_unused]] const WiresharkInterface& iFace) const
132+
{
133+
return true;
134+
}
135+
126136
std::string WiresharkLayer::wiresharkFieldDissectCodeInternal() const
127137
{
128138
auto* field = WiresharkField::wiresharkCast(m_genLayer.genMemberField());

app/commsdsl2wireshark/src/WiresharkLayer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace commsdsl2wireshark
2323
{
2424

25+
class WiresharkInterface;
2526
class WiresharkLayer
2627
{
2728
public:
@@ -46,8 +47,11 @@ class WiresharkLayer
4647
std::string wiresharkDissectName() const;
4748
std::string wiresharkDissectCode() const;
4849

50+
bool wiresharkIsInterfaceSupported(const WiresharkInterface& iFace) const;
51+
4952
protected:
5053
virtual std::string wiresharkDissectBodyImpl() const;
54+
virtual bool wiresharkIsInterfaceSupportedImpl(const WiresharkInterface& iFace) const;
5155

5256
private:
5357
std::string wiresharkFieldDissectCodeInternal() const;

app/commsdsl2wireshark/src/WiresharkValueLayer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "WiresharkValueLayer.h"
1717

1818
#include "WiresharkGenerator.h"
19+
#include "WiresharkInterface.h"
1920

2021
namespace commsdsl2wireshark
2122
{
@@ -26,4 +27,9 @@ WiresharkValueLayer::WiresharkValueLayer(WiresharkGenerator& generator, ParseLay
2627
{
2728
}
2829

30+
bool WiresharkValueLayer::wiresharkIsInterfaceSupportedImpl(const WiresharkInterface& iFace) const
31+
{
32+
return genIsInterfaceSupported(&iFace);
33+
}
34+
2935
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkValueLayer.h

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

3535
WiresharkValueLayer(WiresharkGenerator& generator, ParseLayer parseObj, GenElem* parent);
36+
37+
protected:
38+
virtual bool wiresharkIsInterfaceSupportedImpl(const WiresharkInterface& iFace) const override;
3639
};
3740

3841
} // namespace commsdsl2wireshark

0 commit comments

Comments
 (0)