@@ -149,6 +149,44 @@ Config_Update_Conntrack_Client_Alert_Delay(const char *name, RecDataT dtype, Rec
149149 return Config_Update_Conntrack_Server_Alert_Delay_Helper (name, dtype, data, cookie, config->client_alert_delay );
150150}
151151
152+ // // helper function to build up a json string from the passed conn groups.
153+ std::string
154+ Groups_To_JSON (std::vector<std::shared_ptr<ConnectionTracker::Group const >> const &groups)
155+ {
156+ std::string text;
157+ size_t extent = 0 ;
158+ static const swoc::bwf::Format header_fmt{R"( {{"count": {}, "list": [ )" };
159+ static const swoc::bwf::Format item_fmt{
160+ R"( {{"type": "{}", "ip": "{}", "fqdn": "{}", "current": {}, "max": {}, "blocked": {}, "alert": {}}},
161+ )" };
162+ static const std::string_view trailer{" \n ]}" };
163+
164+ static const auto printer = [](swoc::BufferWriter &w, ConnectionTracker::Group const *g) -> swoc::BufferWriter & {
165+ w.print (item_fmt, g->_match_type , g->_addr , g->_fqdn , g->_count .load (), g->_count_max .load (), g->_blocked .load (),
166+ g->get_last_alert_epoch_time ());
167+ return w;
168+ };
169+
170+ swoc::FixedBufferWriter null_bw{nullptr }; // Empty buffer for sizing work.
171+
172+ null_bw.print (header_fmt, groups.size ()).extent ();
173+ for (auto g : groups) {
174+ printer (null_bw, g.get ());
175+ }
176+ extent = null_bw.extent () + trailer.size () - 2 ; // 2 for the trailing comma newline that will get clipped.
177+
178+ text.resize (extent);
179+ swoc::FixedBufferWriter w (const_cast <char *>(text.data ()), text.size ());
180+ w.restrict (trailer.size ());
181+ w.print (header_fmt, groups.size ());
182+ for (auto g : groups) {
183+ printer (w, g.get ());
184+ }
185+ w.restore (trailer.size ());
186+ w.write (trailer);
187+ return text;
188+ }
189+
152190} // namespace
153191
154192void
@@ -318,56 +356,71 @@ ConnectionTracker::get_outbound_groups(std::vector<std::shared_ptr<Group const>>
318356 }
319357}
320358
359+ void
360+ ConnectionTracker::get_inbound_groups (std::vector<std::shared_ptr<Group const >> &groups)
361+ {
362+ std::lock_guard<std::mutex> lock (_inbound_table._mutex ); // TABLE LOCK
363+ groups.resize (0 );
364+ groups.reserve (_inbound_table._table .size ());
365+ for (auto &&[key, group] : _inbound_table._table ) {
366+ groups.push_back (group);
367+ }
368+ }
369+
321370std::string
322- ConnectionTracker::outbound_to_json_string ()
371+ ConnectionTracker::inbound_to_json_string ()
323372{
324- std::string text;
325- size_t extent = 0 ;
326- static const swoc::bwf::Format header_fmt{R"( {{"count": {}, "list": [
327- )" };
328- static const swoc::bwf::Format item_fmt{
329- R"( {{"type": "{}", "ip": "{}", "fqdn": "{}", "current": {}, "max": {}, "blocked": {}, "alert": {}}},
330- )" };
331- static const std::string_view trailer{" \n ]}" };
373+ std::vector<std::shared_ptr<Group const >> groups;
374+ self_type::get_inbound_groups (groups);
375+ return Groups_To_JSON (groups);
376+ }
332377
333- static const auto printer = [](swoc::BufferWriter &w, Group const *g) -> swoc::BufferWriter & {
334- w.print (item_fmt, g->_match_type , g->_addr , g->_fqdn , g->_count .load (), g->_count_max .load (), g->_blocked .load (),
335- g->get_last_alert_epoch_time ());
336- return w;
337- };
378+ std::string
379+ ConnectionTracker::outbound_to_json_string ()
380+ {
381+ std::vector<std::shared_ptr<Group const >> groups;
382+ self_type::get_outbound_groups (groups);
383+ return Groups_To_JSON (groups);
384+ }
338385
339- swoc::FixedBufferWriter null_bw{nullptr }; // Empty buffer for sizing work.
386+ void
387+ ConnectionTracker::dump_outbound (FILE *f)
388+ {
340389 std::vector<std::shared_ptr<Group const >> groups;
341390
342391 self_type::get_outbound_groups (groups);
343392
344- null_bw.print (header_fmt, groups.size ()).extent ();
345- for (auto g : groups) {
346- printer (null_bw, g.get ());
347- }
348- extent = null_bw.extent () + trailer.size () - 2 ; // 2 for the trailing comma newline that will get clipped.
393+ if (groups.size ()) {
394+ fprintf (f, " \n [O] Peer Connection Tracking\n %7s | %5s | %24s | %33s | %8s |\n " , " Current" , " Block" , " Address" , " Hostname Hash" ,
395+ " Match" );
396+ fprintf (f, " ------|-------|--------------------------|-----------------------------------|----------|\n " );
349397
350- text.resize (extent);
351- swoc::FixedBufferWriter w (const_cast <char *>(text.data ()), text.size ());
352- w.restrict (trailer.size ());
353- w.print (header_fmt, groups.size ());
354- for (auto g : groups) {
355- printer (w, g.get ());
398+ for (std::shared_ptr<Group const > g : groups) {
399+ swoc::LocalBufferWriter<128 > w;
400+ w.print (" {:7} | {:5} | {:24} | {:33} | {:8} |\n " , g->_count .load (), g->_blocked .load (), g->_addr , g->_hash , g->_match_type );
401+ fwrite (w.data (), w.size (), 1 , f);
402+ }
403+
404+ fprintf (f, " ------|-------|--------------------------|-----------------------------------|----------|\n " );
356405 }
357- w.restore (trailer.size ());
358- w.write (trailer);
359- return text;
360406}
361407
362408void
363409ConnectionTracker::dump (FILE *f)
410+ {
411+ dump_outbound (f);
412+ dump_inbound (f);
413+ }
414+
415+ void
416+ ConnectionTracker::dump_inbound (FILE *f)
364417{
365418 std::vector<std::shared_ptr<Group const >> groups;
366419
367- self_type::get_outbound_groups (groups);
420+ self_type::get_inbound_groups (groups);
368421
369422 if (groups.size ()) {
370- fprintf (f, " \n Peer Connection Tracking\n %7s | %5s | %24s | %33s | %8s |\n " , " Current" , " Block" , " Address" , " Hostname Hash" ,
423+ fprintf (f, " \n [I] Peer Connection Tracking\n %7s | %5s | %24s | %33s | %8s |\n " , " Current" , " Block" , " Address" , " Hostname Hash" ,
371424 " Match" );
372425 fprintf (f, " ------|-------|--------------------------|-----------------------------------|----------|\n " );
373426
0 commit comments