-
Notifications
You must be signed in to change notification settings - Fork 2
/
tfrc-server.c
592 lines (529 loc) · 18.6 KB
/
tfrc-server.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
588
589
590
591
592
/*********************************************************
*
* Module Name: tfrc server
*
* File Name: tfrc-server.c
*
* Summary:
* This file contains the echo server code
*
* Revisions:
* Created by Fangyu He for CPSC 8520, Fall 2015
* School of Computing, Clemson University
*
*********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>//for ualarm()
#include <inttypes.h> // for print uint64
#include <sys/time.h>
#include <stdbool.h>
#include "tfrc.h"
#include "tfrc-server.h"
static struct control_t *buffer = NULL;
static struct control_t *ok = NULL;
static struct data_t *data = NULL;
static struct ACK_t *dataAck = NULL;
int sock; /* Socket */
struct sockaddr_in clntAddr; /* Client address */
static uint32_t CxID;
uint64_t array[9];
static uint32_t lossRate = 0;
static uint32_t preLossRate = 0;
int bindFlag = 0; /* server bind with any client or not*/
//static uint32_t recvRate = 0;
/*var for output*/
static int countRecv = 0;
static int countRecvBytes = 0;
static int countAck = 0;
static double countDroped = 0;
static double accuLossrate = 0;
static uint32_t seqMax = 0;
static uint32_t seqMin = 0;
static uint32_t ackNum = 0;
static uint32_t preNum;//for change if any new data come
static uint32_t newestNum;//for change if any new data come
static uint64_t temp;//for reserve for current time
static uint64_t lastestTimeStamp;
/*the newest RTT*/
static uint32_t RTT=0;
/*the struct for store the receive history*/
QUEUE *mylog;
struct logEntry *entry;
/*the struct for storing the loss packets*/
node_t *lossRecord = NULL;
struct timeval tv;
void sigHandler(int);
char Version[] = "1.1";
void sendOk(int sock, struct sockaddr_in *server, uint32_t seqNum, uint16_t msgSize);
void sendDataAck(int sock,struct sockaddr_in *server);
void enQueueAndCheck(struct data_t *data);
void updateLoss();
uint64_t T_lossCompute(uint32_t S_loss);
float getWeight(int i, int I_num);
void compute();
uint64_t max(uint64_t i1, uint64_t i2);
void display();
void handle_alarm(int ignored);
/**
* bin : echo the BIN from client
* seq : echo the seq from client
* bsize : echo the Bisze from client
*
* */
void sendOk(int sock, struct sockaddr_in *server, uint32_t seqNum, uint16_t msgSize)
{
ok->msgLength = htons(CONT_LEN);
ok->msgType = CONTROL;
ok->code = OK;
ok->CxID = htonl(CxID);
ok->seqNum = htonl(seqNum);
ok->msgSize = htons(msgSize);
/* start to send.. */
if (sendto(sock, ok, sizeof(struct control_t), 0, (struct sockaddr *)server, sizeof(*server)) != sizeof(struct control_t))
{
DieWithError("sendto() sent a different number of bytes than expected");
}
}
/**
* bin : echo the BIN from client
* seq : echo the seq from client
* bsize : echo the Bisze from client
*
* */
void sendDataAck(int sock,struct sockaddr_in *server)
{
dataAck->msgLength = htons(ACK_LEN);
dataAck->msgType = ACK;
dataAck->code = OK;
dataAck->CxID = htonl(CxID);
dataAck->ackNum = htonl(ackNum>0?ackNum:data->seqNum+1);
gettimeofday(&tv, NULL);
//if (ackNum>0)
dataAck->timeStamp = lastestTimeStamp;
//else
// dataAck->timeStamp = mylog->qBase[mylog->rear-1]->timeArrived;
temp = 1000000 * tv.tv_sec + tv.tv_usec;
//dataAck->T_delay = htonl(temp - mylog->qBase[mylog->rear-1]->timeArrived);
dataAck->T_delay = htonl(temp - dataAck->timeStamp);
dataAck->lossRate = htonl(lossRate);
//multi 1000 then take the floor for recvRate
if (RTT == 0){
printf("\nRTT equals 0. exited\n");
exit(0);
}
//printf("timeStamp recv%" PRIu64 "\n",data->timeStamp);
dataAck->recvRate = htonl((uint32_t)(getRecvBits(mylog, (data->timeStamp - RTT))*1000000/RTT));
if (dataAck->recvRate == 0)
return;
//printf("\nstart to send ack\n");
//printf("ackNum %u timeStamp %lu T_delay %u lossRate %u recvRate %u nowT %lu \n\n", ntohl(dataAck->ackNum), dataAck->timeStamp, ntohl(dataAck->T_delay), ntohl(dataAck->lossRate), ntohl(dataAck->recvRate), temp);
/* start to send.. */
if (sendto(sock, dataAck, sizeof(struct ACK_t), 0, (struct sockaddr *)server, sizeof(*server) ) != sizeof(struct ACK_t))
{
DieWithError("sendto() sent a different number of bytes than expected");
}
countAck++;
accuLossrate += (double)lossRate/1000;
printf("\n%lf accuLossrate\n", accuLossrate);
//printf("lossRate + dataAck->lossRate %lu\n\n", dataAck->lossRate);
}
void enQueueAndCheck(struct data_t *data)
{
entry->packet = data;
gettimeofday(&tv, NULL);
entry->timeArrived = 1000000 * tv.tv_sec + tv.tv_usec;
lastestTimeStamp = entry->timeArrived;
//printf("%" PRIu64 "\n\n",entry->timeArrived);
//printf("%" PRIu64 "\n",tv.tv_sec);
//printf("%" PRIu64 "\n",tv.tv_usec);
enQueue(mylog, entry);
/*if it is a packet used to be a loss one at receiver*/
//if (remove_by_seqNum(&lossRecord, data->seqNum)!=-1)
//{
// /*remove this loss in the loss record and recompute the lossrate*/
// countDroped--;
// printf("\n--count\n\n");
// compute();
//}else{
/*else to figure if any new packet need to be looked as loss ones and recompute the lossrate*/
updateLoss();
compute();
//}
}
/*add any new loss to the lossRecord*/
void updateLoss ()
{
uint64_t T_loss;
//if (higher==3) add to lossRecord;
//if (mylog->rear - mylog->front <4)
// return;
/*get the third biggest seqNum*/
uint32_t num = getMaxSeqNum(mylog,3);
uint32_t num1 = getMaxSeqNum(mylog,4);
uint32_t i;
int index,j;//index for return value of existSeqNum, j for last receive packet index
uint32_t latestNum;
/*add all packets(not received) that have exactly 3 higher packet seqNum*/
for (i=num;i>num1;i--)
//for (i=num;i>mylog->qBase[mylog->rear]->packet->seqNum;i--)
{
index = existSeqNum(mylog, i-1);
if (index == -1)
{
T_loss = T_lossCompute(i-1);
append(&lossRecord, i-1, T_loss);
countDroped++;
printf("%u lost\n\n", i-1);
latestNum = i-1;
}
else{
j = index;
break;
}
}
//if (ackNum != latestNum){
// preAckNum = ackNum;
ackNum = latestNum;
//}
}
/*compute the T_loss*/
uint64_t T_lossCompute(uint32_t S_loss)
{
uint64_t T_loss;
int index_before = getIndexBefore(mylog, S_loss);
int index_after = getIndexAfter(mylog, S_loss);
uint32_t S_before = mylog->qBase[index_before]->packet->seqNum;
uint32_t S_after = mylog->qBase[index_after]->packet->seqNum;
uint64_t T_before = mylog->qBase[index_before]->timeArrived;
uint64_t T_after = mylog->qBase[index_after]->timeArrived;
if ((S_after - S_before)==0){
printf("(S_after - S_before)==0 exited\n%d %d\n", S_after, S_before);
exit(0);
}
T_loss = T_before + ((T_after - T_before) * (S_loss - S_before) / (S_after - S_before));
return T_loss;
}
/*compute the weight of loss Interval*/
float getWeight(int i, int I_num)
{
if(I_num > 8)
I_num = 8;
float w_i = 0;
if(i < I_num/2)
w_i = 1;
else
w_i = 1-(i-((float)I_num/2-1))/((float)I_num/2+1);
return w_i;
}
/*correct the start loss event mark and compute the lossrate*/
void compute()
{
/*correct the start loss parket mark*/
node_t * p = lossRecord;
p->isNewLoss = true;
uint64_t lossStart;
lossStart = p->timeArrived;
uint64_t interval = 0;
/*compute the number of loss event*/
int I_count = 0;
while(p->next != NULL)
{
p->next->isNewLoss = false;
interval = p->next->timeArrived - p->timeArrived;
if(interval > RTT)
{
lossStart = p->next->timeArrived;
p->next->isNewLoss = true;
I_count++;
}
p=p->next;
}
/*contruct the array to store the Interval of loss event, the order is the reveal as the RFC 3448 description*/
int i = 0;
p = lossRecord;
uint64_t preTime = lossRecord->timeArrived;
while(p != NULL)
{
if (p->isNewLoss == true)
{
if (I_count>=8)
{
i++;
if (i>=I_count-8)
if(I_count-i>0)
array[I_count-i] = p->timeArrived - preTime;
}else{
array[I_count-i] = p->timeArrived - preTime;
i++;
}
preTime = p->timeArrived;
}
p=p->next;
}
//uint64_t min=4444444444444444;
//for (i=0;i<9;i++)
//{
// if (array[i]<min)
// min = array[i];
//}
//for (i=0;i<9;i++)
//{
// if (array[i]>444444444444444)
// array[i] = min;
//}
gettimeofday(&tv, NULL);
array[0] = (1000000 * tv.tv_sec + tv.tv_usec) - preTime;
//printf("array[0]%lu timenow%lu pretime%lu\n\n", array[0], (1000000 * tv.tv_sec + tv.tv_usec), preTime);
/*compute the loss event rate*/
int n;
if (I_count > 8)
n = 9;
else
n = I_count;
double I_tot0 = 0;
double I_tot1 = 0;
double I_tot = 0;
double I_mean = 0;
double W_tot = 0;
double tempTot;
for (i=1;i<n;i++)
{
tempTot = ((double)array[i]*getWeight(i,n)/1000000);
if (tempTot < 10000 && tempTot != 0)
{
I_tot0 = I_tot0 + tempTot;
//printf("Itot part%lf\n\n",tempTot);
W_tot = W_tot + getWeight(i,n);
}
}
for (i=1;i<=n;i++)
I_tot1 = I_tot1 + ((double)array[i]*getWeight(i-1,n)/1000000);
//I_tot = max(I_tot0, I_tot1);
I_tot = I_tot0;
//printf("0%lf 1%lf\n\n", I_tot0, I_tot1);
I_mean = I_tot/W_tot;
//printf("I_mean %lf W_tot %lf I_tot %lf I_count %d", I_mean, W_tot, I_tot, I_count);
lossRate = (uint32_t)((1/I_mean)*1000);
}
uint64_t max(uint64_t i1, uint64_t i2)
{
if (i1>i2)
return i1;
else
return i2;
}
int main(int argc, char *argv[])
{
/* server var */
unsigned short servPort; /* Server port */
struct sockaddr_in servAddr; /* Local address */
uint8_t bindMsgSize;
/* addr&port bind with */
unsigned long bindIP;
unsigned short bindPort;
/* client var */
unsigned int cliAddrLen; /* Length of incoming message */
int recvMsgSize; /* Size of received message */
/* packets var */
uint8_t msgType;
uint8_t code;
/*init the Queue used for receive history*/
mylog = (QUEUE *)malloc(sizeof(QUEUE));
initQueue(mylog);
//mylog->qBase = (struct logEntry **)malloc(sizeof(struct logEntry *)*MAXN);
//mylog->front = mylog->rear = 0;
/*init loss Record*/
lossRecord = (node_t *)malloc(sizeof(node_t));
/*init entry*/
entry = (struct logEntry *)malloc(sizeof(struct logEntry));
entry->packet = (struct data_t*)malloc(sizeof(struct data_t));
/* Check for correct number of parameters */
if (argc >= 2)
{
servPort = atoi(argv[1]); /* local port */
//printf("%s",argv[1] );
}
else
{
fprintf(stderr,"Usage: %s <TFRC SERVER PORT>\n", argv[0]);
exit(1);
}
/* bind SIGINT function */
signal(SIGINT, sigHandler);
signal(SIGALRM, handle_alarm);
/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
printf("Failure on socket call, errno:%d\n", errno);
}
/* Construct local address structure */
memset(&servAddr, 0, sizeof(servAddr)); /* Zero out structure */
servAddr.sin_family = AF_INET; /* Internet address family */
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);/* Any incoming interface */
servAddr.sin_port = htons(servPort); /* Local port */
/* create buffer to store packets. 1600 maximum of packet size */
buffer = (struct control_t*)calloc((size_t)MAX_BUFFER, 1);
ok = (struct control_t*)calloc((size_t)MAX_BUFFER, 1);
dataAck = (struct ACK_t*)calloc((size_t)MAX_BUFFER, 1);
/* Bind to the local address */
if (bind(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
{
printf("Failure on bind, errno:%d\n", errno);
}
/* Forever Loop */
for (;;)
{
cliAddrLen = sizeof(clntAddr);
//printf(" success!!\n");
/* Block until receive message from a client */
if ((recvMsgSize = recvfrom(sock, buffer, MAX_BUFFER, 0, (struct sockaddr *) &clntAddr, &cliAddrLen)) < 0)
{
printf("Failure on recvfrom, client: %s, errno:%d\n", inet_ntoa(clntAddr.sin_addr), errno);
continue;
}
buffer->msgLength = ntohs(buffer->msgLength);
buffer->CxID = ntohl(buffer->CxID);
buffer->seqNum = ntohl(buffer->seqNum);
if(buffer->seqNum > seqMax)
seqMax = buffer->seqNum;
/* Parsing the packet */
msgType = buffer -> msgType;
code = buffer -> code;
switch (msgType)
{
case CONTROL :
{
switch (code)
{
case START :
{
printf("\nreceived START!!\n\n");
if (bindFlag == 0)
{
bindFlag = 1;
//init the record var
countRecv = 0;
countRecvBytes = recvMsgSize;
seqMax = buffer->seqNum;
seqMin = buffer->seqNum;
bindPort = clntAddr.sin_port;
bindIP = clntAddr.sin_addr.s_addr;
CxID = buffer->CxID;
bindMsgSize = ntohs(buffer->msgSize);
printf("start packet:\n");
printf("length:%d\n", buffer->msgLength);
printf("type:%d\n", (int)buffer->msgType);
printf("code:%d\n", (int)buffer->code);
printf("Cxid:%d\n", buffer->CxID);
printf("Seq#:%d\n", buffer->seqNum);
printf("size:%d\n\n", buffer->msgSize);
sendOk(sock, &clntAddr,
buffer->seqNum,
buffer->msgSize
);
}
break;
}
case STOP :
{
if (bindFlag == 1
&& bindIP == clntAddr.sin_addr.s_addr
&& bindPort == clntAddr.sin_port)
{
printf("\nSTOP msg received!\n\n");
sendOk(sock, &clntAddr,
buffer->seqNum,
buffer->msgSize
);
printf("\nSTOP OK send!\n\n");
//display the output information
display();
ualarm(0,0);
mylog = (QUEUE *)malloc(sizeof(QUEUE));
initQueue(mylog);
lossRecord = (node_t *)malloc(sizeof(node_t));
bindFlag = 0;
}
break;
}
default : break;
}
break;
}
case DATA :
{
//printf("received DATA!!\n");
if (bindFlag == 1
&& bindIP == clntAddr.sin_addr.s_addr
&& bindPort == clntAddr.sin_port)
{
//printf("The total packet loss : %lf\n",countDroped);
countRecv++;
countRecvBytes += recvMsgSize;
data = (struct data_t *)buffer;
data->RTT = ntohl(data->RTT);
newestNum = data->seqNum;
// printf("timeStamp recv%" PRIu64 "\n",data->timeStamp);
//printf("timeStamp %lu, %d, %d, %d\n",data->timeStamp, data->seqNum, data->RTT, data->msgLength);
if (RTT == 0)
ualarm(data->RTT,0);
RTT = data->RTT;
//printf("data %" PRIu32 " received\n", data->seqNum);
//RTT = 1000000;//for test
preLossRate = lossRate;
enQueueAndCheck(data);
if(lossRate > preLossRate)
{
//printf("\nalarm now\n");
ualarm(0,0);
sendDataAck(sock, &clntAddr);
ualarm(RTT,0);
}
//send each receive
//else sendDataAck(sock, &clntAddr);
}
break;
}
default : break;
}
}
close(sock);
printf("\nServer terminates.\n\n");
return 0;
}
void display()
{
int count;
if (seqMax==0)
count = 0;
else
count = seqMax-seqMin;
printf("\nAmount of data received: %d packets and %d bytes\n", countRecv,countRecvBytes);
printf("Number of ACKs sent: %d packets\n", countAck);
printf("The total packet loss rate: %.3f\n",(double)(count-countRecv)/(seqMax-seqMin+1));
//printf("seqMax%d seqMin%d countRecv%d\n",seqMax,seqMin,countRecv);
//printf("The total packet : %d\n",count);
//printf("The total packet loss : %lf\n",countDroped);
//printf("The total packet loss : %d\n",count-countRecv);
printf("Average of loss event rates sent to the send: %.3f\n", countAck==0 ? 0 : accuLossrate/countAck);
}
void sigHandler(int sig)
{
//printf("result\n");
display();
exit(1);
}
void handle_alarm(int ignored)
{
//printf("enter alarm handler\n");
if(bindFlag == 0)
return;
if(preNum != newestNum)
sendDataAck(sock, &clntAddr);
preNum = newestNum;
ualarm(RTT,0);
return;
}