PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/ui/gtk/mtp3_stat.c

https://github.com/labx-technologies-llc/wireshark
C | 443 lines | 283 code | 91 blank | 69 comment | 20 complexity | bda3d783448c76f9cef9b93bc5d002fb MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /* mtp3_stat.c
  2. *
  3. * Copyright 2004, Michael Lum <mlum [AT] telostech.com>
  4. * In association with Telos Technology Inc.
  5. *
  6. * Modified from gsm_map_stat.c
  7. *
  8. * $Id$
  9. *
  10. * Wireshark - Network traffic analyzer
  11. * By Gerald Combs <gerald@wireshark.org>
  12. * Copyright 1998 Gerald Combs
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  27. */
  28. /*
  29. * This TAP provides statistics for MTP3:
  30. */
  31. #include "config.h"
  32. #include <string.h>
  33. #include <gtk/gtk.h>
  34. #include "epan/packet_info.h"
  35. #include "epan/epan.h"
  36. #include "epan/value_string.h"
  37. #include <epan/stat_cmd_args.h>
  38. #include <epan/tap.h>
  39. #include <epan/emem.h>
  40. #include <epan/dissectors/packet-mtp3.h>
  41. #include "../stat_menu.h"
  42. #include "ui/simple_dialog.h"
  43. #include "ui/gtk/gui_stat_menu.h"
  44. #include "ui/gtk/dlg_utils.h"
  45. #include "ui/gtk/filter_dlg.h"
  46. #include "ui/gtk/gui_utils.h"
  47. #include "ui/gtk/mtp3_stat.h"
  48. #include "ui/gtk/old-gtk-compat.h"
  49. enum
  50. {
  51. OPC_COLUMN,
  52. DPC_COLUMN,
  53. SI_COLUMN,
  54. NUM_MSUS_COLUMN,
  55. NUM_BYTES_COLUMN,
  56. AVG_BYTES_COLUMN,
  57. N_COLUMN /* The number of columns */
  58. };
  59. typedef struct _mtp3_stat_dlg_t {
  60. GtkWidget *win;
  61. GtkWidget *scrolled_win;
  62. GtkWidget *table;
  63. char *entries[N_COLUMN];
  64. } mtp3_stat_dlg_t;
  65. static mtp3_stat_dlg_t dlg;
  66. mtp3_stat_t mtp3_stat[MTP3_MAX_NUM_OPC_DPC];
  67. guint8 mtp3_num_used;
  68. /* Create list */
  69. static
  70. GtkWidget* create_list(void)
  71. {
  72. GtkListStore *list_store;
  73. GtkWidget *list;
  74. GtkTreeViewColumn *column;
  75. GtkCellRenderer *renderer;
  76. GtkTreeSortable *sortable;
  77. GtkTreeView *list_view;
  78. GtkTreeSelection *selection;
  79. /* Create the store */
  80. list_store = gtk_list_store_new(N_COLUMN, /* Total number of columns XXX*/
  81. G_TYPE_STRING, /* OPC */
  82. G_TYPE_STRING, /* DPC */
  83. G_TYPE_STRING, /* SI */
  84. G_TYPE_INT, /* Num MSUs */
  85. G_TYPE_INT, /* Num Bytes */
  86. G_TYPE_FLOAT); /* Avg Bytes */
  87. /* Create a view */
  88. list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
  89. list_view = GTK_TREE_VIEW(list);
  90. sortable = GTK_TREE_SORTABLE(list_store);
  91. /* Speed up the list display */
  92. gtk_tree_view_set_fixed_height_mode(list_view, TRUE);
  93. /* Setup the sortable columns */
  94. gtk_tree_sortable_set_sort_column_id(sortable, OPC_COLUMN, GTK_SORT_ASCENDING);
  95. gtk_tree_view_set_headers_clickable(list_view, FALSE);
  96. /* The view now holds a reference. We can get rid of our own reference */
  97. g_object_unref (G_OBJECT (list_store));
  98. /*
  99. * Create the first column packet, associating the "text" attribute of the
  100. * cell_renderer to the first column of the model
  101. */
  102. /* 1:st column */
  103. renderer = gtk_cell_renderer_text_new ();
  104. column = gtk_tree_view_column_new_with_attributes ("OPC", renderer,
  105. "text", OPC_COLUMN,
  106. NULL);
  107. gtk_tree_view_column_set_sort_column_id(column, OPC_COLUMN);
  108. gtk_tree_view_column_set_resizable(column, TRUE);
  109. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
  110. gtk_tree_view_column_set_min_width(column, 80);
  111. /* Add the column to the view. */
  112. gtk_tree_view_append_column (list_view, column);
  113. /* 2:nd column... */
  114. renderer = gtk_cell_renderer_text_new ();
  115. column = gtk_tree_view_column_new_with_attributes ("DPC", renderer,
  116. "text", DPC_COLUMN,
  117. NULL);
  118. gtk_tree_view_column_set_sort_column_id(column, DPC_COLUMN);
  119. gtk_tree_view_column_set_resizable(column, TRUE);
  120. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
  121. gtk_tree_view_column_set_min_width(column, 80);
  122. gtk_tree_view_append_column (list_view, column);
  123. /* 3:d column... */
  124. renderer = gtk_cell_renderer_text_new ();
  125. column = gtk_tree_view_column_new_with_attributes ("SI", renderer,
  126. "text", SI_COLUMN,
  127. NULL);
  128. gtk_tree_view_column_set_sort_column_id(column, SI_COLUMN);
  129. gtk_tree_view_column_set_resizable(column, TRUE);
  130. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
  131. gtk_tree_view_column_set_min_width(column, 110);
  132. gtk_tree_view_append_column (list_view, column);
  133. /* 4:th column... */
  134. renderer = gtk_cell_renderer_text_new ();
  135. column = gtk_tree_view_column_new_with_attributes ("Num MSUs", renderer,
  136. "text", NUM_MSUS_COLUMN,
  137. NULL);
  138. gtk_tree_view_column_set_sort_column_id(column, NUM_MSUS_COLUMN);
  139. gtk_tree_view_column_set_resizable(column, TRUE);
  140. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
  141. gtk_tree_view_column_set_min_width(column, 80);
  142. gtk_tree_view_append_column (list_view, column);
  143. /* 5:th column... */
  144. renderer = gtk_cell_renderer_text_new ();
  145. column = gtk_tree_view_column_new_with_attributes ("Num Bytes", renderer,
  146. "text", NUM_BYTES_COLUMN,
  147. NULL);
  148. gtk_tree_view_column_set_sort_column_id(column, NUM_BYTES_COLUMN);
  149. gtk_tree_view_column_set_resizable(column, TRUE);
  150. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
  151. gtk_tree_view_column_set_min_width(column, 100);
  152. gtk_tree_view_append_column (list_view, column);
  153. /* 6:th column... */
  154. renderer = gtk_cell_renderer_text_new ();
  155. column = gtk_tree_view_column_new_with_attributes ("Avg Bytes", renderer,
  156. "text", AVG_BYTES_COLUMN,
  157. NULL);
  158. gtk_tree_view_column_set_cell_data_func(column, renderer, float_data_func,
  159. GINT_TO_POINTER(AVG_BYTES_COLUMN), NULL);
  160. gtk_tree_view_column_set_sort_column_id(column, AVG_BYTES_COLUMN);
  161. gtk_tree_view_column_set_resizable(column, TRUE);
  162. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
  163. gtk_tree_view_column_set_min_width(column, 80);
  164. gtk_tree_view_append_column (list_view, column);
  165. /* Now enable the sorting of each column */
  166. gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view), TRUE);
  167. gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(list_view), TRUE);
  168. /* Setup the selection handler */
  169. selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
  170. gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
  171. return list;
  172. }
  173. static void
  174. mtp3_stat_reset(
  175. void *tapdata)
  176. {
  177. mtp3_stat_t (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = (mtp3_stat_t(*)[MTP3_MAX_NUM_OPC_DPC])tapdata;
  178. mtp3_num_used = 0;
  179. memset(stat_p, 0, MTP3_MAX_NUM_OPC_DPC * sizeof(mtp3_stat_t));
  180. if (dlg.win != NULL)
  181. {
  182. gtk_list_store_clear(GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dlg.table))));
  183. }
  184. }
  185. static gboolean
  186. mtp3_stat_packet(
  187. void *tapdata,
  188. packet_info *pinfo _U_,
  189. epan_dissect_t *edt _U_,
  190. const void *data)
  191. {
  192. mtp3_stat_t (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = (mtp3_stat_t(*)[MTP3_MAX_NUM_OPC_DPC])tapdata;
  193. const mtp3_tap_rec_t *data_p = (mtp3_tap_rec_t *)data;
  194. int i;
  195. if (data_p->si_code >= MTP3_NUM_SI_CODE)
  196. {
  197. /*
  198. * we thought this si_code was not used ?
  199. * is MTP3_NUM_SI_CODE out of date ?
  200. */
  201. return(FALSE);
  202. }
  203. /*
  204. * look for opc/dpc pair
  205. */
  206. i = 0;
  207. while (i < mtp3_num_used)
  208. {
  209. if (memcmp(&data_p->addr_opc, &(*stat_p)[i].addr_opc, sizeof(mtp3_addr_pc_t)) == 0)
  210. {
  211. if (memcmp(&data_p->addr_dpc, &(*stat_p)[i].addr_dpc, sizeof(mtp3_addr_pc_t)) == 0)
  212. {
  213. break;
  214. }
  215. }
  216. i++;
  217. }
  218. if (i == mtp3_num_used)
  219. {
  220. if (mtp3_num_used == MTP3_MAX_NUM_OPC_DPC)
  221. {
  222. /*
  223. * too many
  224. */
  225. return(FALSE);
  226. }
  227. mtp3_num_used++;
  228. }
  229. (*stat_p)[i].addr_opc = data_p->addr_opc;
  230. (*stat_p)[i].addr_dpc = data_p->addr_dpc;
  231. (*stat_p)[i].si_code[data_p->si_code].num_msus++;
  232. (*stat_p)[i].si_code[data_p->si_code].size += data_p->size;
  233. return(TRUE);
  234. }
  235. static void
  236. mtp3_stat_draw(
  237. void *tapdata)
  238. {
  239. mtp3_stat_t (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = (mtp3_stat_t(*)[MTP3_MAX_NUM_OPC_DPC])tapdata;
  240. int i,j;
  241. char *str;
  242. float avg;
  243. GtkListStore *list_store = NULL;
  244. GtkTreeIter iter;
  245. if (!dlg.win || !tapdata)
  246. {
  247. return;
  248. }
  249. str=(char *)ep_alloc(256);
  250. i = 0;
  251. list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW (dlg.table))); /* Get store */
  252. while (i < mtp3_num_used)
  253. {
  254. mtp3_addr_to_str_buf(&(*stat_p)[i].addr_opc, str, 256);
  255. dlg.entries[0] = g_strdup(str);
  256. mtp3_addr_to_str_buf(&(*stat_p)[i].addr_dpc, str, 256);
  257. dlg.entries[1] = g_strdup(str);
  258. for (j=0; j < MTP3_NUM_SI_CODE; j++){
  259. /* Creates a new row at position. iter will be changed to point to this new row.
  260. * If position is larger than the number of rows on the list, then the new row will be appended to the list.
  261. * The row will be filled with the values given to this function.
  262. * :
  263. * should generally be preferred when inserting rows in a sorted list store.
  264. */
  265. avg = 0.0f;
  266. if ((*stat_p)[i].si_code[j].num_msus !=0){
  267. avg = (float)(*stat_p)[i].si_code[j].size/(float)(*stat_p)[i].si_code[j].num_msus;
  268. }
  269. gtk_list_store_insert_with_values( list_store , &iter, G_MAXINT,
  270. OPC_COLUMN, dlg.entries[0],
  271. DPC_COLUMN, dlg.entries[1],
  272. SI_COLUMN, mtp3_service_indicator_code_short_vals[j].strptr,
  273. NUM_MSUS_COLUMN, (*stat_p)[i].si_code[j].num_msus,
  274. NUM_BYTES_COLUMN, (*stat_p)[i].si_code[j].size,
  275. AVG_BYTES_COLUMN, avg,
  276. -1);
  277. }
  278. i++;
  279. }
  280. }
  281. static void
  282. mtp3_stat_gtk_win_destroy_cb(
  283. GtkWindow *win _U_,
  284. gpointer user_data)
  285. {
  286. memset((void *) user_data, 0, sizeof(mtp3_stat_dlg_t));
  287. }
  288. static void
  289. mtp3_stat_gtk_win_create(
  290. mtp3_stat_dlg_t *dlg_p,
  291. const char *title)
  292. {
  293. GtkWidget *vbox;
  294. GtkWidget *bt_close;
  295. GtkWidget *bbox;
  296. dlg_p->win = dlg_window_new(title); /* transient_for top_level */
  297. gtk_window_set_destroy_with_parent (GTK_WINDOW(dlg_p->win), TRUE);
  298. gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 640, 390);
  299. vbox = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 3, FALSE);
  300. gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
  301. gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
  302. dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
  303. gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
  304. dlg_p->table = create_list();
  305. gtk_widget_show(dlg_p->table);
  306. gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
  307. /* Button row. */
  308. bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
  309. gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
  310. bt_close = (GtkWidget *)g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
  311. window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
  312. g_signal_connect(dlg_p->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
  313. g_signal_connect(dlg_p->win, "destroy", G_CALLBACK(mtp3_stat_gtk_win_destroy_cb), dlg_p);
  314. gtk_widget_show_all(dlg_p->win);
  315. window_present(dlg_p->win);
  316. }
  317. void mtp3_stat_gtk_cb(GtkAction *action _U_, gpointer user_data _U_)
  318. {
  319. /*
  320. * if the window is already open, bring it to front
  321. */
  322. if (dlg.win)
  323. {
  324. gdk_window_raise(gtk_widget_get_window(dlg.win));
  325. return;
  326. }
  327. mtp3_stat_gtk_win_create(&dlg, "MTP3 Statistics");
  328. mtp3_stat_draw(&mtp3_stat);
  329. }
  330. static void
  331. mtp3_stat_gtk_init( const char *opt_arg _U_, void* userdata _U_)
  332. {
  333. mtp3_stat_gtk_cb(NULL, NULL);
  334. }
  335. void
  336. register_tap_listener_gtkmtp3_stat(void)
  337. {
  338. GString *err_p;
  339. memset((void *) &mtp3_stat, 0, sizeof(mtp3_stat_t));
  340. err_p =
  341. register_tap_listener("mtp3", &mtp3_stat, NULL, 0,
  342. mtp3_stat_reset,
  343. mtp3_stat_packet,
  344. mtp3_stat_draw);
  345. if (err_p != NULL)
  346. {
  347. simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK, "%s", err_p->str);
  348. g_string_free(err_p, TRUE);
  349. exit(1);
  350. }
  351. register_stat_cmd_arg("mtp3,msus", mtp3_stat_gtk_init,NULL);
  352. }