-
Notifications
You must be signed in to change notification settings - Fork 3
/
tcp.c
217 lines (194 loc) · 3.96 KB
/
tcp.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
//tcp.c
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
#include "tcp.h"
// Gets a human readable string representation of the IP
// Parameters:
// addr - sockaddr containing information about the socket including the ip
char *NET_get_ip(struct sockaddr_in *addr)
{
char ip[INET6_ADDRSTRLEN];
if(inet_ntop(AF_INET, &(addr->sin_addr), ip, INET6_ADDRSTRLEN) != NULL)
return strdup(ip);
return NULL;
}
// Gets a human readable unsigned short of the PORT
// Parameters:
// addr - sockaddr containing information about the socket including the port
unsigned short NET_get_port(struct sockaddr_in *addr)
{
unsigned short port;
port = htons(addr->sin_port);
return port;
}
//Packs the header field for an ACKNOWLEDGEMENT packet
//Does all conversions necessary and gets the timestamp
void pack_ack(char buf[], int id, int seq, double drop, double rx, int seq_recvd)
{
short *l = (short *)buf;
char *type = &(buf[2]);
char *code = &(buf[3]);
int *i = (int *)&(buf[4]);
int *s = (int *)&(buf[8]);
double *t = (double *)&(buf[12]);
int *td = (int *)&(buf[20]);
int *d = (int *)&(buf[24]);
int *r = (int *)&(buf[28]);
int *n = (int *)&(buf[32]);
*td = 0;
double time;
*l = htons(ACK_LENGTH);
//printf("Length\n");
//fflush(stdin);
*type = ACK;
//printf("TYPE\n");
//fflush(stdin);
*code = OK;
//printf("CODE\n");
//fflush(stdin);
*i = htonl(id);
//printf("ID\n");
//fflush(stdin);
*s = htonl(seq);
//printf("SEQ\n");
//fflush(stdin);
time = (get_time())*1000000;
//printf("Time: %g\n",time);
//fflush(stdin);
*t = htond(time);
//printf("Time\n");
//fflush(stdin);
*d = htonl((unsigned int)floor(drop*1000));
//printf("Drop\n");
//fflush(stdin);
*r = htonl((unsigned int)floor((rx*1000)));
//printf("RX\n");
//fflush(stdin);
*n = htonl(seq_recvd);
return;
}
//Converts 8 bytes from host byte order to network byte order
double htond(double host)
{
static int is_big = -1; //1 if Big Endian 0 if Little Endian
double network; //Holds network byte order representation
double host1 = host;
char *n = (char *)&network + sizeof(double)-1;
char *h = (char *)&host1;
unsigned int x = 1;
char *y = (char *)&x;
char z = 1;
int i;
if(is_big < 0)
{
if(*y == z)
{
is_big = 0;
}
else
{
is_big = 1;
}
}
if(is_big)
{
return host;
}
for(i=0;i<sizeof(double);i++)
{
*n = *h;
n--;
h++;
}
return network;
}
//Converts 8 bytes from network byte order to host byte order
double ntohd(double network)
{
static int is_big = -1; //1 if Big Endian 0 if Little Endian
double host; //Holds host byte order representation
char *n = (char *)&network + sizeof(double) -1;
char *h = (char *)&host;
unsigned int x = 1;
char *y = (char *)&x;
char z = 1;
int i;
if(is_big < 0)
{
if(*y == z)
{
is_big = 0;
}
else
{
is_big = 1;
}
}
if(is_big)
{
return network;
}
for(i=0;i<sizeof(double);i++)
{
*h = *n;
n--;
h++;
}
return host;
}
/*Shifts loss interval values*/
void shift_s_values(int s[])
{
int i;
for(i = N_MAX - 1; i>0; i--)
{
s[i] = s[i-1];
}
s[0] = 0;
return;
}
/*used for calculating Average loss rate*/
double calc_s_hat(int s[])
{
int i;
static double w[N_MAX] = {0};
double s_hat = 0;
static double w_sum = 0;
int n = N_MAX;
if(w[0] == 0)
{
for(i = 0; i <n/2; i++)
{
w[i] = 1;
}
for(i = n/2; i <n; i++)
{
w[i] = 1 - (i+1 - n/2.0)/(n/2.0 + 1);
}
for(i = 0; i < n; i++)
{
w_sum += w[i];
}
printf("w_sum: %g\n", w_sum);
}
for(i = 0; i < n; i++)
{
s_hat += w[i]*s[i];
//printf("s_intermediate: %g\n", s_hat);
}
s_hat = s_hat/w_sum;
return s_hat;
}
/*Returns time in seconds with precision down to microsecond*/
double get_time()
{
struct timeval curTime;
(void) gettimeofday (&curTime, (struct timezone *) NULL);
return (((((double) curTime.tv_sec) * 1000000.0)
+ (double) curTime.tv_usec) / 1000000.0); // do we need to divide by 1000000 for usec
}