helloByeProto.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 "helloByeProto.hpp"
10 #include "mbuf.hpp"
11 #include "samplePacket.hpp"
12 
13 // using namespace Headers;
14 // using namespace std;
15 
16 /*
17  * ===================================
18  * HelloByeServerHello
19  * ===================================
20  *
21  */
22 
23 template <class Identifier, class Packet>
25  this->serverCookie = rand() % 10;
26 };
27 
28 template <class Identifier, class Packet>
30  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
31 
32  (void)state;
33 
34  // Get info from packet
35  Headers::Ethernet *ether = reinterpret_cast<Headers::Ethernet *>(pkt->getData());
36  Headers::IPv4 *ipv4 = reinterpret_cast<Headers::IPv4 *>(ether->getPayload());
37  Headers::Udp *udp = reinterpret_cast<Headers::Udp *>(ipv4->getPayload());
38 
39  char clientStr[] = "CLIENT HELLO:";
40  if (memcmp(udp->getPayload(), clientStr, sizeof(clientStr) - 1) != 0) {
41  std::cout << "HelloByeServerHello::fun() clientStr didn't match" << std::endl;
42  funIface.transition(HelloByeServer::Terminate);
43  funIface.freePkt();
44  return;
45  }
46 
47  // Get client cookie
48  uint8_t cookieChar =
49  reinterpret_cast<uint8_t *>(udp->getPayload())[sizeof(clientStr) - 1];
50  this->clientCookie = static_cast<int>(cookieChar) - 48; // ASCII Conversion
51 
52  // Prepare new packet
53  // Set payload string
54  std::stringstream sstream;
55  sstream << "SERVER HELLO:" << this->serverCookie << std::endl;
56  std::string serverHelloStr = sstream.str();
57  memcpy(udp->getPayload(), serverHelloStr.c_str(), serverHelloStr.length());
58 
59  // Set the IP header stuff
60  // Leave the payload length alone for now...
61 
62  ipv4->ttl = 64;
63  uint32_t tmp = ipv4->dstIP;
64  ipv4->dstIP = ipv4->srcIP;
65  ipv4->srcIP = tmp;
66  ipv4->checksum = 0;
67 
68  // Set UDP checksum to 0 and hope for the best
69 
70  udp->checksum = 0;
71  uint16_t tmp16 = udp->dstPort;
72  udp->dstPort = udp->srcPort;
73  udp->srcPort = tmp16;
74 
75  funIface.transition(HelloByeServer::Bye);
76 };
77 
78 /*
79  * ===================================
80  * HelloByeServerBye
81  * ===================================
82  *
83  */
84 
85 template <class Identifier, class Packet>
88  : clientCookie(in->clientCookie), serverCookie(in->serverCookie) {}
89 
90 template <class Identifier, class Packet>
92  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
93 
94  (void)state;
95 
96  // Get info from packet
97  Headers::Ethernet *ether = reinterpret_cast<Headers::Ethernet *>(pkt->getData());
98  Headers::IPv4 *ipv4 = reinterpret_cast<Headers::IPv4 *>(ether->getPayload());
99  Headers::Udp *udp = reinterpret_cast<Headers::Udp *>(ipv4->getPayload());
100 
101  char clientStr[] = "CLIENT BYE:";
102  if (memcmp(udp->getPayload(), clientStr, sizeof(clientStr) - 1) != 0) {
103  std::cout << "HelloByeServerBye::fun() clientStr didn't match" << std::endl;
105  return;
106  }
107 
108  // Get client cookie
109  uint8_t cookieChar =
110  reinterpret_cast<uint8_t *>(udp->getPayload())[sizeof(clientStr) - 1];
111  int recvCookie = static_cast<int>(cookieChar) - 48; // ASCII Conversion
112 
113  if (recvCookie != this->serverCookie) {
114  std::cout << "HelloByeServerBye::fun() Client sent over wrong cookie" << std::endl;
116  return;
117  }
118 
119  // Prepare new packet
120  // Set payload string
121  std::stringstream sstream;
122  sstream << "SERVER BYE:" << this->clientCookie << std::endl;
123  std::string serverByeStr = sstream.str();
124  memcpy(udp->getPayload(), serverByeStr.c_str(), serverByeStr.length());
125 
126  // Set the IP header stuff
127  // Leave the payload length alone for now...
128  ipv4->ttl = 64;
129  uint32_t tmp = ipv4->dstIP;
130  ipv4->dstIP = ipv4->srcIP;
131  ipv4->srcIP = tmp;
132  ipv4->checksum = 0;
133 
134  // Set UDP checksum to 0 and hope for the best
135  udp->checksum = 0;
136  uint16_t tmp16 = udp->dstPort;
137  udp->dstPort = udp->srcPort;
138  udp->srcPort = tmp16;
139 
140  // We are done after this -> transition to Terminate
142 };
143 
144 /*
145  * ===================================
146  * HelloByeClientHello
147  * ===================================
148  *
149  */
150 
151 template <class Identifier, class Packet>
153  uint32_t dstIp, uint16_t srcPort)
154  : dstIp(dstIp), srcPort(srcPort) {
155  this->clientCookie = rand() % 10;
156 };
157 
158 template <class Identifier, class Packet>
160  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
161 
162  (void)state;
163 
164  memset(pkt->getData(), 0, pkt->getDataLen());
165 
166  // Get info from packet
167  Headers::Ethernet *ether = reinterpret_cast<Headers::Ethernet *>(pkt->getData());
168  Headers::IPv4 *ipv4 = reinterpret_cast<Headers::IPv4 *>(ether->getPayload());
169 
171 
172  // Set the IP header stuff
173  // Leave the payload length alone for now...
174  ipv4->setVersion();
175  ipv4->setIHL(5);
176  ipv4->ttl = 64;
177  ipv4->setLength(100 - 14);
178  ipv4->setDstIP(this->dstIp);
179  ipv4->setSrcIP(config->getSrcIP());
180  ipv4->setProtoUDP();
181  ipv4->checksum = 0;
182 
183  Headers::Udp *udp = reinterpret_cast<Headers::Udp *>(ipv4->getPayload());
184 
185  // Prepare new packet
186  // Set payload string
187  std::stringstream sstream;
188  sstream << "CLIENT HELLO:" << this->clientCookie << std::endl;
189  std::string clientHelloStr = sstream.str();
190  memcpy(udp->getPayload(), clientHelloStr.c_str(), clientHelloStr.length());
191 
192  ether->ethertype = htons(0x0800);
193 
194  // Set UDP checksum to 0 and hope for the best
195  udp->checksum = 0;
196  udp->setDstPort(config->getDstPort());
197  udp->setSrcPort(this->srcPort);
198  udp->setPayloadLength(50);
199 
201 };
202 
203 /*
204  * ===================================
205  * HelloByeClientBye
206  * ===================================
207  *
208  */
209 
210 template <class Identifier, class Packet>
213  : clientCookie(in->clientCookie) {}
214 
215 template <class Identifier, class Packet>
217  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
218 
219  (void)state;
220 
221  // Get info from packet
222  Headers::Ethernet *ether = reinterpret_cast<Headers::Ethernet *>(pkt->getData());
223  Headers::IPv4 *ipv4 = reinterpret_cast<Headers::IPv4 *>(ether->getPayload());
224  Headers::Udp *udp = reinterpret_cast<Headers::Udp *>(ipv4->getPayload());
225 
226  char serverStr[] = "SERVER HELLO:";
227  if (memcmp(udp->getPayload(), serverStr, sizeof(serverStr) - 1) != 0) {
228  std::cout << "HelloByeClientBye::fun() serverStr didn't match" << std::endl;
230  return;
231  }
232 
233  // Get server cookie
234  uint8_t cookieChar =
235  reinterpret_cast<uint8_t *>(udp->getPayload())[sizeof(serverStr) - 1];
236  this->serverCookie = static_cast<int>(cookieChar) - 48; // ASCII Conversion
237 
238  // Prepare new packet
239  // Set payload string
240  std::stringstream sstream;
241  sstream << "CLIENT BYE:" << this->serverCookie << std::endl;
242  std::string clientByeStr = sstream.str();
243 
244  // Zero out old data - just assume 20 bytes...
245  memset(udp->getPayload(), 0, 20);
246  memcpy(udp->getPayload(), clientByeStr.c_str(), clientByeStr.length());
247 
248  // Set the IP header stuff
249  // Leave the payload length alone for now...
250  ipv4->ttl = 64;
251  uint32_t tmp = ipv4->dstIP;
252  ipv4->dstIP = ipv4->srcIP;
253  ipv4->srcIP = tmp;
254  ipv4->checksum = 0;
255 
256  // Set UDP checksum to 0 and hope for the best
257  udp->checksum = 0;
258  uint16_t tmp16 = udp->dstPort;
259  udp->dstPort = udp->srcPort;
260  udp->srcPort = tmp16;
261 
262  // We need to wait for the server reply
264 };
265 
266 /*
267  * ===================================
268  * HelloByeClientRecvBye
269  * ===================================
270  *
271  */
272 
273 template <class Identifier, class Packet>
276  : clientCookie(in->clientCookie), serverCookie(in->serverCookie) {}
277 
278 template <class Identifier, class Packet>
280  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
281 
282  (void)state;
283 
284  // Get info from packet
285  Headers::Ethernet *ether = reinterpret_cast<Headers::Ethernet *>(pkt->getData());
286  Headers::IPv4 *ipv4 = reinterpret_cast<Headers::IPv4 *>(ether->getPayload());
287  Headers::Udp *udp = reinterpret_cast<Headers::Udp *>(ipv4->getPayload());
288 
289  char serverStr[] = "SERVER BYE:";
290  if (memcmp(udp->getPayload(), serverStr, sizeof(serverStr) - 1) != 0) {
291  std::cout << "HelloByeClientRecvBye::fun() serverStr didn't match" << std::endl;
293  return;
294  }
295 
296  // Get client cookie
297  uint8_t cookieChar =
298  reinterpret_cast<uint8_t *>(udp->getPayload())[sizeof(serverStr) - 1];
299  int recvCookie = static_cast<int>(cookieChar) - 48; // ASCII Conversion
300 
301  if (recvCookie != this->clientCookie) {
302  std::cout << "HelloByeClientRecvBye::fun() Server sent over wrong cookie"
303  << std::endl;
305  return;
306  }
307 
308  funIface.freePkt();
309 
310  // We are done after this -> transition to Terminate
312 };
313 
314  /*
315  * ===================================
316  * Prepare template instances
317  * ===================================
318  *
319  */
320 
321  /*
322  * This file is now included in the header
323  *
324  template class HelloByeServerHello<IPv4_5TupleL2Ident<SamplePacket>, SamplePacket>;
325  template class HelloByeServerBye<IPv4_5TupleL2Ident<SamplePacket>, SamplePacket>;
326  template class HelloByeClientHello<IPv4_5TupleL2Ident<SamplePacket>, SamplePacket>;
327  template class HelloByeClientBye<IPv4_5TupleL2Ident<SamplePacket>, SamplePacket>;
328  template class HelloByeClientRecvBye<IPv4_5TupleL2Ident<SamplePacket>, SamplePacket>;
329 
330  template class HelloByeServerHello<IPv4_5TupleL2Ident<mbuf>, mbuf>;
331  template class HelloByeServerBye<IPv4_5TupleL2Ident<mbuf>, mbuf>;
332  template class HelloByeClientHello<IPv4_5TupleL2Ident<mbuf>, mbuf>;
333  template class HelloByeClientBye<IPv4_5TupleL2Ident<mbuf>, mbuf>;
334  template class HelloByeClientRecvBye<IPv4_5TupleL2Ident<mbuf>, mbuf>;
335  */
336 
337 #endif /* HELLOBYEPROTO_CPP */
uint16_t checksum
header checksum
Definition: headers.hpp:122
void setSrcIP(uint32_t ip)
Set the source ip.
Definition: headers.hpp:129
void setProtoUDP()
Set the protocol to UDP.
Definition: headers.hpp:120
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
static constexpr StateID Terminate
Main interface for the needs of a state function.
void setDstIP(uint32_t ip)
Set the destination ip.
Definition: headers.hpp:134
HelloByeServerBye(const HelloByeServerHello< Identifier, Packet > *in)
HelloByeClientRecvBye(const HelloByeClientBye< Identifier, Packet > *in)
static constexpr StateID Bye
Representation of the etherner header.
Definition: headers.hpp:15
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
static HelloByeClientConfig * getInstance()
Representation of the UDP header.
Definition: headers.hpp:209
void * getPayload()
Get the SDU.
Definition: headers.hpp:248
static constexpr StateID RecvBye
HelloByeClientHello(uint32_t dstIp, uint16_t srcPort)
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
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
uint16_t dstPort
Destination port.
Definition: headers.hpp:211
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
void setLength(uint16_t len)
Definition: headers.hpp:164
static constexpr StateID Terminate
uint16_t checksum
Checksum.
Definition: headers.hpp:213
Represents one connection.
HelloByeClientBye(const HelloByeClientHello< 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.