DCERPC: factor out proto_tree_add_dcerpc_drep()
[wireshark-wip.git] / plugins / stats_tree / pinfo_stats_tree.c
blob97948fff75de95ce1b7480724bd36b10fea6a2b1
1 /* pinfo_stats_tree.c
2 * Stats tree for ethernet frames
4 * (c) 2005, Luis E. G. Ontanon <luis@ontanon.org>
6 * $Id$
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "config.h"
29 #include <epan/stats_tree.h>
30 #include <epan/prefs.h>
31 #include <epan/uat.h>
32 #include <epan/uat-int.h>
33 #include <epan/to_str.h>
35 #include "pinfo_stats_tree.h"
37 /*-------------------------------------
38 * UAT for Packet Lengths
39 *-------------------------------------
41 typedef struct {
42 range_t *packet_range;
43 } uat_plen_record_t;
45 static range_t default_range[10] = {
46 {1, {{0, 19}}},
47 {1, {{20, 39}}},
48 {1, {{40, 79}}},
49 {1, {{80, 159}}},
50 {1, {{160, 319}}},
51 {1, {{320, 639}}},
52 {1, {{640, 1279}}},
53 {1, {{1280, 2559}}},
54 {1, {{2560, 5119}}},
55 {1, {{5120, 0xFFFFFFFF}}}
57 static uat_plen_record_t *uat_plen_records = NULL;
58 static uat_t * plen_uat = NULL;
59 static guint num_plen_uat = 0;
61 static void* uat_plen_record_copy_cb(void* n, const void* o, size_t siz _U_) {
62 const uat_plen_record_t *r = (const uat_plen_record_t *)o;
63 uat_plen_record_t *rn = (uat_plen_record_t *)n;
65 if (r->packet_range)
66 rn->packet_range = range_copy(r->packet_range);
68 return n;
71 static void
72 uat_plen_record_update_cb(void *r, const char **err)
74 uat_plen_record_t *rec = (uat_plen_record_t*)r;
75 if (rec->packet_range->nranges < 1) {
76 *err = g_strdup("Invalid range string");
77 return;
80 *err = NULL;
83 static void uat_plen_record_free_cb(void*r) {
84 uat_plen_record_t* record = (uat_plen_record_t*)r;
86 if (record->packet_range)
87 g_free(record->packet_range);
90 static void uat_plen_record_post_update_cb(void) {
91 guint i, num_default;
92 uat_plen_record_t rec;
94 /* If there are no records, create default list */
95 if (num_plen_uat == 0) {
96 num_default = sizeof(default_range)/sizeof(range_t);
98 /* default values for packet lengths */
99 for (i = 0; i < num_default; i++)
101 rec.packet_range = &default_range[i];
102 uat_add_record(plen_uat, &rec, TRUE);
107 UAT_RANGE_CB_DEF(uat_plen_records, packet_range, uat_plen_record_t)
109 /* ip host stats_tree -- basic test */
110 static int st_node_ip = -1;
111 static const gchar* st_str_ip = "IP Addresses";
113 static void ip_hosts_stats_tree_init(stats_tree* st) {
114 st_node_ip = stats_tree_create_node(st, st_str_ip, 0, TRUE);
117 static int ip_hosts_stats_tree_packet(stats_tree *st , packet_info *pinfo, epan_dissect_t *edt _U_, const void *p _U_) {
118 tick_stat_node(st, st_str_ip, 0, FALSE);
119 tick_stat_node(st, ep_address_to_str(&pinfo->net_src), st_node_ip, FALSE);
120 tick_stat_node(st, ep_address_to_str(&pinfo->net_dst), st_node_ip, FALSE);
122 return 1;
125 /* packet type stats_tree -- test pivot node */
126 static int st_node_ptype = -1;
127 static const gchar* st_str_ptype = "IP Protocol Types";
129 static void ptype_stats_tree_init(stats_tree* st) {
130 st_node_ptype = stats_tree_create_pivot(st, st_str_ptype, 0);
133 static int ptype_stats_tree_packet(stats_tree* st, packet_info* pinfo, epan_dissect_t *edt _U_, const void *p _U_) {
134 const gchar* ptype;
136 ptype = port_type_to_str(pinfo->ptype);
138 stats_tree_tick_pivot(st,st_node_ptype,ptype);
140 return 1;
143 /* packet length stats_tree -- test range node */
144 static int st_node_plen = -1;
145 static const gchar* st_str_plen = "Packet Lengths";
147 static void plen_stats_tree_init(stats_tree* st) {
148 guint i;
149 char **str_range_array = (char **)ep_alloc(num_plen_uat*sizeof(char*));
151 /* Convert the ranges to strings for the stats tree API */
152 for (i = 0; i < num_plen_uat; i++) {
153 str_range_array[i] = range_convert_range(uat_plen_records[i].packet_range);
156 st_node_plen = stats_tree_create_range_node_string(st, st_str_plen, 0, num_plen_uat, str_range_array);
159 static int plen_stats_tree_packet(stats_tree* st, packet_info* pinfo, epan_dissect_t *edt _U_, const void *p _U_) {
160 tick_stat_node(st, st_str_plen, 0, FALSE);
161 stats_tree_tick_range(st, st_str_plen, 0, pinfo->fd->pkt_len);
163 return 1;
166 /* a tree example
167 - IP
168 - PROTO
169 - PORT
172 static int st_node_dsts = -1;
173 static const gchar* st_str_dsts = "IP Destinations";
175 static void dsts_stats_tree_init(stats_tree* st) {
176 st_node_dsts = stats_tree_create_node(st, st_str_dsts, 0, TRUE);
179 static int dsts_stats_tree_packet(stats_tree* st, packet_info* pinfo, epan_dissect_t *edt _U_, const void *p _U_) {
180 static gchar str[128];
181 int ip_dst_node;
182 int protocol_node;
184 tick_stat_node(st, st_str_dsts, 0, FALSE);
186 ip_dst_node = tick_stat_node(st, ep_address_to_str(&pinfo->net_src), st_node_dsts, TRUE);
188 protocol_node = tick_stat_node(st,port_type_to_str(pinfo->ptype),ip_dst_node,TRUE);
190 g_snprintf(str, sizeof(str),"%u",pinfo->destport);
191 tick_stat_node(st,str,protocol_node,TRUE);
193 return 1;
196 /* register all pinfo trees */
197 void register_pinfo_stat_trees(void) {
198 module_t *stat_module;
200 static uat_field_t plen_uat_flds[] = {
201 UAT_FLD_RANGE(uat_plen_records, packet_range, "Packet Range", 0xFFFFFFFF, "Range of packet sizes to count"),
202 UAT_END_FIELDS
205 stats_tree_register_plugin("ip","ip_hosts",st_str_ip, 0, ip_hosts_stats_tree_packet, ip_hosts_stats_tree_init, NULL );
206 stats_tree_register_plugin("ip","ptype",st_str_ptype, 0, ptype_stats_tree_packet, ptype_stats_tree_init, NULL );
207 stats_tree_register_with_group("frame","plen",st_str_plen, 0, plen_stats_tree_packet, plen_stats_tree_init, NULL, REGISTER_STAT_GROUP_GENERIC );
208 stats_tree_register_plugin("ip","dests",st_str_dsts, 0, dsts_stats_tree_packet, dsts_stats_tree_init, NULL );
210 stat_module = prefs_register_stat("stat_tree", "Stats Tree", "Stats Tree", NULL);
212 plen_uat = uat_new("Packet Lengths",
213 sizeof(uat_plen_record_t), /* record size */
214 "packet_lengths", /* filename */
215 TRUE, /* from_profile */
216 (void**) &uat_plen_records, /* data_ptr */
217 &num_plen_uat, /* numitems_ptr */
218 0, /* not a dissector, so affects neither dissection nor fields */
219 NULL, /* help */
220 uat_plen_record_copy_cb, /* copy callback */
221 uat_plen_record_update_cb, /* update callback */
222 uat_plen_record_free_cb, /* free callback */
223 uat_plen_record_post_update_cb, /* post update callback */
224 plen_uat_flds); /* UAT field definitions */
226 prefs_register_uat_preference(stat_module, "packet_lengths",
227 "Packet Lengths", "Delineated packet sizes to count", plen_uat);