c4ab0456448c004dcff85437efcbe2c46ba402b7
[obnox/wireshark/wip.git] / 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 #ifdef HAVE_FCNTL_H
36 #include <fcntl.h>
37 #endif
38
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
42
43 #include <time.h>
44 #include <string.h>
45
46 #include <gtk/gtk.h>
47
48 #include <epan/epan.h>
49 #include <epan/packet.h>
50 #include <epan/tap.h>
51 #include <epan/strutil.h>
52
53 #include "../globals.h"
54 #include "../register.h"
55 #include "../alert_box.h"
56 #include "../simple_dialog.h"
57
58 #include "gtk/mcast_stream.h"
59 #include "gtk/mcast_stream_dlg.h"
60 #include "gtk/main.h"
61
62
63
64 gint32 trigger=50; /* limit for triggering the burst alarm (in packets per second) */
65 gint32 bufferalarm = 10000; /* limit for triggernig the buffer alarm (in bytes) */
66 guint16 burstint = 100; /* burts interval in ms */
67 gint32 emptyspeed = 5000; /* outgoing speed for single stream (kbps)*/
68 gint32 cumulemptyspeed = 100000; /* outgoiong speed for all streams (kbps)*/
69
70 t_buffer **bufflist;
71
72 /* sliding window and buffer usage */
73 gint32 buffsize = (int)((double)MAX_SPEED * 100 / 1000) * 2;
74 guint16 comparetimes(struct timeval *t1, struct timeval *t2, guint16 burstint);
75 static void buffusagecalc(mcast_stream_info_t *strinfo, packet_info *pinfo, double emptyspeed);
76 static void slidingwindow(mcast_stream_info_t *strinfo, packet_info *pinfo);
77
78
79 /****************************************************************************/
80 /* the one and only global mcaststream_tapinfo_t structure */
81 static mcaststream_tapinfo_t the_tapinfo_struct =
82         {0, NULL, 0, NULL, 0, FALSE};
83
84
85 /****************************************************************************/
86 /* GCompareFunc style comparison function for _mcast_stream_info */
87 static gint mcast_stream_info_cmp(gconstpointer aa, gconstpointer bb)
88 {
89         const struct _mcast_stream_info* a = aa;
90         const struct _mcast_stream_info* b = bb;
91
92         if (a==b)
93                 return 0;
94         if (a==NULL || b==NULL)
95                 return 1;
96         if (ADDRESSES_EQUAL(&(a->src_addr), &(b->src_addr))
97                 && (a->src_port == b->src_port)
98                 && ADDRESSES_EQUAL(&(a->dest_addr), &(b->dest_addr))
99                 && (a->dest_port == b->dest_port))
100                 return 0;
101         else
102                 return 1;
103
104 }
105
106
107 /****************************************************************************/
108 /* when there is a [re]reading of packet's */
109 void mcaststream_reset(mcaststream_tapinfo_t *tapinfo)
110 {
111         GList* list;
112
113         /* free the data items first */
114         list = g_list_first(tapinfo->strinfo_list);
115         while (list)
116         {
117                 /* XYZ I don't know how to clean this */
118                 /*g_free(list->element.buff); */
119                 g_free(list->data);
120                 list = g_list_next(list);
121         }
122         g_list_free(tapinfo->strinfo_list);
123         tapinfo->strinfo_list = NULL;
124
125         /* XYZ and why does the line below causes a crach? */
126         /*g_free(tapinfo->allstreams->element.buff);*/
127         g_free(tapinfo->allstreams);
128         tapinfo->allstreams = NULL;
129
130         tapinfo->nstreams = 0;
131         tapinfo->npackets = 0;
132
133         ++(tapinfo->launch_count);
134
135         return;
136 }
137
138 static void mcaststream_reset_cb(void *arg)
139 {
140         mcaststream_reset(arg);
141 }
142
143 /****************************************************************************/
144 /* redraw the output */
145 static void 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 mcaststream_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *arg2 _U_)
159 {
160         mcaststream_tapinfo_t *tapinfo = arg;
161         mcast_stream_info_t tmp_strinfo;
162         mcast_stream_info_t *strinfo = NULL;
163         GList* list;
164         float deltatime;
165
166         /* gather infos on the stream this packet is part of */
167         COPY_ADDRESS(&(tmp_strinfo.src_addr), &(pinfo->src));
168         tmp_strinfo.src_port = pinfo->srcport;
169         COPY_ADDRESS(&(tmp_strinfo.dest_addr), &(pinfo->dst));
170         tmp_strinfo.dest_port = pinfo->destport;
171
172         /* first we ignore non multicast packets; we filter out only those ethernet packets
173          * which start with the 01:00:5E multicast address (for IPv4) and 33:33 multicast
174          * address (for IPv6).
175          */
176         if ((pinfo->dl_dst.type != AT_ETHER) ||
177             ((strncmp("01005E", bytes_to_str(pinfo->dl_dst.data, pinfo->dl_dst.len), 6) != 0) &&
178              (strncmp("3333", bytes_to_str(pinfo->dl_dst.data, pinfo->dl_dst.len), 4) != 0)) )
179                 return 0;
180
181         /* check wether we already have a stream with these parameters in the list */
182         list = g_list_first(tapinfo->strinfo_list);
183         while (list)
184         {
185                 if (mcast_stream_info_cmp(&tmp_strinfo, (mcast_stream_info_t*)(list->data))==0)
186                 {
187                         strinfo = (mcast_stream_info_t*)(list->data);  /*found!*/
188                         break;
189                 }
190                 list = g_list_next(list);
191         }
192
193         /* not in the list? then create a new entry */
194         if (!strinfo) {
195                 /*printf("nov sip %s sp %d dip %s dp %d\n", g_strdup(get_addr_name(&(pinfo->src))),
196                         pinfo->srcport, g_strdup(get_addr_name(&(pinfo->dst))), pinfo->destport);*/
197                 tmp_strinfo.npackets = 0;
198                 tmp_strinfo.apackets = 0;
199                 tmp_strinfo.first_frame_num = pinfo->fd->num;
200                 tmp_strinfo.start_sec = (guint32) pinfo->fd->abs_ts.secs;
201                 tmp_strinfo.start_usec = pinfo->fd->abs_ts.nsecs/1000;
202                 tmp_strinfo.start_rel_sec = (guint32) pinfo->fd->rel_ts.secs;
203                 tmp_strinfo.start_rel_usec = pinfo->fd->rel_ts.nsecs/1000;
204                 tmp_strinfo.vlan_id = 0;
205
206                 /* reset Mcast stats */
207                 tmp_strinfo.average_bw = 0;
208                 tmp_strinfo.total_bytes = 0;
209
210                 /* reset slidingwindow and buffer parameters */
211                 tmp_strinfo.element.buff = (struct timeval *)g_malloc(buffsize * sizeof(struct timeval));
212                 tmp_strinfo.element.first=0;
213                 tmp_strinfo.element.last=0;
214                 tmp_strinfo.element.burstsize=1;
215                 tmp_strinfo.element.topburstsize=1;
216                 tmp_strinfo.element.numbursts=0;
217                 tmp_strinfo.element.burststatus=0;
218                 tmp_strinfo.element.count=1;
219                 tmp_strinfo.element.buffusage=pinfo->fd->pkt_len;
220                 tmp_strinfo.element.topbuffusage=pinfo->fd->pkt_len;
221                 tmp_strinfo.element.numbuffalarms=0;
222                 tmp_strinfo.element.buffstatus=0;
223                 tmp_strinfo.element.maxbw=0;
224
225                 strinfo = g_malloc(sizeof(mcast_stream_info_t));
226                 *strinfo = tmp_strinfo;  /* memberwise copy of struct */
227                 tapinfo->strinfo_list = g_list_append(tapinfo->strinfo_list, strinfo);
228                 strinfo->element.buff = (struct timeval *)g_malloc(buffsize * sizeof(struct timeval));
229
230                 /* set time with the first packet */
231                 if (tapinfo->npackets == 0) {
232                         tapinfo->allstreams = g_malloc(sizeof(mcast_stream_info_t));
233                         tapinfo->allstreams->element.buff =
234                                         (struct timeval *)g_malloc(buffsize * sizeof(struct timeval));
235                         tapinfo->allstreams->start_rel_sec = (guint32) pinfo->fd->rel_ts.secs;
236                         tapinfo->allstreams->start_rel_usec = pinfo->fd->rel_ts.nsecs/1000;
237                         tapinfo->allstreams->total_bytes = 0;
238                         tapinfo->allstreams->element.first=0;
239                         tapinfo->allstreams->element.last=0;
240                         tapinfo->allstreams->element.burstsize=1;
241                         tapinfo->allstreams->element.topburstsize=1;
242                         tapinfo->allstreams->element.numbursts=0;
243                         tapinfo->allstreams->element.burststatus=0;
244                         tapinfo->allstreams->element.count=1;
245                         tapinfo->allstreams->element.buffusage=pinfo->fd->pkt_len;
246                         tapinfo->allstreams->element.topbuffusage=pinfo->fd->pkt_len;
247                         tapinfo->allstreams->element.numbuffalarms=0;
248                         tapinfo->allstreams->element.buffstatus=0;
249                         tapinfo->allstreams->element.maxbw=0;
250                 }
251         }
252
253         /* time between first and last packet in the group */
254         strinfo->stop_rel_sec = (guint32) pinfo->fd->rel_ts.secs;
255         strinfo->stop_rel_usec = pinfo->fd->rel_ts.nsecs/1000;
256         deltatime = ((float)((strinfo->stop_rel_sec * 1000000 + strinfo->stop_rel_usec)
257                                         - (strinfo->start_rel_sec*1000000 + strinfo->start_rel_usec)))/1000000;
258
259         /* calculate average bandwidth for this stream */
260         strinfo->total_bytes = strinfo->total_bytes + pinfo->fd->pkt_len;
261         if (deltatime > 0)
262                 strinfo->average_bw = (((float)(strinfo->total_bytes*8) / deltatime) / 1000000);
263
264         /* increment the packets counter for this stream and calculate average pps */
265         ++(strinfo->npackets);
266         strinfo->apackets = (guint32) (strinfo->npackets / deltatime);
267
268         /* time between first and last packet in any group */
269         tapinfo->allstreams->stop_rel_sec = (guint32) pinfo->fd->rel_ts.secs;
270         tapinfo->allstreams->stop_rel_usec = pinfo->fd->rel_ts.nsecs/1000;
271         deltatime = ((float)((tapinfo->allstreams->stop_rel_sec * 1000000 + tapinfo->allstreams->stop_rel_usec)
272                 - (tapinfo->allstreams->start_rel_sec*1000000 + tapinfo->allstreams->start_rel_usec)))/1000000;
273
274         /* increment the packets counter of all streams */
275         ++(tapinfo->npackets);
276
277         /* calculate average bandwidth for all streams */
278         tapinfo->allstreams->total_bytes = tapinfo->allstreams->total_bytes + pinfo->fd->pkt_len;
279         if (deltatime > 0)
280                 tapinfo->allstreams->average_bw = (((float)(tapinfo->allstreams->total_bytes *8) / deltatime) / 1000000);
281
282         /* sliding window and buffercalc for this group*/
283         slidingwindow(strinfo, pinfo);
284         buffusagecalc(strinfo, pinfo, emptyspeed*1000);
285         /* sliding window and buffercalc for all groups */
286         slidingwindow(tapinfo->allstreams, pinfo);
287         buffusagecalc(tapinfo->allstreams, pinfo, cumulemptyspeed*1000);
288         /* end of sliding window */
289
290         return 1;  /* refresh output */
291
292 }
293
294 /****************************************************************************/
295 /* scan for Mcast streams */
296 void mcaststream_scan(void)
297 {
298         gboolean was_registered = the_tapinfo_struct.is_registered;
299         if (!the_tapinfo_struct.is_registered)
300                 register_tap_listener_mcast_stream();
301
302         cf_retap_packets(&cfile, FALSE);
303
304         if (!was_registered)
305                 remove_tap_listener_mcast_stream();
306 }
307
308
309 /****************************************************************************/
310 const mcaststream_tapinfo_t* mcaststream_get_info(void)
311 {
312         return &the_tapinfo_struct;
313 }
314
315
316 /****************************************************************************/
317 /* TAP INTERFACE */
318 /****************************************************************************/
319
320 /****************************************************************************/
321 void
322 remove_tap_listener_mcast_stream(void)
323 {
324         if (the_tapinfo_struct.is_registered) {
325                 protect_thread_critical_region();
326                 remove_tap_listener(&the_tapinfo_struct);
327                 unprotect_thread_critical_region();
328
329                 the_tapinfo_struct.is_registered = FALSE;
330         }
331 }
332
333
334 /****************************************************************************/
335 void
336 register_tap_listener_mcast_stream(void)
337 {
338         GString *error_string;
339         if (!the_tapinfo_struct.is_registered) {
340                 error_string = register_tap_listener("udp", &the_tapinfo_struct,
341                         NULL, mcaststream_reset_cb, mcaststream_packet,
342                         mcaststream_draw);
343
344                 if (error_string != NULL) {
345                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
346                                       error_string->str);
347                         g_string_free(error_string, TRUE);
348                         exit(1);
349                 }
350
351                 the_tapinfo_struct.is_registered = TRUE;
352         }
353 }
354
355 /*******************************************************************************/
356 /* sliding window and buffer calculations */
357
358 /* compare two times */
359 guint16 comparetimes(struct timeval *t1, struct timeval *t2, guint16 burstint){
360     if(((t2->tv_sec - t1->tv_sec)*1000 + (t2->tv_usec - t1->tv_usec)/1000) > burstint){
361         return 1;
362     } else{
363         return 0;
364     }
365 }
366
367 /* calculate buffer usage */
368 void buffusagecalc(mcast_stream_info_t *strinfo, packet_info *pinfo, double emptyspeed)
369 {
370     gint32 sec=0, usec=0, cur, prev;
371     struct timeval *buffer;
372     double timeelapsed;
373
374     buffer = strinfo->element.buff;
375     cur = strinfo->element.last;
376     if(cur == 0){
377         cur = buffsize - 1;
378         prev = cur - 1;
379     } else if(cur == 1){
380         prev = buffsize - 1;
381         cur = 0;
382     } else{
383         cur=cur-1;
384         prev=cur-1;
385     }
386
387     sec = buffer[cur].tv_sec - buffer[prev].tv_sec;
388     usec = buffer[cur].tv_usec - buffer[prev].tv_usec;
389     timeelapsed = (double)usec/1000000 + (double)sec;
390
391     /* bytes added to buffer */
392     strinfo->element.buffusage+=pinfo->fd->pkt_len;
393
394     /* bytes cleared from buffer */
395     strinfo->element.buffusage-= (guint32) (timeelapsed * emptyspeed / 8);
396
397     if(strinfo->element.buffusage < 0) strinfo->element.buffusage=0;
398     if(strinfo->element.buffusage > strinfo->element.topbuffusage)
399                                 strinfo->element.topbuffusage = strinfo->element.buffusage;
400     /* check for buffer losses */
401     if((strinfo->element.buffusage >= bufferalarm) && (strinfo->element.buffstatus == 0)){
402         strinfo->element.buffstatus = 1;
403         strinfo->element.numbuffalarms++;
404     } else if(strinfo->element.buffusage < bufferalarm){
405         strinfo->element.buffstatus = 0;
406     }
407
408     return;
409 }
410
411 /* sliding window calculation */
412 void slidingwindow(mcast_stream_info_t *strinfo, packet_info *pinfo)
413 {
414     struct timeval *buffer;
415     gint32 diff;
416
417     buffer = strinfo->element.buff;
418
419     diff = strinfo->element.last - strinfo->element.first;
420     if(diff < 0) diff+=buffsize;
421
422     /* check if buffer is full */
423     if(diff >= (buffsize - 2)){
424         fprintf(stderr, "Warning: capture buffer full\n");
425         strinfo->element.first++;
426         if(strinfo->element.first >= buffsize) strinfo->element.first = strinfo->element.first % buffsize;
427     }
428
429     /* burst count */
430     buffer[strinfo->element.last].tv_sec = (guint32) pinfo->fd->rel_ts.secs;
431     buffer[strinfo->element.last].tv_usec = pinfo->fd->rel_ts.nsecs/1000;
432     while(comparetimes((struct timeval *)&(buffer[strinfo->element.first]),
433                                                 (struct timeval *)&(buffer[strinfo->element.last]), burstint)){
434         strinfo->element.first++;
435         if(strinfo->element.first >= buffsize) strinfo->element.first = strinfo->element.first % buffsize;
436         diff--;
437     }
438     strinfo->element.burstsize = diff;
439     if(strinfo->element.burstsize > strinfo->element.topburstsize) {
440         strinfo->element.topburstsize = strinfo->element.burstsize;
441         strinfo->element.maxbw = (float)(strinfo->element.topburstsize) * 1000 / burstint * pinfo->fd->pkt_len * 8 / 1000000;
442     }
443
444     strinfo->element.last++;
445     if(strinfo->element.last >= buffsize) strinfo->element.last = strinfo->element.last % buffsize;
446     /* trigger check */
447     if((strinfo->element.burstsize >= trigger) && (strinfo->element.burststatus == 0)){
448         strinfo->element.burststatus = 1;
449         strinfo->element.numbursts++;
450     } else if(strinfo->element.burstsize < trigger){
451         strinfo->element.burststatus = 0;
452     }
453
454     strinfo->element.count++;
455 }
456