2424
2525#include < cassert>
2626#include < fstream>
27+ #include < type_traits>
2728
2829namespace util = commsdsl::gen::util;
2930namespace strings = commsdsl::gen::strings;
3031
3132namespace commsdsl2wireshark
3233{
3334
34- bool Wireshark::wiresharkWrite (WiresharkGenerator& generator)
35+ bool Wireshark::wiresharkWrite (const WiresharkGenerator& generator)
3536{
3637 Wireshark obj (generator);
3738 return obj.wiresharkWriteInternal ();
@@ -57,6 +58,12 @@ std::string Wireshark::wiresharkFieldsListName(const WiresharkGenerator& generat
5758 return wiresharkProtocolObjName (generator) + " _fields_list" ;
5859}
5960
61+ std::string Wireshark::wiresharkStatusCodeStr (const WiresharkGenerator& generator, StatusCode code)
62+ {
63+ Wireshark obj (generator);
64+ return obj.wiresharkStatusCodeNameInternal () + " ." + wiresharkStatusCodeStrInternal (code);
65+ }
66+
6067bool Wireshark::wiresharkWriteInternal () const
6168{
6269 auto fileName = wiresharkFileName (m_wiresharkGenerator);
@@ -73,6 +80,7 @@ bool Wireshark::wiresharkWriteInternal() const
7380 const std::string Templ =
7481 " #^#GEN_COMMENT#$#\n "
7582 " #^#PROTOCOL#$#\n "
83+ " #^#STATUS_CODE#$#\n "
7684 " #^#FIELDS_REG#$#\n "
7785 " #^#CODE#$#\n "
7886 " #^#DISSECT_FUNC#$#\n "
@@ -89,6 +97,7 @@ bool Wireshark::wiresharkWriteInternal() const
8997 {" NAME" , wiresharkProtocolObjName (m_wiresharkGenerator)},
9098 {" FIELDS_LIST" , wiresharkFieldsListName (m_wiresharkGenerator)},
9199 {" CODE" , wiresharkCodeInternal ()},
100+ {" STATUS_CODE" , wiresharkStatusCodeDefInternal ()},
92101 };
93102
94103 auto str = commsdsl::gen::util::genProcessTemplate (Templ, repl, true );
@@ -209,13 +218,14 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
209218 " \n "
210219 " pinfo.cols.protocol = #^#NAME#$#.name\n "
211220 " \n "
212- " local result = false \n "
221+ " local result = #^#SUCCESS#$# \n "
213222 " local next_offset = 0\n "
214223 " repeat\n "
215224 " #^#FRAMES#$#\n "
216225 " until true\n "
217226 " \n "
218- " if not result then\n "
227+ " if (result ~= #^#NOT_ENOUGH_DATA#$#) and (result ~= #^#SUCCESS#$#) then\n "
228+ " -- Consume everything\n "
219229 " return\n "
220230 " end\n "
221231 " \n "
@@ -227,11 +237,58 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
227237
228238 util:: GenReplacementMap repl = {
229239 {" NAME" , wiresharkProtocolObjName (m_wiresharkGenerator)},
230- {" FRAMES" , util::genStrListToString (elems, " \n " , " " )}
240+ {" FRAMES" , util::genStrListToString (elems, " \n " , " " )},
241+ {" SUCCESS" , wiresharkStatusCodeStr (m_wiresharkGenerator, StatusCode::Success)},
242+ {" NOT_ENOUGH_DATA" , wiresharkStatusCodeStr (m_wiresharkGenerator, StatusCode::NotEnoughData)},
231243 };
232244
233245 return util::genProcessTemplate (Templ, repl);
234246}
235247
248+ std::string Wireshark::wiresharkStatusCodeNameInternal () const
249+ {
250+ return wiresharkProtocolObjName (m_wiresharkGenerator) + " _StatusCode" ;
251+ }
252+
253+ std::string Wireshark::wiresharkStatusCodeDefInternal () const
254+ {
255+ util::GenStringsList vals;
256+
257+ for (auto idx = 0U ; idx < static_cast <unsigned >(StatusCode::ValuesLimit); ++idx) {
258+ vals.push_back (wiresharkStatusCodeStrInternal (static_cast <StatusCode>(idx)) + " = " + std::to_string (idx));
259+ }
260+
261+ const std::string Templ =
262+ " local #^#NAME#$# = {\n "
263+ " #^#VALS#$#\n "
264+ " }\n "
265+ ;
266+
267+ util::GenReplacementMap repl = {
268+ {" NAME" , wiresharkStatusCodeNameInternal ()},
269+ {" VALS" , util::genStrListToString (vals, " ,\n " , " " )},
270+ };
271+
272+ return util::genProcessTemplate (Templ, repl);
273+ }
274+
275+ const std::string& Wireshark::wiresharkStatusCodeStrInternal (StatusCode code)
276+ {
277+ static const std::string Map[] = {
278+ /* Success */ " SUCCESS" ,
279+ /* NotEnoughData */ " NOT_ENOUGH_DATA" ,
280+ /* MalformedData */ " MALFORMED_PACKET" ,
281+ /* InvalidMsgId */ " INVALID_MSG_ID" ,
282+ /* InvalidMsgData */ " INVALID_MSG_DATA" ,
283+ /* CodegenError */ " CODEGEN_ERROR" ,
284+ };
285+ static const std::size_t MapSize = std::extent_v<decltype (Map)>;
286+ static_assert (MapSize == static_cast <unsigned >(StatusCode::ValuesLimit));
287+
288+ auto idx = static_cast <unsigned >(code);
289+ assert (idx < MapSize);
290+ return Map[idx];
291+ }
292+
236293} // namespace commsdsl2wireshark
237294
0 commit comments