INET Framework for OMNeT++/OMNEST
inet::ExtInterface Class Reference

Implements an interface that corresponds to a real interface on the host running the simulation. More...

#include <ExtInterface.h>

Inheritance diagram for inet::ExtInterface:
inet::MACBase inet::ILifecycle

Public Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void finish () override
 
- Public Member Functions inherited from inet::MACBase
 MACBase ()
 
virtual ~MACBase ()
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 

Protected Member Functions

void displayBusy ()
 
void displayIdle ()
 
virtual void refreshDisplay () const override
 
InterfaceEntrycreateInterfaceEntry () override
 should create InterfaceEntry More...
 
virtual void flushQueue () override
 should clear queue and emit signal "dropPkFromHLIfaceDown" with entire packets More...
 
virtual void clearQueue () override
 should clear queue silently More...
 
virtual bool isUpperMsg (cMessage *msg) override
 should return true if the msg arrived from upper layer, else return false More...
 
- Protected Member Functions inherited from inet::MACBase
void registerInterface ()
 
virtual void receiveSignal (cComponent *source, simsignal_t signalID, cObject *obj, cObject *details) override
 
virtual bool handleOperationStage (LifecycleOperation *operation, int stage, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
virtual void updateOperationalFlag (bool isNodeUp)
 
virtual bool isNodeUp ()
 
virtual void handleMessageWhenDown (cMessage *msg)
 

Protected Attributes

bool connected
 
uint8 buffer [1<< 16]
 
const char * device
 
int numSent
 
int numRcvd
 
int numDropped
 
cSocketRTSchedulerrtScheduler
 
- Protected Attributes inherited from inet::MACBase
cModule * hostModule = nullptr
 
bool isOperational = false
 
InterfaceEntryinterfaceEntry = nullptr
 

Detailed Description

Implements an interface that corresponds to a real interface on the host running the simulation.

Suitable for hardware-in-the-loop simulations.

Requires cSocketRTScheduler to be configured as scheduler in omnetpp.ini.

See NED file for more details.

Member Function Documentation

void inet::ExtInterface::clearQueue ( )
overrideprotectedvirtual

should clear queue silently

Implements inet::MACBase.

198 {
199  // does not have a queue, do nothing
200 }
InterfaceEntry * inet::ExtInterface::createInterfaceEntry ( )
overrideprotectedvirtual

should create InterfaceEntry

Implements inet::MACBase.

73 {
74  InterfaceEntry *e = new InterfaceEntry(this);
75 
76  e->setMtu(par("mtu"));
77  e->setMulticast(true);
78  e->setPointToPoint(true);
79 
80  return e;
81 }
const value< double, units::C > e(1.602176487e-19)
void inet::ExtInterface::displayBusy ( )
protected
156 {
157  getDisplayString().setTagArg("i", 1, "yellow");
158  gate("physOut")->getDisplayString().setTagArg("ls", 0, "yellow");
159  gate("physOut")->getDisplayString().setTagArg("ls", 1, "3");
160 }
void inet::ExtInterface::displayIdle ( )
protected
163 {
164  getDisplayString().setTagArg("i", 1, "");
165  gate("physOut")->getDisplayString().setTagArg("ls", 0, "black");
166  gate("physOut")->getDisplayString().setTagArg("ls", 1, "1");
167 }
void inet::ExtInterface::finish ( )
overridevirtual
187 {
188  std::cout << getFullPath() << ": " << numSent << " packets sent, "
189  << numRcvd << " packets received, " << numDropped << " packets dropped.\n";
190 }
int numSent
Definition: ExtInterface.h:56
int numRcvd
Definition: ExtInterface.h:57
int numDropped
Definition: ExtInterface.h:58
void inet::ExtInterface::flushQueue ( )
overrideprotectedvirtual

should clear queue and emit signal "dropPkFromHLIfaceDown" with entire packets

Implements inet::MACBase.

193 {
194  // does not have a queue, do nothing
195 }
void inet::ExtInterface::handleMessage ( cMessage *  msg)
overridevirtual
84 {
85  using namespace serializer;
86 
87  if (!isOperational) {
89  return;
90  }
91 
92  if (dynamic_cast<ExtFrame *>(msg) != nullptr) {
93  // incoming real packet from wire (captured by pcap)
94  uint32 packetLength;
95  ExtFrame *rawPacket = check_and_cast<ExtFrame *>(msg);
96 
97  packetLength = rawPacket->getDataArraySize();
98  for (uint32 i = 0; i < packetLength; i++)
99  buffer[i] = rawPacket->getData(i);
100 
101  Buffer b(const_cast<unsigned char *>(buffer), packetLength);
102  Context c;
103  IPv4Datagram *ipPacket = check_and_cast<IPv4Datagram *>(IPv4Serializer().deserializePacket(b, c));
104  EV << "Delivering an IPv4 packet from "
105  << ipPacket->getSrcAddress()
106  << " to "
107  << ipPacket->getDestAddress()
108  << " and length of"
109  << ipPacket->getByteLength()
110  << " bytes to IPv4 layer.\n";
111  send(ipPacket, "upperLayerOut");
112  numRcvd++;
113  }
114  else {
115  memset(buffer, 0, sizeof(buffer));
116  IPv4Datagram *ipPacket = check_and_cast<IPv4Datagram *>(msg);
117 
118  if (connected) {
119  struct sockaddr_in addr;
120  addr.sin_family = AF_INET;
121 #if !defined(linux) && !defined(__linux) && !defined(_WIN32)
122  addr.sin_len = sizeof(struct sockaddr_in);
123 #endif // if !defined(linux) && !defined(__linux) && !defined(_WIN32)
124  addr.sin_port = 0;
125  addr.sin_addr.s_addr = htonl(ipPacket->getDestAddress().getInt());
126  Buffer b(const_cast<unsigned char *>(buffer), sizeof(buffer));
127  Context c;
128  c.throwOnSerializerNotFound = false;
129  IPv4Serializer().serializePacket(ipPacket, b, c);
130  if (b.hasError() || c.errorOccured) {
131  EV_ERROR << "Cannot serialize and send packet << '" << ipPacket->getName() << "' with protocol " << ipPacket->getTransportProtocol() << ".\n";
132  numDropped++;
133  delete (msg);
134  return;
135  }
136  int32 packetLength = b.getPos();
137  EV << "Delivering an IPv4 packet from "
138  << ipPacket->getSrcAddress()
139  << " to "
140  << ipPacket->getDestAddress()
141  << " and length of "
142  << ipPacket->getByteLength()
143  << " bytes to link layer.\n";
144  rtScheduler->sendBytes(buffer, packetLength, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
145  numSent++;
146  }
147  else {
148  EV << "Interface is not connected, dropping packet " << msg << endl;
149  numDropped++;
150  }
151  }
152  delete (msg);
153 }
uint8 buffer[1<< 16]
Definition: ExtInterface.h:52
int numSent
Definition: ExtInterface.h:56
int numRcvd
Definition: ExtInterface.h:57
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
bool isOperational
Definition: MACBase.h:37
virtual void handleMessageWhenDown(cMessage *msg)
Definition: MACBase.cc:108
u32_t htonl(u32_t n)
int32_t int32
Definition: Compat.h:31
uint32_t uint32
Definition: Compat.h:30
int numDropped
Definition: ExtInterface.h:58
bool connected
Definition: ExtInterface.h:51
void sendBytes(unsigned char *buf, size_t numBytes, struct sockaddr *from, socklen_t addrlen)
Send on the currently open connection.
Definition: cSocketRTScheduler.cc:339
value< double, units::m > b
Definition: Units.h:1054
cSocketRTScheduler * rtScheduler
Definition: ExtInterface.h:61
void inet::ExtInterface::initialize ( int  stage)
overridevirtual

Reimplemented from inet::MACBase.

44 {
45  MACBase::initialize(stage);
46 
47  // subscribe at scheduler for external messages
48  if (stage == INITSTAGE_LOCAL) {
49  if (dynamic_cast<cSocketRTScheduler *>(getSimulation()->getScheduler()) != nullptr) {
50  rtScheduler = check_and_cast<cSocketRTScheduler *>(getSimulation()->getScheduler());
51  //device = getEnvir()->config()->getAsString("Capture", "device", "lo0");
52  device = par("device");
53  //const char *filter = getEnvir()->config()->getAsString("Capture", "filter-string", "ip");
54  const char *filter = par("filterString");
55  rtScheduler->setInterfaceModule(this, device, filter);
56  connected = true;
57  }
58  else {
59  // this simulation run works without external interface..
60  connected = false;
61  }
62  numSent = numRcvd = numDropped = 0;
63  WATCH(numSent);
64  WATCH(numRcvd);
65  WATCH(numDropped);
66  }
67  else if (stage == INITSTAGE_LINK_LAYER) {
69  }
70 }
int numSent
Definition: ExtInterface.h:56
int numRcvd
Definition: ExtInterface.h:57
virtual void initialize(int stage) override
Definition: MACBase.cc:37
Local initializations.
Definition: InitStages.h:35
Initialization of link-layer protocols.
Definition: InitStages.h:59
const char * device
Definition: ExtInterface.h:53
int numDropped
Definition: ExtInterface.h:58
bool connected
Definition: ExtInterface.h:51
void registerInterface()
Definition: MACBase.cc:98
void setInterfaceModule(cModule *mod, const char *dev, const char *filter)
To be called from the module which wishes to receive data from the socket.
Definition: cSocketRTScheduler.cc:113
cSocketRTScheduler * rtScheduler
Definition: ExtInterface.h:61
virtual bool inet::ExtInterface::isUpperMsg ( cMessage *  msg)
inlineoverrideprotectedvirtual

should return true if the msg arrived from upper layer, else return false

Implements inet::MACBase.

72 { return msg->arrivedOn("upperLayerIn"); }
virtual int inet::ExtInterface::numInitStages ( ) const
inlineoverridevirtual

Reimplemented from inet::MACBase.

75 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::ExtInterface::refreshDisplay ( ) const
overrideprotectedvirtual
170 {
171  const char *str;
172 
173  if (connected) {
174  char buf[80];
175  sprintf(buf, "pcap device: %s\nrcv:%d snt:%d", device, numRcvd, numSent);
176  str = buf;
177  getDisplayString().setTagArg("t", 0, buf);
178  }
179  else {
180  getDisplayString().setTagArg("i", 1, "#707070");
181  getDisplayString().setTagArg("i", 2, "100");
182  getDisplayString().setTagArg("t", 0, "not connected");
183  }
184 }
int numSent
Definition: ExtInterface.h:56
int numRcvd
Definition: ExtInterface.h:57
const char * device
Definition: ExtInterface.h:53
bool connected
Definition: ExtInterface.h:51

Member Data Documentation

uint8 inet::ExtInterface::buffer[1<< 16]
protected

Referenced by handleMessage().

bool inet::ExtInterface::connected
protected
const char* inet::ExtInterface::device
protected

Referenced by initialize(), and refreshDisplay().

int inet::ExtInterface::numDropped
protected

Referenced by finish(), handleMessage(), and initialize().

int inet::ExtInterface::numRcvd
protected
int inet::ExtInterface::numSent
protected
cSocketRTScheduler* inet::ExtInterface::rtScheduler
protected

Referenced by handleMessage(), and initialize().


The documentation for this class was generated from the following files: