From 1ea16f009304a0eb48937456584fd7e200dfa409 Mon Sep 17 00:00:00 2001 From: Kaian Date: Sun, 29 Jul 2018 17:49:07 +0200 Subject: [PATCH] call: make message a pointer array --- CMakeLists.txt | 2 +- src/curses/screens/ui_save.c | 12 +++++------- src/curses/screens/ui_stats.c | 5 ++--- src/filter.c | 5 ++--- src/glib-utils.h | 4 ++++ src/group.c | 5 ++--- src/sip_call.c | 34 ++++++++++++++-------------------- src/sip_call.h | 4 ++-- src/sip_msg.c | 19 ++++++------------- 9 files changed, 38 insertions(+), 52 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4027a10..68b310c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(sngrep set(PROJECT_NAME sngrep) set(CMAKE_C_STANDARD 99) -add_compile_options(-Werror -pg) +add_compile_options(-Werror -g) #add_compile_options(-Werror -Wall -pedantic -Wextra) configure_file( diff --git a/src/curses/screens/ui_save.c b/src/curses/screens/ui_save.c index fdcb367..a2dad3a 100644 --- a/src/curses/screens/ui_save.c +++ b/src/curses/screens/ui_save.c @@ -508,10 +508,9 @@ save_to_file(ui_t *ui) // Save selected packets to file for (;!g_sequence_iter_is_end(calls); calls = g_sequence_iter_next(calls)) { call = g_sequence_get(calls); - msgs = g_sequence_get_begin_iter(call->msgs); // Save SIP message content - for (;!g_sequence_iter_is_end(msgs); msgs = g_sequence_iter_next(msgs)) { - msg = g_sequence_get(msgs); + for (guint i = 0; i < g_ptr_array_len(call->msgs); i++) { + msg = g_ptr_array_index(call->msgs, i); output->write(output, msg->packet); } } @@ -524,7 +523,7 @@ save_to_file(ui_t *ui) call = g_sequence_get(calls); if (info->savemode == SAVE_DISPLAYED && !filter_check_call(call, NULL)) continue; - total += g_sequence_get_length(call->msgs); + total += call_msg_count(call); if (info->saveformat == SAVE_PCAP_RTP) total += g_sequence_get_length(call->rtp_packets); } @@ -538,10 +537,9 @@ save_to_file(ui_t *ui) call = g_sequence_get(calls); if (info->savemode == SAVE_DISPLAYED && !filter_check_call(call, NULL)) continue; - msgs = g_sequence_get_begin_iter(call->msgs); // Save SIP message content - for (;!g_sequence_iter_is_end(msgs); msgs = g_sequence_iter_next(msgs)) { - msg = g_sequence_get(msgs); + for (guint i = 0; i < g_ptr_array_len(call->msgs); i++) { + msg = g_ptr_array_index(call->msgs, i); // Update progress bar dialog dialog_progress_set_value(progress, (++cur * 100) / total); g_sequence_insert_sorted(sorted, msg->packet, capture_packet_time_sorter, NULL); diff --git a/src/curses/screens/ui_stats.c b/src/curses/screens/ui_stats.c index b803a29..dd57511 100644 --- a/src/curses/screens/ui_stats.c +++ b/src/curses/screens/ui_stats.c @@ -133,9 +133,8 @@ stats_create(ui_t *ui) guint method = packet_sip_method(msg->packet); // For each message in call - msgs = g_sequence_get_begin_iter(call->msgs); - for (msg = NULL; !g_sequence_iter_is_end(msgs); msgs = g_sequence_iter_next(msgs)) { - msg = g_sequence_get(msgs); + for (guint i = 0; i < g_ptr_array_len(call->msgs); i++) { + msg = g_ptr_array_index(call->msgs, i); // Increase message counter stats.mtotal++; // Check message type diff --git a/src/filter.c b/src/filter.c index 05e592c..36b4e17 100644 --- a/src/filter.c +++ b/src/filter.c @@ -133,9 +133,8 @@ filter_check_call(gconstpointer item, gconstpointer user_data) // Assume this call doesn't match the filter call->filtered = 1; // Create an iterator for the call messages - it = g_sequence_get_begin_iter(call->msgs); - for (msg = NULL; !g_sequence_iter_is_end(it); it = g_sequence_iter_next(it)) { - msg = g_sequence_get(it); + for (guint i = 0; i < g_ptr_array_len(call->msgs); i++) { + msg = g_ptr_array_index(call->msgs, i); // Copy message payload strcpy(data, msg_get_payload(msg)); // Check if this payload matches the filter diff --git a/src/glib-utils.h b/src/glib-utils.h index e525785..47d18ce 100644 --- a/src/glib-utils.h +++ b/src/glib-utils.h @@ -7,6 +7,10 @@ #include +#define g_ptr_array_len(array) (array->len) +#define g_ptr_array_first(array) g_ptr_array_index(array, 0) +#define g_ptr_array_last(array) g_ptr_array_index(array, array->len-1) + #define g_sequence_first(sequence) g_sequence_nth(sequence, 0) #define g_sequence_last(sequence) g_sequence_nth(sequence, g_sequence_get_length(sequence)) diff --git a/src/group.c b/src/group.c index 4c96737..148bb69 100644 --- a/src/group.c +++ b/src/group.c @@ -108,9 +108,8 @@ call_group_add(SipCallGroup *group, SipCall *call) call->locked = TRUE; group->calls = g_list_append(group->calls, call); // Add all call messages - GSequenceIter *msgs = g_sequence_get_begin_iter(call->msgs); - for (;!g_sequence_iter_is_end(msgs); msgs = g_sequence_iter_next(msgs)) { - group->msgs = g_list_insert_sorted(group->msgs, g_sequence_get(msgs), call_group_msg_sorter); + for (guint i = 0; i < g_ptr_array_len(call->msgs); i++) { + group->msgs = g_list_append(group->msgs, g_ptr_array_index(call->msgs, i)); } } } diff --git a/src/sip_call.c b/src/sip_call.c index 35d8a6f..c0f024e 100644 --- a/src/sip_call.c +++ b/src/sip_call.c @@ -43,7 +43,7 @@ call_create(char *callid, char *xcallid) SipCall *call = g_malloc0(sizeof(SipCall)); // Create a vector to store call messages - call->msgs = g_sequence_new(msg_destroy); + call->msgs = g_ptr_array_new_with_free_func(msg_destroy); // Create an empty vector to store rtp packets call->rtp_packets = g_sequence_new((GDestroyNotify) packet_free); @@ -69,7 +69,7 @@ call_destroy(gpointer item) { SipCall *call = item; // Remove all call messages - g_sequence_free(call->msgs); + g_ptr_array_free(call->msgs, TRUE); // Remove all call streams g_sequence_free(call->streams); // Remove all call rtp packets @@ -90,7 +90,7 @@ call_add_message(SipCall *call, SipMsg *msg) // Set the message owner msg->call = call; // Put this msg at the end of the msg list - g_sequence_append(call->msgs, msg); + g_ptr_array_add(call->msgs, msg); // Flag this call as changed call->changed = true; // Check if message is a retransmission @@ -118,7 +118,7 @@ call_add_rtp_packet(SipCall *call, Packet *packet) guint call_msg_count(const SipCall *call) { - return (guint) g_sequence_get_length(call->msgs); + return g_ptr_array_len(call->msgs); } int @@ -130,11 +130,10 @@ call_is_active(SipCall *call) int call_is_invite(SipCall *call) { - SipMsg *first; - if ((first = g_sequence_first(call->msgs))) - return (packet_sip_method(first->packet) == SIP_METHOD_INVITE); + SipMsg *first = g_ptr_array_first(call->msgs); + g_return_val_if_fail(first != NULL, 0); - return 0; + return packet_sip_method(first->packet) == SIP_METHOD_INVITE; } SipMsg * @@ -142,13 +141,11 @@ call_msg_with_media(SipCall *call, Address dst) { SipMsg *msg; PacketSdpMedia *media; - GSequenceIter *itmsg; GSequenceIter *itmedia; // Get message with media address configured in given dst - itmsg = g_sequence_get_begin_iter(call->msgs); - for (;!g_sequence_iter_is_end(itmsg); itmsg = g_sequence_iter_next(itmsg)) { - msg = g_sequence_get(itmsg); + for (guint i = 0; i < g_ptr_array_len(call->msgs); i++) { + msg = g_ptr_array_index(call->msgs, i); itmedia = g_sequence_get_begin_iter(msg->medias); for (;!g_sequence_iter_is_end(itmedia); itmedia = g_sequence_iter_next(itmedia)) { media = g_sequence_get(itmedia); @@ -214,10 +211,10 @@ call_update_state(SipCall *call, SipMsg *msg) const char * call_get_attribute(const SipCall *call, enum sip_attr_id id, char *value) { - SipMsg *first, *last; + g_return_val_if_fail(call != NULL, NULL); - if (!call) - return NULL; + SipMsg *first = g_ptr_array_first(call->msgs); + SipMsg *last = g_ptr_array_last(call->msgs); switch (id) { case SIP_ATTR_CALLINDEX: @@ -230,21 +227,18 @@ call_get_attribute(const SipCall *call, enum sip_attr_id id, char *value) sprintf(value, "%s", call->xcallid); break; case SIP_ATTR_MSGCNT: - sprintf(value, "%d", g_sequence_get_length(call->msgs)); + sprintf(value, "%d", call_msg_count(call)); break; case SIP_ATTR_CALLSTATE: sprintf(value, "%s", call_state_to_str(call->state)); break; case SIP_ATTR_TRANSPORT: - first = g_sequence_first(call->msgs); //@todo sprintf(value, "%s", sip_transport_str(first->packet->type)); break; case SIP_ATTR_CONVDUR: timeval_to_duration(msg_get_time(call->cstart_msg), msg_get_time(call->cend_msg), value); break; case SIP_ATTR_TOTALDUR: - first = g_sequence_first(call->msgs); - last = g_sequence_last(call->msgs); timeval_to_duration(msg_get_time(first), msg_get_time(last), value); break; case SIP_ATTR_REASON_TXT: @@ -256,7 +250,7 @@ call_get_attribute(const SipCall *call, enum sip_attr_id id, char *value) sprintf(value, "%d", call->warning); break; default: - return msg_get_attribute(g_sequence_first(call->msgs), id, value); + return msg_get_attribute(g_ptr_array_first(call->msgs), id, value); } return strlen(value) ? value : NULL; diff --git a/src/sip_call.h b/src/sip_call.h index a817dfd..75d3e73 100644 --- a/src/sip_call.h +++ b/src/sip_call.h @@ -81,8 +81,8 @@ struct _SipCall { GSequence *xcalls; //! Cseq from invite startint the call gint invitecseq; - //! List of messages of this call (sip_msg_t*) - GSequence *msgs; + //! Array of messages of this call (sip_msg_t*) + GPtrArray *msgs; //! Message when conversation started and ended SipMsg *cstart_msg, *cend_msg; //! RTP streams for this call (rtp_stream_t *) diff --git a/src/sip_msg.c b/src/sip_msg.c index ee854c4..6f9d2fe 100644 --- a/src/sip_msg.c +++ b/src/sip_msg.c @@ -175,24 +175,17 @@ msg_get_header(SipMsg *msg, char *out) const SipMsg * msg_is_retrans(SipMsg *msg) { - SipMsg *prev = NULL; - GSequenceIter *it; + for (gint i = g_ptr_array_len(msg->call->msgs) - 1; i >= 0; i--) { + SipMsg *prev = g_ptr_array_index(msg->call->msgs, i); - // Get previous message in call with same origin and destination - it = g_sequence_get_end_iter(msg->call->msgs); + // Skip yourself + if (prev == msg) continue; - // Skip already added message - it = g_sequence_iter_prev(it); - - while(!g_sequence_iter_is_begin(it)) { - it = g_sequence_iter_prev(it); - prev = g_sequence_get(it); - // Same addresses + // Check source and destination addresses are equal if (addressport_equals(msg_src_address(prev), msg_src_address(msg)) && addressport_equals(msg_dst_address(prev), msg_dst_address(msg))) { - // Same payload + // Check they have the same payload if (!strcasecmp(msg_get_payload(msg), msg_get_payload(prev))) { - // Store the flag that determines if message is retrans return prev; } }