diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2017-04-07 19:03:05 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2017-04-07 19:03:05 +0200 |
commit | 36e59a1991f075e36e117a08321d5e4c4dc00eac (patch) | |
tree | 2c89d0b9ad67eb6ebdcd5ed4b1789ebc61ce6723 | |
parent | 84939234eb66fe7957eaf39956f18224e3108c25 (diff) | |
download | wifi-triangulator-36e59a1991f075e36e117a08321d5e4c4dc00eac.tar.gz wifi-triangulator-36e59a1991f075e36e117a08321d5e4c4dc00eac.tar.bz2 wifi-triangulator-36e59a1991f075e36e117a08321d5e4c4dc00eac.tar.xz wifi-triangulator-36e59a1991f075e36e117a08321d5e4c4dc00eac.zip |
o Splitting capture into just two, capture and send to stdout and transmit from stdin.
o Adding formatter that formats the incoming message.
-rw-r--r-- | CMakeLists.txt | 14 | ||||
-rw-r--r-- | capture.cpp | 108 | ||||
-rw-r--r-- | src/capture.cpp | 47 | ||||
-rw-r--r-- | src/core.cpp (renamed from core.cpp) | 18 | ||||
-rw-r--r-- | src/formatter.cpp | 54 | ||||
-rw-r--r-- | src/misc.cpp (renamed from misc.cpp) | 0 | ||||
-rw-r--r-- | src/receiver.cpp (renamed from receiver.cpp) | 9 | ||||
-rw-r--r-- | src/transmitter.cpp | 79 | ||||
-rw-r--r-- | wifi-triangulator.proto | 13 |
9 files changed, 203 insertions, 139 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6fa065d..141beda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ include_directories(${Protobuf_INCLUDE_DIRS} ${Protobuf_INCLUDE_DIR}) protobuf_generate_cpp(PROTO_SRC PROTO_HEADER wifi-triangulator.proto) add_library(wifi-triangulator - core.cpp misc.cpp + src/core.cpp src/misc.cpp ${PROTO_SRC} ${PROTO_HEADER} include/wifi-triangulator/core.h @@ -34,9 +34,17 @@ target_include_directories(wifi-triangulator PUBLIC include ${CMAKE_CURRENT_BINA target_link_libraries(wifi-triangulator PUBLIC pcap ${Protobuf_LIBRARIES}) # capture -add_executable(capture capture.cpp) +add_executable(capture src/capture.cpp) target_link_libraries(capture PUBLIC wifi-triangulator) +# transmitter +add_executable(transmitter src/transmitter.cpp) +target_link_libraries(transmitter PUBLIC wifi-triangulator) + # receiver -add_executable(receiver receiver.cpp) +add_executable(receiver src/receiver.cpp) target_link_libraries(receiver PUBLIC wifi-triangulator) + +# formatter +add_executable(formatter src/formatter.cpp) +target_link_libraries(formatter PUBLIC wifi-triangulator) diff --git a/capture.cpp b/capture.cpp deleted file mode 100644 index 7e51fcd..0000000 --- a/capture.cpp +++ /dev/null @@ -1,108 +0,0 @@ -#include <iostream> -#include "wifi-triangulator/core.h" -#include "wifi-triangulator.pb.h" - -#include <sstream> -#include <arpa/inet.h> -#include <netinet/in.h> -#include <stdio.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <unistd.h> -#include <exception> -#include <cstring> - -using namespace std; -using namespace wifi_triangulator; - -class output { -public: - virtual void handle(const data &) = 0; -}; - -class human_output : public output { -public: - void handle(const data &data) { - printf("timestamp=%lu, rssi=%d, src=%s, dst=%s, type=%s\n", ((data.sec * 1000 * 1000) + data.usec) * 1000, - data.rssi, data.src.to_string().c_str(), data.dst.to_string().c_str(), to_string(data.type).c_str()); - fflush(stdout); - }; -}; - -class protobuf_output : public output { - int s; - struct sockaddr_in addr_si; - struct sockaddr *addr; - socklen_t addr_len; - -public: - protobuf_output(string host, uint16_t port) { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) - throw std::runtime_error("socket"); - - - memset((char *) &addr_si, 0, sizeof(addr_si)); - addr_si.sin_family = AF_INET; - addr_si.sin_port = htons(port); - if (inet_aton(host.c_str(), &addr_si.sin_addr) == 0) { - throw std::runtime_error("inet_aton() failed"); - } - - addr = reinterpret_cast<struct sockaddr *>(&addr_si); - addr_len = sizeof(addr_si); - } - - ~protobuf_output() { - google::protobuf::ShutdownProtobufLibrary(); - } - - void handle(const data &data) override { - pb::envelope envelope; - envelope.set_type(data.type); - - if (data.type == pb::packet_type::probe_request) { - pb::probe *probe = envelope.mutable_probe(); - probe->set_src(data.src); - probe->set_dst(static_cast<uint64_t>(data.dst)); - probe->set_rssi(data.rssi); - } - - std::string str; - envelope.SerializeToString(&str); - - if (sendto(s, str.c_str(), str.length(), 0, addr, addr_len) == -1) { - throw std::runtime_error("sendto failed"); - } - cout << flush; - - static int count = 0; - cerr << "count=" << count << ", len=" << str.length() << "!\r" << flush; - count++; - } -}; - -bool human = false; - -int main(int argc, char *argv[]) { - if (argc != 3) { - fprintf(stderr, "usage: %s [interface] [host]\n", argv[0]); - return EXIT_FAILURE; - } - - string dev = argv[1]; - string host = argv[2]; - - output *output; - if (human) { - output = new human_output; - } else { - cerr << "Sending to stdout" << flush << endl; - output = new protobuf_output(host, 3333); - } - - return launch_capture(dev, [&](const data &data) { - output->handle(data); - }); -} diff --git a/src/capture.cpp b/src/capture.cpp new file mode 100644 index 0000000..5844b25 --- /dev/null +++ b/src/capture.cpp @@ -0,0 +1,47 @@ +#include <iostream> +#include "wifi-triangulator/core.h" + +using namespace std; +using namespace wifi_triangulator; + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "usage: %s [interface]\n", argv[0]); + return EXIT_FAILURE; + } + + string dev = argv[1]; + + GOOGLE_PROTOBUF_VERIFY_VERSION; + + string str; + str.reserve(1 << 16); + + int ret = launch_capture(dev, [&](const data &data) { + pb::envelope envelope; + envelope.set_time_s(data.sec); + envelope.set_time_us(data.usec); + envelope.set_type(data.type); + + if (data.type == pb::packet_type::probe_request) { + pb::probe *probe = envelope.mutable_probe(); + probe->set_src(data.src); + probe->set_dst(static_cast<uint64_t>(data.dst)); + probe->set_rssi(data.rssi); + } + + envelope.SerializeToString(&str); + cout << static_cast<uint16_t>(str.length()) << str << flush; + +// static int count = 0; +// cerr << "count=" << count << "!\r" << flush; +// count++; + }); + + google::protobuf::ShutdownProtobufLibrary(); + + cerr << "Capture exiting" << endl; + cout.flush(); + + return ret; +} @@ -4,7 +4,7 @@ #include <cinttypes> #include <sstream> #include <iomanip> -#include "third-party/radiotap-library/radiotap.h" +#include "../third-party/radiotap-library/radiotap.h" #include "wifi-triangulator/core.h" using std::string; @@ -72,7 +72,7 @@ struct radio_tap_it { }; struct capture_context { - std::function<void( const data &)> data_consumer; + std::function<void(const data &)> data_consumer; }; void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { @@ -311,23 +311,13 @@ int launch_capture(string dev, std::function<void(const class data &)> data_cons // handle = pcap_open_offline(dev, errbuf); if (!handle) { - fprintf(stderr, - "Could not open %s: %s\n", dev. - - c_str(), errbuf - - ); + fprintf(stderr, "Could not open %s: %s\n", dev.c_str(), errbuf); return EXIT_FAILURE; } int i = pcap_datalink(handle); if (i != DLT_IEEE802_11_RADIO) { - fprintf(stderr, - "Device %s doesn't provide IEEE 802.11 radio headers\n", dev. - - c_str() - - ); + fprintf(stderr, "Device %s doesn't provide IEEE 802.11 radio headers\n", dev.c_str()); return EXIT_FAILURE; } diff --git a/src/formatter.cpp b/src/formatter.cpp new file mode 100644 index 0000000..b165a89 --- /dev/null +++ b/src/formatter.cpp @@ -0,0 +1,54 @@ +#include <iostream> +#include "wifi-triangulator/core.h" + +using namespace std; +using namespace wifi_triangulator; + +void on_probe_request(const pb::probe &p) { + cout << "PROBE REQUEST" + << ", src=" << eth_mac(p.src()).to_string() + << ", dst=" << eth_mac(p.dst()).to_string() + << ", rssi=" << p.rssi() + << flush + << endl; +} + +void on_envelope(pb::envelope envelope) { + if (envelope.type() == pb::probe_request) { + on_probe_request(envelope.probe()); + } else { + cout << to_string(envelope.type()) << endl; + } +} + +int main(int argc, char *argv[]) { + GOOGLE_PROTOBUF_VERIFY_VERSION; + + if (argc != 1) { + fprintf(stderr, "usage: %s\n", argv[0]); + return EXIT_FAILURE; + } + + int count = 0; + uint8_t buffer[1 << 16]; + while (!feof(stdin)) { + pb::envelope envelope; + + uint16_t size; + cin >> size; + cin.read(reinterpret_cast<char *>(buffer), size); + bool ok = envelope.ParseFromArray(buffer, size); + if (!ok) { + cerr << "bad read" << endl; + continue; + } + +// cerr << "count=" << count++ << endl; + + on_envelope(envelope); + } + + google::protobuf::ShutdownProtobufLibrary(); + + return EXIT_SUCCESS; +} diff --git a/receiver.cpp b/src/receiver.cpp index c6c8f72..c6c5bdc 100644 --- a/receiver.cpp +++ b/src/receiver.cpp @@ -1,16 +1,9 @@ #include "wifi-triangulator/core.h" -#include "wifi-triangulator.pb.h" #include <iostream> #include <unistd.h> #include <arpa/inet.h> -#include <netinet/in.h> -#include <stdio.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <unistd.h> -#include <exception> #include <cstring> using namespace std; @@ -46,8 +39,6 @@ int main(int argc, char *argv[]) { throw std::runtime_error("bind"); } - sockaddr *me = reinterpret_cast<sockaddr *>(&si_me); - pb::envelope envelope; while (true) { size_t len = 64 * 1024; diff --git a/src/transmitter.cpp b/src/transmitter.cpp new file mode 100644 index 0000000..047d37d --- /dev/null +++ b/src/transmitter.cpp @@ -0,0 +1,79 @@ +#include <iostream> +#include "wifi-triangulator/core.h" + +#include <arpa/inet.h> +#include <cstring> +#include <unistd.h> + +using namespace std; +using namespace wifi_triangulator; + +class human_output { +public: + void handle(const data &data) { + printf("timestamp=%lu, rssi=%d, src=%s, dst=%s, type=%s\n", ((data.sec * 1000 * 1000) + data.usec) * 1000, + data.rssi, data.src.to_string().c_str(), data.dst.to_string().c_str(), to_string(data.type).c_str()); + fflush(stdout); + }; +}; + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "usage: %s [host]\n", argv[0]); + return EXIT_FAILURE; + } + + string host = argv[1]; + + GOOGLE_PROTOBUF_VERIFY_VERSION; + + int s; + struct sockaddr_in addr_si; + struct sockaddr *addr; + socklen_t addr_len; + + if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { + throw std::runtime_error("socket"); + } + + memset((char *) &addr_si, 0, sizeof(addr_si)); + addr_si.sin_family = AF_INET; + addr_si.sin_port = htons(3333); + if (inet_aton(host.c_str(), &addr_si.sin_addr) == 0) { + throw std::runtime_error("Could not resolve " + host); + } + + addr = reinterpret_cast<struct sockaddr *>(&addr_si); + addr_len = sizeof(addr_si); + + int count = 0; + uint8_t buffer[1 << 16]; + while (!feof(stdin)) { + pb::envelope envelope; + + uint16_t size; + + cin >> size; + cin.read(reinterpret_cast<char *>(buffer), size); + + /* + bool ok = envelope.ParseFromArray(buffer, size); + if (!ok) { + cerr << "bad read" << endl; + continue; + } + */ + + if (sendto(s, buffer, size, 0, addr, addr_len) == -1) { + throw std::runtime_error("sendto failed"); + } + + cerr << "count=" << count++ << endl; + } + + cerr << "transmitter exiting" << endl; + + google::protobuf::ShutdownProtobufLibrary(); + + return EXIT_SUCCESS; +} diff --git a/wifi-triangulator.proto b/wifi-triangulator.proto index 7daf9bf..959a370 100644 --- a/wifi-triangulator.proto +++ b/wifi-triangulator.proto @@ -18,12 +18,15 @@ enum packet_type { }; message probe { - sint32 rssi = 2; - fixed64 src = 3; - fixed64 dst = 4; + sint32 rssi = 1; + fixed64 src = 2; + fixed64 dst = 3; } message envelope { - packet_type type = 1; - probe probe = 2; + sfixed64 time_s = 1; + sfixed64 time_us = 2; + + packet_type type = 3; + probe probe = 20; } |