ncurses: refactored msg diff win functions

This commit is contained in:
Kaian 2018-11-19 14:00:19 +01:00
parent 056728b95d
commit 7d9a842788
10 changed files with 154 additions and 171 deletions

View File

@ -39,7 +39,7 @@ set(SOURCES
src/ncurses/call_raw_win.c
src/ncurses/column_select_win.c
src/ncurses/filter_win.c
src/ncurses/ui_msg_diff.c
src/ncurses/msg_diff_win.c
src/ncurses/ui_save.c
src/ncurses/ui_settings.c
src/ncurses/stats_win.c

View File

@ -35,7 +35,7 @@
#include "ncurses/manager.h"
#include "ncurses/call_flow_win.h"
#include "ncurses/call_raw_win.h"
#include "ncurses/ui_msg_diff.h"
#include "ncurses/msg_diff_win.h"
#include "ncurses/ui_save.h"
#include "timeval.h"
#include "glib-utils.h"
@ -1367,7 +1367,7 @@ static int
call_flow_handle_key(Window *window, int key)
{
int raw_width;
Window *next_ui, *next_window;
Window *next_window;
Call *call = NULL;
guint rnpag_steps = (guint) setting_get_intvalue(SETTING_CF_SCROLLSTEP);
int action = -1;
@ -1472,10 +1472,9 @@ call_flow_handle_key(Window *window, int key)
dialog_run("Saving is not possible when multiple input sources are specified.");
break;
}
next_ui = ncurses_create_window(PANEL_SAVE);
save_set_group(next_ui, info->group);
save_set_msg(next_ui,
call_flow_arrow_message(g_ptr_array_index(info->darrows, info->cur_idx)));
next_window = ncurses_create_window(WINDOW_SAVE);
save_set_group(next_window, info->group);
save_set_msg(next_window, call_flow_arrow_message(g_ptr_array_index(info->darrows, info->cur_idx)));
break;
case ACTION_TOGGLE_TIME:
info->arrowtime = (info->arrowtime) ? false : true;
@ -1488,10 +1487,10 @@ call_flow_handle_key(Window *window, int key)
info->selected = -1;
} else {
// Show diff panel
next_ui = ncurses_create_window(PANEL_MSG_DIFF);
msg_diff_set_msgs(next_ui,
call_flow_arrow_message(g_ptr_array_index(info->darrows, info->selected)),
call_flow_arrow_message(g_ptr_array_index(info->darrows, info->cur_idx)));
next_window = ncurses_create_window(WINDOW_MSG_DIFF);
msg_diff_win_set_msgs(next_window,
call_flow_arrow_message(g_ptr_array_index(info->darrows, info->selected)),
call_flow_arrow_message(g_ptr_array_index(info->darrows, info->cur_idx)));
}
}
break;
@ -1503,7 +1502,7 @@ call_flow_handle_key(Window *window, int key)
next_window = ncurses_create_window(WINDOW_CALL_RAW);
call_raw_win_set_group(next_window, info->group);
call_raw_win_set_msg(next_window,
call_flow_arrow_message(g_ptr_array_index(info->darrows, info->cur_idx)));
call_flow_arrow_message(g_ptr_array_index(info->darrows, info->cur_idx)));
break;
case ACTION_CLEAR_CALLS:
case ACTION_CLEAR_CALLS_SOFT:

View File

@ -931,7 +931,7 @@ call_list_handle_key(Window *window, int key)
dialog_run("Saving is not possible when multiple input sources are specified.");
break;
}
save_set_group(ncurses_create_window(PANEL_SAVE), info->group);
save_set_group(ncurses_create_window(WINDOW_SAVE), info->group);
break;
case ACTION_CLEAR:
// Clear group calls
@ -953,7 +953,7 @@ call_list_handle_key(Window *window, int key)
info->autoscroll = (info->autoscroll) ? 0 : 1;
break;
case ACTION_SHOW_SETTINGS:
ncurses_create_window(PANEL_SETTINGS);
ncurses_create_window(WINDOW_SETTINGS);
break;
case ACTION_SELECT:
call = g_ptr_array_index(info->dcalls, info->cur_idx);

View File

@ -282,7 +282,7 @@ call_raw_handle_key(Window *window, int key)
}
if (info->group) {
// KEY_S, Display save panel
next_ui = ncurses_create_window(PANEL_SAVE);
next_ui = ncurses_create_window(WINDOW_SAVE);
save_set_group(next_ui, info->group);
}
break;

View File

@ -40,7 +40,7 @@
#include "ncurses/call_flow_win.h"
#include "ncurses/call_raw_win.h"
#include "ncurses/filter_win.h"
#include "ncurses/ui_msg_diff.h"
#include "ncurses/msg_diff_win.h"
#include "ncurses/column_select_win.h"
#include "ncurses/stats_win.h"
#include "ncurses/ui_save.h"
@ -221,6 +221,9 @@ ncurses_find_by_type(enum WindowTypes type)
case WINDOW_FILTER:
window = filter_win_new();
break;
case WINDOW_MSG_DIFF:
window = msg_diff_win_new();
break;
default: break;
}
@ -331,7 +334,7 @@ ncurses_default_keyhandler(Window *window, int key)
setting_toggle(SETTING_DISPLAY_ALIAS);
break;
case ACTION_SHOW_SETTINGS:
ncurses_create_window(PANEL_SETTINGS);
ncurses_create_window(WINDOW_SETTINGS);
break;
case ACTION_TOGGLE_PAUSE:
// Pause/Resume capture

View File

@ -28,7 +28,7 @@
*/
#include <stdlib.h>
#include <string.h>
#include "ui_msg_diff.h"
#include "msg_diff_win.h"
#include "option.h"
/***
@ -53,65 +53,24 @@
*/
/**
* Ui Structure definition for Message Diff panel
* @brief Get panel information structure
*
* All required information of the panel is stored in the info pointer
* of the panel.
* This function will return the pointer to the info structure of the
* panel.
*
* @param ui UI structure pointer
* @return a pointer to the info structure or NULL if no structure exists
*/
Window ui_msg_diff = {
.type = PANEL_MSG_DIFF,
.panel = NULL,
.create = msg_diff_create,
.handle_key = NULL,
.destroy = msg_diff_destroy,
.draw = msg_diff_draw,
.help = NULL
};
void
msg_diff_create(Window *ui)
{
int hwidth;
msg_diff_info_t *info;
// Create a new panel to fill all the screen
window_init(ui, LINES, COLS);
// Initialize panel specific data
info = g_malloc0(sizeof(msg_diff_info_t));
// Store it into panel userptr
set_panel_userptr(ui->panel, (void*) info);
// Calculate subwindows width
hwidth = ui->width / 2 - 1;
// Create 2 subwindows, one for each message
info->one_win = subwin(ui->win, ui->height - 2, hwidth, 1, 0);
info->two_win = subwin(ui->win, ui->height - 2, hwidth, 1, hwidth + 1); // Header - Footer - Address
// Draw a vertical line to separe both subwindows
mvwvline(ui->win, 0, hwidth, ACS_VLINE, ui->height);
// Draw title
window_set_title(ui, "sngrep - SIP messages flow viewer");
// Draw keybindings
msg_diff_draw_footer(ui);
}
void
msg_diff_destroy(Window *ui)
{
g_free(msg_diff_info(ui));
window_deinit(ui);
}
msg_diff_info_t *
static MsgDiffWinInfo *
msg_diff_info(Window *ui)
{
return (msg_diff_info_t*) panel_userptr(ui->panel);
return (MsgDiffWinInfo*) panel_userptr(ui->panel);
}
int
msg_diff_line_highlight(const char* payload1, const char* payload2, char *highlight)
static int
msg_diff_line_highlight(const char *payload1, const char *payload2, char *highlight)
{
char search[MAX_SIP_PAYLOAD];
int len;
@ -140,40 +99,33 @@ msg_diff_line_highlight(const char* payload1, const char* payload2, char *highli
return 0;
}
void
msg_diff_draw_footer(Window *ui)
/**
* @brief Draw panel footer
*
* Usually panel footer contains useful keybidings. This function
* will draw that footer
*
* @param window UI structure pointer
*/
static void
msg_diff_draw_footer(Window *window)
{
const char *keybindings[] = {
key_action_key_str(ACTION_PREV_SCREEN), "Calls Flow",
key_action_key_str(ACTION_SHOW_HELP), "Help"
};
window_draw_bindings(ui, keybindings, 4);
window_draw_bindings(window, keybindings, 4);
}
int
msg_diff_draw(Window *ui)
{
// Get panel information
msg_diff_info_t *info = msg_diff_info(ui);
char highlight[MAX_SIP_PAYLOAD];
// Draw first message
memset(highlight, 0, sizeof(highlight));
msg_diff_line_highlight(msg_get_payload(info->one), msg_get_payload(info->two), highlight);
msg_diff_draw_message(info->one_win, info->one, highlight);
// Draw second message
memset(highlight, 0, sizeof(highlight));
msg_diff_line_highlight(msg_get_payload(info->two), msg_get_payload(info->one), highlight);
msg_diff_draw_message(info->two_win, info->two, highlight);
// Redraw footer
msg_diff_draw_footer(ui);
return 0;
}
int
/**
* @brief Draw a message into a raw subwindow
*
* This function will be called for each message that wants to be draw
* in the panel.
*
*/
static int
msg_diff_draw_message(WINDOW *win, Message *msg, char *highlight)
{
int height, width, line, column;
@ -222,16 +174,93 @@ msg_diff_draw_message(WINDOW *win, Message *msg, char *highlight)
return 0;
}
int
msg_diff_set_msgs(Window *ui, Message *one, Message *two)
/**
* @brief Redraw panel data
*
* This function will be called from ui manager logic when the panels
* needs to be redrawn.
*
* @param window UI structure pointer
* @return 0 in all cases
*/
static int
msg_diff_draw(Window *window)
{
msg_diff_info_t *info;
// Get panel information
info = msg_diff_info(ui);
info->one = one;
info->two = two;
MsgDiffWinInfo *info = msg_diff_info(window);
g_return_val_if_fail(info != NULL, -1);
char highlight[MAX_SIP_PAYLOAD];
// Draw first message
memset(highlight, 0, sizeof(highlight));
msg_diff_line_highlight(msg_get_payload(info->one), msg_get_payload(info->two), highlight);
msg_diff_draw_message(info->one_win, info->one, highlight);
// Draw second message
memset(highlight, 0, sizeof(highlight));
msg_diff_line_highlight(msg_get_payload(info->two), msg_get_payload(info->one), highlight);
msg_diff_draw_message(info->two_win, info->two, highlight);
// Redraw footer
msg_diff_draw_footer(window);
return 0;
}
void
msg_diff_win_set_msgs(Window *window, Message *one, Message *two)
{
MsgDiffWinInfo *info = msg_diff_info(window);
g_return_if_fail(info != NULL);
g_return_if_fail(one != NULL);
g_return_if_fail(two != NULL);
info->one = one;
info->two = two;
}
void
msg_diff_win_free(Window *window)
{
MsgDiffWinInfo *info = msg_diff_info(window);
g_return_if_fail(info != NULL);
g_free(info);
window_deinit(window);
}
Window *
msg_diff_win_new()
{
Window *window = g_malloc0(sizeof(Window));
window->type = WINDOW_MSG_DIFF;
window->destroy = msg_diff_win_free;
window->draw = msg_diff_draw;
// Create a new panel to fill all the screen
window_init(window, getmaxy(stdscr), getmaxx(stdscr));
// Initialize panel specific data
MsgDiffWinInfo *info = g_malloc0(sizeof(MsgDiffWinInfo));
// Store it into panel userptr
set_panel_userptr(window->panel, (void*) info);
// Calculate subwindows width
gint hwidth = window->width / 2 - 1;
// Create 2 subwindows, one for each message
info->one_win = subwin(window->win, window->height - 2, hwidth, 1, 0);
info->two_win = subwin(window->win, window->height - 2, hwidth, 1, hwidth + 1); // Header - Footer - Address
// Draw a vertical line to separe both subwindows
mvwvline(window->win, 0, hwidth, ACS_VLINE, window->height);
// Draw title
window_set_title(window, "sngrep - SIP messages flow viewer");
return window;
}

View File

@ -20,19 +20,20 @@
**
****************************************************************************/
/**
* @file ui_msg_diff.h
* @file msg_diff_win.h
* @author Ivan Alonso [aka Kaian] <kaian@irontec.com>
*
* @brief Functions to manage diff display
*
*/
#ifndef __UI_MSG_DIFF_H
#define __UI_MSG_DIFF_H
#ifndef __SNGREP_MSG_DIFF_WIN_H
#define __SNGREP_MSG_DIFF_WIN_H
#include "config.h"
#include "ncurses/manager.h"
//! Sorter declaration of struct msg_diff_info
typedef struct msg_diff_info msg_diff_info_t;
typedef struct _MsgDiffWinInfo MsgDiffWinInfo;
/**
* @brief Call raw status information
@ -40,7 +41,7 @@ typedef struct msg_diff_info msg_diff_info_t;
* This data stores the actual status of the panel. It's stored in the
* PANEL user pointer.
*/
struct msg_diff_info {
struct _MsgDiffWinInfo {
//! First message to compare
Message *one;
//! Second message to compare
@ -59,11 +60,10 @@ struct msg_diff_info {
* It will also create an information structure of the panel status and
* store it in the panel's userpointer
*
* @param ui UI structure pointer
* @return the allocated ncurses panel
*/
void
msg_diff_create(Window *ui);
Window *
msg_diff_win_new();
/**
* @brief Deallocate panel memory
@ -71,57 +71,10 @@ msg_diff_create(Window *ui);
* This function will be called from ui manager logic to free
* message diff panel memory
*
* @param ui UI structure pointer
* @param window UI structure pointer
*/
void
msg_diff_destroy(Window *ui);
/**
* @brief Get panel information structure
*
* All required information of the panel is stored in the info pointer
* of the panel.
* This function will return the pointer to the info structure of the
* panel.
*
* @param ui UI structure pointer
* @return a pointer to the info structure or NULL if no structure exists
*/
msg_diff_info_t *
msg_diff_info(Window *ui);
/**
* @brief Redraw panel data
*
* This function will be called from ui manager logic when the panels
* needs to be redrawn.
*
* @param ui UI structure pointer
* @return 0 in all cases
*/
int
msg_diff_draw(Window *ui);
/**
* @brief Draw panel footer
*
* Usually panel footer contains useful keybidings. This function
* will draw that footer
*
* @param ui UI structure pointer
*/
void
msg_diff_draw_footer(Window *ui);
/**
* @brief Draw a message into a raw subwindow
*
* This function will be called for each message that wants to be draw
* in the panel.
*
*/
int
msg_diff_draw_message(WINDOW *win, Message *msg, char *highlight);
msg_diff_win_free(Window *window);
/**
* @brief Set the panel working messages
@ -129,12 +82,11 @@ msg_diff_draw_message(WINDOW *win, Message *msg, char *highlight);
* This function will access the panel information and will set the
* msg pointers to the processed messages.
*
* @param ui UI structure pointer
* @param window UI structure pointer
* @param one Message pointer to be set in the internal info struct
* @param two Message pointer to be set in the internal info struct
* @return 0 in all cases
*/
int
msg_diff_set_msgs(Window *ui, Message *one, Message *two);
void
msg_diff_win_set_msgs(Window *window, Message *one, Message *two);
#endif

View File

@ -45,7 +45,7 @@
* Ui Structure definition for Save panel
*/
Window ui_save = {
.type = PANEL_SAVE,
.type = WINDOW_SAVE,
.panel = NULL,
.create = save_create,
.draw = save_draw,

View File

@ -42,7 +42,7 @@
* Ui Structure definition for Settings panel
*/
Window ui_settings = {
.type = PANEL_SETTINGS,
.type = WINDOW_SETTINGS,
.panel = NULL,
.create = settings_create,
.draw = settings_draw,

View File

@ -63,13 +63,13 @@ enum WindowTypes {
//! Filters panel
WINDOW_FILTER,
//! Save to pcap panel
PANEL_SAVE,
WINDOW_SAVE,
//! Message comprare
PANEL_MSG_DIFF,
WINDOW_MSG_DIFF,
//! Column selector panel
WINDOW_COLUMN_SELECT,
//! Settings panel
PANEL_SETTINGS,
WINDOW_SETTINGS,
//! Stats panel
WINDOW_STATS,
//! Panel Counter