If something is only supposed to be included if we have libpcap, don't
[obnox/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/to_str.h"
59 #include "epan/dissectors/packet-diameter.h"
60
61
62 /* used to keep track of the statistics for an entire program interface */
63 typedef struct _diameteravp_t {
64         guint32 frame;
65         guint32 diammsg_toprocess;
66         guint32 cmd_code;
67         guint32 req_count;
68         guint32 ans_count;
69         guint32 paired_ans_count;
70         gchar* filter;
71 } diameteravp_t;
72
73 /* Copied from proto.c */
74 static gboolean
75 tree_traverse_pre_order(proto_tree *tree, proto_tree_traverse_func func, gpointer data)
76 {
77         proto_node *pnode = tree;
78         proto_node *child;
79         proto_node *current;
80
81         if (func(pnode, data))
82                 return TRUE;
83
84         child = pnode->first_child;
85         while (child != NULL) {
86                 current = child;
87                 child = current->next;
88                 if (tree_traverse_pre_order((proto_tree *)current, func, data))
89                         return TRUE;
90         }
91         return FALSE;
92 }
93
94 static gboolean
95 diam_tree_to_csv(proto_node* node, gpointer data)
96 {
97         char* val_str=NULL;
98         char* val_tmp=NULL;
99         ftenum_t ftype;
100         field_info* fi;
101         header_field_info       *hfi;
102         if(!node) {
103                 fprintf(stderr,"traverse end: empty node. node='%p' data='%p'\n",node,data);
104                 return FALSE;
105         }
106         fi=node->finfo;
107         hfi=fi ? fi->hfinfo : NULL;
108         if(!hfi) {
109                 fprintf(stderr,"traverse end: hfi not found. node='%p'\n",node);
110                 return FALSE;
111         }
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)
116                 {
117                         val_tmp=fvalue_to_string_repr(&fi->value,FTREPR_DISPLAY,NULL);
118                         if(val_tmp)
119                         {
120                                 val_str=ep_strdup(val_tmp);
121                                 g_free(val_tmp);
122                         }
123                 }
124                 if(!val_str)
125                         val_str=ep_strdup_printf("unsuprorted type: %s",ftype_name(ftype));
126
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);
129         }
130         return FALSE;
131 }
132
133 static int
134 diameteravp_packet(void *pds, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pdi)
135 {
136         int ret = 0;
137         double resp_time=0.;
138         gboolean is_request=TRUE;
139         guint32 cmd_code=0;
140         guint32 result_code=0;
141         guint32 req_frame=0;
142         guint32 ans_frame=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;
150
151         /* Validate paramerers. */
152         if(!dp || !edt || !edt->tree)
153                 return ret;
154
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;
161         } else {
162                         ds->diammsg_toprocess+=1;
163         }
164
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;
171         if(!is_request) {
172                 nstime_t ns;
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;
176         }
177
178         /* Check command code provided by command line option.*/
179         if (ds->cmd_code && ds->cmd_code!=cmd_code)
180                 return ret;
181
182         /* Loop over top level nodes */
183         node = edt->tree->first_child;
184         while (node != NULL) {
185                 current = node;
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) {
194                                 if(is_request) {
195                                         ds->req_count++;
196                                 } else {
197                                         ds->ans_count++;
198                                         if (req_frame>0)
199                                                 ds->paired_ans_count++;
200                                 }
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);
206                                 /* End of message.*/
207                                 printf("\n");
208                                 /*printf("hfi: name='%s', msg_curr='%d' abbrev='%s',type='%s'\n",hfi->name,diam_child_node,hfi->abbrev,ftype_name(hfi->type));*/
209                         }
210                         diam_child_node++;
211                 }
212         }
213         return ret;
214 }
215
216 static void
217 diameteravp_draw(void* pds)
218 {
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);
222 }
223
224
225 static void
226 diameteravp_init(const char *optarg, void* userdata _U_)
227 {
228         diameteravp_t *ds;
229         gchar* field=NULL;
230         gchar** tokens;
231         guint opt_count=0;
232         guint opt_idx=0;
233         GString* filter=NULL;
234         GString* error_string=NULL;
235
236         ds=g_malloc(sizeof(diameteravp_t));
237         ds->frame=0;
238         ds->diammsg_toprocess=0;
239         ds->cmd_code=0;
240         ds->req_count=0;
241         ds->ans_count=0;
242         ds->paired_ans_count=0;
243         ds->filter=NULL;
244
245         filter=g_string_new("diameter");
246
247         /* Split command line options. */
248         tokens = g_strsplit(optarg,",", 1024);
249         opt_count=0;
250         while (tokens[opt_count])
251                 opt_count++;
252         if (opt_count>2)
253                 ds->cmd_code=(guint32)atoi(tokens[2]);
254
255         /* Loop over diameter field names. */
256         for(opt_idx=3;opt_idx<opt_count;opt_idx++)
257         {
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);
267         }
268         g_strfreev(tokens);
269         ds->filter=g_string_free(filter,FALSE);
270
271         error_string=register_tap_listener("diameter", ds, ds->filter, 0, NULL, diameteravp_packet, diameteravp_draw);
272         if(error_string){
273                 /* error, we failed to attach to the tap. clean up */
274                 g_free(ds);
275
276                 fprintf(stderr, "tshark: Couldn't register diam,csv tap: %s\n",
277                                 error_string->str);
278                 g_string_free(error_string, TRUE);
279                 exit(1);
280         }
281 }
282
283
284 void
285 register_tap_listener_diameteravp(void)
286 {
287         register_stat_cmd_arg("diameter,avp", diameteravp_init, NULL);
288 }
289