-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_test.cpp
More file actions
79 lines (65 loc) · 2.12 KB
/
tcp_test.cpp
File metadata and controls
79 lines (65 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "l4_protocols.h"
#include "l3_protocols.h"
#include "pdu.h"
#include "addressing.h"
#include "wqueue.h"
#include <cstdint>
#include <thread>
#include <iostream>
using namespace std;
#define FIRST_IP 1
#define FIRST_PORT 20000
#define SECOND_IP 2
#define SECOND_PORT 30000
#define SYN 0x0002
#define ACK 0x0010
void TCP_tester(/*int my_IP; int dst_IP, */uint16_t my_port, uint16_t dst_port, int is_sender, wqueue<TCP*> * tx_interface,
wqueue<TCP*> * rx_interface) {
// if is_sender is true, this is the tester that initiates the sync
if (is_sender) {
// generate a TCP frame with the SYN bit set to 1 and with a random value in the sequence number
TCP * SYN_segment = new TCP;
SYN_segment->source_port = my_port;
SYN_segment->destination_port = dst_port;
SYN_segment->sequence_number = my_port;
SYN_segment->details = SYN;
cout << my_port << " sending SYN" << endl;
tx_interface->add(SYN_segment);
}
TCP * rcv_segment;
TCP * send_segment;
while (true) {
rcv_segment = rx_interface->remove();
if (rcv_segment->details == (SYN & ACK)) {
delete rcv_segment;
if (is_sender) {
cout << my_port << " got SYNACK, ACKing" << endl;
send_segment = new TCP;
send_segment->destination_port = dst_port;
send_segment->sequence_number = my_port;
send_segment->details = SYN & ACK;
tx_interface->add(send_segment);
}
cout << my_port << " fully synced, exiting" << endl;
break;
} else if (rcv_segment->details == SYN) {
cout << my_port << " got sync request, replying" << endl;
send_segment = new TCP;
send_segment->destination_port = dst_port;
send_segment->sequence_number = my_port;
send_segment->details = SYN & ACK;
tx_interface->add(send_segment);
}
}
}
int main() {
// create queue for each host to reference
wqueue<TCP*> * interface_1 = new wqueue<TCP*>;
wqueue<TCP*> * interface_2 = new wqueue<TCP*>;
// have two threads which reference one queue
std::thread server(TCP_tester, FIRST_PORT, SECOND_PORT, 0, interface_1, interface_2);
std::thread client(TCP_tester, SECOND_PORT, FIRST_PORT, 1, interface_2, interface_1);
server.join();
client.join();
// each
}