Repository navigation
Expand file tree
/
Copy pathbuffer.c
More file actions
298 lines (253 loc) · 9.65 KB
/
Copy pathbuffer.c
File metadata and controls
298 lines (253 loc) · 9.65 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
#include "buffer.h"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
/**
* Initialize buffer with specified size
*
* @param buffer_size Size of the buffer (same as window size)
* @param output_fd File descriptor for output file
* @return Pointer to initialized Buffer structure or NULL on error
*/
Buffer* buffer_init(uint32_t buffer_size, int output_fd) {
if (buffer_size == 0) {
fprintf(stderr, "Error: Buffer size cannot be zero\n");
return NULL;
}
if (output_fd < 0) {
fprintf(stderr, "Error: Invalid file descriptor\n");
return NULL;
}
Buffer* buf = (Buffer*)malloc(sizeof(Buffer));
if (!buf) {
perror("Failed to allocate memory for Buffer");
return NULL;
}
buf->entries = (BufferEntry*)malloc(buffer_size * sizeof(BufferEntry));
if (!buf->entries) {
perror("Failed to allocate memory for buffer entries");
free(buf);
return NULL;
}
// Initialize all buffer entries
for (uint32_t i = 0; i < buffer_size; i++) {
buf->entries[i].valid = 0;
buf->entries[i].seq_num = 0;
buf->entries[i].data_len = 0;
}
buf->buffer_size = buffer_size;
buf->expected = 0; // Start with sequence number 0
buf->highest = 0; // No packets received yet
buf->state = BUFFER_STATE_IN_ORDER;
buf->fd = output_fd;
return buf;
}
/**
* Process received packet based on current buffer state
*
* @param buf Pointer to Buffer structure
* @param seq_num Sequence number of received packet
* @param data Pointer to packet data payload (excluding header)
* @param len Length of data
* @return 1 for RR, 0 for SREJ, -1 on error
*/
int buffer_process_packet(Buffer* buf, uint32_t seq_num, uint8_t* data, uint16_t len) {
if (!buf || !data) {
fprintf(stderr, "Error: NULL pointer passed to buffer_process_packet\n");
return -1;
}
if (len > MAX_DATA_SIZE) {
fprintf(stderr, "Error: Data size exceeds maximum allowed (%d > %d)\n", len, MAX_DATA_SIZE);
return -1;
}
// Process based on current state
switch (buf->state) {
case BUFFER_STATE_IN_ORDER:
if (seq_num == buf->expected) {
// In-order packet, write to file directly
ssize_t written = write(buf->fd, data, len);
if (written < 0) {
perror("Failed to write data to file");
return -1;
}
if ((size_t)written != len) {
fprintf(stderr, "Warning: Only wrote %zd of %d bytes\n", written, len);
}
// Update expected sequence number
buf->expected++;
// Return 1 to send RR for next expected packet
return 1;
}
else if (seq_num > buf->expected) {
// Out-of-order packet, buffer it
uint32_t index = seq_num % buf->buffer_size;
// Store data in buffer
buf->entries[index].seq_num = seq_num;
memcpy(buf->entries[index].data, data, len);
buf->entries[index].data_len = len;
buf->entries[index].valid = 1;
// Update highest received sequence number
if (seq_num > buf->highest) {
buf->highest = seq_num;
}
// Transition to BUFFERING state
buf->state = BUFFER_STATE_BUFFERING;
// Return 0 to send SREJ for missing packet
return 0;
}
else {
// Duplicate packet (seq_num < expected), ignore it
fprintf(stderr, "Warning: Received duplicate packet %u, expected %u\n", seq_num, buf->expected);
return 1; // Still send RR for expected packet
}
break;
case BUFFER_STATE_BUFFERING:
if (seq_num == buf->expected) {
// Received the expected packet while buffering
ssize_t written = write(buf->fd, data, len);
if (written < 0) {
perror("Failed to write data to file");
return -1;
}
// Update expected sequence number
buf->expected++;
// Transition to FLUSHING state to process buffered packets
buf->state = BUFFER_STATE_FLUSHING;
// Flush buffer and determine next action
return buffer_flush(buf);
}
else if (seq_num > buf->expected) {
// Another out-of-order packet
uint32_t index = seq_num % buf->buffer_size;
// Store data in buffer
buf->entries[index].seq_num = seq_num;
memcpy(buf->entries[index].data, data, len);
buf->entries[index].data_len = len;
buf->entries[index].valid = 1;
// Update highest received if needed
if (seq_num > buf->highest) {
buf->highest = seq_num;
}
// Still need the expected packet, send SREJ
return 0;
}
else {
// Duplicate packet, ignore it
fprintf(stderr, "Warning: Received duplicate packet %u while buffering\n", seq_num);
return 0; // Still need the expected packet, send SREJ
}
break;
case BUFFER_STATE_FLUSHING:
// This should not be called directly during FLUSHING
// buffer_flush handles the flushing state
fprintf(stderr, "Warning: buffer_process_packet called during FLUSHING state\n");
// Store packet for later processing
if (seq_num >= buf->expected) {
uint32_t index = seq_num % buf->buffer_size;
buf->entries[index].seq_num = seq_num;
memcpy(buf->entries[index].data, data, len);
buf->entries[index].data_len = len;
buf->entries[index].valid = 1;
if (seq_num > buf->highest) {
buf->highest = seq_num;
}
}
// Continue flushing
return buffer_flush(buf);
default:
fprintf(stderr, "Error: Invalid buffer state\n");
return -1;
}
}
/**
* Handle flushing state - process buffered packets in sequence
*
* @param buf Pointer to Buffer structure
* @return 1 if flushing complete (send RR), 0 if another gap found (send SREJ), -1 on error
*/
int buffer_flush(Buffer* buf) {
if (!buf) {
fprintf(stderr, "Error: NULL buffer pointer\n");
return -1;
}
// Flush all consecutive packets from buffer
while (buf->expected <= buf->highest) {
uint32_t index = buf->expected % buf->buffer_size;
// Check if this packet is in the buffer
if (!buf->entries[index].valid || buf->entries[index].seq_num != buf->expected) {
// Missing packet, exit flushing and send SREJ
buf->state = BUFFER_STATE_BUFFERING;
return 0; // Send SREJ for this packet
}
// Write packet data to file
ssize_t written = write(buf->fd, buf->entries[index].data, buf->entries[index].data_len);
if (written < 0) {
perror("Failed to write buffered data to file");
return -1;
}
// Mark entry as invalid (free for reuse)
buf->entries[index].valid = 0;
// Move to next expected packet
buf->expected++;
}
// If we get here, all buffered packets have been processed
buf->state = BUFFER_STATE_IN_ORDER;
return 1; // Send RR for next expected packet
}
/**
* Get sequence number to include in RR or SREJ response
*
* @param buf Pointer to Buffer structure
* @return Sequence number to use in response
*/
uint32_t buffer_get_response_seq(Buffer* buf) {
if (!buf) {
fprintf(stderr, "Error: NULL buffer pointer\n");
return 0;
}
// Return the expected sequence number
return buf->expected;
}
/**
* Check if buffer should send SREJ for this sequence number
*
* @param buf Pointer to Buffer structure
* @param seq_num Sequence number to check
* @return 1 if should send SREJ, 0 otherwise
*/
int buffer_should_srej(Buffer* buf, uint32_t seq_num) {
if (!buf) {
fprintf(stderr, "Error: NULL buffer pointer\n");
return 0;
}
// If we're in BUFFERING state and this is the expected sequence number,
// we should send a SREJ for it
if (buf->state == BUFFER_STATE_BUFFERING && seq_num == buf->expected) {
return 1;
}
// If we're in any state and we find a gap in the sequence,
// we should send a SREJ
if (seq_num > buf->expected && seq_num <= buf->highest) {
uint32_t index = seq_num % buf->buffer_size;
if (!buf->entries[index].valid) {
return 1;
}
}
return 0;
}
/**
* Free buffer resources
*
* @param buf Pointer to Buffer structure
*/
void buffer_free(Buffer* buf) {
if (!buf) {
return;
}
if (buf->entries) {
free(buf->entries);
}
// Note: We don't close the file descriptor as it should be managed
// by the caller
free(buf);
}