Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using SIGHUP to rotate the pcap and detect any compatible WiFi dongles #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 46 additions & 12 deletions wifi_coconut.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ struct coconut_tool_context {
#define DLT_IEEE802_11 105 /* IEEE 802.11 wireless */
#define DLT_IEEE802_11_RADIO 127 /* 802.11 plus radiotap radio header */

int open_coconut_pcap(struct coconut_tool_context *tool_context) {

static int do_open_pcap_file(struct coconut_tool_context *tool_context) {
struct {
uint32_t magic_number; /* magic number */
uint16_t version_major; /* major version number */
Expand All @@ -112,33 +113,59 @@ int open_coconut_pcap(struct coconut_tool_context *tool_context) {
pcap_hdr_t.sigfigs = 0;
pcap_hdr_t.snaplen = 8192;

tool_context->enable_pcap_log = true;
pthread_mutex_init(&tool_context->pcap_mutex, NULL);

if (tool_context->no_radiotap)
pcap_hdr_t.network = DLT_IEEE802_11;
else
pcap_hdr_t.network = DLT_IEEE802_11_RADIO;

if (strcmp(tool_context->pcap_fname, "-") == 0)
tool_context->dump_filep = stdout;
else
tool_context->dump_filep = fopen(tool_context->pcap_fname, "wb");
pthread_mutex_lock(&tool_context->pcap_mutex);

if (tool_context->dump_filep == NULL) {
FILE *old_file = tool_context->dump_filep;
FILE *new_file;
if (strcmp(tool_context->pcap_fname, "-") == 0) {
new_file = stdout;
} else {
new_file = fopen(tool_context->pcap_fname, "wb");
}

if (new_file == NULL) {
fprintf(stderr, "ERROR: Could not open dump: %d %s\n", errno, strerror(errno));
pthread_mutex_unlock(&tool_context->pcap_mutex);
return -1;
}

fwrite(&pcap_hdr_t, sizeof(pcap_hdr_t), 1, tool_context->dump_filep);
fwrite(&pcap_hdr_t, sizeof(pcap_hdr_t), 1, new_file);

if (!tool_context->coconut_context->quiet && tool_context->dump_filep != stdout) {
if (!tool_context->coconut_context->quiet && new_file != stdout) {
fprintf(stderr, "Opened PCAP file '%s'\n", tool_context->pcap_fname);
}

if (old_file != NULL) {
fclose(old_file);
}
tool_context->dump_filep = new_file;

pthread_mutex_unlock(&tool_context->pcap_mutex);

return 1;
}

int reopen_pcap_file(struct coconut_tool_context *tool_context) {
if (tool_context->dump_filep == NULL) {
return 1;
}

return do_open_pcap_file(tool_context);

}

int open_coconut_pcap(struct coconut_tool_context *tool_context) {
tool_context->enable_pcap_log = true;
pthread_mutex_init(&tool_context->pcap_mutex, NULL);

return do_open_pcap_file(tool_context);
}

void ANSI_PREP() {
#ifdef _WIN32
HANDLE *console;
Expand Down Expand Up @@ -872,6 +899,12 @@ void print_usage(char *argv) {
" --quiet Disable most output\n");
}

struct coconut_tool_context tool_context;

void sighup_handler() {
reopen_pcap_file(&tool_context);
}

int main(int argc, char *argv[]) {
#define OPT_HELP 'h'
#define OPT_LIST 2
Expand Down Expand Up @@ -908,11 +941,12 @@ int main(int argc, char *argv[]) {
int r;

struct userspace_wifi_context *context;
struct coconut_tool_context tool_context;
struct wifi_coconut_context *coconut_context;

memset(&tool_context, 0, sizeof(struct coconut_tool_context));

signal(SIGHUP, sighup_handler);

tool_context.coconut_context = init_coconut_context();
coconut_context = tool_context.coconut_context;

Expand Down
8 changes: 5 additions & 3 deletions wifi_coconut/wifi_coconut.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ int find_coconuts_cluster(struct wifi_coconut_context *coconut_context,

*coconut = matched_coconuts;

/* If we didn't find any coconuts on a nested bus, look for just 14 radios. some platforms
/* If we didn't find any coconuts on a nested bus, look for just any radios. some platforms
* like catalina are having real trouble exposing the bus tree through libusb right now.
*/
if (*coconut == NULL) {
Expand All @@ -268,9 +268,11 @@ int find_coconuts_cluster(struct wifi_coconut_context *coconut_context,
device = device->next;
}

/* If we found at least 14 raw compatible devices, just make a coconut out of them
/* If we found any raw compatible devices, just make a coconut out of them
* and call it good enough */
if (num_raw_devices == 14) {
if (num_raw_devices > 0) {
fprintf(stderr, "Falling back to raw WiFi devices. Found %d\n", num_raw_devices);

coconut_iter = (struct wifi_coconut *) malloc(sizeof(*coconut_iter));
if (coconut_iter == NULL) {
goto failure;
Expand Down