Skip to content

Commit

Permalink
Merge pull request #7 from mimi-net/ilya-patch-2
Browse files Browse the repository at this point in the history
Add sleep if interface is not ready
  • Loading branch information
i1ya committed Mar 29, 2024
2 parents 14d0dd0 + 58104f3 commit 32e6e5e
Showing 1 changed file with 75 additions and 41 deletions.
116 changes: 75 additions & 41 deletions mimidump.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include <pthread.h>
#include <signal.h>
Expand Down Expand Up @@ -69,15 +70,7 @@ void sig_handler(int signo)
}
}

static void *thread_handle_inout_packets(void *arg)
{
struct thread_info *tinfo = arg;

pcap_loop(tinfo->handler, tinfo->num_packets, &pcap_dump, (u_char *)tinfo->pd);
return 0;
}

static void *thread_handle_out_packets(void *arg)
static void *thread_handle_packets(void *arg)
{
struct thread_info *tinfo = arg;

Expand All @@ -101,6 +94,7 @@ int main(int argc, char **argv)
char pcap_out_filename[PCAP_FILENAME_SIZE];

pthread_attr_t attr;
unsigned int mSleep = 100000; /* Sleep 0.1 second */
size_t num_threads = 2;
int s;
void *res;
Expand Down Expand Up @@ -159,42 +153,82 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE);
}

/* open capture device */
handle_inout = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
if (handle_inout == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
exit(EXIT_FAILURE);
}
/* At some point mimidump starts when interface already exists, but not activated.
* Try to sleep and then try again.
* By default we try 100 times with 0.1 sec. sleep interval
*/

handle_out = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
if (handle_out == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
exit(EXIT_FAILURE);
}
for (int i = 0; i < 100; i++) {

/* set direction IN */
pcap_setdirection(handle_inout, PCAP_D_INOUT);
pcap_setdirection(handle_out, PCAP_D_OUT);
/* open capture device */
handle_inout = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
if (handle_inout == NULL) {

/* Set filters */
if (pcap_compile(handle_inout, &bprog, filter_string, 1, PCAP_NETMASK_UNKNOWN) < 0) {
fprintf(stderr, "Error compiling IN/OUT bpf filter on\n");
exit(EXIT_FAILURE);
}
/* If device is not up, sleep 0.1 second and try again */
if (strstr(errbuf, "device is not up") != NULL) {
usleep(mSleep);
continue;
}

if (pcap_setfilter(handle_inout, &bprog) < 0) {
fprintf(stderr, "Error installing IN/OUT bpf filter\n");
exit(EXIT_FAILURE);
}
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
exit(EXIT_FAILURE);
}

if (pcap_compile(handle_out, &bprog, filter_string, 1, PCAP_NETMASK_UNKNOWN) < 0) {
fprintf(stderr, "Error compiling OUT bpf filter on\n");
exit(EXIT_FAILURE);
}
handle_out = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
if (handle_out == NULL) {

if (pcap_setfilter(handle_out, &bprog) < 0) {
fprintf(stderr, "Error installing OUT bpf filter\n");
exit(EXIT_FAILURE);
if (strstr(errbuf, "device is not up") != NULL) {
usleep(mSleep);
continue;
}

fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
exit(EXIT_FAILURE);
}

/* set direction IN */
pcap_setdirection(handle_inout, PCAP_D_INOUT);
pcap_setdirection(handle_out, PCAP_D_OUT);

/* Set filters */
if (pcap_compile(handle_inout, &bprog, filter_string, 1, PCAP_NETMASK_UNKNOWN) < 0) {
fprintf(stderr, "Error compiling IN/OUT bpf filter on\n");
exit(EXIT_FAILURE);
}

if (pcap_setfilter(handle_inout, &bprog) < 0) {

sprintf(errbuf, "%s", pcap_geterr(handle_inout));

if (strstr(errbuf, "Network is down") != NULL) {
usleep(mSleep);
continue;
}

fprintf(stderr, "Error installing IN/OUT bpf filter: %s\n", errbuf);
exit(EXIT_FAILURE);
}

if (pcap_compile(handle_out, &bprog, filter_string, 1, PCAP_NETMASK_UNKNOWN) < 0) {
fprintf(stderr, "Error compiling OUT bpf filter on\n");
exit(EXIT_FAILURE);
}

if (pcap_setfilter(handle_out, &bprog) < 0) {

sprintf(errbuf, "%s", pcap_geterr(handle_out));

if (strstr(errbuf, "Network is down") != NULL) {
usleep(mSleep);
continue;
}

fprintf(stderr, "Error installing OUT bpf filter: %s\n", errbuf);
exit(EXIT_FAILURE);
}

/* All looks good */
break;
}

/*
Expand Down Expand Up @@ -230,7 +264,7 @@ int main(int argc, char **argv)
tinfo[0].handler = handle_inout;
tinfo[0].num_packets = MAX_PACKET_CAPTURE;
tinfo[0].pd = pd_inout;
s = pthread_create(&tinfo[0].thread_id, &attr, &thread_handle_inout_packets, &tinfo[0]);
s = pthread_create(&tinfo[0].thread_id, &attr, &thread_handle_packets, &tinfo[0]);

if (s != 0) {
fprintf(stderr, "Can't create thread_handle_inout_packets\n");
Expand All @@ -241,7 +275,7 @@ int main(int argc, char **argv)
tinfo[1].handler = handle_out;
tinfo[1].num_packets = MAX_PACKET_CAPTURE;
tinfo[1].pd = pd_out;
s = pthread_create(&tinfo[1].thread_id, &attr, &thread_handle_out_packets, &tinfo[1]);
s = pthread_create(&tinfo[1].thread_id, &attr, &thread_handle_packets, &tinfo[1]);

if (s != 0) {
fprintf(stderr, "Can't create thread_handle_out_packets\n");
Expand Down

0 comments on commit 32e6e5e

Please sign in to comment.