-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog2_arq.c
314 lines (257 loc) · 8.17 KB
/
prog2_arq.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
#include <stdio.h>
#include <stdlib.h>
#include <prog2.h>
int compute_checksum(struct pkt packet)
{
int i;
int checksum;
checksum = 0;
checksum += packet.seqnum;
checksum += packet.acknum;
for (i = 0; i < 20; i++) {
checksum += (int) packet.payload[i];
}
return checksum;
}
int compare_checksum(struct pkt packet)
{
if (packet.checksum == compute_checksum(packet)) return CHKSUM_CORRECT;
return CHKSUM_WRONG;
}
struct pkt *make_pkt(struct msg *message, struct conn_ncb *ncb)
{
struct pkt *pkt;
pkt = malloc(sizeof(struct pkt));
pkt->seqnum = -1;
pkt->acknum = -1;
pkt->checksum = -1;
int i;
for (i = 0; i < 20; i++) pkt->payload[i] = message->data[i];
return pkt;
}
int put_pkt_in_buffer(struct pkt *pkt, struct conn_ncb *ncb)
{
int head = ncb->tx_ncb->head;
int tail = ncb->tx_ncb->tail;
if ((head + 1) % SIZE_TX_BUFFER_ARQ == tail) {
printf("**** Buffer full, so DROP messages coming from Layer 5.\n");
return PKT_DROP;
}
ncb->tx_ncb->buffers[head].pkt = pkt;
ncb->tx_ncb->head += 1;
ncb->tx_ncb->head %= SIZE_TX_BUFFER_ARQ;
printf("--> Put packet received from Layer 5 in buffer.\n");
return PKT_SAVE;
}
void send_pkt(struct conn_ncb *ncb, int mode)
{
int head = ncb->tx_ncb->head;
int tail = ncb->tx_ncb->tail;
int base = ncb->tx_ncb->base;
int nextseg = ncb->tx_ncb->nextseg;
if (head == tail) {
printf("**** Buffer is empty. NO messages to send to Layer 3.\n");
return;
}
if (nextseg - base >= ncb->tx_ncb->cwnd) {
printf("**** cwnd = %d is full. NO messages sending to Layer 3.\n", ncb->tx_ncb->cwnd);
return;
}
struct pkt *pkt;
pkt = ncb->tx_ncb->buffers[nextseg].pkt;
//if (ncb->seqnum == -1 && ncb->acknum == -1) {
if (ncb->conn == -1) {
ncb->conn = END_SENDER;
ncb->seqnum = 0;
ncb->acknum = 0;
}
if (mode != MODE_RESEND) {
pkt->seqnum = ncb->seqnum;
pkt->acknum = ncb->acknum; //-1;//ncb->acknum;
pkt->checksum = compute_checksum(*pkt);
ncb->seqnum += 1;
ncb->seqnum %= MAX_SEQ_ACK;
}
if (base == nextseg)
starttimer(0, ncb->increment);
ncb->tx_ncb->nextseg += 1;
ncb->tx_ncb->nextseg %= SIZE_TX_BUFFER_ARQ;
printf("--> Send packet with: seqnum: %d; acknum: %d; checksum: %d\n", pkt->seqnum, pkt->acknum, pkt->checksum);
tolayer3(0, *pkt);
}
void A_output(message)
struct msg message;
{
if (A_conn_ncb->host == A) printf("[A] ");
else printf("[B] ");
printf("--------------------------------------------------\n");
struct pkt *pkt;
pkt = make_pkt(&message, A_conn_ncb);
if (put_pkt_in_buffer(pkt, A_conn_ncb) != PKT_DROP) send_pkt(A_conn_ncb, MODE_SEND);
printf("==================================================\n");
}
void recv_pkt(struct pkt *pkt, struct conn_ncb *ncb) {
printf("--> Receive packet with: seqnum: %d; acknum: %d; checksum: %d\n", pkt->seqnum, pkt->acknum, pkt->checksum);
printf("--> Compute checksum: %d\n", compute_checksum(*pkt));
//if (pkt->seqnum == -1 && pkt->acknum != -1) {
if (ncb->conn == END_SENDER) {
if (compare_checksum(*pkt) == CHKSUM_WRONG || pkt->acknum == -2) {
printf("--> Receive CORRUPTED packet\n");
} else if (pkt->acknum == -2) {
printf("--> Receive NAK\n");
printf("--> Resend packet:\n");
ncb->tx_ncb->nextseg = ncb->tx_ncb->base;
if (ncb->host == A) stoptimer(0);
else stoptimer(1);
send_pkt(ncb, MODE_RESEND);
} else {
printf("--> Receive correct packet\n");
ncb->tx_ncb->tail += 1;
ncb->tx_ncb->tail %= SIZE_TX_BUFFER_ARQ;
ncb->tx_ncb->base += 1;
ncb->tx_ncb->base %= SIZE_TX_BUFFER_ARQ;
if (ncb->host == A) stoptimer(0);
else stoptimer(1);
if (ncb->tx_ncb->base != ncb->tx_ncb->nextseg) {
if (ncb->host == A) starttimer(0, ncb->increment);
else starttimer(1, ncb->increment);
}
}
//} else if (pkt->seqnum != -1 && pkt->acknum == -1) {
} else if (ncb->conn == END_RECEIVER) {
if (compare_checksum(*pkt) == CHKSUM_WRONG) {
printf("--> Receive CORRUPTED packet\n");
printf("--> Send NAK\n");
struct msg *nak_msg;
struct pkt *nak_pkt;
nak_msg = malloc(sizeof(struct msg));
int i;
for (i = 0; i < 20; i++) nak_msg->data[i] = 0;
nak_pkt = make_pkt(nak_msg, ncb);
nak_pkt->seqnum = ncb->seqnum;
nak_pkt->acknum = -2;
nak_pkt->checksum = compute_checksum(*nak_pkt);
printf("--> Send packet with: seqnum: %d; acknum: %d; checksum: %d\n", nak_pkt->seqnum, nak_pkt->acknum, nak_pkt->checksum);
if (ncb->host == A) tolayer3(0, *nak_pkt);
else tolayer3(1, *nak_pkt);
} else {
printf("--> Receive correct packet\n");
printf("--> Send ACK\n");
if (ncb->acknum != pkt->seqnum) {
ncb->rx_ncb->success++;
}
struct msg *ack_msg;
struct pkt *ack_pkt;
ack_msg = malloc(sizeof(struct msg));
int i;
for (i = 0; i < 20; i++) ack_msg->data[i] = 0;
ack_pkt = make_pkt(ack_msg, ncb);
ack_pkt->seqnum = ncb->seqnum;
ack_pkt->acknum = pkt->seqnum;
ncb->acknum = pkt->seqnum;
ack_pkt->checksum = compute_checksum(*ack_pkt);
printf("--> Send packet with: seqnum: %d; acknum: %d; checksum: %d\n", ack_pkt->seqnum, ack_pkt->acknum, ack_pkt->checksum);
if (ncb->host == A) tolayer3(0, *ack_pkt);
else tolayer3(1, *ack_pkt);
}
} else {
printf("!!!Error in recv_pkt.\n\n");
}
}
/* called from layer 3, when a packet arrives for layer 4 */
void A_input(packet)
struct pkt packet;
{
printf("[A] ");
printf("--------------------------------------------------\n");
if (A_conn_ncb->conn == -1) A_conn_ncb->conn = END_RECEIVER;
recv_pkt(&packet, A_conn_ncb);
/*if (A_conn_ncb->acknum != packet->seqnum + 20) {
}*/
printf("==================================================\n");
}
/* called when A's timer goes off */
void A_timerinterrupt()
{
printf("[A] ");
printf("--------------------------------------------------\n");
printf("--> Timeout\n");
//stoptimer(0);
if (A_conn_ncb->tx_ncb->base != A_conn_ncb->tx_ncb->nextseg) {
A_conn_ncb->tx_ncb->nextseg = A_conn_ncb->tx_ncb->base;
send_pkt(A_conn_ncb, MODE_RESEND);
}
printf("==================================================\n");
}
/* the following routine will be called once (only) before any other */
/* entity A routines are called. You can use it to do any initialization */
struct conn_ncb *init_ncb()
{
struct conn_ncb *ncb = malloc(sizeof(struct conn_ncb));
ncb->conn = -1;
ncb->seqnum = 0; //-1;
ncb->acknum = -1;
ncb->increment = 15.0;
ncb->tx_ncb = malloc(sizeof(struct tx));
ncb->tx_ncb->head = 0;
ncb->tx_ncb->tail = 0;
ncb->tx_ncb->base = 0;
ncb->tx_ncb->nextseg = 0;
ncb->rx_ncb = malloc(sizeof(struct rx));
ncb->rx_ncb->head = 0;
ncb->rx_ncb->tail = 0;
ncb->rx_ncb->success = 0;
return ncb;
}
void A_init()
{
A_conn_ncb = init_ncb();
A_conn_ncb->host = A;
// For Alternating Bit Protocol
A_conn_ncb->tx_ncb->cwnd = 1;
}
/* Note that with simplex transfer from a-to-B, there is no B_output() */
void B_output(message) /* need be completed only for extra credit */
struct msg message;
{
printf("[B] ");
printf("--------------------------------------------------\n");
struct pkt *pkt;
pkt = make_pkt(&message, B_conn_ncb);
if (put_pkt_in_buffer(pkt, B_conn_ncb) != PKT_DROP) send_pkt(B_conn_ncb, MODE_SEND);
printf("==================================================\n");
}
/* called from layer 3, when a packet arrives for layer 4 at B*/
void B_input(packet)
struct pkt packet;
{
printf("[B] ");
printf("--------------------------------------------------\n");
if (B_conn_ncb->conn == -1) B_conn_ncb->conn = END_RECEIVER;
recv_pkt(&packet, B_conn_ncb);
/*if (A_conn_ncb->acknum != packet->seqnum + 20) {
}*/
printf("==================================================\n");
}
/* called when B's timer goes off */
void B_timerinterrupt()
{
printf("[B] ");
printf("--------------------------------------------------\n");
printf("--> Timeout\n");
//stoptimer(1);
if (B_conn_ncb->tx_ncb->base != B_conn_ncb->tx_ncb->nextseg) {
B_conn_ncb->tx_ncb->nextseg = B_conn_ncb->tx_ncb->base;
send_pkt(B_conn_ncb, MODE_RESEND);
}
printf("==================================================\n");
}
/* the following rouytine will be called once (only) before any other */
/* entity B routines are called. You can use it to do any initialization */
void B_init()
{
B_conn_ncb = init_ncb();
B_conn_ncb->host = B;
// For Alternating Bit Protocol
B_conn_ncb->tx_ncb->cwnd = 1;
}