-
Notifications
You must be signed in to change notification settings - Fork 0
/
c2.c
229 lines (208 loc) · 8.26 KB
/
c2.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
FILE: c2.c
RUN: gcc c2.c -o c2
./c2
NAME: Dhruv Rawat
ID: 2019B3A70537P
SEC: P2
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/time.h>
#include <errno.h>
#define BUFLEN 10000 // Max length of buffer
#define PORT 8537 // The port on which to send data
#define RETRANS_TIMEOUT_SEC 2 // Retransmission timeout in seconds
#define RED "\033[0;31m"
#define GREEN "\033[0;32m"
#define YELLOW "\033[0;33m"
#define BLUE "\033[0;34m"
#define PURPLE "\033[0;35m"
#define CYAN "\033[0;36m"
#define INVERT "\033[0;7m"
#define RESET "\e[0m"
#define BOLD "\e[1m"
#define ITALICS "\e[3m"
#define UNDERLINE "\e[4m"
typedef struct packet {
int payload_id;
int seq_no;
char data[BUFLEN];
int payload_size;
int TYPE; // 0 for DATA, 1 for ACK
bool isLast; // To check if sent data packet is last packet
} PKT;
void printError(char *s) {
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in server_addr;
int sock_fd;
char buf[BUFLEN];
PKT send_pkt, rcv_ack;
//Socket creation
if ((sock_fd=socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printError("socket");
}
//Initializing socket
memset((char *) &server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//Connect to server
if (connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
printError("Error connecting to server");
}
//Open file to read data from
FILE *input = fopen("id.txt", "r");
if (input == NULL) {
printError("Error opening file");
exit(EXIT_FAILURE);
}
char *token;
fgets(buf, BUFLEN, input); // Read the line from the file which contains comma-separated IDs.
// Assumption: The file only contains a single line which has IDs separated by a comma and terminated by a period
int state = 0;
int start = 1;
int seq = 0;
while (1) {
switch(state) {
case 0: {
//Tokenizing the buffer on the basis of commas and periods
if(start) {
token = strtok(buf, ",.");
start = 0;
} else {
token = strtok(NULL, ",.");
}
if(token!=NULL) {
send_pkt.isLast = false;
} else {
// If token is NULL, it means the buf is completely tokenized
// Send an empty dummy packet to tell server that the file has ended
token = "";
send_pkt.isLast = true;
}
strcpy(send_pkt.data, token);
send_pkt.payload_id = 0;
send_pkt.TYPE = 0;
send_pkt.payload_size = strlen(send_pkt.data);
send_pkt.seq_no = seq;
seq+=send_pkt.payload_size;
if(!send_pkt.isLast)
printf(BLUE BOLD "SENT PKT: Seq. No. = %d, Size = %d Bytes\n" RESET, send_pkt.seq_no, send_pkt.payload_size);
if (send(sock_fd, &send_pkt, sizeof(send_pkt), 0) == -1) {
printError("send()");
}
// Set timeout timer
struct timeval timer;
timer.tv_sec = RETRANS_TIMEOUT_SEC;
timer.tv_usec = 0;
if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timer, sizeof(timer)) < 0) {
printError("setsockopt");
}
state = 1;
break;
}
case 1: {
if (recv(sock_fd, &rcv_ack, sizeof(rcv_ack), 0) == -1) {
// Handle timeout
if (errno == EAGAIN || errno == EWOULDBLOCK) {
if(!send_pkt.isLast)
printf(YELLOW BOLD "RE-TRANSMIT PKT: Seq. No. = %d, Size = %d Bytes\n" RESET, send_pkt.seq_no, send_pkt.payload_size);
if (send(sock_fd, &send_pkt, sizeof(send_pkt), 0) == -1) {
printError("send()");
}
break;
}
printError("recv()");
}
// Correct ACK received
if (rcv_ack.payload_id==0) {
state = 2;
if(!rcv_ack.isLast)
printf(GREEN BOLD "RCVD ACK: Seq. No. = %d\n" RESET, rcv_ack.seq_no);
else
state = 4; // If ACK to dummy packet is received, don't print but move to close connection
break;
}
// If incorrect ACK received, ignore
break;
}
case 2: {
//Tokenizing the buffer on the basis of commas and periods
token = strtok(NULL, ",.");
if(token!=NULL) {
send_pkt.isLast = false;
} else {
// If token is NULL, it means the buf is completely tokenized
// Send an empty dummy packet to tell server that the file has ended
token = "";
send_pkt.isLast = true;
}
strcpy(send_pkt.data, token);
send_pkt.payload_id = 1;
send_pkt.TYPE = 0;
send_pkt.payload_size = strlen(send_pkt.data);
send_pkt.seq_no = seq;
seq+=send_pkt.payload_size;
if(!send_pkt.isLast)
printf(BLUE BOLD "SENT PKT: Seq. No. = %d, Size = %d Bytes\n" RESET, send_pkt.seq_no, send_pkt.payload_size);
if (send(sock_fd, &send_pkt, sizeof(send_pkt), 0)==-1) {
printError("send()");
}
// Set timeout timer
struct timeval timer;
timer.tv_sec = RETRANS_TIMEOUT_SEC;
timer.tv_usec = 0;
if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timer, sizeof(timer)) < 0) {
printError("setsockopt");
}
state = 3;
break;
}
case 3: {
if (recv(sock_fd, &rcv_ack, sizeof(rcv_ack), 0) == -1) {
// Handle timeout
if (errno == EAGAIN || errno == EWOULDBLOCK) {
if(!send_pkt.isLast)
printf(YELLOW BOLD "RE-TRANSMIT PKT: Seq. No. = %d, Size = %d Bytes\n" RESET, send_pkt.seq_no, send_pkt.payload_size);
if (send(sock_fd, &send_pkt, sizeof(send_pkt), 0) == -1) {
printError("send()");
}
break;
}
printError("recv()");
}
// Correct ACK received
if (rcv_ack.payload_id==1) {
state = 0;
if(!rcv_ack.isLast)
printf(GREEN BOLD "RCVD ACK: Seq. No. = %d\n" RESET, rcv_ack.seq_no);
else
state = 4; // If ACK to dummy packet is received, don't print but move to close connection
break;
}
// If incorrect ACK received, ignore
break;
}
case 4: {
fflush(stdout);
fclose(input);
close(sock_fd);
exit(0);
}
}
}
close(sock_fd);
fclose(input);
return 0;
}