@@ -34,15 +34,22 @@ namespace strings = commsdsl::gen::strings;
3434namespace commsdsl2wireshark
3535{
3636
37+ namespace
38+ {
39+
40+ const std::string PlugSuffix (" _plug" );
41+
42+ } // namespace
43+
3744bool 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
4855const std::string& Wireshark::wiresharkProtocolObjName (const WiresharkGenerator& generator)
@@ -124,6 +131,13 @@ std::string Wireshark::wiresharkCreateCrcFuncName(const WiresharkGenerator& gene
124131}
125132
126133bool 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+
191320std::string Wireshark::wiresharkProtocolDefInternal () const
192321{
193322 const std::string Templ =
0 commit comments