Make tap-diameter-avp.c compile on windows.
[metze/wireshark/wip.git] / tap-diameter-avp.c
1 /* tap-diameter-avp.c
2  * Copyright 2010 Andrej Kuehnal <andrejk@freenet.de>
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 /*
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)
37  */
38
39 #ifdef HAVE_CONFIG_H
40 # include "config.h"
41 #endif
42
43 #include <stdio.h>
44
45 #ifdef HAVE_SYS_TYPES_H
46 # include <sys/types.h>
47 #endif
48
49 #include <string.h>
50 #include "epan/packet_info.h"
51 #include <epan/tap.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 "register.h"
58 #include <epan/dissectors/packet-diameter.h>
59
60
61 /* used to keep track of the statistics for an entire program interface */
62 typedef struct _diameteravp_t {
63         guint32 frame;
64         guint32 diammsg_toprocess;
65         guint32 req_count;
66         guint32 ans_count;
67         guint32 paired_ans_count;
68         char* filter;
69 } diameteravp_t;
70
71 /* Copied from proto.c */
72 static gboolean
73 tree_traverse_pre_order(proto_tree *tree, proto_tree_traverse_func func, gpointer data)
74 {
75         proto_node *pnode = tree;
76         proto_node *child;
77         proto_node *current;
78
79         if (func(pnode, data))
80                 return TRUE;
81
82         child = pnode->first_child;
83         while (child != NULL) {
84                 current = child;
85                 child = current->next;
86                 if (tree_traverse_pre_order((proto_tree *)current, func, data))
87                         return TRUE;
88         }
89         return FALSE;
90 }
91
92 static gboolean
93 diam_tree_to_csv(proto_node* node, gpointer data)
94 {
95         char* val_str=NULL;
96         char* val_tmp=NULL;
97         ftenum_t ftype;
98         field_info* fi;
99         header_field_info       *hfi;
100         if(!node) {
101                 fprintf(stderr,"traverse end: node='%p' data='%p'\n",node,data);
102                 return FALSE;
103         }
104         fi=node->finfo;
105         hfi=fi ? fi->hfinfo : NULL;
106         if(!hfi) {
107                 fprintf(stderr,"traverse end2: Hfi not found node='%p'\n",node);
108                 return FALSE;
109         }
110         ftype=fi->value.ftype->ftype;
111         if (ftype!=FT_NONE&&ftype!=FT_PROTOCOL) {
112                 /* convert value to string */
113                 if(fi->value.ftype->val_to_string_repr)
114                 {
115                         val_tmp=fvalue_to_string_repr(&fi->value,FTREPR_DISPLAY,NULL);
116                         if(val_tmp)
117                         {
118                                 val_str=ep_strdup(val_tmp);
119                                 g_free(val_tmp);
120                         }
121                 }
122                 if(!val_str)
123                         val_str=ep_strdup_printf("unsuprorted type: %s",ftype_name(ftype));
124
125                 /*printf("traverse: name='%s', abbrev='%s',desc='%s', val='%s'\n",hfi->name,hfi->abbrev,ftype_name(hfi->type),val_str);*/
126                 printf("%s='%s' ",hfi->name,val_str);
127         }
128         return FALSE;
129 }
130
131 static int
132 diameteravp_packet(void *pds, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pdi)
133 {
134         int ret = 0;
135         double resp_time=0.;
136         gboolean is_request=TRUE;
137         guint32 cmd_code=0;
138         guint32 result_code=0;
139         guint32 req_frame=0;
140         guint32 ans_frame=0;
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
148         /* Several diameter messages within one frame are possible.                    *
149          * Check if we processing the message in same frame like befor or in new frame.*/
150         diameteravp_t *ds=(diameteravp_t *)pds;
151         if(pinfo->fd->num > ds->frame) {
152                 ds->frame=pinfo->fd->num;
153                 ds->diammsg_toprocess=0;
154         } else {
155                         ds->diammsg_toprocess+=1;
156         }
157         /* Extract data from request/answer pair provided by diameter dissector.*/
158         if(dp) {
159                 is_request=dp->processing_request;
160                 cmd_code=dp->cmd_code;
161                 result_code=dp->result_code;
162                 req_frame=dp->req_frame;
163                 ans_frame=dp->ans_frame;
164                 if(!is_request) {
165                         nstime_t ns;
166                         nstime_delta(&ns, &pinfo->fd->abs_ts, &dp->req_time);
167                         resp_time=nstime_to_sec(&ns);
168                         resp_time=resp_time<0?0.:resp_time;
169                 }
170         }
171         if (!edt || !edt->tree || cmd_code!=272)
172                 return ret;
173         /* Loop over top level nodes */
174         node = edt->tree->first_child;
175         while (node != NULL) {
176                 current = node;
177                 node = current->next;
178                 finfo=current->finfo;
179                 hfi=finfo ? finfo->hfinfo : NULL;
180                 /*fprintf(stderr,"diameteravp_packet %d %p %p node=%p abbrev=%s\n",cmd_code,edt,edt->tree,current,hfi->abbrev);*/
181                 /* process current diameter subtree in the current frame. */
182                 if(hfi && hfi->abbrev && strcmp(hfi->abbrev,"diameter")==0) {
183                         /* Process current diameter message in the frame */
184                         if (ds->diammsg_toprocess==diam_child_node) {
185                                 if(is_request) {
186                                         ds->req_count++;
187                                 } else {
188                                         ds->ans_count++;
189                                         if (req_frame>0)
190                                                 ds->paired_ans_count++;
191                                 }
192                                 /* Output frame data.*/
193                                 printf("frame='%d' proto='diameter' msgnr='%d' is_request='%d' cmd='%d' req_frame='%d' ans_frame='%d' resp_time='%f' ",pinfo->fd->num,ds->diammsg_toprocess,is_request,cmd_code,req_frame,ans_frame,resp_time);
194                                 /* Visit selected nodes of one diameter message.*/
195                                 tree_traverse_pre_order(current, diam_tree_to_csv, &ds);
196                                 /* End of message.*/
197                                 printf("\n");
198                                 /*printf("hfi: name='%s', msg_curr='%d' abbrev='%s',type='%s'\n",hfi->name,diam_child_node,hfi->abbrev,ftype_name(hfi->type));*/
199                         }
200                         diam_child_node++;
201                 }
202         }
203         return ret;
204 }
205
206 static void
207 diameteravp_draw(void* pds)
208 {
209         diameteravp_t *ds=(diameteravp_t *)pds;
210         /* printing results */
211         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);
212 }
213
214
215 static void
216 diameteravp_init(const char *optarg, void* userdata _U_)
217 {
218         diameteravp_t *ds;
219         char* options=NULL;
220         char* saveptr=NULL;
221         char* str=NULL;
222         int field_count=0;
223         int filter_len=0;
224         GString *error_string;
225
226         ds=g_malloc(sizeof(diameteravp_t));
227         ds->frame=0;
228         ds->diammsg_toprocess=0;
229         ds->req_count=0;
230         ds->ans_count=0;
231         ds->paired_ans_count=0;
232         str=NULL;
233         ds->filter=NULL;
234
235         options=g_strdup(optarg);
236         for(str=options;*str;str++)
237         {
238                 if(*str==',')
239                         field_count++;
240         }
241         filter_len=strlen(optarg)+sizeof("diameter")+field_count*sizeof("||diameter.");
242         ds->filter=g_malloc0(filter_len);
243         strcat(ds->filter,"diameter");
244
245         for(str=strtok_s(options+sizeof("diameter,avp"),",",&saveptr);str;str=strtok_s(NULL,",",&saveptr))
246         {
247                 /* Connect all requested fields with logical OR. */
248                 strcat(ds->filter,"||");
249                 /* Prefix field name with "diameter." by default. */
250                 if(!strchr(str,'.'))
251                         strcat(ds->filter,"diameter.");
252                 /* Append field name to the filter. */
253                 strcat(ds->filter,str);
254         }
255         g_free(options);
256
257         error_string=register_tap_listener("diameter", ds, ds->filter, 0, NULL, diameteravp_packet, diameteravp_draw);
258         if(error_string){
259                 /* error, we failed to attach to the tap. clean up */
260                 g_free(ds);
261
262                 fprintf(stderr, "tshark: Couldn't register diam,csv tap: %s\n",
263                                 error_string->str);
264                 g_string_free(error_string, TRUE);
265                 exit(1);
266         }
267 }
268
269
270 void
271 register_tap_listener_diameteravp(void)
272 {
273         register_stat_cmd_arg("diameter,avp", diameteravp_init, NULL);
274 }
275