Skip to content

Commit b68cc00

Browse files
committed
Generating proper plugin file by commsdsl2wireshark.
1 parent b4d49fd commit b68cc00

8 files changed

Lines changed: 186 additions & 31 deletions

File tree

app/commsdsl2wireshark/src/Wireshark.cpp

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,22 @@ namespace strings = commsdsl::gen::strings;
3434
namespace commsdsl2wireshark
3535
{
3636

37+
namespace
38+
{
39+
40+
const std::string PlugSuffix("_plug");
41+
42+
} // namespace
43+
3744
bool Wireshark::wiresharkWrite(const WiresharkGenerator& generator)
3845
{
3946
Wireshark obj(generator);
4047
return obj.wiresharkWriteInternal();
4148
}
4249

43-
std::string Wireshark::wiresharkFileName(const WiresharkGenerator& generator)
50+
std::string Wireshark::wiresharkFileName(const WiresharkGenerator& generator, const std::string& suffix)
4451
{
45-
return generator.genProtocolSchema().genMainNamespace() + ".lua";
52+
return generator.genProtocolSchema().genMainNamespace() + suffix + ".lua";
4653
}
4754

4855
const std::string& Wireshark::wiresharkProtocolObjName(const WiresharkGenerator& generator)
@@ -124,6 +131,13 @@ std::string Wireshark::wiresharkCreateCrcFuncName(const WiresharkGenerator& gene
124131
}
125132

126133
bool Wireshark::wiresharkWriteInternal() const
134+
{
135+
return
136+
wiresharkWriteMainInternal() &&
137+
wiresharkWriteTcpInternal();
138+
}
139+
140+
bool Wireshark::wiresharkWriteMainInternal() const
127141
{
128142
auto fileName = wiresharkFileName(m_wiresharkGenerator);
129143
auto filePath = util::genPathAddElem(m_wiresharkGenerator.genGetOutputDir(), fileName);
@@ -188,6 +202,121 @@ bool Wireshark::wiresharkWriteInternal() const
188202
return true;
189203
}
190204

205+
bool Wireshark::wiresharkWriteTcpInternal() const
206+
{
207+
auto fileName = wiresharkFileName(m_wiresharkGenerator, PlugSuffix);
208+
auto filePath = util::genPathAddElem(m_wiresharkGenerator.genGetOutputDir(), fileName);
209+
210+
m_wiresharkGenerator.genLogger().genInfo("Generating " + filePath);
211+
std::ofstream stream(filePath);
212+
if (!stream) {
213+
m_wiresharkGenerator.genLogger().genError("Failed to open \"" + filePath + "\" for writing.");
214+
return false;
215+
}
216+
217+
do {
218+
const std::string Templ =
219+
"#^#GEN_COMMENT#$#\n"
220+
"\n"
221+
"-- Ensure we can load from the current directory\n"
222+
"local info = debug.getinfo(1, \"S\")\n"
223+
"local script_path = info.source:match(\"@?(.*/)\") or \"./\"\n"
224+
"package.path = script_path .. \"?.lua;\" .. package.path\n"
225+
"\n"
226+
"local success, result = pcall(require, \"#^#NAME#$#\")\n"
227+
"if not success then\n"
228+
" -- If 'success' is false, 'result' contains the error message\n"
229+
" io.stderr:write(\"FATAL ERROR loading '\" .. \"#^#NAME#$#\" .. \"':\\n\" .. tostring(result) .. \"\\n\")\n"
230+
" os.exit(1)\n"
231+
"end\n"
232+
"\n"
233+
"-- If we get here, require succeeded!\n"
234+
"-- 'result' now contains the protocol object\n"
235+
"local #^#NAME#$#_transport_type = {\n"
236+
" TCP = 0,\n"
237+
" UDP = 1,\n"
238+
" BOTH = 2\n"
239+
"}\n"
240+
"\n"
241+
"local #^#NAME#$#_transport_types_map = {\n"
242+
" {#^#NAME#$#_transport_type.TCP, \"TCP Only\", #^#NAME#$#_transport_type.TCP},\n"
243+
" {#^#NAME#$#_transport_type.UDP, \"UDP Only\", #^#NAME#$#_transport_type.UDP},\n"
244+
" {#^#NAME#$#_transport_type.BOTH, \"Both (TCP & UDP)\", #^#NAME#$#_transport_type.BOTH}\n"
245+
"}\n"
246+
"\n"
247+
"local #^#NAME#$#_port = #^#PORT#$#\n"
248+
"local #^#NAME#$#_transport = #^#NAME#$#_transport_type.BOTH\n"
249+
"\n"
250+
"result.prefs.port = Pref.uint(\"Default Port\", #^#NAME#$#_port)\n"
251+
"result.prefs.transport = Pref.enum(\"Transport Protocol\", #^#NAME#$#_transport, \"The transport protocol to use\", #^#NAME#$#_transport_types_map)\n"
252+
"\n"
253+
"local #^#NAME#$#_tcp_port = DissectorTable.get(\"tcp.port\")\n"
254+
"local #^#NAME#$#_udp_port = DissectorTable.get(\"udp.port\")\n"
255+
"\n"
256+
"local function #^#NAME#$#_add_port_bindings()\n"
257+
" if #^#NAME#$#_port == 0 then\n"
258+
" return\n"
259+
" end\n"
260+
"\n"
261+
" if (#^#NAME#$#_transport == #^#NAME#$#_transport_type.TCP) or (#^#NAME#$#_transport == #^#NAME#$#_transport_type.BOTH) then\n"
262+
" #^#NAME#$#_tcp_port:add(#^#NAME#$#_port, result)\n"
263+
" end\n"
264+
" if (#^#NAME#$#_transport == #^#NAME#$#_transport_type.UDP) or (#^#NAME#$#_transport == #^#NAME#$#_transport_type.BOTH) then\n"
265+
" #^#NAME#$#_udp_port:add(#^#NAME#$#_port, result)\n"
266+
" end\n"
267+
"end\n"
268+
"\n"
269+
"local function #^#NAME#$#_remove_port_bindings()\n"
270+
" if #^#NAME#$#_port == 0 then\n"
271+
" return\n"
272+
" end\n"
273+
"\n"
274+
" if (#^#NAME#$#_transport == #^#NAME#$#_transport_type.TCP) or (#^#NAME#$#_transport == #^#NAME#$#_transport_type.BOTH) then\n"
275+
" #^#NAME#$#_tcp_port:remove(#^#NAME#$#_port, result)\n"
276+
" end\n"
277+
" if (#^#NAME#$#_transport == #^#NAME#$#_transport_type.UDP) or (#^#NAME#$#_transport == #^#NAME#$#_transport_type.BOTH) then\n"
278+
" #^#NAME#$#_udp_port:remove(#^#NAME#$#_port, result)\n"
279+
" end\n"
280+
"end\n"
281+
"\n"
282+
"local function #^#NAME#$#_assign_prefs()\n"
283+
" #^#NAME#$#_port = result.prefs.port\n"
284+
" #^#NAME#$#_transport = result.prefs.transport\n"
285+
"end\n"
286+
"\n"
287+
"function result.prefs_changed()\n"
288+
" if #^#NAME#$#_port == result.prefs.port and #^#NAME#$#_transport == result.prefs.transport then\n"
289+
" return\n"
290+
" end\n"
291+
"\n"
292+
" #^#NAME#$#_remove_port_bindings()\n"
293+
" #^#NAME#$#_assign_prefs()\n"
294+
" #^#NAME#$#_add_port_bindings()\n"
295+
"end\n"
296+
"\n"
297+
"#^#NAME#$#_assign_prefs()\n"
298+
"#^#NAME#$#_add_port_bindings()\n"
299+
;
300+
301+
util::GenReplacementMap repl = {
302+
{"GEN_COMMENT", m_wiresharkGenerator.wiresharkFileGeneratedComment()},
303+
{"NAME", wiresharkProtocolObjName(m_wiresharkGenerator)},
304+
{"PORT", std::to_string(m_wiresharkGenerator.wiresharkDefaultPort())},
305+
};
306+
307+
auto str = commsdsl::gen::util::genProcessTemplate(Templ, repl, true);
308+
stream << str;
309+
} while (false);
310+
311+
stream.flush();
312+
if (!stream.good()) {
313+
m_wiresharkGenerator.genLogger().genError("Failed to write \"" + filePath + "\".");
314+
return false;
315+
}
316+
317+
return true;
318+
}
319+
191320
std::string Wireshark::wiresharkProtocolDefInternal() const
192321
{
193322
const std::string Templ =

app/commsdsl2wireshark/src/Wireshark.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Wireshark
4444
};
4545

4646
static bool wiresharkWrite(const WiresharkGenerator& generator);
47-
static std::string wiresharkFileName(const WiresharkGenerator& generator);
47+
static std::string wiresharkFileName(const WiresharkGenerator& generator, const std::string& suffix = std::string());
4848
static const std::string& wiresharkProtocolObjName(const WiresharkGenerator& generator);
4949
static std::string wiresharkCreateFieldFuncName(const WiresharkGenerator& generator);
5050
static std::string wiresharkCreateExtractorFuncName(const WiresharkGenerator& generator);
@@ -66,6 +66,8 @@ class Wireshark
6666

6767
private:
6868
bool wiresharkWriteInternal() const;
69+
bool wiresharkWriteMainInternal() const;
70+
bool wiresharkWriteTcpInternal() const;
6971
std::string wiresharkProtocolDefInternal() const;
7072
std::string wiresharkLocalInternal() const;
7173
std::string wiresharkDissectFuncInternal() const;

app/commsdsl2wireshark/src/WiresharkGenerator.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "WiresharkNamespace.h"
3333
#include "WiresharkOptionalField.h"
3434
#include "WiresharkPayloadLayer.h"
35+
#include "WiresharkProgramOptions.h"
3536
#include "WiresharkRefField.h"
3637
#include "WiresharkSchema.h"
3738
#include "WiresharkSetField.h"
@@ -244,6 +245,23 @@ std::string WiresharkGenerator::wiresharkInputAbsPathFor(const GenElem& elem, co
244245
return genGetCodeDir() + '/' + wiresharkInputRelPathFor(elem, suffix);
245246
}
246247

248+
unsigned WiresharkGenerator::wiresharkDefaultPort() const
249+
{
250+
return m_defaultPort;
251+
}
252+
253+
WiresharkGenerator::OptsProcessResult WiresharkGenerator::genProcessOptionsImpl(const GenProgramOptions& options)
254+
{
255+
auto& opts = WiresharkProgramOptions::wiresharkCast(options);
256+
m_defaultPort = opts.wiresharkDefaultPort();
257+
if (m_defaultPort == 0) {
258+
genLogger().genError("Wrong default port value");
259+
return OptsProcessResult_Failure;
260+
}
261+
262+
return OptsProcessResult_Continue;
263+
}
264+
247265
const std::string& WiresharkGenerator::genCommentPrefixImpl() const
248266
{
249267
static const std::string Str("-- ");

app/commsdsl2wireshark/src/WiresharkGenerator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class WiresharkGenerator final : public commsdsl::gen::GenGenerator
6868
std::string wiresharkInputRelPathFor(const GenElem& elem, const std::string& suffix) const;
6969
std::string wiresharkInputAbsPathFor(const GenElem& elem, const std::string& suffix) const;
7070

71+
unsigned wiresharkDefaultPort() const;
72+
7173
protected:
7274
virtual GenSchemaPtr genCreateSchemaImpl(ParseSchema parseObj, GenElem* parent) override;
7375
virtual GenNamespacePtr genCreateNamespaceImpl(ParseNamespace parseObj, GenElem* parent) override;
@@ -97,10 +99,13 @@ class WiresharkGenerator final : public commsdsl::gen::GenGenerator
9799
virtual GenFieldPtr genCreateVariantFieldImpl(ParseField parseObj, GenElem* parent) override;
98100

99101
virtual bool genWriteImpl() override;
102+
virtual OptsProcessResult genProcessOptionsImpl(const GenProgramOptions& options) override;
100103
virtual const std::string& genCommentPrefixImpl() const override;
101104

102105
private:
103106
bool wiresharkWriteExtraFilesInternal() const;
107+
108+
unsigned m_defaultPort = 0U;
104109
};
105110

106111
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkProgramOptions.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,39 @@
1515

1616
#include "WiresharkProgramOptions.h"
1717

18+
#include "commsdsl/gen/util.h"
19+
20+
namespace util = commsdsl::gen::util;
21+
1822
namespace commsdsl2wireshark
1923
{
2024

25+
namespace
26+
{
27+
28+
const std::string WiresharkDefaultPortStr("default-port");
29+
const unsigned WiresharkDefaultPort = 12345;
30+
31+
} // namespace
32+
2133
WiresharkProgramOptions::WiresharkProgramOptions()
2234
{
2335
genAddCommonOptions();
2436
genAddMessagesSelectionOptions();
25-
genAddInterfaceSelectionOptions();
37+
genAddInterfaceSelectionOptions()
38+
(WiresharkDefaultPortStr,
39+
"Default network port. Defaults to " + std::to_string(WiresharkDefaultPort) + '.',
40+
true)
41+
;
42+
}
43+
44+
unsigned WiresharkProgramOptions::wiresharkDefaultPort() const
45+
{
46+
if (!genIsOptUsed(WiresharkDefaultPortStr)) {
47+
return WiresharkDefaultPort;
48+
}
49+
50+
return util::genStrToUnsigned(genValue(WiresharkDefaultPortStr));
2651
}
2752

2853
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkProgramOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class WiresharkProgramOptions : public commsdsl::gen::GenProgramOptions
3131
{
3232
return static_cast<const WiresharkProgramOptions&>(obj);
3333
}
34+
35+
unsigned wiresharkDefaultPort() const;
3436
};
3537

3638
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/test/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ foreach (name ${tests})
3939
add_custom_target(${output_tgt} ALL
4040
COMMAND ${CMAKE_COMMAND} -E remove_directory ${output_dir}.tmp
4141
COMMAND $<TARGET_FILE:${APP_NAME}> -s -d --warn-as-err --code-inject-comments ${code_input_param} ${extra_params} -o ${output_dir}.tmp ${schema_files}
42-
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/common_test.lua ${output_dir}.tmp/
4342
COMMAND ${CMAKE_COMMAND}
4443
-DGENERATED="${output_dir}.tmp" -DOUTPUT="${output_dir}" -DCLEANUP_DIRS="."
4544
-P "${PROJECT_SOURCE_DIR}/cmake/CopyGenerated.cmake"
@@ -87,7 +86,7 @@ foreach (name ${tests})
8786

8887
add_test(
8988
NAME ${APP_NAME}.${name}
90-
COMMAND ${TSHARK_EXECUTABLE} -r ${pcap_output} -X lua_script:common_test.lua -O ${prot_name}
89+
COMMAND ${TSHARK_EXECUTABLE} -r ${pcap_output} -X lua_script:${prot_name}_plug.lua -O ${prot_name}
9190
WORKING_DIRECTORY ${output_dir}
9291
)
9392
endforeach ()

app/commsdsl2wireshark/test/common_test.lua

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)