diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f1cf77d..a9f2e31d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,9 @@ target_link_libraries(injection_rate_test PRIVATE ${WB_TARGET_LINK_LIBRARIES}) add_executable(example_pollute executables/example_pollute.cpp) target_link_libraries(example_pollute PRIVATE ${WB_TARGET_LINK_LIBRARIES}) +add_executable(example_udp_rx "executables/example_udp_rx.cpp") +target_link_libraries(example_udp_rx PRIVATE ${WB_TARGET_LINK_LIBRARIES}) + # When it is a static library, we don't need to install it. # But if it is a shared library, we need to install it. #install(TARGETS wifibroadcast DESTINATION lib) diff --git a/executables/example_udp_rx.cpp b/executables/example_udp_rx.cpp new file mode 100644 index 00000000..cea479db --- /dev/null +++ b/executables/example_udp_rx.cpp @@ -0,0 +1,74 @@ +// +// Created by consti10 on 01.07.23. +// + +#include "../src/WBStreamRx.h" +#include "../src/WBStreamTx.h" +#include "../src/ExtTxRx.h" +#include "../src/wifibroadcast_spdlog.h" + +/** + * Simple demo application that sends out hello messages and listens for hello messages. + * You can run it either on 2 different systems (air unit and ground unit) + * or run it on the same system with 2 different wifi cards for testing. + * + * On success, you should see the air unit talking to the ground unit and vice versa. + * + * NOTE: Card(s) need to be in monitor mode & on match on the selected frequency ! + * NOTE: This example is as simple as possible, and therefore doesn't even need to use the WBStreamTX / WBStreamRX pair + */ +int main(int argc, char *const *argv) { + bool advanced_debugging= false; + bool is_air= false; + bool air_or_ground_explicitly_specified= false; + int opt; + + if(!air_or_ground_explicitly_specified){ + std::cerr<<"Warning - please specify air or ground, air only talks to ground and vice versa"< cards; + ExtTxRx::UdpWifiCard tmp_card{4321}; + cards.push_back(tmp_card); + ExtTxRx::Options options_txrx{}; + options_txrx.pcap_rx_set_direction = true; + options_txrx.use_gnd_identifier=!is_air; + if(advanced_debugging){ + options_txrx.log_all_received_validated_packets= true; + options_txrx.advanced_debugging_rx= true; + } + + std::shared_ptr txrx = std::make_shared(cards, options_txrx); + + txrx->start_receiving(); + + WBTxRx::OUTPUT_DATA_CALLBACK cb=[](uint64_t nonce,int wlan_index,const uint8_t radioPort,const uint8_t *data, const std::size_t data_len){ + std::string message((const char*)data,data_len); + fmt::print("Got packet[{}]\n",message); + }; + txrx->rx_register_callback(cb); + + auto lastLog=std::chrono::steady_clock::now(); + int packet_index=0; + while (true){ + auto message=is_air ? fmt::format("Air says hello {}",packet_index) : fmt::format("Ground says hello {}",packet_index); + packet_index++; + + // Just use radio port 0 - we don't need multiplexing in this example + // This message is injected on the wifi card + txrx->tx_inject_packet(0,(uint8_t*)message.data(),message.size()); + + std::this_thread::sleep_for(std::chrono::milliseconds (1000)); + const auto elapsed_since_last_log=std::chrono::steady_clock::now()-lastLog; + if(elapsed_since_last_log>std::chrono::seconds(4)){ + lastLog=std::chrono::steady_clock::now(); + auto txStats=txrx->get_tx_stats(); + auto rxStats=txrx->get_rx_stats(); + auto rx_stats_card0=txrx->get_rx_stats_for_card(0); + std::cout<debug("Got packet {} {}",wlan_idx,pkt_len); } - const auto parsedPacket = RadiotapHelper::process_received_radiotap_packet(pkt,pkt_len); + const auto& rx_iee80211_hdr_openhd = *((Ieee80211HeaderOpenHD*)pkt); - if (parsedPacket == std::nullopt) - { - if(m_options.advanced_debugging_rx) - { - m_console->warn("Discarding packet due to pcap parsing error!"); - } - return; - } - - const uint8_t *pkt_payload = parsedPacket->payload; - const size_t pkt_payload_size = parsedPacket->payloadSize; + const uint8_t *pkt_payload = pkt+24; + const size_t pkt_payload_size = pkt_len+28; m_rx_stats.count_p_any++; m_rx_stats.count_bytes_any+=pkt_payload_size; @@ -251,17 +242,6 @@ void ExtTxRx::on_new_packet(const uint8_t wlan_idx,const uint8_t *pkt,const int m_pollution_total_rx_packets++; } - if (parsedPacket->frameFailedFCSCheck) - { - if(m_options.advanced_debugging_rx) - { - m_console->debug("Discarding packet due to bad FCS!"); - } - - return; - } - - const auto& rx_iee80211_hdr_openhd=*((Ieee80211HeaderOpenHD*)parsedPacket->ieee80211Header); //m_console->debug(parsedPacket->ieee80211Header->header_as_string()); if (!rx_iee80211_hdr_openhd.is_data_frame()) { @@ -274,16 +254,17 @@ void ExtTxRx::on_new_packet(const uint8_t wlan_idx,const uint8_t *pkt,const int } // All these edge cases should NEVER happen if using a proper tx/rx setup and the wifi driver isn't complete crap - if (parsedPacket->payloadSize <= 0 || parsedPacket->payloadSize > RAW_WIFI_FRAME_MAX_PAYLOAD_SIZE) + if (pkt_payload_size <= 0) { m_console->warn("Discarding packet due to no actual payload !"); return; } // Generic packet validation end - now to the openhd specific validation(s) - if (parsedPacket->payloadSize > RAW_WIFI_FRAME_MAX_PAYLOAD_SIZE) + if (pkt_payload_size > RAW_WIFI_FRAME_MAX_PAYLOAD_SIZE) { - m_console->warn("Discarding packet due to payload exceeding max {}",(int) parsedPacket->payloadSize); + m_console->warn("Discarding packet due to payload exceeding max {}", + (int)pkt_payload_size); return; } @@ -377,7 +358,7 @@ void ExtTxRx::on_new_packet(const uint8_t wlan_idx,const uint8_t *pkt,const int /*if(wlan_idx!=0){ return ; }*/ - SessionKeyPacket &sessionKeyPacket = *((SessionKeyPacket*) parsedPacket->payload); + SessionKeyPacket& sessionKeyPacket = *((SessionKeyPacket*)pkt_payload); const auto decrypt_res=m_decryptor->onNewPacketSessionKeyData(sessionKeyPacket.sessionKeyNonce, sessionKeyPacket.sessionKeyData); if(decrypt_res==wb::Decryptor::SESSION_VALID_NEW || decrypt_res==wb::Decryptor::SESSION_VALID_NOT_NEW) @@ -452,64 +433,64 @@ void ExtTxRx::on_new_packet(const uint8_t wlan_idx,const uint8_t *pkt,const int PerCardCalculators& this_wifi_card_calc= *m_per_card_calc.at(wlan_idx); if(m_options.debug_rssi>=2) { - m_console->debug("{}",all_rssi_to_string(parsedPacket->allAntennaValues)); + //m_console->debug("{}",all_rssi_to_string(parsedPacket->allAntennaValues)); } // assumes driver gives 1st and 2nd antenna as 2nd and 3rd value - if(parsedPacket->allAntennaValues.size()>=1) - { - const auto rssi=parsedPacket->allAntennaValues[0].rssi; - auto opt_minmaxavg= this_wifi_card_calc.card_rssi.add_and_recalculate_if_needed(rssi); - if(opt_minmaxavg.has_value()) - { - if(m_options.debug_rssi>=1) - { - m_console->debug("Card{}:{}",wlan_idx, RSSIAccumulator::min_max_avg_to_string(opt_minmaxavg.value(), false)); - } - } - } - - if(parsedPacket->allAntennaValues.size()>=2) - { - const auto rssi=parsedPacket->allAntennaValues[1].rssi; - auto opt_minmaxavg= this_wifi_card_calc.antenna1_rssi.add_and_recalculate_if_needed(rssi); - if(opt_minmaxavg.has_value()){ - this_wifi_card_stats.antenna1_dbm=opt_minmaxavg.value().avg; - if(m_options.debug_rssi>=1){ - m_console->debug("Card{} Antenna{}:{}",wlan_idx,0, RSSIAccumulator::min_max_avg_to_string(opt_minmaxavg.value(), false)); - } - } - } - - if(parsedPacket->allAntennaValues.size()>=3) - { - const auto rssi=parsedPacket->allAntennaValues[2].rssi; - auto opt_minmaxavg= this_wifi_card_calc.antenna2_rssi.add_and_recalculate_if_needed(rssi); - if(opt_minmaxavg.has_value()){ - this_wifi_card_stats.antenna2_dbm=opt_minmaxavg.value().avg; - if(m_options.debug_rssi>=1){ - m_console->debug("Card{} Antenna{}:{}",wlan_idx,1, RSSIAccumulator::min_max_avg_to_string(opt_minmaxavg.value(), false)); - } - } - } + //if(parsedPacket->allAntennaValues.size()>=1) + //{ + // const auto rssi=parsedPacket->allAntennaValues[0].rssi; + // auto opt_minmaxavg= this_wifi_card_calc.card_rssi.add_and_recalculate_if_needed(rssi); + // if(opt_minmaxavg.has_value()) + // { + // if(m_options.debug_rssi>=1) + // { + // m_console->debug("Card{}:{}",wlan_idx, RSSIAccumulator::min_max_avg_to_string(opt_minmaxavg.value(), false)); + // } + // } + //} + + //if(parsedPacket->allAntennaValues.size()>=2) + //{ + // const auto rssi=parsedPacket->allAntennaValues[1].rssi; + // auto opt_minmaxavg= this_wifi_card_calc.antenna1_rssi.add_and_recalculate_if_needed(rssi); + // if(opt_minmaxavg.has_value()){ + // this_wifi_card_stats.antenna1_dbm=opt_minmaxavg.value().avg; + // if(m_options.debug_rssi>=1){ + // m_console->debug("Card{} Antenna{}:{}",wlan_idx,0, RSSIAccumulator::min_max_avg_to_string(opt_minmaxavg.value(), false)); + // } + // } + //} + + //if(parsedPacket->allAntennaValues.size()>=3) + //{ + // const auto rssi=parsedPacket->allAntennaValues[2].rssi; + // auto opt_minmaxavg= this_wifi_card_calc.antenna2_rssi.add_and_recalculate_if_needed(rssi); + // if(opt_minmaxavg.has_value()){ + // this_wifi_card_stats.antenna2_dbm=opt_minmaxavg.value().avg; + // if(m_options.debug_rssi>=1){ + // m_console->debug("Card{} Antenna{}:{}",wlan_idx,1, RSSIAccumulator::min_max_avg_to_string(opt_minmaxavg.value(), false)); + // } + // } + //} this_wifi_card_stats.count_p_valid++; - if(parsedPacket->mcs_index.has_value()) - { - m_rx_stats.last_received_packet_mcs_index=parsedPacket->mcs_index.value(); - } - - if(parsedPacket->channel_width.has_value()) - { - m_rx_stats.last_received_packet_channel_width=parsedPacket->channel_width.value(); - } - - if(parsedPacket->signal_quality.has_value()) - { - this_wifi_card_calc.signal_quality.add_signal_quality(parsedPacket->signal_quality.value()); - this_wifi_card_stats.signal_quality=this_wifi_card_calc.signal_quality.get_current_signal_quality(); - } + //if(parsedPacket->mcs_index.has_value()) + //{ + // m_rx_stats.last_received_packet_mcs_index=parsedPacket->mcs_index.value(); + //} + + //if(parsedPacket->channel_width.has_value()) + //{ + // m_rx_stats.last_received_packet_channel_width=parsedPacket->channel_width.value(); + //} + + //if(parsedPacket->signal_quality.has_value()) + //{ + // this_wifi_card_calc.signal_quality.add_signal_quality(parsedPacket->signal_quality.value()); + // this_wifi_card_stats.signal_quality=this_wifi_card_calc.signal_quality.get_current_signal_quality(); + //} if(wlan_idx==0) { diff --git a/src/ExtTxRx.h b/src/ExtTxRx.h index 4cd6ed2e..5cc6232c 100644 --- a/src/ExtTxRx.h +++ b/src/ExtTxRx.h @@ -2,8 +2,8 @@ // Created by consti10 on 27.06.23. // -#ifndef WIFIBROADCAST_WBTXRX_H -#define WIFIBROADCAST_WBTXRX_H +#ifndef WIFIBROADCAST_EXTTXRX_H +#define WIFIBROADCAST_EXTTXRX_H #include #include