aboutsummaryrefslogtreecommitdiffstats
path: root/ui/gtk/camel_counter.c
blob: a2bf30f5ff5246535516788823ad2acb2babbcb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* camel_counter.c
 * camel message counter for Wireshark
 * Copyright 2006 Florent Drouin (based on h225_counter.c from Lars Roland)
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif


#include <string.h>
#include <gtk/gtk.h>

#include <epan/epan.h>
#include <epan/packet_info.h>
#include <epan/value_string.h>
#include <epan/tap.h>
#include <epan/packet.h>
#include <epan/asn1.h>
#include <epan/camel-persistentdata.h>

#include "../stat_menu.h"
#include "../timestats.h"

#include "ui/simple_dialog.h"

#include "ui/gtk/main.h"
#include "ui/gtk/dlg_utils.h"
#include "ui/gtk/gui_utils.h"
#include "ui/gtk/gui_stat_util.h"
#include "ui/gtk/tap_param_dlg.h"

#include "ui/gtk/old-gtk-compat.h"

static void gtk_camelcounter_reset(void *phs);
static int gtk_camelcounter_packet(void *phs,
				   packet_info *pinfo _U_,
				   epan_dissect_t *edt _U_,
				   const void *phi);
static void gtk_camelcounter_draw(void *phs);
static void win_destroy_cb(GtkWindow *win _U_, gpointer data);
static void gtk_camelcounter_init(const char *optarg, void *userdata _U_);
void register_tap_listener_gtk_camelcounter(void);

/* following values represent the size of their valuestring arrays */

struct camelcounter_t {
  GtkWidget *win;
  GtkWidget *vbox;
  char *filter;
  GtkWidget *scrolled_window;
  GtkTreeView *table;
  guint32 camel_msg[camel_MAX_NUM_OPR_CODES];
};

static void gtk_camelcounter_reset(void *phs)
{
  struct camelcounter_t * p_counter= ( struct camelcounter_t *) phs;
  int i;

  /* Erase Message Type count */
  for(i=0;i<camel_MAX_NUM_OPR_CODES;i++) {
    p_counter->camel_msg[i]=0;
  }
}


/*
 * If there is a valid camel operation, increase the value in the array of counter
 */
static int gtk_camelcounter_packet(void *phs,
				   packet_info *pinfo _U_,
				   epan_dissect_t *edt _U_,
				   const void *phi)
{
  struct camelcounter_t * p_counter =(struct camelcounter_t *)phs;
  const struct camelsrt_info_t * pi=phi;
  if (pi->opcode != 255)
    p_counter->camel_msg[pi->opcode]++;

  return 1;
}

static void gtk_camelcounter_draw(void *phs)
{
  struct camelcounter_t *p_counter=(struct camelcounter_t *)phs;
  int i;
  char str[256];
  GtkListStore *store;
  GtkTreeIter iter;

  /* Now print Message and Reason Counter Table */
  /* clear list before printing */
  store = GTK_LIST_STORE(gtk_tree_view_get_model(p_counter->table));
  gtk_list_store_clear(store);

  for(i=0;i<camel_MAX_NUM_OPR_CODES;i++) {
    /* Message counter */
    if(p_counter->camel_msg[i]!=0) {
      g_snprintf(str, 256, "Request %s", val_to_str(i,camel_opr_code_strings,"Unknown message "));

      gtk_list_store_append(store, &iter);
      gtk_list_store_set(store, &iter,
				   0, str,
				   1, p_counter->camel_msg[i],
				   -1);
    }
  } /* Message Type */
}

static void win_destroy_cb(GtkWindow *win _U_, gpointer data)
{
  struct camelcounter_t *hs=(struct camelcounter_t *)data;

  protect_thread_critical_region();
  remove_tap_listener(hs);
  unprotect_thread_critical_region();

  if(hs->filter){
    g_free(hs->filter);
    hs->filter=NULL;
  }
  g_free(hs);
}

static const stat_column titles[]={
  {G_TYPE_STRING, LEFT, "Message Type or Reason"},
  {G_TYPE_UINT, RIGHT, "Count" }
};

static void gtk_camelcounter_init(const char *optarg, void *userdata _U_)
{
  struct camelcounter_t *p_camelcounter;
  const char *filter=NULL;
  GString *error_string;
  GtkWidget *bbox;
  GtkWidget *close_bt;

  if(strncmp(optarg,"camel,counter,",14) == 0){
    filter=optarg+14;
  } else {
    filter=NULL;
  }

  p_camelcounter=g_malloc(sizeof(struct camelcounter_t));
  p_camelcounter->filter=g_strdup(filter);

  gtk_camelcounter_reset(p_camelcounter);

  /* transient_for top_level */
  p_camelcounter->win=dlg_window_new("Wireshark: CAMEL counters");
  gtk_window_set_destroy_with_parent (GTK_WINDOW(p_camelcounter->win), TRUE);

	gtk_window_set_default_size(GTK_WINDOW(p_camelcounter->win), 500, 300);

  p_camelcounter->vbox=ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 3, FALSE);
  gtk_container_set_border_width(GTK_CONTAINER(p_camelcounter->vbox), 12);

  init_main_stat_window(p_camelcounter->win, p_camelcounter->vbox, "CAMEL Messages Counters", filter);

  /* init a scrolled window*/
  p_camelcounter->scrolled_window = scrolled_window_new(NULL, NULL);

  p_camelcounter->table = create_stat_table(p_camelcounter->scrolled_window, p_camelcounter->vbox, 2, titles);

  error_string=register_tap_listener("CAMEL", p_camelcounter, filter, 0,
				       gtk_camelcounter_reset,
				       gtk_camelcounter_packet,
				       gtk_camelcounter_draw);

  if(error_string){
    simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
    g_string_free(error_string, TRUE);
    g_free(p_camelcounter);
    return;
  }

  /* Button row. */
  bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
  gtk_box_pack_end(GTK_BOX(p_camelcounter->vbox), bbox, FALSE, FALSE, 0);

  close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
  window_set_cancel_button(p_camelcounter->win, close_bt, window_cancel_button_cb);

  g_signal_connect(p_camelcounter->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
  g_signal_connect(p_camelcounter->win, "destroy", G_CALLBACK(win_destroy_cb), p_camelcounter);

  gtk_widget_show_all(p_camelcounter->win);
  window_present(p_camelcounter->win);

  cf_retap_packets(&cfile);
  gdk_window_raise(gtk_widget_get_window(p_camelcounter->win));
}

static tap_param camel_counter_params[] = {
  { PARAM_FILTER, "Filter", NULL }
};

static tap_param_dlg camel_counter_dlg = {
  "CAMEL Messages and Response Status",
  "camel,counter",
  gtk_camelcounter_init,
  -1,
  G_N_ELEMENTS(camel_counter_params),
  camel_counter_params
};

void  /* Next line mandatory */
register_tap_listener_gtk_camelcounter(void)
{
  register_dfilter_stat(&camel_counter_dlg, "_GSM/CAMEL",
  			REGISTER_STAT_GROUP_TELEPHONY);

}

void camel_counter_cb(GtkAction *action, gpointer user_data _U_)
{
	tap_param_dlg_cb(action, &camel_counter_dlg);
}