Fix https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8105 :
[metze/wireshark/wip.git] / ui / gtk / mcast_stream.c
1 /* mcast_stream.c
2  *
3  * Copyright 2006, Iskratel , Slovenia
4  * By Jakob Bratkovic <j.bratkovic@iskratel.si> and
5  * Miha Jemec <m.jemec@iskratel.si>
6  *
7  * $Id$
8  *
9  * based on rtp_stream.c
10  * Copyright 2003, Alcatel Business Systems
11  * By Lars Ruoff <lars.ruoff@gmx.net>
12  *
13  * Wireshark - Network traffic analyzer
14  * By Gerald Combs <gerald@wireshark.org>
15  * Copyright 1998 Gerald Combs
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation,  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30  */
31
32 #include "config.h"
33 #ifdef HAVE_FCNTL_H
34 #include <fcntl.h>
35 #endif
36
37 #include <glib.h>
38
39 #include <stdio.h>
40 #include <time.h>
41 #include <string.h>
42
43 #include <gtk/gtk.h>
44
45 #include <epan/epan.h>
46 #include <epan/address.h>
47 #include <epan/packet.h>
48 #include <epan/tap.h>
49 #include <epan/strutil.h>
50
51 #include "ui/alert_box.h"
52 #include "ui/simple_dialog.h"
53
54 #include "ui/gtk/mcast_stream.h"
55 #include "ui/gtk/mcast_stream_dlg.h"
56 #include "ui/gtk/main.h"
57
58 #ifdef HAVE_WINSOCK2_H
59 #include <winsock2.h>
60 #endif
61
62 gint32  mcast_stream_trigger         =     50; /* limit for triggering the burst alarm (in packets per second) */
63 gint32  mcast_stream_bufferalarm     =  10000; /* limit for triggernig the buffer alarm (in bytes) */
64 guint16 mcast_stream_burstint        =    100; /* burst interval in ms */
65 gint32  mcast_stream_emptyspeed      =   5000; /* outgoing speed for single stream (kbps)*/
66 gint32  mcast_stream_cumulemptyspeed = 100000; /* outgoiong speed for all streams (kbps)*/
67
68 /* sliding window and buffer usage */
69 static gint32  buffsize = (int)((double)MAX_SPEED * 100 / 1000) * 2;
70 static guint16 comparetimes(struct timeval *t1, struct timeval *t2, guint16 burstint_lcl);
71 static void    buffusagecalc(mcast_stream_info_t *strinfo, packet_info *pinfo, double emptyspeed_lcl);
72 static void    slidingwindow(mcast_stream_info_t *strinfo, packet_info *pinfo);
73
74
75 /****************************************************************************/
76 /* the one and only global mcaststream_tapinfo_t structure */
77 static mcaststream_tapinfo_t the_tapinfo_struct =
78         {0, NULL, 0, NULL, 0, FALSE};
79
80
81 /****************************************************************************/
82 /* GCompareFunc style comparison function for _mcast_stream_info */
83 static gint
84 mcast_stream_info_cmp(gconstpointer aa, gconstpointer bb)
85 {
86         const struct _mcast_stream_info* a = aa;
87         const struct _mcast_stream_info* b = bb;
88
89         if (a==b)
90                 return 0;
91         if (a==NULL || b==NULL)
92                 return 1;
93         if (ADDRESSES_EQUAL(&(a->src_addr), &(b->src_addr))
94                 && (a->src_port == b->src_port)
95                 && ADDRESSES_EQUAL(&(a->dest_addr), &(b->dest_addr))
96                 && (a->dest_port == b->dest_port))
97                 return 0;
98         else
99                 return 1;
100
101 }
102
103
104 /****************************************************************************/
105 /* when there is a [re]reading of packet's */
106 void
107 mcaststream_reset(mcaststream_tapinfo_t *tapinfo)
108 {
109         GList* list;
110
111         /* free the data items first */
112         list = g_list_first(tapinfo->strinfo_list);
113         while (list)
114         {
115                 /* XYZ I don't know how to clean this */
116                 /*g_free(list->element.buff); */
117                 g_free(list->data);
118                 list = g_list_next(list);
119         }
120         g_list_free(tapinfo->strinfo_list);
121         tapinfo->strinfo_list = NULL;
122
123         /* XYZ and why does the line below causes a crach? */
124         /*g_free(tapinfo->allstreams->element.buff);*/
125         g_free(tapinfo->allstreams);
126         tapinfo->allstreams = NULL;
127
128         tapinfo->nstreams = 0;
129         tapinfo->npackets = 0;
130
131         ++(tapinfo->launch_count);
132
133         return;
134 }
135
136 static void
137 mcaststream_reset_cb(void *arg)
138 {
139         mcaststream_reset(arg);
140 }
141
142 /****************************************************************************/
143 /* redraw the output */
144 static void
145 mcaststream_draw(void *arg _U_)
146 {
147 /* XXX: see mcaststream_on_update in mcast_streams_dlg.c for comments
148         g_signal_emit_by_name(top_level, "signal_mcaststream_update");
149 */
150         mcaststream_dlg_update(the_tapinfo_struct.strinfo_list);
151         return;
152 }
153
154
155
156 /****************************************************************************/
157 /* whenever a udp packet is seen by the tap listener */
158 static int
159 mcaststream_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *arg2 _U_)
160 {
161         mcaststream_tapinfo_t *tapinfo = arg;
162         mcast_stream_info_t tmp_strinfo;
163         mcast_stream_info_t *strinfo = NULL;
164         GList* list;
165         float deltatime;
166
167         /* gather infos on the stream this packet is part of */
168         COPY_ADDRESS(&(tmp_strinfo.src_addr), &(pinfo->src));
169         tmp_strinfo.src_port = pinfo->srcport;
170         COPY_ADDRESS(&(tmp_strinfo.dest_addr), &(pinfo->dst));
171         tmp_strinfo.dest_port = pinfo->destport;
172
173         /* first we ignore non multicast packets; we filter out only those ethernet packets
174          * which start with the 01:00:5E multicast address (for IPv4) and 33:33 multicast
175          * address (for IPv6).
176          */
177         if ((pinfo->dl_dst.type != AT_ETHER) ||
178             ((g_ascii_strncasecmp("01005E", bytes_to_str(pinfo->dl_dst.data, pinfo->dl_dst.len), 6) != 0) &&
179              (g_ascii_strncasecmp("3333", bytes_to_str(pinfo->dl_dst.data, pinfo->dl_dst.len), 4) != 0)) )
180                 return 0;
181
182         /* check whether we already have a stream with these parameters in the list */
183         list = g_list_first(tapinfo->strinfo_list);
184         while (list)
185         {
186                 if (mcast_stream_info_cmp(&tmp_strinfo, (mcast_stream_info_t*)(list->data))==0)
187                 {
188                         strinfo = (mcast_stream_info_t*)(list->data);  /*found!*/
189                         break;
190                 }
191                 list = g_list_next(list);
192         }
193
194         /* not in the list? then create a new entry */
195         if (!strinfo) {
196                 /*printf("nov sip %s sp %d dip %s dp %d\n", g_strdup(get_addr_name(&(pinfo->src))),
197                         pinfo->srcport, g_strdup(get_addr_name(&(pinfo->dst))), pinfo->destport);*/
198                 tmp_strinfo.npackets = 0;
199                 tmp_strinfo.apackets = 0;
200                 tmp_strinfo.first_frame_num = pinfo->fd->num;
201                 tmp_strinfo.start_sec = (guint32) pinfo->fd->abs_ts.secs;
202                 tmp_strinfo.start_usec = pinfo->fd->abs_ts.nsecs/1000;
203                 tmp_strinfo.start_rel_sec = (guint32) pinfo->fd->rel_ts.secs;
204                 tmp_strinfo.start_rel_usec = pinfo->fd->rel_ts.nsecs/1000;
205                 tmp_strinfo.vlan_id = 0;
206
207                 /* reset Mcast stats */
208                 tmp_strinfo.average_bw = 0;
209                 tmp_strinfo.total_bytes = 0;
210
211                 /* reset slidingwindow and buffer parameters */
212                 tmp_strinfo.element.buff = (struct timeval *)g_malloc(buffsize * sizeof(struct timeval));
213                 tmp_strinfo.element.first=0;
214                 tmp_strinfo.element.last=0;
215                 tmp_strinfo.element.burstsize=1;
216                 tmp_strinfo.element.topburstsize=1;
217                 tmp_strinfo.element.numbursts=0;
218                 tmp_strinfo.element.burststatus=0;
219                 tmp_strinfo.element.count=1;
220                 tmp_strinfo.element.buffusage=pinfo->fd->pkt_len;
221                 tmp_strinfo.element.topbuffusage=pinfo->fd->pkt_len;
222                 tmp_strinfo.element.numbuffalarms=0;
223                 tmp_strinfo.element.buffstatus=0;
224                 tmp_strinfo.element.maxbw=0;
225
226                 strinfo = g_malloc(sizeof(mcast_stream_info_t));
227                 *strinfo = tmp_strinfo;  /* memberwise copy of struct */
228                 tapinfo->strinfo_list = g_list_append(tapinfo->strinfo_list, strinfo);
229                 strinfo->element.buff = (struct timeval *)g_malloc(buffsize * sizeof(struct timeval));
230
231                 /* set time with the first packet */
232                 if (tapinfo->npackets == 0) {
233                         tapinfo->allstreams = g_malloc(sizeof(mcast_stream_info_t));
234                         tapinfo->allstreams->element.buff =
235                                         (struct timeval *)g_malloc(buffsize * sizeof(struct timeval));
236                         tapinfo->allstreams->start_rel_sec = (guint32) pinfo->fd->rel_ts.secs;
237                         tapinfo->allstreams->start_rel_usec = pinfo->fd->rel_ts.nsecs/1000;
238                         tapinfo->allstreams->total_bytes = 0;
239                         tapinfo->allstreams->element.first=0;
240                         tapinfo->allstreams->element.last=0;
241                         tapinfo->allstreams->element.burstsize=1;
242                         tapinfo->allstreams->element.topburstsize=1;
243                         tapinfo->allstreams->element.numbursts=0;
244                         tapinfo->allstreams->element.burststatus=0;
245                         tapinfo->allstreams->element.count=1;
246                         tapinfo->allstreams->element.buffusage=pinfo->fd->pkt_len;
247                         tapinfo->allstreams->element.topbuffusage=pinfo->fd->pkt_len;
248                         tapinfo->allstreams->element.numbuffalarms=0;
249                         tapinfo->allstreams->element.buffstatus=0;
250                         tapinfo->allstreams->element.maxbw=0;
251                 }
252         }
253
254         /* time between first and last packet in the group */
255         strinfo->stop_rel_sec = (guint32) pinfo->fd->rel_ts.secs;
256         strinfo->stop_rel_usec = pinfo->fd->rel_ts.nsecs/1000;
257         deltatime = ((float)((strinfo->stop_rel_sec * 1000000 + strinfo->stop_rel_usec)
258                                         - (strinfo->start_rel_sec*1000000 + strinfo->start_rel_usec)))/1000000;
259
260         /* calculate average bandwidth for this stream */
261         strinfo->total_bytes = strinfo->total_bytes + pinfo->fd->pkt_len;
262         if (deltatime > 0)
263                 strinfo->average_bw = (((float)(strinfo->total_bytes*8) / deltatime) / 1000000);
264
265         /* increment the packets counter for this stream and calculate average pps */
266         ++(strinfo->npackets);
267         strinfo->apackets = (guint32) (strinfo->npackets / deltatime);
268
269         /* time between first and last packet in any group */
270         tapinfo->allstreams->stop_rel_sec = (guint32) pinfo->fd->rel_ts.secs;
271         tapinfo->allstreams->stop_rel_usec = pinfo->fd->rel_ts.nsecs/1000;
272         deltatime = ((float)((tapinfo->allstreams->stop_rel_sec * 1000000 + tapinfo->allstreams->stop_rel_usec)
273                 - (tapinfo->allstreams->start_rel_sec*1000000 + tapinfo->allstreams->start_rel_usec)))/1000000;
274
275         /* increment the packets counter of all streams */
276         ++(tapinfo->npackets);
277
278         /* calculate average bandwidth for all streams */
279         tapinfo->allstreams->total_bytes = tapinfo->allstreams->total_bytes + pinfo->fd->pkt_len;
280         if (deltatime > 0)
281                 tapinfo->allstreams->average_bw = (((float)(tapinfo->allstreams->total_bytes *8) / deltatime) / 1000000);
282
283         /* sliding window and buffercalc for this group*/
284         slidingwindow(strinfo, pinfo);
285         buffusagecalc(strinfo, pinfo, mcast_stream_emptyspeed*1000);
286         /* sliding window and buffercalc for all groups */
287         slidingwindow(tapinfo->allstreams, pinfo);
288         buffusagecalc(tapinfo->allstreams, pinfo, mcast_stream_cumulemptyspeed*1000);
289         /* end of sliding window */
290
291         return 1;  /* refresh output */
292
293 }
294
295 /****************************************************************************/
296 /* scan for Mcast streams */
297 void
298 mcaststream_scan(void)
299 {
300         gboolean was_registered = the_tapinfo_struct.is_registered;
301         if (!the_tapinfo_struct.is_registered)
302                 register_tap_listener_mcast_stream();
303
304         cf_retap_packets(&cfile);
305
306         if (!was_registered)
307                 remove_tap_listener_mcast_stream();
308 }
309
310
311 /****************************************************************************/
312 const mcaststream_tapinfo_t *
313 mcaststream_get_info(void)
314 {
315         return &the_tapinfo_struct;
316 }
317
318
319 /****************************************************************************/
320 /* TAP INTERFACE */
321 /****************************************************************************/
322
323 /****************************************************************************/
324 void
325 remove_tap_listener_mcast_stream(void)
326 {
327         if (the_tapinfo_struct.is_registered) {
328                 remove_tap_listener(&the_tapinfo_struct);
329
330                 the_tapinfo_struct.is_registered = FALSE;
331         }
332 }
333
334
335 /****************************************************************************/
336 void
337 register_tap_listener_mcast_stream(void)
338 {
339         GString *error_string;
340         if (!the_tapinfo_struct.is_registered) {
341                 error_string = register_tap_listener("udp", &the_tapinfo_struct,
342                         NULL, 0, mcaststream_reset_cb, mcaststream_packet,
343                         mcaststream_draw);
344
345                 if (error_string != NULL) {
346                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
347                                       "%s", error_string->str);
348                         g_string_free(error_string, TRUE);
349                         exit(1);
350                 }
351
352                 the_tapinfo_struct.is_registered = TRUE;
353         }
354 }
355
356 /*******************************************************************************/
357 /* sliding window and buffer calculations */
358
359 /* compare two times */
360 static guint16
361 comparetimes(struct timeval *t1, struct timeval *t2, guint16 burstint_lcl)
362 {
363         if(((t2->tv_sec - t1->tv_sec)*1000 + (t2->tv_usec - t1->tv_usec)/1000) > burstint_lcl){
364                 return 1;
365         } else{
366                 return 0;
367         }
368 }
369
370 /* calculate buffer usage */
371 static void
372 buffusagecalc(mcast_stream_info_t *strinfo, packet_info *pinfo, double emptyspeed_lcl)
373 {
374         time_t sec=0;
375         gint32 usec=0, cur, prev;
376         struct timeval *buffer;
377         double timeelapsed;
378
379         buffer = strinfo->element.buff;
380         cur = strinfo->element.last;
381         if(cur == 0){
382                 cur = buffsize - 1;
383                 prev = cur - 1;
384         } else if(cur == 1){
385                 prev = buffsize - 1;
386                 cur = 0;
387         } else{
388                 cur=cur-1;
389                 prev=cur-1;
390         }
391
392         sec = buffer[cur].tv_sec - buffer[prev].tv_sec;
393         usec = buffer[cur].tv_usec - buffer[prev].tv_usec;
394         timeelapsed = (double)usec/1000000 + (double)sec;
395
396         /* bytes added to buffer */
397         strinfo->element.buffusage+=pinfo->fd->pkt_len;
398
399         /* bytes cleared from buffer */
400         strinfo->element.buffusage-= (guint32) (timeelapsed * emptyspeed_lcl / 8);
401
402         if(strinfo->element.buffusage < 0) strinfo->element.buffusage=0;
403         if(strinfo->element.buffusage > strinfo->element.topbuffusage)
404                 strinfo->element.topbuffusage = strinfo->element.buffusage;
405         /* check for buffer losses */
406         if((strinfo->element.buffusage >= mcast_stream_bufferalarm) && (strinfo->element.buffstatus == 0)){
407                 strinfo->element.buffstatus = 1;
408                 strinfo->element.numbuffalarms++;
409         } else if(strinfo->element.buffusage < mcast_stream_bufferalarm){
410                 strinfo->element.buffstatus = 0;
411         }
412
413         return;
414 }
415
416 /* sliding window calculation */
417 static void
418 slidingwindow(mcast_stream_info_t *strinfo, packet_info *pinfo)
419 {
420         struct timeval *buffer;
421         gint32 diff;
422
423         buffer = strinfo->element.buff;
424
425         diff = strinfo->element.last - strinfo->element.first;
426         if(diff < 0) diff+=buffsize;
427
428         /* check if buffer is full */
429         if(diff >= (buffsize - 2)){
430                 fprintf(stderr, "Warning: capture buffer full\n");
431                 strinfo->element.first++;
432                 if(strinfo->element.first >= buffsize) strinfo->element.first = strinfo->element.first % buffsize;
433         }
434
435         /* burst count */
436         buffer[strinfo->element.last].tv_sec = (guint32) pinfo->fd->rel_ts.secs;
437         buffer[strinfo->element.last].tv_usec = pinfo->fd->rel_ts.nsecs/1000;
438         while(comparetimes((struct timeval *)&(buffer[strinfo->element.first]),
439                            (struct timeval *)&(buffer[strinfo->element.last]), mcast_stream_burstint)){
440                 strinfo->element.first++;
441                 if(strinfo->element.first >= buffsize) strinfo->element.first = strinfo->element.first % buffsize;
442                 diff--;
443         }
444         strinfo->element.burstsize = diff;
445         if(strinfo->element.burstsize > strinfo->element.topburstsize) {
446                 strinfo->element.topburstsize = strinfo->element.burstsize;
447                 strinfo->element.maxbw = (float)(strinfo->element.topburstsize) * 1000 / mcast_stream_burstint * pinfo->fd->pkt_len * 8 / 1000000;
448         }
449
450         strinfo->element.last++;
451         if(strinfo->element.last >= buffsize) strinfo->element.last = strinfo->element.last % buffsize;
452         /* trigger check */
453         if((strinfo->element.burstsize >= mcast_stream_trigger) && (strinfo->element.burststatus == 0)){
454                 strinfo->element.burststatus = 1;
455                 strinfo->element.numbursts++;
456         } else if(strinfo->element.burstsize < mcast_stream_trigger){
457                 strinfo->element.burststatus = 0;
458         }
459
460         strinfo->element.count++;
461 }
462