aboutsummaryrefslogtreecommitdiff
path: root/src/core.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core.cpp')
-rw-r--r--src/core.cpp133
1 files changed, 109 insertions, 24 deletions
diff --git a/src/core.cpp b/src/core.cpp
index 8cb595f..69d38cf 100644
--- a/src/core.cpp
+++ b/src/core.cpp
@@ -1,4 +1,5 @@
-#include <stdlib.h>
+#include <unistd.h>
+#include <cstdlib>
#include <iostream>
#include <pcap.h>
#include <cinttypes>
@@ -7,11 +8,13 @@
#include "../third-party/radiotap-library/radiotap.h"
#include "wifi-triangulator/core.h"
-using std::string;
+using namespace std;
using namespace wifi_triangulator::pb;
namespace wifi_triangulator {
+std::string app_name;
+
struct ieee802_11_header {
uint8_t frame_1;
uint8_t frame_2;
@@ -97,6 +100,7 @@ void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *pa
auto *present_ptr = &rtaphdr->it_present;
bool is_radiotap = true;
+ uint16_t mhz = 0;
int rssi = 0;
do {
uint32_t present = *present_ptr;
@@ -128,7 +132,7 @@ void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *pa
present &= ~(1 << IEEE80211_RADIOTAP_RATE);
}
if (present & 1 << IEEE80211_RADIOTAP_CHANNEL) {
- uint16_t mhz = it.read16u();
+ mhz = it.read16u();
uint16_t bitmap = it.read16u();
snprintf(buf, 100, "IEEE80211_RADIOTAP_CHANNEL: %d MHz, flags=%04x\n", mhz, bitmap);
@@ -181,7 +185,7 @@ void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *pa
if (present & 1 << IEEE80211_RADIOTAP_ANTENNA) {
uint8_t antenna = it.read8u();
- snprintf(buf, 100, "IEEE80211_RADIOTAP_ANTENNA: antenna=%d\n", antenna);
+// snprintf(buf, 100, "IEEE80211_RADIOTAP_ANTENNA: antenna=%d\n", antenna);
present &= ~(1 << IEEE80211_RADIOTAP_ANTENNA);
}
if (present & 1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) {
@@ -199,7 +203,7 @@ void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *pa
if (present & 1 << IEEE80211_RADIOTAP_RX_FLAGS) {
uint16_t rx_flags = it.read16u();
- snprintf(buf, 100, "IEEE80211_RADIOTAP_RX_FLAGS: rx_flags=%d\n", rx_flags);
+// snprintf(buf, 100, "IEEE80211_RADIOTAP_RX_FLAGS: rx_flags=%d\n", rx_flags);
present &= ~(1 << IEEE80211_RADIOTAP_RX_FLAGS);
}
if (present & 1 << IEEE80211_RADIOTAP_TX_FLAGS) {
@@ -214,7 +218,8 @@ void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *pa
break;
}
-// printf(buf);
+// fprintf(stderr, buf);
+// fflush(stderr);
/*
int bit = 0;
uint32_t mask = 1;
@@ -235,43 +240,43 @@ void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *pa
uint8_t type = ieee802_11_header->frame_1;
- packet_type t;
+ frame_type t;
switch (type) {
case 0x40:
- t = packet_type::probe_request;
+ t = frame_type::probe_request;
break;
case 0x80:
- t = packet_type::beacon;
+ t = frame_type::beacon;
break;
case 0x50:
- t = packet_type::probe_response;
+ t = frame_type::probe_response;
break;
case 0x48:
- t = packet_type::null;
+ t = frame_type::null;
break;
case 0xd4:
- t = packet_type::ack;
+ t = frame_type::ack;
break;
case 0x08:
- t = packet_type::data;
+ t = frame_type::data;
break;
case 0xc4:
- t = packet_type::cts;
+ t = frame_type::cts;
break;
case 0xb4:
- t = packet_type::rts;
+ t = frame_type::rts;
break;
case 0x1e:
- t = packet_type::cf_end;
+ t = frame_type::cf_end;
break;
case 0x1f:
- t = packet_type::cf_end_cf_ack;
+ t = frame_type::cf_end_cf_ack;
break;
case 0x1a:
- t = packet_type::ps_poll;
+ t = frame_type::ps_poll;
break;
default:
- t = packet_type::unknown;
+ t = frame_type::unknown;
}
// printf("ieee802_11_header->frame_ctl=%02x, %s\n", type, type_str ? type_str : "???");
@@ -299,7 +304,8 @@ void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *pa
ctx->data_consumer(data{
t,
header->ts.tv_sec, header->ts.tv_usec,
- rssi, src, dst});
+ rssi, mhz,
+ src, dst});
}
int launch_capture(string dev, std::function<void(const class data &)> data_consumer) {
@@ -336,12 +342,10 @@ int launch_capture(string dev, std::function<void(const class data &)> data_cons
auto ret = pcap_loop(handle, 1000, got_packet, reinterpret_cast<u_char *>(&ctx));
if (ret == -1) {
- printf("pcap failed: %s\n",
- pcap_geterr(handle)
- );
+ printf("pcap failed: %s\n", pcap_geterr(handle));
break;
} else if (ret == -2) {
-// someone called break loop
+ // someone called break loop
break;
}
}
@@ -351,4 +355,85 @@ int launch_capture(string dev, std::function<void(const class data &)> data_cons
return EXIT_SUCCESS;
}
+int launch_reader(std::function<bool(uint16_t size, const uint8_t *data)> on_buffer) {
+ uint8_t buffer[1 << 16];
+ while (true) {
+ uint16_t size;
+
+ if (read_stdin(reinterpret_cast<uint8_t *>(&size), 2) < 0) {
+ break;
+ }
+
+ ssize_t got;
+ if ((got = read_stdin(buffer, size)) != size) {
+ cout << "short read, got=" << got << endl;
+ return EXIT_FAILURE;
+ }
+
+ bool ok = on_buffer(size, buffer);
+
+ if (!ok) {
+ break;
+ }
+ }
+
+ return EXIT_SUCCESS;
+}
+
+int launch_reader_envelope(std::function<bool(const pb::envelope &)> on_envelope) {
+ pb::envelope envelope;
+
+ return launch_reader([&](uint16_t size, const uint8_t *data) {
+ bool ok = envelope.ParseFromArray(data, size);
+ return ok && on_envelope(envelope);
+ });
+}
+
+ssize_t read_stdin(uint8_t *data, const size_t count) {
+ auto left = count;
+ while (left > 0) {
+ ssize_t n_read = read(STDIN_FILENO, data, left);
+
+ if (n_read < 0) {
+ return n_read;
+ }
+
+ left -= n_read;
+ }
+
+ return count;
+}
+
+//void write_stdout(const bstring &s) {
+// write_stdout(s.c_str(), s.length());
+//}
+
+void write_stdout(const std::string &s) {
+ static_assert(sizeof(char) == sizeof(uint8_t), "bad sizes");
+ write_stdout(reinterpret_cast<const uint8_t *>(s.c_str()), s.length());
+}
+
+void write_stdout(const uint8_t *data, size_t count) {
+ size_t left = count;
+ const uint8_t *buf = data;
+ while (left > 0) {
+ auto written = write(STDOUT_FILENO, data, left);
+ buf += written;
+ left -= written;
+ }
+ syncfs(STDOUT_FILENO);
+}
+
+void write_envelope(pb::letter_type lt, pb::envelope &envelope) {
+ std::string str;
+ str.reserve(64 * 1024);
+
+ envelope.set_type(lt);
+
+ envelope.SerializeToString(&str);
+ uint16_t len = static_cast<uint16_t>(str.length());
+ write_stdout(reinterpret_cast<uint8_t *>(&len), 2);
+ write_stdout(str);
+}
+
} // namespace wifi_triangulator