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