Commit 125923b9 authored by garciay's avatar garciay
Browse files

Layers ongoing

parent c649d32a
Loading
Loading
Loading
Loading
+63 −49
Original line number Diff line number Diff line
@@ -8,23 +8,36 @@
#include "loggers.hh"

class OCTETSTRING;
class CHARSTRING;

class Layer {
  std::vector<Layer*> upperLayers;
  std::vector<Layer*> lowerLayers;
protected:
  std::string type;
public:
    Layer(){}
    virtual ~Layer() {};
  Layer(const std::string& p_type) : upperLayers(), lowerLayers(), type(std::string(p_type.begin(), p_type.end())) {};
  virtual ~Layer() { upperLayers.clear(); lowerLayers.clear(); };
  void deleteLayer() { };
public:
    void addUpperLayer(Layer*) {};
    void removeUpperLayer(Layer*) {};
  inline void addUpperLayer(Layer* p_layer) { 
    //loggers::loggers::log(">>> Layer::addUpperLayer");
    if (p_layer != NULL) {
      loggers::loggers::log("Layer::addUpperLayer: %s is upper layer of %s", p_layer->to_string().c_str(), to_string().c_str());
      upperLayers.push_back(p_layer);
      loggers::loggers::log(" Layer::addUpperLayer: %s is loweer layer of %s", to_string().c_str(), p_layer->to_string().c_str());
      p_layer->lowerLayers.push_back(this);
    };
  };
  void removeUpperLayer(Layer* p_layer) {  };

  virtual void sendData(const OCTETSTRING& data, const Params& params) {};
    virtual void receiveData(const OCTETSTRING& data, const Params& info) {};
  virtual void receiveData(OCTETSTRING& data, Params& info) {};

  inline const std::string& to_string() const { return type; };

protected:
    inline void toAllLayers(std::vector<Layer*>&layers, const OCTETSTRING& data, const Params& info) {
  inline void toAllLayers(std::vector<Layer*>&layers, OCTETSTRING& data, Params& info) {
    //loggers::loggers::log_msg(">>> Layer::toAllLayer: ", data);
    loggers::loggers::log(">>> Layer::toAllLayer: %d", layers.size());
    for (std::vector<Layer*>::const_iterator it = layers.cbegin(); it != layers.cend(); ++it) {
@@ -32,10 +45,10 @@ class Layer {
      p->receiveData(data, info);
    } // End of 'for' statement
  };
    inline void toAllUpperLayers(const OCTETSTRING& data, const Params& info) { toAllLayers(upperLayers, data, info); }
    inline void toAllLowerLayers(const OCTETSTRING& data, const Params& info) { toAllLayers(upperLayers, data, info); }
    inline void receiveToAllLayers(const OCTETSTRING& data, const Params& info) { toAllLayers(upperLayers, data, info); }
    inline void sendToAllLayers(const OCTETSTRING& data, const Params& info)  { toAllLayers(lowerLayers, data, info); }
  inline void toAllUpperLayers(OCTETSTRING& data, Params& info) { toAllLayers(upperLayers, data, info); }
  inline void toAllLowerLayers(OCTETSTRING& data, Params& info) { toAllLayers(lowerLayers, data, info); }
  inline void receiveToAllLayers(OCTETSTRING& data, Params& info) { toAllLayers(upperLayers, data, info); }
  inline void sendToAllLayers(OCTETSTRING& data, Params& info)  { toAllLayers(lowerLayers, data, info); }
};


@@ -46,12 +59,13 @@ template <typename TPort> class TLayer : public Layer {
  TPortList upperPorts;
  
public:
    TLayer(){}
    void addUpperPort(TPort*);
  TLayer(const std::string& p_type) : Layer(p_type), upperPorts() {};
  void addUpperPort(TPort * p_port) { upperPorts.push_back(p_port); };
  void removeUpperPort(TPort*);

protected:
  template <typename TMessage> void toAllUpperPorts(const TMessage& m, const Params& param) {
    loggers::loggers::log(">>> TLayer::toAllUpperPorts: %d", upperPorts.size());
    for(TPortListIterator it=upperPorts.begin(); it<upperPorts.end(); ++it){
      (*it)->receiveMsg(m, param);
    }
+1 −2
Original line number Diff line number Diff line
@@ -2,11 +2,10 @@

#include <map>

//typedef std::map<std::string, std::string> Params;

class Params : public std::map<std::string, std::string> {
  public:
    Params() : std::map<std::string, std::string>() {};
    Params(const Params& p_params);
    virtual ~Params() {};
    void log();
    static void convert(Params& p_param, const std::string p_parameters);
+33 −36
Original line number Diff line number Diff line
@@ -40,21 +40,18 @@ Layer* LayerStackBuilder::createLayerStack(const char* s)
    std::sregex_iterator end = std::sregex_iterator();
    for (std::sregex_iterator it = begin; it != end; ++it) {
      std::smatch m = *it;
            loggers::loggers::log("LayerStackBuilder::createLayerStack: %d - %s - %s - %s - %s", m.size(), m[0].str().c_str(), m[1].str().c_str(), m[2].str().c_str(), m[3].str().c_str());
            std::string mname = m[1].str();
            LayerFactoryMap::iterator i =_fs.find(mname);
      //loggers::loggers::log("LayerStackBuilder::createLayerStack: %d - %s - %s - %s - %s", m.size(), m[0].str().c_str(), m[1].str().c_str(), m[2].str().c_str(), m[3].str().c_str());
      LayerFactoryMap::iterator i =_fs.find(m[1].str());
      if (i == _fs.end()) {
                loggers::loggers::error("LayerStackBuilder::createLayerStack: %s: Unknown layer type", mname.c_str());
                throw (std::logic_error(mname + ": Unknown layer type"));
	loggers::loggers::error("LayerStackBuilder::createLayerStack: %s: Unknown layer type", m[1].str().c_str());
      }
            // TODO: parse parameters
            loggers::loggers::log("LayerStackBuilder::createLayerStack: Create layer %s", i->first.c_str());
            Layer * l = i->second->createLayer(mname, m[3]);
      loggers::loggers::log("LayerStackBuilder::createLayerStack: Create layer %s, %s", m[1].str().c_str(), m[3].str().c_str());
      Layer * l = i->second->createLayer(m[1].str(), m[3].str());
      if (NULL == l) {
                loggers::loggers::error("LayerStackBuilder::createLayerStack: %s: Layer creation error", mname.c_str());
                throw (std::logic_error(mname + ": Layer creation error"));
	loggers::loggers::error("LayerStackBuilder::createLayerStack: %s: Layer creation error", m[1].str().c_str());
      }

      loggers::loggers::log("LayerStackBuilder::createLayerStack: Setup layers for %s", l->to_string().c_str());
      l->addUpperLayer(up);
      up = l;
    } // End of 'for' statement
+3 −0
Original line number Diff line number Diff line
@@ -4,6 +4,9 @@
#include "Params.hh"
#include "loggers.hh"

Params::Params(const Params& p_params) : std::map<std::string, std::string>(p_params.begin(), p_params.end()) {
}

void Params::convert(Params& p_param, const std::string p_parameters) {
    //loggers::loggers::log(">>> Params::convert: '%s'", p_parameters.c_str());
    // Sanity checks
+61 −29
Original line number Diff line number Diff line
#include "EthernetLayer.hh"
#include "loggers.hh"

EthernetLayer::EthernetLayer(const std::string & type, const std::string & param) : EthernetLayer() {
    loggers::loggers::log(">>> EthernetLayer::EthernetLayer: %s, %s", type.c_str(), param.c_str());
EthernetLayer::EthernetLayer(const std::string & p_type, const std::string & param) : Layer(p_type), _params() {
  loggers::loggers::log(">>> EthernetLayer::EthernetLayer: %s, %s", to_string().c_str(), param.c_str());
  // Setup parameters
  Params::convert(_params, param);
    _params.log();
  //_params.log();
}

void EthernetLayer::sendData(const OCTETSTRING& data, const Params& params) {
void EthernetLayer::sendData(OCTETSTRING& data, Params& params) {
  loggers::loggers::log_msg(">>> EthernetLayer::sendData: ", data);
  OCTETSTRING eth;
  std::map<std::string, std::string>::const_iterator it = params.find("mac_dst");
  if (it != params.cend()) {
    eth += str2oct(CHARSTRING(it->second.c_str()));    
  } else {
    const unsigned char mac_address[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
    eth += OCTETSTRING(sizeof(mac_address), static_cast<const unsigned char *>(mac_address));
  }
  it = params.find("mac_src");
  if (it != params.cend()) {
    eth += str2oct(CHARSTRING(it->second.c_str()));    
  } else {
    const unsigned char mac_address[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0xFF};
    eth += OCTETSTRING(sizeof(mac_address), static_cast<const unsigned char *>(mac_address));
  }
  it = params.find("eth_type");
  if (it != params.cend()) {
    eth += str2oct(CHARSTRING(it->second.c_str()));        
  } else {
    const unsigned char proto[] = {0x47, 0x89};
    eth += OCTETSTRING(sizeof(proto), static_cast<const unsigned char *>(proto));
  }
  
void EthernetLayer::receiveData(const OCTETSTRING& data, const Params& info) {
  data = eth + data;
  toAllLowerLayers(data, params);
}

void EthernetLayer::receiveData(OCTETSTRING& data, Params& params) {
  loggers::loggers::log_msg(">>> EthernetLayer::receiveData: ", data);

  // Extract dest MAC Address
    OCTETSTRING dst = data << 6;
    loggers::loggers::log_msg("EthernetLayer::receiveData: dst: ", dst);
  OCTETSTRING dst = OCTETSTRING(6, static_cast<const unsigned char *>(data));
  //loggers::loggers::log_msg("EthernetLayer::receiveData: dst: ", dst);
  // Extract source MAC Address
    OCTETSTRING src = data << 6;    
    loggers::loggers::log_msg("EthernetLayer::receiveData: src: ", src);
  OCTETSTRING src = OCTETSTRING(6, 6 + static_cast<const unsigned char *>(data));
  //loggers::loggers::log_msg("EthernetLayer::receiveData: src: ", src);
  // Extract ethertype
    OCTETSTRING type = data << 2;
    loggers::loggers::log_msg("EthernetLayer::receiveData: type: ", type);
  OCTETSTRING proto = OCTETSTRING(2, 2 + static_cast<const unsigned char *>(data));
  //loggers::loggers::log_msg("EthernetLayer::receiveData: proto: ", proto);
  data = OCTETSTRING(data.lengthof() - 14, 14 + static_cast<const unsigned char *>(data));
  // Update params
  CHARSTRING s = oct2str(dst);
  params.insert(std::pair<std::string, std::string>(std::string("mac_dst"), std::string(static_cast<const char *>(s))));
  s = oct2str(src);
  params.insert(std::pair<std::string, std::string>(std::string("mac_src2"), std::string(static_cast<const char *>(s))));
  //loggers::loggers::log_msg("EthernetLayer::receiveData: payload for upper layer:", data);
  
    loggers::loggers::log_msg("<<< EthernetLayer::receiveData: ", data);   
  toAllUpperLayers(data, params);
}

class EthernetFactory: public LayerFactory {
Loading