Skip to content

Commit

Permalink
Instant convert pcap to txt file (#427)
Browse files Browse the repository at this point in the history
* add siplify command for saving pcap to txt file

* remove unnecessary printf and commented code

* Refactor siplify into text; minor changes according to pull request review
  • Loading branch information
sebk2307 committed Feb 14, 2023
1 parent 69ddc89 commit b1cb646
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "vector.h"
#include "capture.h"
#include "capture_eep.h"
#include "curses/ui_save.h"
#ifdef WITH_GNUTLS
#include "capture_gnutls.h"
#endif
Expand Down Expand Up @@ -76,6 +77,7 @@ usage()
" -D --dump-config\t Print active configuration settings and exit\n"
" -f --config\t\t Read configuration from file\n"
" -F --no-config\t Do not read configuration from default config file\n"
" -T --text\t Save pcap to text file\n"
" -R --rotate\t\t Rotate calls when capture limit have been reached\n"
#ifdef USE_EEP
" -H --eep-send\t Homer sipcapture url (udp:X.X.X.X:XXXX)\n"
Expand Down Expand Up @@ -128,7 +130,7 @@ int
main(int argc, char* argv[])
{
int opt, idx, limit, only_calls, no_incomplete, pcap_buffer_size, i;
const char *device, *outfile;
const char *device, *outfile, *text_outfile;

This comment has been minimized.

Copy link
@GangZhuo

GangZhuo Mar 9, 2023

Contributor

text_outfile should be initialized to NULL.

This comment has been minimized.

Copy link
@Kaian

Kaian Mar 9, 2023

Member

All three variables could be initializated, are you willing to PR the change?

This comment has been minimized.

Copy link
@GangZhuo

GangZhuo Mar 9, 2023

Contributor

I'll have to wait until tomorrow, you can just make the changes directly.

char bpf[512];
#if defined(WITH_GNUTLS) || defined(WITH_OPENSSL)
const char *keyfile;
Expand Down Expand Up @@ -161,6 +163,7 @@ main(int argc, char* argv[])
{ "rotate", no_argument, 0, 'R' },
{ "config", required_argument, 0, 'f' },
{ "no-config", no_argument, 0, 'F' },
{ "text", required_argument, 0, 'T' },
#ifdef USE_EEP
{ "eep-listen", required_argument, 0, 'L' },
{ "eep-send", required_argument, 0, 'H' },
Expand All @@ -171,7 +174,7 @@ main(int argc, char* argv[])

// Parse command line arguments that have high priority
opterr = 0;
char *options = "hVd:I:O:B:pqtW:k:crl:ivNqDL:H:ERf:F";
char *options = "hVd:I:O:B:pqtW:k:crl:ivNqDL:H:ERf:F:T";
while ((opt = getopt_long(argc, argv, options, long_options, &idx)) != -1) {
switch (opt) {
case 'h':
Expand Down Expand Up @@ -226,6 +229,11 @@ main(int argc, char* argv[])
case 'O':
outfile = optarg;
break;
case 'T':
text_outfile = optarg;
no_interface = 1;
setting_set_value(SETTING_CAPTURE_STORAGE, "none");
break;
case 'B':
if(!(pcap_buffer_size = atoi(optarg))) {
fprintf(stderr, "Invalid buffer size.\n");
Expand Down Expand Up @@ -383,6 +391,9 @@ main(int argc, char* argv[])
return 1;
}




if (outfile)
{
ino_t dump_inode;
Expand Down Expand Up @@ -454,6 +465,32 @@ main(int argc, char* argv[])
printf("\rDialog count: %d\n", sip_calls_count_unrotated());
}


if (text_outfile)
{
vector_iter_t calls;
calls = sip_calls_iterator();

sip_call_t *call = NULL;
sip_msg_t *msg = NULL;
vector_iter_t msgs;

FILE *f = NULL;

if (!(f = fopen(text_outfile, "w"))) {
fprintf(stderr, "Couldn't open sip output file");
return 0;
}

while ((call = vector_iterator_next(&calls))) {
msgs = vector_iterator(call->msgs);
// Save SIP message content
while ((msg = vector_iterator_next(&msgs))) {
save_msg_txt(f, msg);
}
}
fclose(f);
}
// Capture deinit
capture_deinit();

Expand Down

0 comments on commit b1cb646

Please sign in to comment.