ANNOUNCEMENT: Live Wireshark University & Allegro Packets online APAC Wireshark Training Session
April 17th, 2024 | 14:30-16:00 SGT (UTC+8) | Online

Wireshark-dev: [Wireshark-dev] MPEG 2 TS Statistics Menu

From: Shaan Ahmed <frahmed99@xxxxxxxxxxx>
Date: Thu, 19 Feb 2009 09:26:27 +0000
Hi I m trying to make a wireshark statistics menu like that of the 'WLAN Traffic' under the statistics menu. My code has been compiling but I am unable to see the menu appear once complied. I am running it on Ubuntu. This is the code, the main packet tapping code is not important right now. All I want right now is the button appearing on the statistics menu.

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <gtk/gtk.h>

#include <epan/packet_info.h>
#include <epan/epan.h>

#include <epan/stat_cmd_args.h>
#include "../stat_menu.h"
#include "gui_stat_menu.h"
#include <epan/tap.h>
#include "../register.h"
#include "../globals.h"
#include "gui_utils.h"
#include "dlg_utils.h"
#include "compat_macros.h"

static GtkWidget *win=NULL;
static GtkWidget *table=NULL;
static int num_progs=0;

/* used to keep track of statistics for a specific program/version */
typedef struct _mp2t_program_t {
    struct _mp2t_program_t *next;
    guint32 program;
    GtkWidget *wprogram;
    gchar sprogram[24];

    guint32 version;
    GtkWidget *wversion;
    gchar sversion[16];

    int num;
    GtkWidget *wnum;
    gchar snum[16];

    nstime_t min;
    GtkWidget *wmin;
    gchar smin[16];

    nstime_t max;
    GtkWidget *wmax;
    gchar smax[16];

    nstime_t tot;
    GtkWidget *wavg;
    gchar savg[16];
} mp2t_program_t;
static mp2t_program_t *prog_list=NULL;

static char *
mp2tstat_gen_title(void)
{
    char *title;

    title = g_strdup_printf("MPEG 2 TS Statistics: %s",
        cf_get_display_name(&cfile));
    return title;
}

static void
mp2tstat_reset(void *dummy _U_)
{
    mp2t_program_t *rp;

    while(prog_list){
        rp=prog_list;
        prog_list=prog_list->next;

        gtk_widget_destroy(rp->wprogram);
        rp->wprogram=NULL;
        gtk_widget_destroy(rp->wversion);
        rp->wversion=NULL;
        gtk_widget_destroy(rp->wnum);
        rp->wnum=NULL;
        gtk_widget_destroy(rp->wmin);
        rp->wmin=NULL;
        gtk_widget_destroy(rp->wmax);
        rp->wmax=NULL;
        gtk_widget_destroy(rp->wavg);
        rp->wavg=NULL;
        g_free(rp);
    }
    gtk_table_resize(GTK_TABLE(table), 1, 6);
    num_progs=0;
}

static void
add_new_program(mp2t_program_t *rp)
{
    num_progs++;
    gtk_table_resize(GTK_TABLE(table), num_progs+1, 6);
    rp->wprogram=gtk_label_new("0");
    gtk_table_attach_defaults(GTK_TABLE(table), rp->wprogram, 0,1,num_progs,num_progs+1);
    gtk_widget_show(rp->wprogram);
    rp->wversion=gtk_label_new("0");
    gtk_table_attach_defaults(GTK_TABLE(table), rp->wversion, 1,2,num_progs,num_progs+1);
    gtk_widget_show(rp->wversion);
    rp->wnum=gtk_label_new("0");
    gtk_table_attach_defaults(GTK_TABLE(table), rp->wnum, 2,3,num_progs,num_progs+1);
    gtk_widget_show(rp->wnum);
    rp->wmin=gtk_label_new("0");
    gtk_table_attach_defaults(GTK_TABLE(table), rp->wmin, 3,4,num_progs,num_progs+1);
    gtk_widget_show(rp->wmin);
    rp->wmax=gtk_label_new("0");
    gtk_table_attach_defaults(GTK_TABLE(table), rp->wmax, 4,5,num_progs,num_progs+1);
    gtk_widget_show(rp->wmax);
    rp->wavg=gtk_label_new("0");
    gtk_table_attach_defaults(GTK_TABLE(table), rp->wavg, 5,6,num_progs,num_progs+1);
    gtk_widget_show(rp->wavg);

    rp->num=0;
    rp->min.secs=0;
    rp->min.nsecs=0;
    rp->max.secs=0;
    rp->max.nsecs=0;
    rp->tot.secs=0;
    rp->tot.nsecs=0;
}

static int
mp2tstat_packet()
{
    return 1;
}

static void
mp2tstat_draw(void *dummy _U_)
{
    mp2t_program_t *rp;
    int i;
#ifdef G_HAVE_UINT64
    guint64 td;
#else
    guint32 td;
#endif

    for(rp=prog_list,i=1;rp;rp=rp->next,i++){
        /* scale it to units of 10us.*/
        /* for long captures with a large tot time, this can overflow on 32bit */
        td=(int)rp->tot.secs;
        td=td*100000+(int)rp->tot.nsecs/10000;
        if(rp->num){
            td/=rp->num;
        } else {
            td=0;
        }

        g_snprintf(rp->sprogram, sizeof(rp->sprogram), "%s", mp2t_prog_name(rp->program));
        gtk_label_set_text(GTK_LABEL(rp->wprogram), rp->sprogram);

        g_snprintf(rp->sversion, sizeof(rp->sversion), "%d",rp->version);
        gtk_label_set_text(GTK_LABEL(rp->wversion), rp->sversion);

        g_snprintf(rp->snum, sizeof(rp->snum), "%d",rp->num);
        gtk_label_set_text(GTK_LABEL(rp->wnum), rp->snum);

        g_snprintf(rp->smin, sizeof(rp->smin), "%3d.%05d",(int)rp->min.secs,(int)rp->min.nsecs/10000);
        gtk_label_set_text(GTK_LABEL(rp->wmin), rp->smin);

        g_snprintf(rp->smax, sizeof(rp->smax), "%3d.%05d",(int)rp->max.secs,(int)rp->max.nsecs/10000);
        gtk_label_set_text(GTK_LABEL(rp->wmax), rp->smax);

        g_snprintf(rp->savg, sizeof(rp->savg), "%3d.%05d",(int)td/100000,(int)td%100000);
        gtk_label_set_text(GTK_LABEL(rp->wavg), rp->savg);

    }
}
/* since the gtk2 implementation of tap is multithreaded we must protect
 * remove_tap_listener() from modifying the list while draw_tap_listener()
 * is running.  the other protected block is in main.c
 *
 * there should not be any other critical regions in gtk2
 */
void protect_thread_critical_region(void);
void unprotect_thread_critical_region(void);

win_destroy_cb(void *dummy _U_, gpointer data _U_)
{
    mp2t_program_t *rp, *rp2;

    protect_thread_critical_region();
    remove_tap_listener(win);
    unprotect_thread_critical_region();

    win=NULL;
    for(rp=prog_list;rp;){
        rp2=rp->next;
        g_free(rp);
        rp=rp2;
    }
    prog_list=NULL;
}

/* When called, this function will start mp2ts Program*/
static void
gtk_mp2t_init(const char *optarg _U_, void* userdata _U_)
{
    char *title_string;
    GtkWidget *vbox;
    GtkWidget *stat_label;
    GtkWidget *tmp;
    GString *error_string;
    GtkWidget *bt_close;
    GtkWidget *bbox;

    if(win){
        gdk_window_raise(win->window);
        return;
    }

    title_string = mp2tprogs_gen_title();
    win=window_new(GTK_WINDOW_TOPLEVEL, title_string);

    vbox=gtk_vbox_new(FALSE, 3);
    gtk_container_add(GTK_CONTAINER(win), vbox);
    gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);

    stat_label=gtk_label_new(title_string);
    g_free(title_string);
    gtk_box_pack_start(GTK_BOX(vbox), stat_label, FALSE, FALSE, 0);


    table=gtk_table_new(1, 5, TRUE);
    gtk_container_add(GTK_CONTAINER(vbox), table);

    tmp=gtk_label_new("ONE");
    gtk_table_attach_defaults(GTK_TABLE(table), tmp, 0,1,0,1);
    gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);

    tmp=gtk_label_new("TWO");
    gtk_table_attach_defaults(GTK_TABLE(table), tmp, 1,2,0,1);
    gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_RIGHT);

    tmp=gtk_label_new("THREE");
    gtk_table_attach_defaults(GTK_TABLE(table), tmp, 2,3,0,1);
    gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_RIGHT);

    tmp=gtk_label_new("FOUR");
    gtk_table_attach_defaults(GTK_TABLE(table), tmp, 3,4,0,1);
    gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_RIGHT);

    tmp=gtk_label_new("FIVE");
    gtk_table_attach_defaults(GTK_TABLE(table), tmp, 4,5,0,1);
    gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_RIGHT);

    tmp=gtk_label_new("SIX");
    gtk_table_attach_defaults(GTK_TABLE(table), tmp, 5,6,0,1);
    gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_RIGHT);

    error_string=register_tap_listener("mp2t", win, NULL, mp2tstat_reset, mp2tstat_packet, mp2tstat_draw);
    if(error_string){
        fprintf(stderr, "wireshark: Couldn't register mpeg 2 programs tap: %s\n",
            error_string->str);
        g_string_free(error_string, TRUE);
        exit(1);
    }

    /* Button row. */
    bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
    gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);

    bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
    window_set_cancel_button(win, bt_close, window_cancel_button_cb);

    SIGNAL_CONNECT(win, "delete_event", window_delete_event_cb, NULL);
    SIGNAL_CONNECT(win, "destroy", win_destroy_cb, win);

    gtk_widget_show_all(win);
    window_present(win);
   
    cf_retap_packets(&cfile, FALSE);
}


static void
gtk_mp2t_cb(GtkWidget *w _U_, gpointer d _U_)
{
    gtk_mp2t_init("",NULL);
}

void
register_tap_listener_mp2tstat(void)
{
    register_stat_menu_item("MPEG2 TS", REGISTER_STAT_GROUP_NONE,
        0, NULL, NULL, NULL);
}



Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy! Try it!