-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserversource.c
319 lines (278 loc) · 8.03 KB
/
serversource.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <signal.h>
#include "packet.h"
#define BACKLOG 10
#define MAXSEQ 25600
#define RWND 20
//TESTING
//const char* testMessage = "Message: \"";
char filename[50];
//TESTING
void printPacket(struct packet* p){
printf("%d %d 0 0%s%s%s%s\n", p->seqNum, p->ackNum, p->ack?" ACK":"", p->syn?" SYN":"", p->fin?" FIN":"", p->dup?" DUP":"");
}
void signalReceived(int sig){
if(sig == SIGQUIT || sig == SIGTERM) {
write(2, "Stopping Server.\n", 17);
if(strlen(filename) > 0){
int outfd = open(filename, O_CREAT | O_WRONLY, S_IRWXU);
if(outfd < 0){
fprintf(stderr, "Could not open file: %s\n", filename);
_exit(0);
}
if(ftruncate(outfd, 0) < 0){
fprintf(stderr, "Could not clear file: %s\n", filename);
fprintf(stderr, "Error: %s\n", strerror(errno));
_exit(0);
}
write(outfd, "INTERRUPT\n", 10);
}
_exit(0);
}
}
void serveClient(int sockfd, int connectionNum){
srand(time(NULL));
struct packet sendingPacket;
memset(&sendingPacket, 0, sizeof(struct packet));
struct packet receivedPacket;
memset(&receivedPacket, 0, sizeof(struct packet));
struct sockaddr_in cliaddr;
memset(&cliaddr, 0, sizeof(struct sockaddr_in));
char* buff[RWND];
int buffLen[RWND];
memset(buff, 0, RWND*sizeof(buff[0]));
memset(buffLen, 0, RWND*sizeof(buffLen[0]));
int outfd = -1;
int len, n;
int window = -1;
int seq = rand() % MAXSEQ;
int fin = 0;
//Testing
// strcpy(sendingPacket->message, "Testing message.\n");
// sendto(sockfd, sendingPacket, 524, 0,
// (const struct sockaddr *) cliaddr,
// (socklen_t) sizeof(struct sockaddr_in));
while(!fin){
memset(&sendingPacket, 0, sizeof(struct packet));
memset(&receivedPacket, 0, sizeof(struct packet));
memset(&cliaddr, 0, sizeof(struct sockaddr));
len = sizeof(cliaddr);
n = recvfrom(sockfd, (char *) &receivedPacket, sizeof(struct packet), MSG_WAITALL,
(struct sockaddr *) &cliaddr, (socklen_t *) &len);
if(n < 1){
fprintf(stderr, "Timeout\n");
if(window == -1 || outfd < 0){
continue;
}
else{
if(ftruncate(outfd, 0) < 0){
fprintf(stderr, "Could not clear file: %s\n", filename);
fprintf(stderr, "Error: %s\n", strerror(errno));
}
filename[0] = 0;
close(outfd);
outfd = -1;
break;
}
}
//Testing
printf("RECV ");
printPacket(&receivedPacket);
if(n < 12 || n-12 < receivedPacket.len || receivedPacket.len > 512){
fprintf(stderr, "Invalid packet dropped\n");
continue;
}
//Received SYN packet
if(receivedPacket.syn != 0){
snprintf(filename, 49, "%d.file", connectionNum);
outfd = open(filename, O_CREAT | O_WRONLY, S_IRWXU);
if(outfd < 0){
fprintf(stderr, "Could not open file: %s\n", filename);
break;
}
if(ftruncate(outfd, 0) < 0){
fprintf(stderr, "Could not clear file: %s\n", filename);
fprintf(stderr, "Error: %s\n", strerror(errno));
close(outfd);
outfd = -1;
break;
}
sendingPacket.syn = 1;
window = receivedPacket.seqNum + 1;
}
//First packet is not SYN
if(window == -1 || outfd < 0){
fprintf(stderr, "First Packet is not SYN. (Dropped)\n");
continue;
}
//Packet in order
if(receivedPacket.seqNum == window){
//Inconsistency Check
if(receivedPacket.ack && receivedPacket.ackNum != seq){
fprintf(stderr, "Packet ACK may not be correct.\n");
}
//Write payload to file and increase window
//write(0, testMessage, strlen(testMessage));
//write(0, receivedPacket->message, n-12);
if(write(outfd, receivedPacket.message, receivedPacket.len) < 0){
fprintf(stderr, "Could not print to fd %d\n", outfd);
fprintf(stderr, "Error %d: %s\n", errno, strerror(errno));
exit(1);
}
//printf("\"\n");
window = (receivedPacket.seqNum + receivedPacket.len) % (MAXSEQ+1);
//Write any (sequential) buffered packets to file
int i;
for(i = 0; buff[i] != NULL && i < RWND; i++){
//write(0, testMessage, strlen(testMessage));
//write(0, buff[i], buffLen[i]);
write(outfd, buff[i], buffLen[i]);
//printf("\"\n");
window += (buffLen[i]);
window = window % (MAXSEQ+1);
free(buff[i]);
buff[i] = NULL;
buffLen[i] = 0;
}
//Move remaining (non-sequential) buffered packets forward
for(int j = i + 1; j < RWND; j++){
if(buff[j] != NULL){
buff[j-i-1] = buff[j];
buffLen[j-i-1] = buffLen[j];
buff[j] = NULL;
buffLen[j] = 0;
}
}
//Packet was FIN
if((fin = receivedPacket.fin)){
window = (window + 1) % (MAXSEQ+1);
filename[0] = 0;
close(outfd);
outfd = -1;
}
}
//Packet out of order
else if(receivedPacket.syn == 0 && receivedPacket.fin == 0){
int i;
//Calculate buffer location. Must handle wrap-around for SeqNum
if(receivedPacket.seqNum > window)
i = ((receivedPacket.seqNum - window) / 512) - 1;
else
i = (((receivedPacket.seqNum + MAXSEQ + 1) - window) / 512) - 1;
if(i < RWND && buff[i] == NULL){
buff[i] = calloc(1, receivedPacket.len);
if(buff[i] == NULL){
fprintf(stderr, "Out of memory. Packet dropped.\n");
}
else{
memcpy(buff[i], receivedPacket.message, receivedPacket.len);
buffLen[i] = receivedPacket.len;
}
}
else{
fprintf(stderr, "Packet dropped\n");
}
sendingPacket.dup = 1;
}
//ACK the next expected packet
sendingPacket.seqNum = seq;
sendingPacket.ackNum = window;
sendingPacket.ack = 1;
if(sendingPacket.syn != 0)
seq++;
//Testing
//printf("\nWindow: %d\n", window);
printf("SEND ");
printPacket(&sendingPacket);
//Send ACK
sendto(sockfd, &sendingPacket, 12,
0, (const struct sockaddr *) &cliaddr,
(socklen_t) sizeof(struct sockaddr_in));
}
if(fin){
memset(&sendingPacket, 0, sizeof(struct packet));
sendingPacket.fin = 1;
sendingPacket.seqNum = seq;
//Testing
printf("SEND ");
printPacket(&sendingPacket);
//Send FIN
sendto(sockfd, &sendingPacket, 12,
0, (const struct sockaddr *) &cliaddr,
(socklen_t) sizeof(struct sockaddr_in));
len = sizeof(cliaddr);
n = recvfrom(sockfd, (char *) &receivedPacket, sizeof(struct packet), MSG_WAITALL,
(struct sockaddr *) &cliaddr, (socklen_t *) &len);
if(n == 12 && receivedPacket.ack == 1){
printf("RECV ");
printPacket(&receivedPacket);
fprintf(stderr, "Connection Closed Successfully\n");
}
else{
fprintf(stderr, "Connection not propperly closed\n");
}
}
close(outfd);
outfd = -1;
}
int main(int argc, char* argv[]){
signal(SIGTERM, signalReceived);
signal(SIGQUIT, signalReceived);
signal(SIGINT, signalReceived);
filename[0] = 0;
if(argc != 2){
fprintf(stderr, "ERROR: usage: %s [port number]\n", argv[0]);
exit(1);
}
int port = strtol(argv[1], (char**) NULL, 10);
if(port < 0 || port > 65535){
fprintf(stderr, "ERROR: Invalid port number.\n");
exit(1);
}
int sockfd;
struct sockaddr_in my_addr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1){
fprintf(stderr, "ERROR: Could not create socket\n");
exit(1);
}
//allows sockfd to be reused
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0){
fprintf(stderr, "ERROR setsockopt: %s\n", strerror(errno));
exit(1);
}
//Sets timeout for receiving new data
struct timeval timeOut;
timeOut.tv_sec = 10;
timeOut.tv_usec = 0;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeOut, sizeof(timeOut)) < 0) {
fprintf(stderr, "ERROR setsockopt: %s\n", strerror(errno));
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(my_addr.sin_zero, '\0', sizeof(my_addr.sin_zero));
if (bind(sockfd, (struct sockaddr*) &my_addr, sizeof(struct sockaddr)) == -1){
fprintf(stderr, "ERROR: Could not bind socket\n");
exit(1);
}
for(int i = 1 ;; i++){
//Serve Clients
serveClient(sockfd, i);
}
}