Skip to content
commsignia_layer.cc 5.75 KiB
Newer Older
garciay's avatar
garciay committed
#include <algorithm>
#include <cctype>
#include <arpa/inet.h>

#include "commsignia_layer_factory.hh"

#include "loggers.hh"

#include "converter.hh"

garciay's avatar
garciay committed
commsignia_layer::commsignia_layer(const std::string & p_type, const std::string & param) : layer(p_type), _params(), _c2p_recv{0}, _802_11p_hdr{0}, _c2p_llc_hdr{0}, _mac_src(), _eth_type() {
garciay's avatar
garciay committed
  loggers::get_instance().log(">>> commsignia_layer::commsignia_layer: %s, %s", to_string().c_str(), param.c_str());
  
  // Setup parameters
garciay's avatar
garciay committed
  params::convert(_params, param);
  params::const_iterator it = _params.find(params::mac_src);
garciay's avatar
garciay committed
  if (it == _params.cend()) {
    _params.insert(std::pair<std::string, std::string>(std::string("mac_src"), "000000000000"));
  }
garciay's avatar
garciay committed
  _mac_src = converter::get_instance().hexa_to_bytes(_params[params::mac_src]);
  it = _params.find(params::mac_bc);
garciay's avatar
garciay committed
  if (it == _params.cend()) {
    _params.insert(std::pair<std::string, std::string>(std::string("mac_bc"), "FFFFFFFFFFFF"));
  }
garciay's avatar
garciay committed
  it = _params.find(params::eth_type);
garciay's avatar
garciay committed
  if (it == _params.cend()) {
    _params.insert(std::pair<std::string, std::string>(std::string("eth_type"), "8947"));
  }
garciay's avatar
garciay committed
  _eth_type = converter::get_instance().hexa_to_bytes(_params[params::eth_type]);
  it = _params.find(params::interface_id);
garciay's avatar
garciay committed
  if (it == _params.cend()) {
    _params.insert(std::pair<std::string, std::string>(std::string("interface_id"), "1"));
  }
  //_params.log();
}

garciay's avatar
garciay committed
void commsignia_layer::send_data(OCTETSTRING& data, params& params) {
garciay's avatar
garciay committed
  loggers::get_instance().log_msg(">>> commsignia_layer::send_data: ", data);
garciay's avatar
garciay committed
void commsignia_layer::receive_data(OCTETSTRING& data, params& params) {
garciay's avatar
garciay committed
  loggers::get_instance().log_msg(">>> commsignia_layer::receive_data: ", data);
garciay's avatar
garciay committed
  
  const unsigned char* p = static_cast<const unsigned char *>(data);

  const commsignia_layer::c2p_recv* r = (const commsignia_layer::c2p_recv*)p;
garciay's avatar
garciay committed
  //loggers::get_instance().log("commsignia_layer::receive_data: version=%02x", r->version);
  //loggers::get_instance().log("commsignia_layer::receive_data: timestamp1=%08x", ntohl(r->timestamp_sec));
  //loggers::get_instance().log("commsignia_layer::receive_data: timestamp2=%08x", ntohl(r->timestamp_msec));
  //loggers::get_instance().log("commsignia_layer::receive_data: timestamp2=%08x", ntohl(r->timestamp_msec));
  //loggers::get_instance().log("commsignia_layer::receive_data: antenna=%02x", r->antenna);
  //loggers::get_instance().log("commsignia_layer::receive_data: rssi_ant2=%02x", r->rssi_ant2);
  //loggers::get_instance().log("commsignia_layer::receive_data: noise_ant1=%02x", r->noise_ant1);
  //loggers::get_instance().log("commsignia_layer::receive_data: noise_ant2=%02x", r->noise_ant2);
garciay's avatar
garciay committed
  // Filtering on antenna index
garciay's avatar
garciay committed
  //loggers::get_instance().log("commsignia_layer::receive_data: compare %02x with %02x", r->antenna, static_cast<unsigned char>(std::strtoul(_params[params::interface_id].c_str(), NULL, 10)));
  if (r->antenna != static_cast<unsigned char>(std::strtoul(_params[params::interface_id].c_str(), NULL, 10))) {
garciay's avatar
garciay committed
    // Discard packet
garciay's avatar
garciay committed
    loggers::get_instance().warning("commsignia_layer::receive_data: Discard packet due to wrong antenna id");
garciay's avatar
garciay committed
    return;
  } // else, continue

  const commsignia_layer::c2p_802_11p_hdr* h = (const commsignia_layer::c2p_802_11p_hdr*)(p + sizeof(commsignia_layer::c2p_recv));
garciay's avatar
garciay committed
  //loggers::get_instance().log("commsignia_layer::receive_data: frame_ctrl=%04x", ntohs(h->frame_ctrl));
garciay's avatar
garciay committed
  OCTETSTRING dst = OCTETSTRING(6, (const unsigned char*)&h->dst_addr);
garciay's avatar
garciay committed
  loggers::get_instance().log_msg("commsignia_layer::receive_data: dst: ", dst);
garciay's avatar
garciay committed
  OCTETSTRING src = OCTETSTRING(6, (const unsigned char*)&h->src_addr);
garciay's avatar
garciay committed
  loggers::get_instance().log_msg("commsignia_layer::receive_data: src: ", src);
garciay's avatar
garciay committed

  // Filtering on source MAC address of the packet
  if (!std::equal(_mac_src.cbegin(), _mac_src.cend(), static_cast<const unsigned char*>(src))) {
    // Discard packet
garciay's avatar
garciay committed
    loggers::get_instance().warning("commsignia_layer::receive_data: Discard packet due to wrong comparison");
garciay's avatar
garciay committed
    return;
  } // else, continue
  const commsignia_layer::c2p_llc_hdr* l;
  int length;
  if ((ntohs(h->frame_ctrl) & 0xf000) == 0x8000) {
    l = (const commsignia_layer::c2p_llc_hdr*)(p + sizeof(commsignia_layer::c2p_recv) + sizeof(commsignia_layer::c2p_802_11p_hdr) + sizeof(commsignia_layer::c2p_qos_ctrl));
    length = sizeof(commsignia_layer::c2p_recv) + sizeof(commsignia_layer::c2p_802_11p_hdr) + sizeof(commsignia_layer::c2p_qos_ctrl) + sizeof(commsignia_layer::c2p_llc_hdr);
  } else {
    l = (const commsignia_layer::c2p_llc_hdr*)(p + sizeof(commsignia_layer::c2p_recv) + sizeof(commsignia_layer::c2p_802_11p_hdr));
    length = sizeof(commsignia_layer::c2p_recv) + sizeof(commsignia_layer::c2p_802_11p_hdr) + sizeof(commsignia_layer::c2p_llc_hdr);
  }
garciay's avatar
garciay committed
  //loggers::get_instance().log("commsignia_layer::receive_data: dsap=%02x", l->dsap);
  //loggers::get_instance().log("commsignia_layer::receive_data: ssap=%02x", l->ssap);
  //loggers::get_instance().log("commsignia_layer::receive_data: type=%04x", l->type);
garciay's avatar
garciay committed
  // Check ether type
  if ((_eth_type[1] == (unsigned char)((l->type & 0xff00) >> 8)) && (_eth_type[0] == (unsigned char)(l->type & 0xff))) { // Warning: Network ordered bytes
    // Extract payload
    data = OCTETSTRING(data.lengthof() - length, length + static_cast<const unsigned char *>(data));
garciay's avatar
garciay committed
    //loggers::get_instance().log_msg("commsignia_layer::receive_data: payload for upper layer:", data);
garciay's avatar
garciay committed
    
    // Update params
    CHARSTRING s = oct2str(dst);
garciay's avatar
garciay committed
    params[params::mac_dst] = std::string(static_cast<const char *>(s));
garciay's avatar
garciay committed
    s = oct2str(src);
garciay's avatar
garciay committed
    params[params::mac_src] = std::string(static_cast<const char *>(s));
garciay's avatar
garciay committed
    
garciay's avatar
garciay committed
    receive_to_all_layers(data, params);
garciay's avatar
garciay committed
  } // else, nothing to do
}

commsignia_layer_factory commsignia_layer_factory::_f;