helloBye2Proto.cpp
Go to the documentation of this file.
1 #ifndef HELLOBYEPROTO_CPP
2 #define HELLOBYEPROTO_CPP
3 
4 #include <cstdlib>
5 #include <sstream>
6 
7 #include "IPv4_5TupleL2Ident.hpp"
8 #include "headers.hpp"
9 #include "helloBye2Proto.hpp"
10 #include "mbuf.hpp"
11 #include "samplePacket.hpp"
12 
13 // using namespace Headers;
14 // using namespace std;
15 
16 namespace HelloBye2 {
17 
18 namespace Server {
19 
20 /*
21  * ===================================
22  * HelloByeServerHello
23  * ===================================
24  *
25  */
26 
27 template <class Identifier, class Packet> Hello<Identifier, Packet>::Hello() {
28  this->serverCookie = rand() % 256;
29  D(std::cout << "HelloBye2::Server::Hello::Hello() serverCookie: "
30  << static_cast<int>(serverCookie) << std::endl;)
31 };
32 
33 template <class Identifier, class Packet>
35  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
36 
37  (void)state;
38 
39  // Get info from packet
40  Headers::Ethernet *ether = reinterpret_cast<Headers::Ethernet *>(pkt->getData());
41  Headers::IPv4 *ipv4 = reinterpret_cast<Headers::IPv4 *>(ether->getPayload());
42  Headers::Udp *udp = reinterpret_cast<Headers::Udp *>(ipv4->getPayload());
43 
44  struct msg *msg = reinterpret_cast<struct msg *>(udp->getPayload());
45 
46  D(std::cout << "HelloBye2::Server::Hello::fun() pkt: " << (void *)pkt->getData()
47  << ", ident: " << msg->ident << std::endl;)
48 
49  if (msg->role != 0) {
50  // std::abort();
51  std::cout << "HelloBye2::Server::Hello::fun() client hello wrong - role not client"
52  << std::endl;
53  funIface.transition(States::Terminate);
54  funIface.freePkt();
55  return;
56  }
57 
58  if (msg->msg != 0) {
59  // std::abort();
60  std::cout << "HelloBye2::Server::Hello::fun() client hello wrong - msg not hello"
61  << std::endl;
62  funIface.transition(States::Terminate);
63  funIface.freePkt();
64  return;
65  }
66 
67  // Get client cookie
68  this->clientCookie = msg->cookie;
69  D(std::cout << "HelloBye2::Server::Hello::fun() clientCookie: "
70  << static_cast<int>(clientCookie) << std::endl;)
71 
74  msg->cookie = serverCookie;
75 
76  // Set the IP header stuff
77  // Leave the payload length alone for now...
78 
79  ipv4->ttl = 64;
80  uint32_t tmp = ipv4->dstIP;
81  ipv4->dstIP = ipv4->srcIP;
82  ipv4->srcIP = tmp;
83  ipv4->checksum = 0;
84 
85  // Set UDP checksum to 0 and hope for the best
86 
87  udp->checksum = 0;
88  uint16_t tmp16 = udp->dstPort;
89  udp->dstPort = udp->srcPort;
90  udp->srcPort = tmp16;
91 
92  funIface.transition(States::Bye);
93 };
94 
95 /*
96  * ===================================
97  * HelloByeServerBye
98  * ===================================
99  *
100  */
101 
102 template <class Identifier, class Packet>
104  : clientCookie(in->clientCookie), serverCookie(in->serverCookie) {}
105 
106 template <class Identifier, class Packet>
108  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
109 
110  (void)state;
111 
112  // Get info from packet
113  Headers::Ethernet *ether = reinterpret_cast<Headers::Ethernet *>(pkt->getData());
114  Headers::IPv4 *ipv4 = reinterpret_cast<Headers::IPv4 *>(ether->getPayload());
115  Headers::Udp *udp = reinterpret_cast<Headers::Udp *>(ipv4->getPayload());
116 
117  struct msg *msg = reinterpret_cast<struct msg *>(udp->getPayload());
118 
119  if ((msg->role != msg::ROLE_CLIENT) || (msg->msg != msg::MSG_BYE)) {
120  std::cout << "HelloBye2::Server::Bye::fun() msg fields wrong" << std::endl;
121  funIface.transition(States::Terminate);
122  funIface.freePkt();
123  return;
124  }
125 
126  if (msg->cookie != this->serverCookie) {
127  std::cout << "HelloBye2::Server::Bye::fun() Client sent over wrong cookie"
128  << std::endl;
129  funIface.transition(States::Terminate);
130  funIface.freePkt();
131  return;
132  }
133 
134  // Prepare new packet
135  msg->cookie = this->clientCookie;
137  msg->msg = msg::MSG_BYE;
138 
139  // Set the IP header stuff
140  // Leave the payload length alone for now...
141  ipv4->ttl = 64;
142  uint32_t tmp = ipv4->dstIP;
143  ipv4->dstIP = ipv4->srcIP;
144  ipv4->srcIP = tmp;
145  ipv4->checksum = 0;
146 
147  // Set UDP checksum to 0 and hope for the best
148  udp->checksum = 0;
149  uint16_t tmp16 = udp->dstPort;
150  udp->dstPort = udp->srcPort;
151  udp->srcPort = tmp16;
152 
153  // We are done after this -> transition to Terminate
154  funIface.transition(States::Terminate);
155 };
156 }; // namespace Server
157 
158 namespace Client {
159 /*
160  * ===================================
161  * HelloByeClientHello
162  * ===================================
163  *
164  */
165 
166 template <class Identifier, class Packet>
167 Hello<Identifier, Packet>::Hello(uint32_t dstIp, uint16_t srcPort, uint64_t ident)
168  : dstIp(dstIp), srcPort(srcPort), ident(ident) {
169  this->clientCookie = rand() % 256;
170 };
171 
172 template <class Identifier, class Packet>
174  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
175 
176  (void)state;
177 
178  memset(pkt->getData(), 0, pkt->getDataLen());
179 
180  // Get info from packet
181  Headers::Ethernet *ether = reinterpret_cast<Headers::Ethernet *>(pkt->getData());
182  Headers::IPv4 *ipv4 = reinterpret_cast<Headers::IPv4 *>(ether->getPayload());
183 
185 
186  // Set the IP header stuff
187  // Leave the payload length alone for now...
188  ipv4->setVersion();
189  ipv4->setIHL(5);
190  ipv4->ttl = 64;
191  ipv4->setLength(100 - 14);
192  ipv4->setDstIP(this->dstIp);
193  ipv4->setSrcIP(config.getSrcIP());
194  ipv4->setProtoUDP();
195  ipv4->checksum = 0;
196 
197  Headers::Udp *udp = reinterpret_cast<Headers::Udp *>(ipv4->getPayload());
198  struct msg *msg = reinterpret_cast<struct msg *>(udp->getPayload());
199 
200  // Prepare msg
201  msg->ident = ident;
203  msg->cookie = clientCookie;
205 
206  ether->ethertype = htons(0x0800);
207 
208  // Set UDP checksum to 0 and hope for the best
209  udp->checksum = 0;
210  udp->setDstPort(config.getDstPort());
211  udp->setSrcPort(this->srcPort);
212  udp->setPayloadLength(50);
213 
214  funIface.transition(States::Bye);
215 };
216 
217 /*
218  * ===================================
219  * HelloByeClientBye
220  * ===================================
221  *
222  */
223 
224 template <class Identifier, class Packet>
226  : clientCookie(in->clientCookie) {}
227 
228 template <class Identifier, class Packet>
230  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
231 
232  (void)state;
233 
234  // Get info from packet
235  Headers::Ethernet *ether = reinterpret_cast<Headers::Ethernet *>(pkt->getData());
236  Headers::IPv4 *ipv4 = reinterpret_cast<Headers::IPv4 *>(ether->getPayload());
237  Headers::Udp *udp = reinterpret_cast<Headers::Udp *>(ipv4->getPayload());
238 
239  struct msg *msg = reinterpret_cast<struct msg *>(udp->getPayload());
240 
241  D(std::cout << "HelloBye2::Client::Bye::fun() function called, ident:" << msg->ident
242  << std::endl;)
243 
244  if ((msg->role != msg::ROLE_SERVER) || (msg->msg != msg::MSG_HELLO)) {
245  std::cout << "HelloBye2::Client::Bye::fun() msg fields wrong" << std::endl;
246  funIface.transition(States::Terminate);
247  funIface.freePkt();
248  return;
249  }
250 
251  // Get server cookie
252  this->serverCookie = msg->cookie;
253 
254  msg->cookie = serverCookie;
256  msg->msg = msg::MSG_BYE;
257 
258  // Set the IP header stuff
259  // Leave the payload length alone for now...
260  ipv4->ttl = 64;
261  uint32_t tmp = ipv4->dstIP;
262  ipv4->dstIP = ipv4->srcIP;
263  ipv4->srcIP = tmp;
264  ipv4->checksum = 0;
265 
266  // Set UDP checksum to 0 and hope for the best
267  udp->checksum = 0;
268  uint16_t tmp16 = udp->dstPort;
269  udp->dstPort = udp->srcPort;
270  udp->srcPort = tmp16;
271 
272  D(std::cout << "HelloBye2::Client::Bye::fun() Dump of outgoing packet" << std::endl;)
273  D(hexdump(pkt->getData(), 64);)
274 
275  // We need to wait for the server reply
276  funIface.transition(States::RecvBye);
277 };
278 
279 /*
280  * ===================================
281  * HelloByeClientRecvBye
282  * ===================================
283  *
284  */
285 
286 template <class Identifier, class Packet>
288  : clientCookie(in->clientCookie), serverCookie(in->serverCookie) {}
289 
290 template <class Identifier, class Packet>
292  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
293 
294  (void)state;
295 
296  // Get info from packet
297  Headers::Ethernet *ether = reinterpret_cast<Headers::Ethernet *>(pkt->getData());
298  Headers::IPv4 *ipv4 = reinterpret_cast<Headers::IPv4 *>(ether->getPayload());
299  Headers::Udp *udp = reinterpret_cast<Headers::Udp *>(ipv4->getPayload());
300 
301  struct msg *msg = reinterpret_cast<struct msg *>(udp->getPayload());
302 
303  if ((msg->role != msg::ROLE_SERVER) || (msg->msg != msg::MSG_BYE)) {
304  std::cout << "HelloBye2::Client::RecvBye::fun() msg fields wrong" << std::endl;
305  funIface.transition(States::Terminate);
306  funIface.freePkt();
307  return;
308  }
309 
310  if (msg->cookie != this->clientCookie) {
311  std::cout << "HelloByeClientRecvBye::fun() Server sent over wrong cookie"
312  << std::endl;
313  std::cout << "Expected: " << static_cast<int>(this->clientCookie);
314  std::cout << ", Got: " << static_cast<int>(msg->cookie) << std::endl;
315  funIface.transition(States::Terminate);
316  funIface.freePkt();
317  return;
318  }
319 
320  funIface.freePkt();
321 
322  // We are done after this -> transition to Terminate
323  funIface.transition(States::Terminate);
324 };
325 }; // namespace Client
326 }; // namespace HelloBye2
327 
328 #endif /* HELLOBYEPROTO_CPP */
uint16_t checksum
header checksum
Definition: headers.hpp:122
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
void setSrcIP(uint32_t ip)
Set the source ip.
Definition: headers.hpp:129
static HelloBye2ClientConfig & getInstance()
void setProtoUDP()
Set the protocol to UDP.
Definition: headers.hpp:120
static constexpr StateID Terminate
Representation of the IPv4 header.
Definition: headers.hpp:65
uint32_t dstIP
destination IPv4 address
Definition: headers.hpp:124
void * getPayload()
Get the SDU.
Definition: headers.hpp:149
uint8_t ttl
Time to live.
Definition: headers.hpp:106
void setSrcPort(uint16_t p)
Set the source port.
Definition: headers.hpp:218
Bye(const Hello< Identifier, Packet > *in)
Hello(uint32_t dstIp, uint16_t srcPort, uint64_t ident)
Main interface for the needs of a state function.
static constexpr StateID Bye
void setDstIP(uint32_t ip)
Set the destination ip.
Definition: headers.hpp:134
static constexpr StateID RecvBye
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
static constexpr StateID Terminate
Representation of the etherner header.
Definition: headers.hpp:15
static constexpr StateID Bye
uint32_t srcIP
source IPv4 address
Definition: headers.hpp:123
void setIHL(uint8_t len)
Sets the IP header length.
Definition: headers.hpp:81
void setDstPort(uint16_t p)
Set the destination port.
Definition: headers.hpp:223
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
static constexpr uint8_t ROLE_SERVER
void hexdump(const void *data, int dataLen)
Dump hex data.
Definition: hexdump.cpp:12
Representation of the UDP header.
Definition: headers.hpp:209
void * getPayload()
Get the SDU.
Definition: headers.hpp:248
RecvBye(const Bye< Identifier, Packet > *in)
#define D(x)
Definition: common.hpp:10
static constexpr uint8_t MSG_BYE
uint16_t srcPort
Source port.
Definition: headers.hpp:210
void freePkt()
Free the packet after the batch is processed, do not send it.
void setPayloadLength(uint16_t length)
Set the length of the L4-SDU (UDP payload)
Definition: headers.hpp:243
static constexpr uint8_t MSG_HELLO
uint16_t dstPort
Destination port.
Definition: headers.hpp:211
static constexpr uint8_t ROLE_CLIENT
void setLength(uint16_t len)
Definition: headers.hpp:164
uint16_t checksum
Checksum.
Definition: headers.hpp:213
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
Represents one connection.
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
Bye(const Hello< Identifier, Packet > *in)
void setVersion()
Set the version field to 4.
Definition: headers.hpp:73
void * getPayload()
Get the SDU.
Definition: headers.hpp:23
void transition(StateID newState)
Transition to another state.