-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.c~
More file actions
404 lines (333 loc) · 10.3 KB
/
common.c~
File metadata and controls
404 lines (333 loc) · 10.3 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
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
#include "common.h"
//// Super simplified serialization: raw memcpy between Packet/buffer. Needs more testing; this may depend on endianness of end systems.
void serializePacket(struct Packet* pkt, byte buf[RXTX_BUFFER_SIZE])
{
//super awesome benefit of not using dynamic mem in packet, since packet size is static
memset((void*)buf,0,RXTX_BUFFER_SIZE);
memcpy((void*)buf,(void*)pkt,sizeof(struct Packet));
}
void deserializePacket(const char buf[RXTX_BUFFER_SIZE], struct Packet* pkt)
{
//super awesome benefit of not using dynamic mem in packet, since packet size is static
memcpy((void*)pkt,(void*)buf,sizeof(struct Packet));
}
//For simple protocols: just verify packet ack field == ACK. Returns TRUE if ACK, else FALSE.
int isAck(struct Packet* pkt)
{
return pkt->ack == ACK ? TRUE : FALSE;
}
//For more complicated protocols: verify pkt contains ACK and has expected seqnum.
//Returns TRUE if packet is ACK and matches passed seqnum; else FALSE.
int isSequentialAck(struct Packet* pkt, int seqnum)
{
int result = FALSE;
if(isAck(pkt) == TRUE){
result = bytesToLint(pkt->seqnum) == seqnum ? TRUE : FALSE;
if(result == FALSE){
printf("ERROR received ACK with incorrect seqnum: expected %d but rxed %d\r\n",seqnum,bytesToLint(pkt->seqnum));
}
}
else{
printf("ERROR received ACK with incorrect seqnum: expected %d but rxed %d\r\n",seqnum,bytesToLint(pkt->seqnum));
}
return result;
}
/*
Returns sum of byte mod some-16b-prime of all the header fields.
This includes the data-checksum, but not the data.
*/
int getHeaderChecksum(struct Packet* pkt)
{
int i, sum = 0;
sum += pkt->ack;
for(i = 0; i < 4; i++){
sum += pkt->seqnum[i];
}
for(i = 0; i < 4; i++){
sum += pkt->dataLen[i];
}
for(i = 0; i < 4; i++){
sum += pkt->name[i];
}
for(i = 0; i < 4; i++){
sum += pkt->dataChecksum[i];
}
return sum % U16_PRIME;
}
//Get the checksum of some data buffer, given its length in bytes. Returns 0 if datalen is zero (no data)
int getDataChecksum(struct Packet* pkt)
{
int i, sum, checksum = 0;
//only set checksum if there is any data; otherwise, it is set to zero
if(pkt->dataLen > 0){
for(i = 0, sum = 0; i < bytesToLint(pkt->dataLen); i++){
sum += (int)pkt->data[i];
}
checksum = sum % U16_PRIME;
}
return checksum;
}
int isCorruptPacket(struct Packet* pkt)
{
int isCorrupt, checksum;
byte buf[4];
isCorrupt = CORRUPT;
//check the header checksum
checksum = getHeaderChecksum(pkt);
lintToBytes(checksum,buf);
if(strncmp(buf,pkt->hdrChecksum,4) == 0){
//check the data checksum
checksum = getDataChecksum(pkt);
lintToBytes(checksum,buf);
if(strncmp(buf,pkt->dataChecksum,4) == 0){
isCorrupt = NOT_CORRUPT;
}
else{
printf("ERROR rxed packet with correct header checksum, but incorrect data checksum. flagged as CORRUPT\r\n");
}
}
else{
printf("ERROR rxed packet with incorrect header checksum, flagged as CORRUPT\r\n");
}
return isCorrupt;
}
//////////////// end packet class ///////////////
int bytesToLint(const byte buf[4])
{
int lint = 0;
lint = (int)buf[3] & 0xFF;
lint |= (int)buf[2] & 0xFF00;
lint |= (int)buf[1] & 0xFF0000;
lint |= (int)buf[0] & 0xFF000000;
return htonl(lint);
}
void lintToBytes(const int i, byte obuf[4])
{
int lint = ntohl(i);
obuf[3] = (byte)(lint & 0xFF);
obuf[2] = (byte)(lint & 0xFF00);
obuf[1] = (byte)(lint & 0xFF0000);
obuf[0] = (byte)(lint & 0xFF000000);
}
/*
Fills chk[] with bytes of the checksum of data, some null-terminated data buffer.
Algorithm:
*/
void setDataChecksum(struct Packet* pkt)
{
int checksum = getDataChecksum(pkt);
lintToBytes(checksum,pkt->dataChecksum);
}
//Sets the socket timeout
void setSocketTimeout(int sockfd, int timeout_s)
{
struct timeval tv;
tv.tv_sec = timeout_s; // second timeout
tv.tv_usec = 0; // 0 us
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv,sizeof(struct timeval));
}
/*
Constructs a packet from data by:
-cleaning old packet contents (freeing old data)
-allocing new data in packet, and copying in
-setting ack
-setting name (just for debugging)
-checksumming data and placing this in the checksum field
To make an empty packet (such as ACK/NACK packets), pass NULL or 0 for "char* data" param.
*/
void makePacket(int seqnum, int ack, byte* data, struct Packet* pkt)
{
int dataLen, checksum;
cleanPacket(pkt);
//set the sequence number
lintToBytes(seqnum,pkt->seqnum);
//copy in the data, if any
if(data != 0){
dataLen = strnlen((char*)data,PKT_DATA_MAX_LEN);
lintToBytes(dataLen, pkt->dataLen);
if(dataLen > PKT_DATA_MAX_LEN - 32){
printf("ERROR length of data too long in makePacket: %d\r\n",dataLen);
}
strncpy((char*)pkt->data,data,dataLen);
}
else{
lintToBytes(0,pkt->dataLen);
memset((void*)pkt->data,0,PKT_DATA_MAX_LEN);
}
//do the data checksum; must be done before the header checksum
setDataChecksum(pkt);
//set the ack field (also must be done before )
pkt->ack = ack == ACK ? (byte)ACK : (byte)NACK;
//an apparent sequence of bytes, when viewed in wireshark
pkt->name[0] = 0xFF;
pkt->name[1] = 0;
pkt->name[2] = 0xFF;
pkt->name[3] = 0;
//must be done only after data checksum is set
checksum = getHeaderChecksum(pkt);
lintToBytes(checksum,pkt->hdrChecksum);
}
/*
Top level function for sending some file/stream. This implements the Kurose/Ross state rdt3.0 machine.
*/
void SendFile(FILE* fptr, int sock, struct sockaddr_in* sin)
{
int seqnum = 0;
int slen;
char buf[128];
memset(buf,0,128);
/* main loop: get and send lines of text */
while(fgets(buf, 80, fptr) != NULL){
slen = strnlen(buf,127);
buf[slen] ='\0';
SendData(sock,sin,seqnum,(byte*)buf);
//update seqnum, which in this case just alternates between 0 and 1
seqnum++;
seqnum %= 2;
}
printf("SEND COMPLETED!\r\n");
}
/*
Must make sure the transmission delay is larger than the propagation delay.
See Kurose on Ethernet.
Derivation of 51.2 us.
Propagation delay constraint is why ethernet must be short range.
Top level client function for sending a single packet. This implements the
stop-and-wait protocol state machine described by Kurose and Ross in figure 3.10:
client calls CliSend():
1) make and send a packet (with checksum)
2) wait for ACK/NACK:
if NACK:
goto 2
if ACK:
goto 3
3) return
*/
int SendData(int sock, struct sockaddr_in* addr, int seqnum, byte* data)
{
int response;
int sendSuccessful;
int state;
//TODO: These belong in some c++ class
const int SENDING = 1;
const int AWAIT_ACK = 2;
//the packet for sending data
struct Packet txPkt;
//the packet in which to receive ACK/NACK messages, exclusively
struct Packet ackPkt;
memset((void*)&txPkt,0,sizeof(struct Packet));
memset((void*)&ackPkt,0,sizeof(struct Packet));
makePacket(seqnum,ACK,data,&txPkt);
state = SENDING;
sendSuccessful = FALSE;
//The state machine for sending a single packet: send until a positive ACK is received
while(sendSuccessful != TRUE ){
//send this packet
if(state == SENDING){
sendPacket(&txPkt,sock,addr);
state = AWAIT_ACK;
}
//await packet acknowledgment from receiver
else if(state == AWAIT_ACK){
//block with timeout for ACK/NACK
response = awaitAck(sock,addr,seqnum,&ackPkt);
switch(response){
case ACK:
sendSuccessful = TRUE;
break;
//for all failure cases, just return to send state to re-send
//TODO: add retry-count limit
case NACK:
state = SENDING;
break;
case CORRUPT:
state = SENDING;
break;
case TIMEOUT:
state = SENDING;
break;
default:
printf("ERROR unmapped state result from _awaitAck function\r\n");
break;
}
}
//should be unreachable
else{
printf("ERROR unmapped state in _send()\r\n");
}
}
return TRUE;
}
/*
Blocks until we receive an ACK packet from the destination, or timeout occurs.
This implements the wait-for-ack state. We call recvfrom (without blocking) until
we receive an uncorrupted ACK, NACK, or until a timout occurs.
Precondition: This function expects that sockfd is a socket with a timeout (socket for which
setsockopts has been called). The intended behavior is for recvfrom to block with a timeout.
Returns: ACK, NACK, TIMEOUT.
*/
int awaitAck(int sock, struct sockaddr_in* addr, int seqnum, struct Packet* ackPkt)
{
int rxed, result;
int sock_len = sizeof(struct sockaddr_in);
char buf[RXTX_BUFFER_SIZE];
int ack = NACK;
memset(buf,0,RXTX_BUFFER_SIZE);
//block until we receive an ACK packet, or timeout occurs (returns -1)
rxed = recvfrom(sock,buf,RXTX_BUFFER_SIZE-1, 0, (struct sockaddr *)addr, &sock_len);
//either a packet was received, or timeout occurred (other errors also possible, but timeout is most likely if packet was dropped)
if(rxed >= 0){
//received packet: extract the received packet and proceed to check its validity
deserializePacket(buf,ackPkt);
//check the packet's status
if(!isCorrupt(ackPkt)){
if(isSequentialAck(ackPkt,seqnum)){
printf("Successfully received ACK packet\r\n");
result = ACK;
}
else{
result = NACK;
printf("NACK or incorrect seqnum received: pkt->seqnum=%d expected: %d ; pkt->ack=%s",bytesToLint(ackPkt->seqnum),seqnum, (ackPkt->ack == ACK ? "ACK" : "NACK"));
}
}
else{
printf("ERROR corrupt packet received. This should not be reachable in assignment's network conditions\r\n");
//just flag it as a NACK to signal failure
result = NACK;
}
}
else if(result == -1){
//only reachable if timeout occurred, though other errors are possible
printf("WARN timeout waiting for packet ACK\r\n");
result = TIMEOUT;
}
else{
//should be unreachable
printf("I have no idea what went wrong! \r\n");
result = NACK;
}
return result;
}
void cleanPacket(struct Packet* pkt)
{
memset((void*)pkt,0,sizeof(struct Packet));
}
/*
The raw send utility. Serializes a packet into the provided sendBuf, then sends
it over udp.
*/
void sendPacket(struct Packet* pkt, int sock, struct sockaddr_in * sin)
{
int pktLen;
byte sendBuf[RXTX_BUFFER_SIZE];
memset(sendBuf,0,RXTX_BUFFER_SIZE);
serializePacket(pkt,sendBuf);
pktLen = strnlen(sendBuf,RXTX_BUFFER_SIZE-1);
if(pktLen > (PKT_DATA_MAX_LEN - 32)){
printf("WARN possible packet data overrun: pkt data size > %d\r\n",(PKT_DATA_MAX_LEN-32));
}
if(sendto(sock, sendBuf, pktLen+1, 0, (struct sockaddr *)sin, sizeof(struct sockaddr)) < 0){
perror("SendTo Error\n");
exit(1);
}
}