2 * Copyright 2010 Andrej Kuehnal <andrejk@freenet.de>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 * This TAP enables extraction of most important diameter fields in text format.
27 * - much more performance than -T text and -T pdml
28 * - more powerfull than -T field and -z proto,colinfo
29 * - exacltly one text line per diameter message
30 * - multiple diameter messages in one frame supported
31 * E.g. one device watchdog answer and two credit control answers
32 * in one TCP packet produces 3 text lines.
33 * - several fields with same name within one diameter message supported
34 * E.g. Multiple AVP(444) Subscription-Id-Data once with IMSI once with MSISDN
35 * - several grouped AVPs supported
36 * E.g. Zero or more Multiple-Services-Credit-Control AVPs(456)
45 #ifdef HAVE_SYS_TYPES_H
46 # include <sys/types.h>
50 #include "epan/packet_info.h"
52 #include <epan/epan_dissect.h>
53 #include <epan/stat_cmd_args.h>
54 #include "epan/value_string.h"
55 #include "epan/nstime.h"
56 #include "epan/ftypes/ftypes.h"
58 #include "epan/to_str.h"
59 #include "epan/dissectors/packet-diameter.h"
62 /* used to keep track of the statistics for an entire program interface */
63 typedef struct _diameteravp_t {
65 guint32 diammsg_toprocess;
69 guint32 paired_ans_count;
73 /* Copied from proto.c */
75 tree_traverse_pre_order(proto_tree *tree, proto_tree_traverse_func func, gpointer data)
77 proto_node *pnode = tree;
81 if (func(pnode, data))
84 child = pnode->first_child;
85 while (child != NULL) {
87 child = current->next;
88 if (tree_traverse_pre_order((proto_tree *)current, func, data))
95 diam_tree_to_csv(proto_node* node, gpointer data)
101 header_field_info *hfi;
103 fprintf(stderr,"traverse end: empty node. node='%p' data='%p'\n",node,data);
107 hfi=fi ? fi->hfinfo : NULL;
109 fprintf(stderr,"traverse end: hfi not found. node='%p'\n",node);
112 ftype=fi->value.ftype->ftype;
113 if (ftype!=FT_NONE&&ftype!=FT_PROTOCOL) {
114 /* convert value to string */
115 if(fi->value.ftype->val_to_string_repr)
117 val_tmp=fvalue_to_string_repr(&fi->value,FTREPR_DISPLAY,NULL);
120 val_str=ep_strdup(val_tmp);
125 val_str=ep_strdup_printf("unsuprorted type: %s",ftype_name(ftype));
127 /*printf("traverse: name='%s', abbrev='%s',desc='%s', val='%s'\n",hfi->name,hfi->abbrev,ftype_name(hfi->type),val_str);*/
128 printf("%s='%s' ",hfi->name,val_str);
134 diameteravp_packet(void *pds, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pdi)
138 gboolean is_request=TRUE;
140 guint32 result_code=0;
143 guint32 diam_child_node=0;
144 proto_node* current=NULL;
145 proto_node* node = NULL;
146 header_field_info* hfi=NULL;
147 field_info* finfo=NULL;
148 const diameter_req_ans_pair_t* dp=pdi;
149 diameteravp_t *ds=NULL;
151 /* Validate paramerers. */
152 if(!dp || !edt || !edt->tree)
155 /* Several diameter messages within one frame are possible. *
156 * Check if we processing the message in same frame like befor or in new frame.*/
157 ds=(diameteravp_t *)pds;
158 if(pinfo->fd->num > ds->frame) {
159 ds->frame=pinfo->fd->num;
160 ds->diammsg_toprocess=0;
162 ds->diammsg_toprocess+=1;
165 /* Extract data from request/answer pair provided by diameter dissector.*/
166 is_request=dp->processing_request;
167 cmd_code=dp->cmd_code;
168 result_code=dp->result_code;
169 req_frame=dp->req_frame;
170 ans_frame=dp->ans_frame;
173 nstime_delta(&ns, &pinfo->fd->abs_ts, &dp->req_time);
174 resp_time=nstime_to_sec(&ns);
175 resp_time=resp_time<0?0.:resp_time;
178 /* Check command code provided by command line option.*/
179 if (ds->cmd_code && ds->cmd_code!=cmd_code)
182 /* Loop over top level nodes */
183 node = edt->tree->first_child;
184 while (node != NULL) {
186 node = current->next;
187 finfo=current->finfo;
188 hfi=finfo ? finfo->hfinfo : NULL;
189 /*fprintf(stderr,"DEBUG: diameteravp_packet %d %p %p node=%p abbrev=%s\n",cmd_code,edt,edt->tree,current,hfi->abbrev);*/
190 /* process current diameter subtree in the current frame. */
191 if(hfi && hfi->abbrev && strcmp(hfi->abbrev,"diameter")==0) {
192 /* Process current diameter message in the frame */
193 if (ds->diammsg_toprocess==diam_child_node) {
199 ds->paired_ans_count++;
201 /* Output frame data.*/
202 printf("frame='%d' time='%f' src='%s' srcport='%d' dst='%s' dstport='%d' proto='diameter' msgnr='%d' is_request='%d' cmd='%d' req_frame='%d' ans_frame='%d' resp_time='%f' ",
203 pinfo->fd->num,nstime_to_sec(&pinfo->fd->abs_ts),ep_address_to_str(&pinfo->src),pinfo->srcport,ep_address_to_str(&pinfo->dst), pinfo->destport,ds->diammsg_toprocess,is_request,cmd_code,req_frame,ans_frame,resp_time);
204 /* Visit selected nodes of one diameter message.*/
205 tree_traverse_pre_order(current, diam_tree_to_csv, &ds);
208 /*printf("hfi: name='%s', msg_curr='%d' abbrev='%s',type='%s'\n",hfi->name,diam_child_node,hfi->abbrev,ftype_name(hfi->type));*/
217 diameteravp_draw(void* pds)
219 diameteravp_t *ds=(diameteravp_t *)pds;
220 /* printing results */
221 printf("=== Diameter Summary ===\nrequset count:\t%d\nanswer count:\t%d\nreq/ans pairs:\t%d\n",ds->req_count,ds->ans_count,ds->paired_ans_count);
226 diameteravp_init(const char *optarg, void* userdata _U_)
233 GString* filter=NULL;
234 GString* error_string=NULL;
236 ds=g_malloc(sizeof(diameteravp_t));
238 ds->diammsg_toprocess=0;
242 ds->paired_ans_count=0;
245 filter=g_string_new("diameter");
247 /* Split command line options. */
248 tokens = g_strsplit(optarg,",", 1024);
250 while (tokens[opt_count])
253 ds->cmd_code=(guint32)atoi(tokens[2]);
255 /* Loop over diameter field names. */
256 for(opt_idx=3;opt_idx<opt_count;opt_idx++)
258 /* Current field from command line arguments. */
259 field=tokens[opt_idx];
260 /* Connect all requested fields with logical OR. */
261 g_string_append(filter,"||");
262 /* Prefix field name with "diameter." by default. */
263 if(!strchr(field,'.'))
264 g_string_append(filter, "diameter.");
265 /* Append field name to the filter. */
266 g_string_append(filter, field);
269 ds->filter=g_string_free(filter,FALSE);
271 error_string=register_tap_listener("diameter", ds, ds->filter, 0, NULL, diameteravp_packet, diameteravp_draw);
273 /* error, we failed to attach to the tap. clean up */
276 fprintf(stderr, "tshark: Couldn't register diam,csv tap: %s\n",
278 g_string_free(error_string, TRUE);
285 register_tap_listener_diameteravp(void)
287 register_stat_cmd_arg("diameter,avp", diameteravp_init, NULL);