packet: replace all old packet_t structs with new packet ones

This commit is contained in:
Kaian 2018-04-21 19:24:35 +02:00
parent aadd3e4d78
commit 52ff95a8c5
21 changed files with 232 additions and 559 deletions

View File

@ -47,7 +47,6 @@ set(SOURCES
src/keybinding.c
src/main.c
src/option.c
src/packet/old_packet.c
src/stream.c
src/setting.c
src/sip_attr.c

View File

@ -182,11 +182,11 @@ capture_is_running(CaptureManager *manager)
// Check if all capture inputs are running
for (GSList *l = manager->inputs; l != NULL; l = l->next) {
CaptureInput *input = l->data;
if (input->running == FALSE) {
return FALSE;
if (input->running == TRUE) {
return TRUE;
}
}
return TRUE;
return FALSE;
}
const gchar *

View File

@ -49,6 +49,10 @@
#include "util.h"
#include "setting.h"
#include "glib-utils.h"
#include "packet/dissectors/packet_ip.h"
#include "packet/dissectors/packet_udp.h"
#include "packet/dissectors/packet_sip.h"
#include "packet/packet.h"
#include "capture_hep.h"
GQuark
@ -167,11 +171,16 @@ capture_input_hep(const gchar *url, GError **error)
input->start = capture_input_hep_start;
input->stop = capture_input_hep_stop;
// Ceate packet parser tree
PacketParser *parser = packet_parser_new(input);
packet_parser_add_proto(parser, parser->dissector_tree, PACKET_SIP);
input->parser = parser;
return input;
}
static packet_t *
capture_input_hep_receive_v2(CaptureHep *hep)
static void
capture_input_hep_receive_v2(CaptureInput *input)
{
uint8_t family, proto;
unsigned char *payload = 0;
@ -183,8 +192,6 @@ capture_input_hep_receive_v2(CaptureHep *hep)
Address dst;
//! Packet header
struct pcap_pkthdr header;
//! New created packet pointer
packet_t *pkt;
//! HEP client data
struct sockaddr hep_client;
socklen_t hep_client_len;
@ -194,20 +201,21 @@ capture_input_hep_receive_v2(CaptureHep *hep)
#ifdef USE_IPV6
struct _CaptureHepIp6Hdr hep_ip6header;
#endif
CaptureHep *hep = input->priv;
// Initialize buffer
memset(buffer, 0, MAX_HEP_BUFSIZE);
/* Receive HEP generic header */
if (recvfrom(hep->socket, buffer, MAX_HEP_BUFSIZE, 0, &hep_client, &hep_client_len) == -1)
return NULL;
return;
/* Copy initial bytes to HEPv2 header */
memcpy(&hdr, buffer, sizeof(struct _CaptureHepHdr));
// Check HEP version
if (hdr.hp_v != 2)
return NULL;
return;
/* IP dissectors */
family = hdr.hp_f;
@ -250,25 +258,40 @@ capture_input_hep_receive_v2(CaptureHep *hep)
header.caplen = header.len = ntohs(hdr.hp_l) - pos;
// Copy packet payload
payload = sng_malloc(header.caplen + 1);
memcpy(payload, (void*) buffer + pos, header.caplen);
GByteArray *data = g_byte_array_new_take((guint8 *)(buffer + pos), header.caplen);
// Create a new packet
/** @todo
pkt = packet_create((family == AF_INET) ? 4 : 6, proto, src, dst, 0);
packet_add_frame(pkt, &header, payload);
packet_set_transport_data(pkt, src.port, dst.port);
packet_set_type(pkt, PACKET_SIP_UDP);
packet_set_payload(pkt, payload, header.caplen);
*/
/* FREE */
sng_free(payload);
return pkt;
Packet *packet = packet_new();
// Generate Packet IP data
PacketIpData *ip = g_malloc0(sizeof(PacketIpData));
ip->saddr = src;
ip->daddr = dst;
ip->protocol = proto;
ip->version = (family == AF_INET) ? 4 : 6;
g_ptr_array_insert(packet->proto, PACKET_IP, ip);
// Generate Packet UDP data
PacketUdpData *udp = g_malloc0(sizeof(PacketUdpData));
udp->sport = src.port;
udp->dport = dst.port;
g_ptr_array_insert(packet->proto, PACKET_UDP, udp);
// Create Packet frame data
PacketFrame *frame = g_malloc0(sizeof(PacketFrame));
frame->header = g_malloc0(sizeof(struct pcap_pkthdr));
frame->header->caplen = header.caplen;
frame->header->len = header.len;
frame->header->ts = header.ts;
frame->data = g_memdup(data->data, data->len);
packet->frames = g_list_append(packet->frames, frame);
// Parse SIP payload
packet_parser_next_dissector(input->parser, packet, data);
}
static packet_t *
capture_input_hep_receive_v3(CaptureHep *hep)
static void
capture_input_hep_receive_v3(CaptureInput *input)
{
struct CaptureHepGeneric hg;
CaptureHepChunkIp4 src_ip4, dst_ip4;
@ -291,19 +314,18 @@ capture_input_hep_receive_v3(CaptureHep *hep)
socklen_t hep_client_len;
//! Packet header
struct pcap_pkthdr header;
//! New created packet pointer
packet_t *pkt;
CaptureHep *hep = input->priv;
/* Receive HEP generic header */
if (recvfrom(hep->socket, buffer, MAX_HEP_BUFSIZE, 0, &hep_client, &hep_client_len) == -1)
return NULL;
return;
/* Copy initial bytes to HEP Generic header */
memcpy(&hg, buffer, sizeof(struct CaptureHepGeneric));
/* header check */
if (memcmp(hg.header.id, "\x48\x45\x50\x33", 4) != 0)
return NULL;
return;
/* IP dissectors */
family = hg.ip_family.data;
@ -360,7 +382,7 @@ capture_input_hep_receive_v3(CaptureHep *hep)
// Validate the password
if (strncmp(password, hep->password, password_len) != 0)
return NULL;
return;
}
if (setting_enabled(SETTING_HEP_LISTEN_UUID)) {
@ -378,47 +400,55 @@ capture_input_hep_receive_v3(CaptureHep *hep)
// Calculate payload size
header.caplen = header.len = ntohs(payload_chunk.length) - sizeof(payload_chunk);
// Receive packet payload
payload = sng_malloc(header.caplen);
memcpy(payload, (void*) buffer + pos, header.caplen);
#if 0
// Create a new packet
pkt = packet_create((family == AF_INET)?4:6, dissectors, src, dst, 0);
packet_add_frame(pkt, &header, payload);
packet_set_type(pkt, PACKET_SIP_UDP);
packet_set_payload(pkt, payload, header.caplen);
// Copy packet payload
GByteArray *data = g_byte_array_new_take((guint8 *)(buffer + pos), header.caplen);
// Create a new packet
Packet *packet = packet_new();
// Generate Packet IP data
PacketIpData *ip = g_malloc0(sizeof(PacketIpData));
ip->saddr = src;
ip->daddr = dst;
ip->protocol = proto;
ip->version = (family == AF_INET) ? 4 : 6;
g_ptr_array_insert(packet->proto, PACKET_IP, ip);
// Generate Packet UDP data
PacketUdpData *udp = g_malloc0(sizeof(PacketUdpData));
udp->sport = src.port;
udp->dport = dst.port;
g_ptr_array_insert(packet->proto, PACKET_UDP, udp);
// Create Packet frame data
PacketFrame *frame = g_malloc0(sizeof(PacketFrame));
frame->header = g_malloc0(sizeof(struct pcap_pkthdr));
frame->header->caplen = header.caplen;
frame->header->len = header.len;
frame->header->ts = header.ts;
frame->data = g_memdup(data->data, data->len);
packet->frames = g_list_append(packet->frames, frame);
// Parse SIP payload
packet_parser_next_dissector(input->parser, packet, data);
/* FREE */
sng_free(payload);
#endif
return pkt;
}
void
capture_input_hep_start(CaptureInput *input)
{
CaptureHep *hep = input->priv;
packet_t *pkt;
// Begin accepting connections
while (hep->socket > 0) {
if (hep->version == 2)
pkt = capture_input_hep_receive_v2(hep);
else
pkt = capture_input_hep_receive_v3(hep);
#if 0
if (pkt != NULL) {
// Avoid parsing from multiples sources.
// Avoid parsing while screen in being redrawn
capture_lock(capture_manager());
if (capture_packet_parse(pkt) != 0) {
packet_destroy(pkt);
} else {
capture_manager_output_packet(input->manager, pkt);
}
capture_unlock(capture_manager());
// Reset dissector for next packet
input->parser->current = input->parser->dissector_tree;
if (hep->version == 2) {
capture_input_hep_receive_v2(input);
} else {
capture_input_hep_receive_v3(input);
}
#endif
}
// Leave the thread gracefully
@ -525,7 +555,7 @@ capture_output_hep(const gchar *url, GError **error)
}
void
capture_output_hep_write_v2(CaptureOutput *output, packet_t *pkt)
capture_output_hep_write_v2(CaptureOutput *output, Packet *packet)
{
void* buffer;
uint32_t buflen = 0, tlen = 0;
@ -535,19 +565,31 @@ capture_output_hep_write_v2(CaptureOutput *output, packet_t *pkt)
#ifdef USE_IPV6
struct _CaptureHepIp6Hdr hep_ip6header;
#endif
unsigned char *data = packet_payload(pkt);
uint32_t len = packet_payloadlen(pkt);
frame_t *frame = g_sequence_first(pkt->frames);
// Get HEP output data
CaptureHep *hep = output->priv;
// Get first frame information (for timestamps)
PacketFrame *frame = g_list_nth_data(packet->frames, 0);
// Packet IP Data
PacketIpData *ip = g_ptr_array_index(packet->proto, PACKET_IP);
g_return_if_fail(ip != NULL);
// Packet UDP Data
PacketUdpData *udp = g_ptr_array_index(packet->proto, PACKET_UDP);
g_return_if_fail(udp != NULL);
// Packet SIP Data
PacketSipData *sip = g_ptr_array_index(packet->proto, PACKET_SIP);
g_return_if_fail(sip != NULL);
/* Version && dissectors */
hdr.hp_v = 2;
hdr.hp_f = pkt->ip_version == 4 ? AF_INET : AF_INET6;
hdr.hp_p = pkt->proto;
hdr.hp_sport = htons(pkt->src.port);
hdr.hp_dport = htons(pkt->dst.port);
hdr.hp_f = (guint8) (ip->version == 4 ? AF_INET : AF_INET6);
hdr.hp_p = ip->protocol;
hdr.hp_sport = htons(udp->sport);
hdr.hp_dport = htons(udp->dport);
/* Timestamp */
hep_time.tv_sec = (guint32) frame->header->ts.tv_sec;
@ -558,25 +600,25 @@ capture_output_hep_write_v2(CaptureOutput *output, packet_t *pkt)
tlen = sizeof(struct _CaptureHepHdr) + sizeof(struct _CaptureHepTimeHdr);
/* IPv4 */
if (pkt->ip_version == 4) {
inet_pton(AF_INET, pkt->src.ip, &hep_ipheader.hp_src);
inet_pton(AF_INET, pkt->dst.ip, &hep_ipheader.hp_dst);
if (ip->version == 4) {
inet_pton(AF_INET, ip->saddr.ip, &hep_ipheader.hp_src);
inet_pton(AF_INET, ip->daddr.ip, &hep_ipheader.hp_dst);
tlen += sizeof(struct _CaptureHepIpHdr);
hdr.hp_l += sizeof(struct _CaptureHepIpHdr);
}
#ifdef USE_IPV6
/* IPv6 */
else if(pkt->ip_version == 6) {
inet_pton(AF_INET6, pkt->src.ip, &hep_ip6header.hp6_src);
inet_pton(AF_INET6, pkt->dst.ip, &hep_ip6header.hp6_dst);
else if(ip->version == 6) {
inet_pton(AF_INET6, ip->saddr.ip, &hep_ip6header.hp6_src);
inet_pton(AF_INET6, ip->daddr.ip, &hep_ip6header.hp6_dst);
tlen += sizeof(struct _CaptureHepIp6Hdr);
hdr.hp_l += sizeof(struct _CaptureHepIp6Hdr);
}
#endif
// Add payload size to the final size of HEP packet
tlen += len;
tlen += strlen(sip->payload);
hdr.hp_l = htons(tlen);
// Allocate memory for HEPv2 packet
@ -589,12 +631,12 @@ capture_output_hep_write_v2(CaptureOutput *output, packet_t *pkt)
buflen += sizeof(struct _CaptureHepHdr);
// Copy IP header
if (pkt->ip_version == 4) {
if (ip->version == 4) {
memcpy(buffer + buflen, &hep_ipheader, sizeof(struct _CaptureHepIpHdr));
buflen += sizeof(struct _CaptureHepIpHdr);
}
#ifdef USE_IPV6
else if(pkt->ip_version == 6) {
else if(ip->version == 6) {
memcpy(buffer + buflen, &hep_ip6header, sizeof(struct _CaptureHepIp6Hdr));
buflen += sizeof(struct _CaptureHepIp6Hdr);
}
@ -605,8 +647,8 @@ capture_output_hep_write_v2(CaptureOutput *output, packet_t *pkt)
buflen += sizeof(struct _CaptureHepTimeHdr);
// Now copy payload itself
memcpy(buffer + buflen, data, len);
buflen += len;
memcpy(buffer + buflen, sip->payload, strlen(sip->payload));
buflen += strlen(sip->payload);
if (send(hep->socket, buffer, buflen, 0) == -1) {
return;
@ -617,9 +659,8 @@ capture_output_hep_write_v2(CaptureOutput *output, packet_t *pkt)
}
void
capture_output_hep_write_v3(CaptureOutput *output, packet_t *pkt)
capture_output_hep_write_v3(CaptureOutput *output, Packet *packet)
{
struct CaptureHepGeneric *hg = NULL;
void* buffer;
uint32_t buflen = 0, iplen = 0, tlen = 0;
CaptureHepChunkIp4 src_ip4, dst_ip4;
@ -628,61 +669,72 @@ capture_output_hep_write_v3(CaptureOutput *output, packet_t *pkt)
#endif
CaptureHepChunk payload_chunk;
CaptureHepChunk authkey_chunk;
frame_t *frame = g_sequence_first(pkt->frames);
unsigned char *data = packet_payload(pkt);
uint32_t len = packet_payloadlen(pkt);
// Get first frame information (for timestamps)
PacketFrame *frame = g_list_nth_data(packet->frames, 0);
// Packet IP Data
PacketIpData *ip = g_ptr_array_index(packet->proto, PACKET_IP);
g_return_if_fail(ip != NULL);
// Packet UDP Data
PacketUdpData *udp = g_ptr_array_index(packet->proto, PACKET_UDP);
g_return_if_fail(udp != NULL);
// Packet SIP Data
PacketSipData *sip = g_ptr_array_index(packet->proto, PACKET_SIP);
g_return_if_fail(sip != NULL);
// Get HEP output data
CaptureHep *hep = output->priv;
hg = sng_malloc(sizeof(struct CaptureHepGeneric));
/* header set "HEP3" */
struct CaptureHepGeneric *hg = sng_malloc(sizeof(struct CaptureHepGeneric));
memcpy(hg->header.id, "\x48\x45\x50\x33", 4);
/* IP dissectors */
hg->ip_family.chunk.vendor_id = htons(0x0000);
hg->ip_family.chunk.type_id = htons(0x0001);
hg->ip_family.chunk.length = htons(sizeof(hg->ip_family));
hg->ip_family.data = pkt->ip_version == 4 ? AF_INET : AF_INET6;
hg->ip_family.data = (guint8) (ip->version == 4 ? AF_INET : AF_INET6);
/* Proto ID */
hg->ip_proto.chunk.vendor_id = htons(0x0000);
hg->ip_proto.chunk.type_id = htons(0x0002);
hg->ip_proto.chunk.length = htons(sizeof(hg->ip_proto));
hg->ip_proto.data = pkt->proto;
hg->ip_proto.data = (guint8) ip->protocol;
/* IPv4 */
if (pkt->ip_version == 4) {
if (ip->version == 4) {
/* SRC IP */
src_ip4.chunk.vendor_id = htons(0x0000);
src_ip4.chunk.type_id = htons(0x0003);
src_ip4.chunk.length = htons(sizeof(src_ip4));
inet_pton(AF_INET, pkt->src.ip, &src_ip4.data);
inet_pton(AF_INET, ip->saddr.ip, &src_ip4.data);
/* DST IP */
dst_ip4.chunk.vendor_id = htons(0x0000);
dst_ip4.chunk.type_id = htons(0x0004);
dst_ip4.chunk.length = htons(sizeof(dst_ip4));
inet_pton(AF_INET, pkt->dst.ip, &dst_ip4.data);
inet_pton(AF_INET, ip->daddr.ip, &dst_ip4.data);
iplen = sizeof(dst_ip4) + sizeof(src_ip4);
}
#ifdef USE_IPV6
/* IPv6 */
else if(pkt->ip_version == 6) {
else if(ip->version == 6) {
/* SRC IPv6 */
src_ip6.chunk.vendor_id = htons(0x0000);
src_ip6.chunk.type_id = htons(0x0005);
src_ip6.chunk.length = htons(sizeof(src_ip6));
inet_pton(AF_INET6, pkt->src.ip, &src_ip6.data);
inet_pton(AF_INET6, ip->saddr.ip, &src_ip6.data);
/* DST IPv6 */
dst_ip6.chunk.vendor_id = htons(0x0000);
dst_ip6.chunk.type_id = htons(0x0006);
dst_ip6.chunk.length = htons(sizeof(dst_ip6));
inet_pton(AF_INET6, pkt->dst.ip, &dst_ip6.data);
inet_pton(AF_INET6, ip->daddr.ip, &dst_ip6.data);
iplen = sizeof(dst_ip6) + sizeof(src_ip6);
}
@ -692,13 +744,13 @@ capture_output_hep_write_v3(CaptureOutput *output, packet_t *pkt)
hg->src_port.chunk.vendor_id = htons(0x0000);
hg->src_port.chunk.type_id = htons(0x0007);
hg->src_port.chunk.length = htons(sizeof(hg->src_port));
hg->src_port.data = htons(pkt->src.port);
hg->src_port.data = htons(udp->sport);
/* DST PORT */
hg->dst_port.chunk.vendor_id = htons(0x0000);
hg->dst_port.chunk.type_id = htons(0x0008);
hg->dst_port.chunk.length = htons(sizeof(hg->dst_port));
hg->dst_port.data = htons(pkt->dst.port);
hg->dst_port.data = htons(udp->dport);
/* TIMESTAMP SEC */
hg->time_sec.chunk.vendor_id = htons(0x0000);
@ -727,9 +779,9 @@ capture_output_hep_write_v3(CaptureOutput *output, packet_t *pkt)
/* Payload */
payload_chunk.vendor_id = htons(0x0000);
payload_chunk.type_id = htons(0x000f);
payload_chunk.length = htons(sizeof(payload_chunk) + len);
payload_chunk.length = htons(sizeof(payload_chunk) + strlen(sip->payload));
tlen = sizeof(struct CaptureHepGeneric) + len + iplen + sizeof(CaptureHepChunk);
tlen = sizeof(struct CaptureHepGeneric) + strlen(sip->payload) + iplen + sizeof(CaptureHepChunk);
/* auth key */
if (hep->password != NULL) {
@ -753,7 +805,7 @@ capture_output_hep_write_v3(CaptureOutput *output, packet_t *pkt)
buflen = sizeof(struct CaptureHepGeneric);
/* IPv4 */
if (pkt->ip_version == 4) {
if (ip->version == 4) {
/* SRC IP */
memcpy(buffer + buflen, &src_ip4, sizeof(struct _CaptureHepChunkIp4));
buflen += sizeof(struct _CaptureHepChunkIp4);
@ -764,7 +816,7 @@ capture_output_hep_write_v3(CaptureOutput *output, packet_t *pkt)
#ifdef USE_IPV6
/* IPv6 */
else if(pkt->ip_version == 6) {
else if(ip->version == 6) {
/* SRC IPv6 */
memcpy(buffer + buflen, &src_ip4, sizeof(struct _CaptureHepChunkIp6));
buflen += sizeof(struct _CaptureHepChunkIp6);
@ -790,8 +842,8 @@ capture_output_hep_write_v3(CaptureOutput *output, packet_t *pkt)
buflen += sizeof(struct _CaptureHepChunk);
/* Now copying payload itself */
memcpy(buffer + buflen, data, len);
buflen += len;
memcpy(buffer + buflen, sip->payload, strlen(sip->payload));
buflen += strlen(sip->payload);
if (send(hep->socket, buffer, buflen, 0) == -1) {
return;
@ -805,13 +857,12 @@ capture_output_hep_write_v3(CaptureOutput *output, packet_t *pkt)
void
capture_output_hep_write(CaptureOutput *output, Packet *packet)
{
#if 0
CaptureHep *hep = output->priv;
if (hep->version == 2)
if (hep->version == 2) {
capture_output_hep_write_v2(output, packet);
else
} else {
capture_output_hep_write_v3(output, packet);
#endif
}
}
void
@ -835,9 +886,3 @@ capture_output_hep_port(CaptureManager *manager)
}
}
}

View File

@ -64,7 +64,7 @@
#include <netinet/in_systm.h>
#endif
#include "packet/old_packet.h"
#include "packet/packet.h"
#include "capture.h"
//! Max allowed packet length (for libpcap)

View File

@ -268,8 +268,8 @@ call_flow_draw_columns(ui_t *ui)
// Load columns
while((msg = call_group_get_next_msg(info->group, msg))) {
call_flow_column_add(ui, msg->call->callid, msg->packet->src);
call_flow_column_add(ui, msg->call->callid, msg->packet->dst);
call_flow_column_add(ui, msg->call->callid, packet_src_address(msg->packet));
call_flow_column_add(ui, msg->call->callid, packet_dst_address(msg->packet));
}
// Add RTP columns FIXME Really
@ -458,8 +458,8 @@ call_flow_draw_message(ui_t *ui, call_flow_arrow_t *arrow, int cline)
// For extended, use xcallid nstead
callid = msg->call->callid;
src = msg->packet->src;
dst = msg->packet->dst;
src = msg_src_address(msg);
dst = msg_dst_address(msg);
media = g_sequence_first(msg->medias);
msg_get_attribute(msg, SIP_ATTR_METHOD, msg_method);
timeval_to_time(msg_get_time(msg), msg_time);
@ -706,11 +706,11 @@ call_flow_draw_rtp_stream(ui_t *ui, call_flow_arrow_t *arrow, int cline)
msg = stream->msg;
// If message and stream share the same IP address
if (address_equals(msg->packet->src, stream->dst)) {
if (address_equals(msg_src_address(msg), stream->dst)) {
// Reuse the msg arrow columns as destination column
if ((msgarrow = call_flow_arrow_find(ui, msg))) {
// Get origin and destination column
arrow->dcolumn = call_flow_column_get(ui, msg->call->callid, msg->packet->src);
arrow->dcolumn = call_flow_column_get(ui, msg->call->callid, msg_src_address(msg));
}
}
@ -731,11 +731,11 @@ call_flow_draw_rtp_stream(ui_t *ui, call_flow_arrow_t *arrow, int cline)
msg = call_msg_with_media(call, stream->src);
// Try to find a message with configured SDP matching the source of this stream
if (msg && address_equals(msg->packet->src, stream->src)) {
if (msg && address_equals(msg_src_address(msg), stream->src)) {
// Reuse the msg arrow columns as destination column
if ((msgarrow = call_flow_arrow_find(ui, msg))) {
// Get origin and destination column
arrow->scolumn = call_flow_column_get(ui, msg->call->callid, msg->packet->src);
arrow->scolumn = call_flow_column_get(ui, msg->call->callid, msg_src_address(msg));
}
}
@ -743,10 +743,10 @@ call_flow_draw_rtp_stream(ui_t *ui, call_flow_arrow_t *arrow, int cline)
if (!arrow->scolumn) {
msg = stream->msg;
// If message and stream share the same IP address
if (address_equals(msg->packet->dst, stream->src)) {
if (address_equals(msg_dst_address(msg), stream->src)) {
// Reuse the msg arrow columns as destination column
if ((msgarrow = call_flow_arrow_find(ui, msg))) {
arrow->scolumn = call_flow_column_get(ui, msg->call->callid, msg->packet->dst);
arrow->scolumn = call_flow_column_get(ui, msg->call->callid, msg_dst_address(msg));
}
}
}

View File

@ -420,7 +420,6 @@ save_to_file(ui_t *ui)
int cur = 0, total = 0;
WINDOW *progress;
GSequenceIter *calls, *msgs, *rtps, *packets;
packet_t *packet;
GSequence *sorted;
GError *error = NULL;
@ -499,7 +498,7 @@ save_to_file(ui_t *ui)
if (info->savemode == SAVE_MESSAGE) {
// Save selected message packet to pcap
output->write(output, info->msg->packet->newpacket);
output->write(output, info->msg->packet);
} else if (info->saveformat == SAVE_TXT) {
// Save selected packets to file
for (;!g_sequence_iter_is_end(calls); calls = g_sequence_iter_next(calls)) {
@ -508,7 +507,7 @@ save_to_file(ui_t *ui)
// Save SIP message content
for (;!g_sequence_iter_is_end(msgs); msgs = g_sequence_iter_next(msgs)) {
msg = g_sequence_get(msgs);
output->write(output, msg->packet->newpacket);
output->write(output, msg->packet);
}
}
} else {
@ -547,10 +546,9 @@ save_to_file(ui_t *ui)
if (info->saveformat == SAVE_PCAP_RTP) {
rtps = g_sequence_get_begin_iter(call->rtp_packets);
for (;!g_sequence_iter_is_end(rtps); rtps = g_sequence_iter_next(rtps)) {
packet = g_sequence_get(rtps);
// Update progress bar dialog
dialog_progress_set_value(progress, (++cur * 100) / total);
g_sequence_insert_sorted(sorted, packet, capture_packet_time_sorter, NULL);
g_sequence_insert_sorted(sorted, g_sequence_get(rtps), capture_packet_time_sorter, NULL);
}
}
}
@ -558,8 +556,7 @@ save_to_file(ui_t *ui)
// Save sorted packets
packets = g_sequence_get_begin_iter(sorted);
for (;!g_sequence_iter_is_end(packets); packets = g_sequence_iter_next(packets)) {
packet = g_sequence_get(packets);
output->write(output, packet->newpacket);
output->write(output, g_sequence_get(packets));
}
dialog_progress_destroy(progress);

View File

@ -29,9 +29,8 @@
#include "config.h"
#include <glib.h>
#include "storage.h"
#include "packet/packet.h"
#include "packet/dissector.h"
#include "packet/old_packet.h"
#include "packet/packet.h"
#include "packet/dissectors/packet_ip.h"
#include "packet/dissectors/packet_udp.h"
#include "packet_rtp.h"
@ -151,7 +150,7 @@ packet_rtcp_parse(G_GNUC_UNUSED PacketParser *parser, Packet *packet, GByteArray
g_ptr_array_insert(packet->proto, PACKET_RTCP, rtcp);
/** @TODO Backwards compatibility during refactoring */
storage_check_rtp_packet(packet_to_oldpkt(packet));
storage_check_rtp_packet(packet);
return NULL;
}

View File

@ -29,9 +29,8 @@
#include "config.h"
#include <glib.h>
#include "storage.h"
#include "packet/packet.h"
#include "packet/dissector.h"
#include "packet/old_packet.h"
#include "packet/packet.h"
#include "packet/dissectors/packet_ip.h"
#include "packet/dissectors/packet_udp.h"
#include "packet_rtp.h"
@ -125,7 +124,7 @@ packet_rtp_parse(G_GNUC_UNUSED PacketParser *parser, Packet *packet, GByteArray
g_ptr_array_insert(packet->proto, PACKET_RTP, rtp);
/** @TODO Backwards compatibility during refactoring */
storage_check_rtp_packet(packet_to_oldpkt(packet));
storage_check_rtp_packet(packet);
return NULL;
}

View File

@ -28,12 +28,11 @@
#include "config.h"
#include <stdlib.h>
#include <packet/old_packet.h>
#include "packet_tcp.h"
#include "packet_sdp.h"
#include "storage.h"
#include "packet_sip.h"
#include "packet/old_packet.h"
#include "packet/packet.h"
#include "packet/dissectors/packet_tcp.h"
#include "packet/dissectors/packet_sdp.h"
#include "packet/dissectors/packet_sip.h"
/* @brief list of methods and responses */
PacketSipCode sip_codes[] = {

View File

@ -1,178 +0,0 @@
/**************************************************************************
**
** sngrep - SIP Messages flow viewer
**
** Copyright (C) 2013-2018 Ivan Alonso (Kaian)
** Copyright (C) 2013-2018 Irontec SL. All rights reserved.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
/**
* @file packet.c
* @author Ivan Alonso [aka Kaian] <kaian@irontec.com>
*
* @brief Source of functions defined in packet.h
*
*/
#include "config.h"
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include "glib-utils.h"
#include "old_packet.h"
packet_t *
packet_create(uint8_t ip_ver, uint8_t proto, Address src, Address dst, uint32_t id)
{
// Create a new packet
packet_t *packet;
packet = malloc(sizeof(packet_t));
memset(packet, 0, sizeof(packet_t));
packet->ip_version = ip_ver;
packet->proto = proto;
packet->frames = g_sequence_new(free);
packet->ip_id = id;
packet->src = src;
packet->dst = dst;
return packet;
}
packet_t*
packet_clone(packet_t *packet)
{
packet_t *clone;
frame_t *frame;
// Create a new packet with the original information
clone = packet_create(packet->ip_version, packet->proto, packet->src, packet->dst, packet->ip_id);
clone->tcp_seq = packet->tcp_seq;
// Append this frames to the original packet
GSequenceIter *it = g_sequence_get_begin_iter(packet->frames);
for (;!g_sequence_iter_is_end(it); it = g_sequence_iter_next(it)) {
frame = g_sequence_get(it);
packet_add_frame(clone, frame->header, frame->data);
}
return clone;
}
void
packet_destroy(gpointer item)
{
packet_t *packet = item;
frame_t *frame;
// Check we have a valid packet pointer
if (!packet) return;
// Destroy frames
GSequenceIter *it = g_sequence_get_begin_iter(packet->frames);
for (;!g_sequence_iter_is_end(it); it = g_sequence_iter_next(it)) {
frame = g_sequence_get(it);
free(frame->header);
free(frame->data);
}
// TODO Free remaining packet data
g_sequence_free(packet->frames);
free(packet->payload);
free(packet);
}
void
packet_free_frames(packet_t *pkt)
{
frame_t *frame;
GSequenceIter *it = g_sequence_get_begin_iter(pkt->frames);
for (;!g_sequence_iter_is_end(it); it = g_sequence_iter_next(it)) {
frame = g_sequence_get(it);
free(frame->data);
frame->data = NULL;
}
}
packet_t *
packet_set_transport_data(packet_t *pkt, uint16_t sport, uint16_t dport)
{
pkt->src.port = sport;
pkt->dst.port = dport;
return pkt;
}
frame_t *
packet_add_frame(packet_t *pkt, const struct pcap_pkthdr *header, const u_char *packet)
{
frame_t *frame = malloc(sizeof(frame_t));
frame->header = malloc(sizeof(struct pcap_pkthdr));
memcpy(frame->header, header, sizeof(struct pcap_pkthdr));
frame->data = malloc(header->caplen);
memcpy(frame->data, packet, header->caplen);
g_sequence_append(pkt->frames, frame);
return frame;
}
void
packet_set_type(packet_t *packet, enum packet_oldtype type)
{
packet->type = type;
}
void
packet_set_payload(packet_t *packet, u_char *payload, uint32_t payload_len)
{
// Free previous payload
if (packet->payload)
free(packet->payload);
packet->payload_len = 0;
// Set new payload
if (payload) {
packet->payload = malloc(payload_len + 1);
memset(packet->payload, 0, payload_len + 1);
memcpy(packet->payload, payload, payload_len);
packet->payload_len = payload_len;
}
}
uint32_t
packet_payloadlen(packet_t *packet)
{
return packet->payload_len;
}
u_char *
packet_payload(packet_t *packet)
{
return packet->payload;
}
struct timeval
packet_time(const packet_t *packet)
{
frame_t *first;
struct timeval ts = { 0 };
// Return first frame timestamp
if (packet && (first = g_sequence_first(packet->frames))) {
ts.tv_sec = first->header->ts.tv_sec;
ts.tv_usec = first->header->ts.tv_usec;
}
// Return packe timestamp
return ts;
}

View File

@ -1,176 +0,0 @@
/**************************************************************************
**
** sngrep - SIP Messages flow viewer
**
** Copyright (C) 2013-2018 Ivan Alonso (Kaian)
** Copyright (C) 2013-2018 Irontec SL. All rights reserved.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
/**
* @file packet.h
* @author Ivan Alonso [aka Kaian] <kaian@irontec.com>
*
* @brief Functions to manage captured packet
*
* Capture packet contains the information about a one or more packets captured
* from network interface or readed from a .PCAP file.
*
* The binary content of the packet can be stored in one or more frames (if
* packet has been reassembled).
*
*/
#ifndef __SNGREP_CAPTURE_OLD_PACKET_H
#define __SNGREP_CAPTURE_OLD_PACKET_H
#include <time.h>
#include <sys/types.h>
#include <pcap.h>
#include <glib.h>
#include "packet/address.h"
//! Stored packet types
enum packet_oldtype {
PACKET_OLD_SIP_UDP = 0,
PACKET_OLD_SIP_TCP,
PACKET_OLD_SIP_TLS,
PACKET_OLD_SIP_WS,
PACKET_OLD_SIP_WSS,
PACKET_OLD_RTP,
PACKET_OLD_RTCP,
};
//! Shorter declaration of packet structure
typedef struct old_packet packet_t;
//! Shorter declaration of frame structure
typedef struct frame frame_t;
//! Forward declaration Packet structure
typedef struct _Packet Packet;
/**
* @brief Packet capture data.
*
* One packet can contain more than one frame after assembly. We assume than
* one SIP message has one packet (maybe in multiple frames) and that one
* packet can only contain one SIP message.
*/
struct old_packet {
Packet *newpacket;
//! IP protocol
uint8_t ip_version;
//! Transport protocol
uint8_t proto;
//! Packet type as defined in capture_packet_type
enum packet_oldtype type;
//! Source
struct _Address src;
//! Destination
struct _Address dst;
//! Packet IP id
uint16_t ip_id;
//! Packet IP fragmentation captured data
uint32_t ip_cap_len;
//! Packet IP fragmentation expected data
uint32_t ip_exp_len;
//! Last TCP sequence frame
uint32_t tcp_seq;
//! PCAP Packet payload when it can not be get from data
u_char *payload;
//! Payload length
uint32_t payload_len;
//! Packet frame list (frame_t)
GSequence *frames;
};
/**
* @brief Capture frame.
*
* One packet can contain multiple frames. This structure is designed to store
* the required information to save a packet into a PCAP file.
*/
struct frame {
//! PCAP Frame Header data
struct pcap_pkthdr *header;
//! PCAP Frame content
u_char *data;
};
packet_t *
packet_create(uint8_t ip_ver, uint8_t proto, Address src, Address dst, uint32_t id);
/**
* @brief Deep clone one packet
*/
packet_t*
packet_clone(packet_t *packet);
/**
* @brief Set Transport layer information
*/
packet_t *
packet_set_transport_data(packet_t *pkt, uint16_t sport, uint16_t dport);
/**
* @brief Add a new frame to the given packet
*/
frame_t *
packet_add_frame(packet_t *pkt, const struct pcap_pkthdr *header, const u_char *packet);
/**
* @brief Deallocate a packet structure memory
*/
void
packet_destroy(gpointer item);
/**
* @brief Free packet frames data.
*
* This can be used to avoid storing packet payload in memory or disk
*/
void
packet_free_frames(packet_t *pkt);
/**
* @brief Set packet type
*/
void
packet_set_type(packet_t *packet, enum packet_oldtype type);
/**
* @brief Set packet payload when it can not be get from packet
*/
void
packet_set_payload(packet_t *packet, u_char *payload, uint32_t payload_len);
/**
* @brief Getter for capture payload size
*/
uint32_t
packet_payloadlen(packet_t *packet);
/**
* @brief Getter for capture payload pointer
*/
u_char *
packet_payload(packet_t *packet);
/**
* @brief Get The timestamp for a packet.
*/
struct timeval
packet_time(const packet_t *packet);
#endif /* __SNGREP_CAPTURE_OLD_PACKET_H */

View File

@ -122,36 +122,18 @@ packet_transport(Packet *packet)
return "???";
}
packet_t *
packet_to_oldpkt(Packet *packet)
struct timeval
packet_time(const Packet *packet)
{
packet_t *oldpkt = g_malloc0(sizeof(packet_t));
PacketFrame *first;
struct timeval ts = { 0 };
PacketIpData *ipdata = g_ptr_array_index(packet->proto, PACKET_IP);
oldpkt->newpacket = packet;
oldpkt->src = ipdata->saddr;
oldpkt->dst = ipdata->daddr;
if (packet_has_type(packet, PACKET_TCP)) {
PacketTcpData *tcpdata = g_ptr_array_index(packet->proto, PACKET_TCP);
oldpkt->src.port = tcpdata->sport;
oldpkt->dst.port = tcpdata->dport;
} else {
PacketUdpData *udpdata = g_ptr_array_index(packet->proto, PACKET_UDP);
oldpkt->src.port = udpdata->sport;
oldpkt->dst.port = udpdata->dport;
// Return first frame timestamp
if (packet && (first = g_list_nth_data(packet->frames, 0))) {
ts.tv_sec = first->header->ts.tv_sec;
ts.tv_usec = first->header->ts.tv_usec;
}
PacketSipData *sipdata = g_ptr_array_index(packet->proto, PACKET_SIP);
if (sipdata) {
packet_set_payload(oldpkt, (guchar *) sipdata->payload, strlen(sipdata->payload));
}
oldpkt->frames = g_sequence_new(NULL);
for (GList *l = packet->frames; l != NULL; l = l->next) {
PacketFrame *frame = l->data;
packet_add_frame(oldpkt, frame->header, frame->data);
}
return oldpkt;
}
// Return packe timestamp
return ts;
}

View File

@ -39,7 +39,6 @@
#include <glib.h>
#include <time.h>
#include <sys/types.h>
#include "packet/old_packet.h"
#include "packet/address.h"
/**
@ -134,7 +133,10 @@ packet_dst_address(const Packet *packet);
//const char *
//packet_transport(Packet *packet);
packet_t *
packet_to_oldpkt(Packet *packet);
/**
* @brief Get The timestamp for a packet.
*/
struct timeval
packet_time(const Packet *packet);
#endif /* __SNGREP_PACKET_H */

View File

@ -49,7 +49,7 @@ call_create(char *callid, char *xcallid)
call->msgs = g_sequence_new(msg_destroy);
// Create an empty vector to store rtp packets
call->rtp_packets = g_sequence_new(packet_destroy);
call->rtp_packets = g_sequence_new((GDestroyNotify) packet_free);
// Create an empty vector to strore stream data
call->streams = g_sequence_new(sng_free);
@ -114,7 +114,7 @@ call_add_stream(SipCall *call, rtp_stream_t *stream)
}
void
call_add_rtp_packet(SipCall *call, packet_t *packet)
call_add_rtp_packet(SipCall *call, Packet *packet)
{
// Store packet
g_sequence_append(call->rtp_packets, packet);
@ -160,8 +160,8 @@ call_msg_retrans_check(SipMsg *msg)
it = g_sequence_iter_prev(it);
prev = g_sequence_get(it);
// Same addresses
if (addressport_equals(prev->packet->src, msg->packet->src) &&
addressport_equals(prev->packet->dst, msg->packet->dst)) {
if (addressport_equals(msg_src_address(prev), msg_src_address(msg)) &&
addressport_equals(msg_dst_address(prev), msg_dst_address(msg))) {
// Same payload
if (!strcasecmp(msg_get_payload(msg), msg_get_payload(prev))) {
// Store the flag that determines if message is retrans

View File

@ -158,7 +158,7 @@ call_add_stream(SipCall *call, rtp_stream_t *stream);
* @param packet new RTP packet from call rtp streams
*/
void
call_add_rtp_packet(SipCall *call, packet_t *packet);
call_add_rtp_packet(SipCall *call, Packet *packet);
/**
* @brief Getter for call messages linked list size

View File

@ -56,7 +56,7 @@ msg_destroy(gpointer item)
g_sequence_free(msg->medias);
// Free message packets
packet_destroy(msg->packet);
packet_free(msg->packet);
// Free all memory
sng_free(msg->resp_str);
sng_free(msg->sip_from);
@ -90,15 +90,15 @@ msg_is_request(SipMsg *msg)
const gchar *
msg_get_payload(SipMsg *msg)
{
return (const char *) packet_payload(msg->packet);
return packet_sip_payload(msg->packet);
}
struct timeval
msg_get_time(const SipMsg *msg) {
struct timeval t = { };
frame_t *frame;
PacketFrame *frame;
if (msg && (frame = g_sequence_first(msg->packet->frames)))
if (msg && (frame = g_list_nth_data(msg->packet->frames, 0)))
return frame->header->ts;
return t;
}
@ -110,10 +110,10 @@ msg_get_attribute(SipMsg *msg, gint id, char *value)
switch (id) {
case SIP_ATTR_SRC:
sprintf(value, "%s:%u", msg->packet->src.ip, msg->packet->src.port);
sprintf(value, "%s:%u", msg_src_address(msg).ip, msg_src_address(msg).port);
break;
case SIP_ATTR_DST:
sprintf(value, "%s:%u", msg->packet->dst.ip, msg->packet->dst.port);
sprintf(value, "%s:%u", msg_dst_address(msg).ip, msg_dst_address(msg).port);
break;
case SIP_ATTR_METHOD:
sprintf(value, "%.*s", SIP_ATTR_MAXLEN, msg_reqresp_str(msg));

View File

@ -34,6 +34,11 @@
#include "sip_attr.h"
#include "util.h"
//! Get IP Address info from message's packet
#define msg_src_address(msg) (packet_src_address(msg->packet))
#define msg_dst_address(msg) (packet_dst_address(msg->packet))
//! Shorter declaration of sip_msg structure
typedef struct _SipMsg SipMsg;
//! Forward declarition of SipCall type
@ -61,7 +66,7 @@ struct _SipMsg {
//! SDP payload information (sdp_media_t *)
GSequence *medias;
//! Captured packet for this message
packet_t *packet;
Packet *packet;
//! Index of this message in call
guint32 index;
//! Message owner

View File

@ -172,7 +172,7 @@ storage_check_sip_packet(Packet *packet)
}
// At this point we know we're handling an interesting SIP Packet
msg->packet = packet_to_oldpkt(packet);
msg->packet = packet;
// Always dissect first call message
if (call_msg_count(call) == 0) {
@ -213,6 +213,10 @@ storage_check_sip_packet(Packet *packet)
// Mark the list as changed
calls.changed = true;
// Send this packet to all capture outputs
capture_manager_output_packet(capture_manager(), packet);
// Return the loaded message
return msg;
@ -224,14 +228,14 @@ skip_message:
}
rtp_stream_t *
storage_check_rtp_packet(packet_t *packet)
storage_check_rtp_packet(Packet *packet)
{
Address src, dst;
rtp_stream_t *stream;
rtp_stream_t *reverse;
u_char format = 0;
u_char *payload;
uint32_t size, bsize;
const gchar *payload;
size_t size, bsize;
uint16_t len;
struct rtcp_hdr_generic hdr;
struct rtcp_hdr_sr hdr_sr;
@ -239,16 +243,12 @@ storage_check_rtp_packet(packet_t *packet)
struct rtcp_blk_xr blk_xr;
struct rtcp_blk_xr_voip blk_xr_voip;
// Get packet data
payload = packet_payload(packet);
size = packet_payloadlen(packet);
// Get Addresses from packet
src = packet->src;
dst = packet->dst;
src = packet_src_address(packet);
dst = packet_dst_address(packet);
// Check if packet has RTP data
PacketRtpData *rtp = g_ptr_array_index(packet->newpacket->proto, PACKET_RTP);
PacketRtpData *rtp = g_ptr_array_index(packet->proto, PACKET_RTP);
if (rtp != NULL) {
// Get RTP Encoding information
guint8 format = rtp->encoding->id;
@ -263,7 +263,7 @@ storage_check_rtp_packet(packet_t *packet)
// We have found a stream, but with different format
if (stream_is_complete(stream) && stream->fmtcode != format) {
// Create a new stream for this new format
stream = stream_create(packet->newpacket, stream->media);
stream = stream_create(packet, stream->media);
stream_complete(stream, src);
stream_set_format(stream, format);
call_add_stream(msg_get_call(stream->msg), stream);
@ -295,7 +295,7 @@ storage_check_rtp_packet(packet_t *packet)
// Check if an stream in the opposite direction exists
if (!(reverse = call_find_stream(stream->msg->call, stream->dst, stream->src))) {
reverse = stream_create(packet->newpacket, stream->media);
reverse = stream_create(packet, stream->media);
stream_complete(reverse, stream->dst);
stream_set_format(reverse, format);
call_add_stream(msg_get_call(stream->msg), reverse);
@ -304,7 +304,7 @@ storage_check_rtp_packet(packet_t *packet)
if (reverse->src.port && !addressport_equals(stream->src, reverse->src)) {
if (!(reverse = call_find_stream_exact(stream->msg->call, stream->dst, stream->src))) {
// Create a new reverse stream
reverse = stream_create(packet->newpacket, stream->media);
reverse = stream_create(packet, stream->media);
stream_complete(reverse, stream->dst);
stream_set_format(reverse, format);
call_add_stream(msg_get_call(stream->msg), reverse);
@ -318,7 +318,7 @@ storage_check_rtp_packet(packet_t *packet)
}
// Check if packet has RTP data
PacketRtcpData *rtcp = g_ptr_array_index(packet->newpacket->proto, PACKET_RTP);
PacketRtcpData *rtcp = g_ptr_array_index(packet->proto, PACKET_RTP);
if (rtcp != NULL) {
// Add packet to stream
stream_complete(stream, src);
@ -393,7 +393,7 @@ storage_find_by_callid(const char *callid)
void
storage_register_streams(SipMsg *msg)
{
Packet *packet = msg->packet->newpacket;
Packet *packet = msg->packet;
Address emptyaddr = {};
PacketSdpData *sdp = g_ptr_array_index(packet->proto, PACKET_SDP);
@ -426,11 +426,11 @@ storage_register_streams(SipMsg *msg)
}
// Create RTP stream with source of message as destination address
if (call_find_stream(msg->call, msg->packet->src, media->address) == NULL) {
if (call_find_stream(msg->call, msg_src_address(msg), media->address) == NULL) {
rtp_stream_t *stream = stream_create(packet, media);
stream->type = PACKET_RTP;
stream->msg = msg;
stream->dst = msg->packet->src;
stream->dst = msg_src_address(msg);
stream->dst.port = media->rtpport;
call_add_stream(msg->call, stream);
}

View File

@ -155,7 +155,7 @@ storage_check_sip_packet(Packet *packet);
* @return a SIP msg structure pointer
*/
rtp_stream_t *
storage_check_rtp_packet(packet_t *packet);
storage_check_rtp_packet(Packet *packet);
/**
* @brief Return if the call list has changed

View File

@ -68,7 +68,7 @@ stream_set_format(rtp_stream_t *stream, uint32_t format)
}
void
stream_add_packet(rtp_stream_t *stream, packet_t *packet)
stream_add_packet(rtp_stream_t *stream, Packet *packet)
{
if (stream->pktcnt == 0)
stream->time = packet_time(packet);

View File

@ -73,7 +73,7 @@ void
stream_set_format(rtp_stream_t *stream, uint32_t format);
void
stream_add_packet(rtp_stream_t *stream, packet_t *packet);
stream_add_packet(rtp_stream_t *stream, Packet *packet);
uint32_t
stream_get_count(rtp_stream_t *stream);