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"
57 #include "epan/to_str.h"
58 #include "epan/dissectors/packet-diameter.h"
61 /* used to keep track of the statistics for an entire program interface */
62 typedef struct _diameteravp_t {
64 guint32 diammsg_toprocess;
68 guint32 paired_ans_count;
72 /* Copied from proto.c */
74 tree_traverse_pre_order(proto_tree *tree, proto_tree_traverse_func func, gpointer data)
76 proto_node *pnode = tree;
80 if (func(pnode, data))
83 child = pnode->first_child;
84 while (child != NULL) {
86 child = current->next;
87 if (tree_traverse_pre_order((proto_tree *)current, func, data))
94 diam_tree_to_csv(proto_node* node, gpointer data)
100 header_field_info *hfi;
102 fprintf(stderr,"traverse end: empty node. node='%p' data='%p'\n",node,data);
106 hfi=fi ? fi->hfinfo : NULL;
108 fprintf(stderr,"traverse end: hfi not found. node='%p'\n",node);
111 ftype=fi->value.ftype->ftype;
112 if (ftype!=FT_NONE&&ftype!=FT_PROTOCOL) {
113 /* convert value to string */
114 if(fi->value.ftype->val_to_string_repr)
116 val_tmp=fvalue_to_string_repr(&fi->value,FTREPR_DISPLAY,NULL);
119 val_str=ep_strdup(val_tmp);
124 val_str=ep_strdup_printf("unsupported type: %s",ftype_name(ftype));
126 /*printf("traverse: name='%s', abbrev='%s',desc='%s', val='%s'\n",hfi->name,hfi->abbrev,ftype_name(hfi->type),val_str);*/
127 printf("%s='%s' ",hfi->name,val_str);
133 diameteravp_packet(void *pds, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pdi)
137 gboolean is_request=TRUE;
141 guint32 diam_child_node=0;
142 proto_node* current=NULL;
143 proto_node* node = NULL;
144 header_field_info* hfi=NULL;
145 field_info* finfo=NULL;
146 const diameter_req_ans_pair_t* dp=pdi;
147 diameteravp_t *ds=NULL;
149 /* Validate paramerers. */
150 if(!dp || !edt || !edt->tree)
153 /* Several diameter messages within one frame are possible. *
154 * Check if we processing the message in same frame like befor or in new frame.*/
155 ds=(diameteravp_t *)pds;
156 if(pinfo->fd->num > ds->frame) {
157 ds->frame=pinfo->fd->num;
158 ds->diammsg_toprocess=0;
160 ds->diammsg_toprocess+=1;
163 /* Extract data from request/answer pair provided by diameter dissector.*/
164 is_request=dp->processing_request;
165 cmd_code=dp->cmd_code;
166 req_frame=dp->req_frame;
167 ans_frame=dp->ans_frame;
170 nstime_delta(&ns, &pinfo->fd->abs_ts, &dp->req_time);
171 resp_time=nstime_to_sec(&ns);
172 resp_time=resp_time<0?0.:resp_time;
175 /* Check command code provided by command line option.*/
176 if (ds->cmd_code && ds->cmd_code!=cmd_code)
179 /* Loop over top level nodes */
180 node = edt->tree->first_child;
181 while (node != NULL) {
183 node = current->next;
184 finfo=current->finfo;
185 hfi=finfo ? finfo->hfinfo : NULL;
186 /*fprintf(stderr,"DEBUG: diameteravp_packet %d %p %p node=%p abbrev=%s\n",cmd_code,edt,edt->tree,current,hfi->abbrev);*/
187 /* process current diameter subtree in the current frame. */
188 if(hfi && hfi->abbrev && strcmp(hfi->abbrev,"diameter")==0) {
189 /* Process current diameter message in the frame */
190 if (ds->diammsg_toprocess==diam_child_node) {
196 ds->paired_ans_count++;
198 /* Output frame data.*/
199 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' ",
200 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);
201 /* Visit selected nodes of one diameter message.*/
202 tree_traverse_pre_order(current, diam_tree_to_csv, &ds);
205 /*printf("hfi: name='%s', msg_curr='%d' abbrev='%s',type='%s'\n",hfi->name,diam_child_node,hfi->abbrev,ftype_name(hfi->type));*/
214 diameteravp_draw(void* pds)
216 diameteravp_t *ds=(diameteravp_t *)pds;
217 /* printing results */
218 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);
223 diameteravp_init(const char *optarg, void* userdata _U_)
230 GString* filter=NULL;
231 GString* error_string=NULL;
233 ds=g_malloc(sizeof(diameteravp_t));
235 ds->diammsg_toprocess=0;
239 ds->paired_ans_count=0;
242 filter=g_string_new("diameter");
244 /* Split command line options. */
245 tokens = g_strsplit(optarg,",", 1024);
247 while (tokens[opt_count])
250 ds->cmd_code=(guint32)atoi(tokens[2]);
252 /* Loop over diameter field names. */
253 for(opt_idx=3;opt_idx<opt_count;opt_idx++)
255 /* Current field from command line arguments. */
256 field=tokens[opt_idx];
257 /* Connect all requested fields with logical OR. */
258 g_string_append(filter,"||");
259 /* Prefix field name with "diameter." by default. */
260 if(!strchr(field,'.'))
261 g_string_append(filter, "diameter.");
262 /* Append field name to the filter. */
263 g_string_append(filter, field);
266 ds->filter=g_string_free(filter,FALSE);
268 error_string=register_tap_listener("diameter", ds, ds->filter, 0, NULL, diameteravp_packet, diameteravp_draw);
270 /* error, we failed to attach to the tap. clean up */
273 fprintf(stderr, "tshark: Couldn't register diam,csv tap: %s\n",
275 g_string_free(error_string, TRUE);
282 register_tap_listener_diameteravp(void)
284 register_stat_cmd_arg("diameter,avp", diameteravp_init, NULL);