-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.c
587 lines (498 loc) · 17.1 KB
/
transport.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
* transport.c
*
* COS461: Assignment 3 (STCP)
*
* This file implements the STCP layer that sits between the
* mysocket and network layers. You are required to fill in the STCP
* functionality in this file.
*
*/
#include "transport.h"
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mysock.h"
#include "stcp_api.h"
const unsigned int WINDOW_SIZE = 3072;
const unsigned int MSS = 536;
struct timespec spec;
enum {
CSTATE_ESTABLISHED,
SYN_SENT,
SYN_RECEIVED,
SYN_ACK_RECEIVED,
SYN_ACK_SENT,
CSTATE_CLOSED,
FIN_SENT,
}; /* you should have more states */
typedef struct segment_t {
unsigned int sequenceNumber; // segment sequence number
ssize_t length; // segment length
bool acked; // this segment has been ack (used for sender segment)
bool fin; // this is a fin segment
char* data; // points to the data
};
/* this structure is global to a mysocket descriptor */
typedef struct context_t {
bool_t done; /* TRUE once connection is closed */
unsigned int connection_state;
unsigned int seq_num; // next sequence number to send
unsigned int rec_seq_num; // next wanted sequence number
unsigned int rec_wind_size;
/* any other connection-wide global variables go here */
struct receriverBuffer* rb;
struct senderBuffer* sb;
} ctx;
// represent sender windows
struct senderBuffer {
char buffer[WINDOW_SIZE];
char* endOfSegment;
char* endOfAckdSegment;
unsigned int nextSeq;
segment_t* segments;
};
struct receriverBuffer {
char buffer[WINDOW_SIZE];
char* endOfSegment;
unsigned int nextSeq;
segment_t* segments;
};
static void generate_initial_seq_num(context_t* ctx);
static void control_loop(mysocket_t sd, context_t* ctx);
void initializeBuffers(context_t* ctx);
STCPHeader* create_SYN_packet(unsigned int seq, unsigned int ack);
bool send_SYN(mysocket_t sd, context_t* ctx);
void wait_for_SYN_ACK(mysocket_t sd, context_t* ctx);
STCPHeader* create_ACK_packet(unsigned int seq, unsigned int ack);
bool send_ACK(mysocket_t sd, context_t* ctx);
void wait_for_SYN(mysocket_t sd, context_t* ctx);
STCPHeader* create_SYN_ACK_packet(unsigned int seq, unsigned int ack);
bool send_SYN_ACK(mysocket_t sd, context_t* ctx);
void wait_for_ACK(mysocket_t sd, context_t* ctx);
void app_data_event(mysocket_t sd, context_t* ctx);
STCPHeader* create_DATA_packet(unsigned int seq, unsigned int ack,
char* payload, size_t payload_length);
bool send_DATA_packet_network(mysocket_t sd, context_t* ctx, char* payload,
size_t payload_length);
void network_data_event(mysocket_t sd, context_t* ctx);
void send_DATA_packet_app(mysocket_t sd, context_t* ctx, char* payload,
size_t length);
void parse_DATA_packet(context_t* ctx, char* payload, bool& isFIN, bool& isDUP);
STCPHeader* create_FIN_packet(unsigned int seq, unsigned int ack);
bool send_FIN_packet(mysocket_t sd, context_t* ctx);
void app_close_event(mysocket_t sd, context_t* ctx);
void printSTCPHeader(STCPHeader* print);
int min(int a, int b);
/* initialise the transport layer, and start the main loop, handling
* any data from the peer or the application. this function should not
* return until the connection is closed.
*/
void transport_init(mysocket_t sd, bool_t is_active) {
context_t* ctx;
ctx = (context_t*)calloc(1, sizeof(context_t));
assert(ctx);
generate_initial_seq_num(ctx);
if (is_active) { // Client control path, initiate connection
ctx->seq_num = 1;
// Send SYN
if (!send_SYN(sd, ctx)) return;
// Wait for SYN-ACK
wait_for_SYN_ACK(sd, ctx);
// Send ACK Packet
if (!send_ACK(sd, ctx)) return;
} else { // Server control path, wait for connection
ctx->seq_num = 101;
wait_for_SYN(sd, ctx);
if (!send_SYN_ACK(sd, ctx)) return;
wait_for_ACK(sd, ctx);
}
ctx->connection_state = CSTATE_ESTABLISHED;
stcp_unblock_application(sd);
control_loop(sd, ctx);
/* do any cleanup here */
free(ctx);
}
/* generate random initial sequence number for an STCP connection */
static void generate_initial_seq_num(context_t* ctx) {
assert(ctx);
const unsigned int MAX = 255;
#ifdef FIXED_INITNUM
/* please don't change this! */
ctx->seq_num = 1;
#else
/* you have to fill this up */
/*ctx->seq_num =;*/
srand(time(NULL)); // seed random number generator
ctx->seq_num = rand() % MAX + 1;
#endif
}
/* control_loop() is the main STCP loop; it repeatedly waits for one of the
* following to happen:
* - incoming data from the peer
* - new data from the application (via mywrite())
* - the socket to be closed (via myclose())
* - a timeout
*/
static void control_loop(mysocket_t sd, context_t* ctx) {
assert(ctx);
assert(!ctx->done);
int count = 0;
while (!ctx->done) {
if (ctx->connection_state == CSTATE_CLOSED) {
ctx->done = true;
continue;
}
unsigned int event = stcp_wait_for_event(sd, ANY_EVENT, NULL);
if (event == APP_DATA) {
clock_gettime(CLOCK_REALTIME, &spec);
// printf("%d APP DATA EVENT\n", spec.tv_nsec);
app_data_event(sd, ctx);
}
if (event == NETWORK_DATA) {
clock_gettime(CLOCK_REALTIME, &spec);
// printf("%d NETWORK DATA EVENT\n", spec.tv_nsec);
network_data_event(sd, ctx);
}
if (event == APP_CLOSE_REQUESTED) {
clock_gettime(CLOCK_REALTIME, &spec);
printf("%d APP CLOSE EVENT\n", spec.tv_nsec);
app_close_event(sd, ctx);
}
if (event == ANY_EVENT) {
clock_gettime(CLOCK_REALTIME, &spec);
// printf("%d ANY EVENT\n", spec.tv_nsec);
}
}
}
STCPHeader* create_SYN_packet(unsigned int seq, unsigned int ack) {
STCPHeader* SYN_packet = (STCPHeader*)malloc(sizeof(STCPHeader));
SYN_packet->th_seq = htonl(seq);
SYN_packet->th_ack = htonl(ack);
SYN_packet->th_off = htons(5); // header size offset for packed data
SYN_packet->th_flags = TH_SYN; // set packet type to SYN
SYN_packet->th_win = htons(WINDOW_SIZE); // default value
return SYN_packet;
}
bool send_SYN(mysocket_t sd, context_t* ctx) {
// Create SYN Packet
STCPHeader* SYN_packet = create_SYN_packet(ctx->seq_num, 0);
ctx->seq_num++;
// Send SYN packet
ssize_t sentBytes =
stcp_network_send(sd, SYN_packet, sizeof(STCPHeader), NULL);
// Verify sending of SYN packet
if (sentBytes > 0) { // If SYN packet suucessfully sent
ctx->connection_state = SYN_SENT;
free(SYN_packet);
return true;
} else {
free(SYN_packet);
free(ctx);
// stcp_unblock_application(sd);
errno = ECONNREFUSED;
return false;
}
}
void wait_for_SYN_ACK(mysocket_t sd, context_t* ctx) {
char buffer[sizeof(STCPHeader)];
unsigned int event = stcp_wait_for_event(sd, NETWORK_DATA, NULL);
ssize_t receivedBytes = stcp_network_recv(sd, buffer, MSS);
// Verify size of received packet
if (receivedBytes < sizeof(STCPHeader)) {
free(ctx);
// stcp_unblock_application(sd);
errno = ECONNREFUSED; // TODO
return;
}
// Parse received data
STCPHeader* receivedPacket = (STCPHeader*)buffer;
// Check for appropriate flags and set connection state
if (receivedPacket->th_flags == (TH_ACK | TH_SYN)) {
ctx->rec_seq_num = ntohl(receivedPacket->th_seq);
ctx->rec_wind_size =
ntohs(receivedPacket->th_win) > 0 ? ntohs(receivedPacket->th_win) : 1;
ctx->connection_state = SYN_ACK_RECEIVED;
}
}
STCPHeader* create_ACK_packet(unsigned int seq, unsigned int ack) {
STCPHeader* ACK_packet = (STCPHeader*)malloc(sizeof(STCPHeader));
ACK_packet->th_seq = htonl(seq);
ACK_packet->th_ack = htonl(ack);
ACK_packet->th_off = htons(5); // header size offset for packed data
ACK_packet->th_flags = TH_ACK; // set packet type to ACK
ACK_packet->th_win = htons(WINDOW_SIZE); // default value
return ACK_packet;
}
bool send_ACK(mysocket_t sd, context_t* ctx) {
// printf("Sending ACK\n");
// Create ACK Packet
STCPHeader* ACK_packet =
create_ACK_packet(ctx->seq_num, ctx->rec_seq_num + 1);
// Send ACK packet
ssize_t sentBytes =
stcp_network_send(sd, ACK_packet, sizeof(STCPHeader), NULL);
// Verify sending of ACK packet
if (sentBytes > 0) { // If ACK packet suucessfully sent
free(ACK_packet);
return true;
} else {
free(ACK_packet);
free(ctx);
// stcp_unblock_application(sd);
errno = ECONNREFUSED; // TODO
return false;
}
}
void wait_for_SYN(mysocket_t sd, context_t* ctx) {
char buffer[sizeof(STCPHeader)];
unsigned int event = stcp_wait_for_event(sd, NETWORK_DATA, NULL);
ssize_t receivedBytes = stcp_network_recv(sd, buffer, MSS);
// Verify size of received packet
if (receivedBytes < sizeof(STCPHeader)) {
free(ctx);
// stcp_unblock_application(sd);
errno = ECONNREFUSED; // TODO
return;
}
// Parse received data
STCPHeader* receivedPacket = (STCPHeader*)buffer;
// Check for appropriate flags and set connection state
if (receivedPacket->th_flags == TH_SYN) {
ctx->rec_seq_num = ntohl(receivedPacket->th_seq);
ctx->rec_wind_size =
ntohs(receivedPacket->th_win) > 0 ? ntohs(receivedPacket->th_win) : 1;
ctx->connection_state = SYN_RECEIVED;
}
}
STCPHeader* create_SYN_ACK_packet(unsigned int seq, unsigned int ack) {
STCPHeader* SYN_ACK_packet = (STCPHeader*)malloc(sizeof(STCPHeader));
SYN_ACK_packet->th_seq = htonl(seq);
SYN_ACK_packet->th_ack = htonl(ack);
SYN_ACK_packet->th_off = htons(5); // header size offset for packed data
SYN_ACK_packet->th_flags = (TH_SYN | TH_ACK); // set packet type to SYN_ACK
SYN_ACK_packet->th_win = htons(WINDOW_SIZE); // default value
return SYN_ACK_packet;
}
bool send_SYN_ACK(mysocket_t sd, context_t* ctx) {
// Create SYN_ACK Packet
STCPHeader* SYN_ACK_packet =
create_SYN_ACK_packet(ctx->seq_num, ctx->rec_seq_num + 1);
ctx->seq_num++;
// Send SYN_ACK packet
ssize_t sentBytes =
stcp_network_send(sd, SYN_ACK_packet, sizeof(STCPHeader), NULL);
// Verify sending of SYN_ACK packet
if (sentBytes > 0) { // If SYN_ACK packet suucessfully sent
ctx->connection_state = SYN_ACK_SENT;
free(SYN_ACK_packet);
return true;
} else {
free(SYN_ACK_packet);
free(ctx);
stcp_unblock_application(sd);
errno = ECONNREFUSED; // TODO
return false;
}
}
void wait_for_ACK(mysocket_t sd, context_t* ctx) {
char buffer[sizeof(STCPHeader)];
unsigned int event = stcp_wait_for_event(sd, NETWORK_DATA, NULL);
ssize_t receivedBytes = stcp_network_recv(sd, buffer, MSS);
// Verify size of received packet
if (receivedBytes < sizeof(STCPHeader)) {
free(ctx);
// stcp_unblock_application(sd);
errno = ECONNREFUSED; // TODO
return;
}
// Parse received data
STCPHeader* receivedPacket = (STCPHeader*)buffer;
printSTCPHeader(receivedPacket);
// Check for appropriate flags and set connection state
if (receivedPacket->th_flags == TH_ACK) {
ctx->rec_seq_num = ntohl(receivedPacket->th_seq);
ctx->rec_wind_size =
ntohs(receivedPacket->th_win) > 0 ? ntohs(receivedPacket->th_win) : 1;
if (ctx->connection_state == FIN_SENT) {
ctx->connection_state = CSTATE_CLOSED;
}
}
}
void app_data_event(mysocket_t sd, context_t* ctx) {
size_t max_payload_length = min(MSS, ctx->rec_wind_size) - sizeof(STCPHeader);
char payload[max_payload_length];
ssize_t app_bytes = stcp_app_recv(sd, payload, max_payload_length);
// printf("App Data Bytes: %d\n", app_bytes);
// printf("App Data Payload: %s\n", payload);
if (app_bytes == 0) {
free(ctx);
// stcp_unblock_application(sd);
errno = ECONNREFUSED; // TODO
return;
}
send_DATA_packet_network(sd, ctx, payload, app_bytes);
wait_for_ACK(sd, ctx);
}
void app_close_event(mysocket_t sd, context_t* ctx) {
if (ctx->connection_state == CSTATE_ESTABLISHED) {
send_FIN_packet(sd, ctx);
}
printf("connection_state: %d\n", ctx->connection_state);
}
STCPHeader* create_FIN_packet(unsigned int seq, unsigned int ack) {
STCPHeader* FIN_packet = (STCPHeader*)malloc(sizeof(STCPHeader));
FIN_packet->th_seq = htonl(seq);
FIN_packet->th_ack = htonl(ack);
FIN_packet->th_flags = TH_FIN;
FIN_packet->th_win = htons(WINDOW_SIZE);
FIN_packet->th_off = htons(5);
return FIN_packet;
}
bool send_FIN_packet(mysocket_t sd, context_t* ctx) {
STCPHeader* FIN_packet =
create_FIN_packet(ctx->seq_num, ctx->rec_seq_num + 1);
ctx->seq_num++;
// Send FIN packet
ssize_t sentBytes =
stcp_network_send(sd, FIN_packet, sizeof(STCPHeader), NULL);
// Verify sending of FIN packet
if (sentBytes > 0) { // If FIN packet suucessfully sent
ctx->connection_state = FIN_SENT;
wait_for_ACK(sd, ctx);
free(FIN_packet);
return true;
} else {
free(FIN_packet);
free(ctx);
// stcp_unblock_application(sd);
errno = ECONNREFUSED; // TODO
return false;
}
}
void network_data_event(mysocket_t sd, context_t* ctx) {
bool isFIN = false;
bool isDUP = false; // test if packet is a duplicate
char payload[MSS];
ssize_t network_bytes = stcp_network_recv(sd, payload, MSS);
if (network_bytes < sizeof(STCPHeader)) {
free(ctx);
// stcp_unblock_application(sd);
errno = ECONNREFUSED; // TODO
return;
}
printSTCPHeader((STCPHeader*)payload);
// printf("Network Data Payload: %s\n", payload + sizeof(STCPHeader));
// printf("Network Bytes: %d\n", network_bytes);
parse_DATA_packet(ctx, payload, isFIN, isDUP);
if (isDUP) {
send_ACK(sd, ctx);
return;
}
if (isFIN) {
clock_gettime(CLOCK_REALTIME, &spec);
// printf("%d isFIN\n", spec.tv_nsec);
send_ACK(sd, ctx);
stcp_fin_received(sd);
ctx->connection_state = CSTATE_CLOSED;
return;
}
if (network_bytes - sizeof(STCPHeader)) {
// printf("isDATA\n");
send_DATA_packet_app(sd, ctx, payload, network_bytes);
send_ACK(sd, ctx);
}
}
void send_DATA_packet_app(mysocket_t sd, context_t* ctx, char* payload,
size_t length) {
// Send DATA packet
stcp_app_send(sd, payload + sizeof(STCPHeader), length - sizeof(STCPHeader));
}
void parse_DATA_packet(context_t* ctx, char* payload, bool& isFIN,
bool& isDUP) {
STCPHeader* payloadHeader = (STCPHeader*)payload;
ctx->rec_seq_num = ntohl(payloadHeader->th_seq);
ctx->rec_wind_size = ntohs(payloadHeader->th_win);
isFIN = payloadHeader->th_flags == TH_FIN;
}
STCPHeader* create_DATA_packet(unsigned int seq, unsigned int ack,
char* payload, size_t payload_length) {
// printf("Create DATA Packet Payload: %s\n", payload);
unsigned int DATA_packet_size = sizeof(STCPHeader) + payload_length;
// printf("DATA Packet Payload Size: %d\n", DATA_packet_size);
STCPHeader* DATA_packet = (STCPHeader*)malloc(DATA_packet_size);
DATA_packet->th_seq = htonl(seq);
DATA_packet->th_ack = htonl(ack);
DATA_packet->th_flags = NETWORK_DATA;
DATA_packet->th_win = htons(WINDOW_SIZE);
DATA_packet->th_off = htons(5);
memcpy((char*)DATA_packet + sizeof(STCPHeader), payload, payload_length);
return DATA_packet;
}
bool send_DATA_packet_network(mysocket_t sd, context_t* ctx, char* payload,
size_t payload_length) {
STCPHeader* DATA_packet = create_DATA_packet(
ctx->seq_num, ctx->rec_seq_num + 1, payload, payload_length);
printSTCPHeader(DATA_packet);
ctx->seq_num += payload_length;
// Send DATA packet
ssize_t sentBytes = stcp_network_send(
sd, DATA_packet, sizeof(STCPHeader) + payload_length, NULL);
// printf("Network Sent Bytes: %d\n", sentBytes);
if (sentBytes > 0) { // If SYN_ACK packet suucessfully sent
free(DATA_packet);
return true;
} else {
free(DATA_packet);
free(ctx);
// stcp_unblock_application(sd);
errno = ECONNREFUSED; // TODO
return false;
}
}
void printSTCPHeader(STCPHeader* print) {
// printf("\n****HEADER****\n");
// printf("th_sport: %d\n", ntohs(print->th_sport)); /* source port */
// printf("th_dport: %d\n", ntohs(print->th_dport)); /* destination port */
// printf("th_seq: %d\n", ntohl(print->th_seq)); /* sequence number */
// printf("th_ack: %d\n", ntohl(print->th_ack)); /* acknowledgement number
// */
// printf("th_flags: %d\n", print->th_flags);
// printf("th_win: %d\n", ntohs(print->th_win)); /* window */
// printf("th_off: %d\n", ntohs(print->th_off));
// printf("th_sum: %d\n", ntohs(print->th_sum)); /* checksum */
// printf("th_urp: %d\n",
// ntohs(print->th_urp)); /* urgent pointer (unused in STCP) */
// printf("\n****HEADER****\n\n");
}
int min(int a, int b) {
return (a < b ? a : b);
}
/**********************************************************************/
/* our_dprintf
*
* Send a formatted message to stdout.
*
* format A printf-style format string.
*
* This function is equivalent to a printf, but may be
* changed to log errors to a file if desired.
*
* Calls to this function are generated by the dprintf amd
* dperror macros in transport.h
*/
void our_dprintf(const char* format, ...) {
va_list argptr;
char buffer[1024];
assert(format);
va_start(argptr, format);
vsnprintf(buffer, sizeof(buffer), format, argptr);
va_end(argptr);
fputs(buffer, stdout);
fflush(stdout);
}