EmulatedEthernetInterface.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
19 
20 // Custom includes
21 
22 #define prefix_
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 // senf::emu::detail::EmulatedEthernetInterfaceNet
27 
28 prefix_ senf::emu::detail::EmulatedEthernetInterfaceNet::
29 EmulatedEthernetInterfaceNet()
30  : receiveFilter_ (self()), transmitFilter_ (self()),
31  receiveInput (receiveFilter_.input),
32  receiveOutput (receiveFilter_.output), transmitInput (transmitFilter_.input),
33  transmitOutput (transmitFilter_.output)
34 {}
35 
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37 // senf::emu::EmulatedEthernetInterface
38 
39 namespace {
40  // Arbitrary magic number used to identify the emulation header for Ethernet packets
41  // Defined to be the 32bit integer representation of the string 'eth' in network byte order
42  boost::uint32_t const ethernetMagic (0x657468);
43 }
44 
46  : Base(receiveOutput, transmitInput), vlanGroupRange_ ("239.203.0.0/16"),
47  portbase_(11264), vlanId_(0), dev_("NetEMU-Ethernet")
48 {
49  ppi::connect(receiverJack, receiveInput);
50  ppi::connect(transmitOutput, transmitterJack);
51 
52  disable();
53  id( uniqueMAC(ethernetMagic));
54  mtu(1500u);
55 
56  init();
57 }
58 
60 {
61  Base::init();
62 
63  namespace kw = console::kw;
64  namespace fty = console::factory;
65  consoleDir()
66  .add("vlan",
67  fty::Command(SENF_MEMBINDFNP(void, EmulatedEthernetInterface,
68  vlan, (boost::uint16_t) ))
69  .arg("vlan", "VLAN id")
70  .doc("Get/set the VLAN id of the ethernet interface")
71  );
72  consoleDir()
73  .add("vlan",
74  fty::Command(SENF_MEMBINDFNP(boost::uint16_t, EmulatedEthernetInterface,
75  vlan, () const))
76  );
77 
78  UDPClientHandle::Address addr (vlanGroupRange_.host(vlanId_),portbase_+vlanId_);
79  emulationAddress(addr);
80 }
81 
82 prefix_ void senf::emu::EmulatedEthernetInterface::v_id(MACAddress const & eui)
83 {
84  id_ = eui;
85 }
86 
87 prefix_ senf::MACAddress senf::emu::EmulatedEthernetInterface::v_id()
88  const
89 {
90  return id_;
91 }
92 
94  const
95 {
96  return vlanId_;
97 }
98 
100 {
101  vlanId_ = id;
102  UDPClientHandle::Address addr (vlanGroupRange_.host(vlanId_),portbase_+vlanId_);
103  emulationAddress(addr);
104 }
105 
107  const
108 {
109  return dev_;
110 }
111 
112 
113 //-/////////////////////////////////////////////////////////////////////////////////////////////////
114 // senf::emu::detail::EmulatedEthernetReceiveFilter
115 
116 prefix_ senf::emu::detail::EmulatedEthernetReceiveFilter::
117 EmulatedEthernetReceiveFilter(EmulatedEthernetInterface & iface)
118  : iface_ (iface)
119 {
120  route(input, output);
121  input.onRequest(&EmulatedEthernetReceiveFilter::request);
122 }
123 
124 prefix_ void senf::emu::detail::EmulatedEthernetReceiveFilter::request()
125 {
126  EthernetPacketHeader p (input());
128  if (e) {
129  e.annotation<annotations::Interface>().value = iface_.id();
130  {
131  annotations::Quality & q (e.annotation<annotations::Quality>());
132  q.rssi = 127; // for now, we report the maximum signal 'quality'
133  q.snr = q.rssi - (-128); // 'default' noise floor to ensure that we always report a higher SNR compared to wireless
134  q.flags.frameLength = e.size();
135  }
136 
137  if (iface_.annotationMode())
138  output(prependAnnotationsPacket(e));
139  else
140  output(e);
141  }
142  return;
143 }
144 
145 //-/////////////////////////////////////////////////////////////////////////////////////////////////
146 // senf::emu::detail::EmulatedEthernetTransmitFilter
147 
148 prefix_ senf::emu::detail::EmulatedEthernetTransmitFilter::
149 EmulatedEthernetTransmitFilter(EmulatedEthernetInterface & iface)
150  : iface_ (iface)
151 {
152  route(input, output);
153  input.onRequest(&EmulatedEthernetTransmitFilter::request);
154 }
155 
156 prefix_ void senf::emu::detail::EmulatedEthernetTransmitFilter::request()
157 {
158  EthernetPacket e (input());
159  e->source() = iface_.id();
161  eph.finalizeThis();
162  output(eph);
163 }
164 //-/////////////////////////////////////////////////////////////////////////////////////////////////
165 #undef prefix_
166 
167 
168 // Local Variables:
169 // mode: c++
170 // fill-column: 100
171 // comment-column: 40
172 // c-file-style: "senf"
173 // indent-tabs-mode: nil
174 // ispell-local-dictionary: "american"
175 // compile-command: "scons -u test"
176 // End:
#define SENF_MEMBINDFNP(ret, cls, fn, args)
static ConcretePacket createBefore(Packet const &packet)
Incoming or outgoing interface.
Definition: Annotations.hh:37
OtherPacket find() const
EthernetPacket prependAnnotationsPacket(Packet const &pkt, MACAddress const &src_=senf::MACAddress::None, MACAddress const &dst_=senf::MACAddress::Broadcast)
std::string const & v_device() const override
console::ScopedDirectory & consoleDir()
EmulatedEthernetInterface public header.
nothrow
#define prefix_
INet4Address host(boost::uint32_t number)
MACAddress uniqueMAC(boost::uint32_t magic)
Get pseudo-random but unique MACAddress.
void finalizeThis()
ConcretePacket< EthernetPacketType > EthernetPacket
AnnotationsPacket public header.