-
Notifications
You must be signed in to change notification settings - Fork 0
/
3600send.c
277 lines (229 loc) · 7.52 KB
/
3600send.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
/*
* CS3600, Spring 2013
* Project 4 Starter Code
* (c) 2013 Alan Mislove
*
*/
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "3600sendrecv.h"
static int DATA_SIZE = 1460;
static int WINDOW_SIZE = 20;
unsigned int last_ackd = 0;
unsigned int sequence = 0;
void usage() {
printf("Usage: 3600send host:port\n");
exit(1);
}
/**
* Reads the next block of data from stdin
*/
int get_next_data(char *data, int size) {
return read(0, data, size);
}
/**
* Builds and returns the next packet info, or NULL
* if no more data is available.
* max on data_len is 254, size of header is 8
*/
packet_info *get_next_packet_info(int sequence) {
char *data = malloc(DATA_SIZE);
int data_len = get_next_data(data, DATA_SIZE);
if (data_len == 0) {
free(data);
return NULL;
}
header *myheader = make_header(sequence, data_len, 0, 0);
void *packet = malloc(sizeof(header) + data_len);
memcpy(packet, myheader, sizeof(header));
memcpy(((char *) packet) +sizeof(header), data, data_len);
free(data);
free(myheader);
packet_info *info = (packet_info *) malloc(sizeof(packet_info));
info->packet = packet;
info->retrieved = time(0);
info->data_len = data_len;
info->sequence = sequence;
return info;
}
// They don't think it be like it is, but it do.
int send_packet_info(int sock, struct sockaddr_in out, packet_info *info) {
if (info->packet == NULL)
return 0;
mylog("[send data] sequence: %d, data_len: %d\n", info->sequence, info->data_len);
if(sendto(sock, info->packet, info->data_len + sizeof(header),
0, (struct sockaddr *) &out, sizeof(out)) < 0) {
mylog("error in sendto?\n");
perror("sendto");
//exit(1);
}
return 1;
}
void send_final_packet(int sock, struct sockaddr_in out) {
header *myheader = make_header(sequence+1, 0, 1, 0);
mylog("[send eof]\n");
if (sendto(sock, myheader, sizeof(header), 0, (struct sockaddr *) &out, (socklen_t) sizeof(out)) < 0) {
mylog("final packet errored out!\n");
perror("sendto");
exit(1);
}
}
int main(int argc, char *argv[]) {
/**
* I've included some basic code for opening a UDP socket in C,
* binding to a empheral port, printing out the port number.
*
* I've also included a very simple transport protocol that simply
* acknowledges every received packet. It has a header, but does
* not do any error handling (i.e., it does not have sequence
* numbers, timeouts, retries, a "window"). You will
* need to fill in many of the details, but this should be enough to
* get you started.
*/
// extract the host IP and port
if ((argc != 2) || (strstr(argv[1], ":") == NULL)) {
usage();
}
char *tmp = (char *) malloc(strlen(argv[1])+1);
strcpy(tmp, argv[1]);
char *ip_s = strtok(tmp, ":");
char *port_s = strtok(NULL, ":");
// first, open a UDP socket
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// next, construct the local port
struct sockaddr_in out;
out.sin_family = AF_INET;
out.sin_port = htons(atoi(port_s));
out.sin_addr.s_addr = inet_addr(ip_s);
// socket for received packets
struct sockaddr_in in;
socklen_t in_len = sizeof(in);
// construct the socket set
fd_set socks;
//*REPURPOSED! READ!!! *********************************************
/**
*Since our protocol in its current state checks the sockets for acks
*every loop, we want this timeout to be very small to avoid blocking
* for too long. You want to create a new timeout using the time in
* a packet's packet_info for proper timeouts.
* See packet_info in 3600sendrecv.h
*/
struct timeval t;
t.tv_sec = 0;
t.tv_usec = 5000;
packet_info packet_buffer[WINDOW_SIZE];
// Oldest packet in buffer that has been sent but has not been ackd
int packet_unackd = 0;
// next free spot in buffer
int packet_next = 0;
// Note:
// To send packet: send_packet(sock, out, packet_info)
// Make sure to end with send_final_packet(sock, out)
packet_info *info;
/**
* main program loop. Do the following steps:
* -check if buffer has empty spots
* -fill one empty buffer spot & send off that packet
* -check if any acks came in
* -remove the ack'd packet
*/
while(1) {
/**
* if buffer isn't full, get the next packet and add it to the buffer
* then immediately send it off
*/
if(packet_next - packet_unackd < WINDOW_SIZE) {
// Are there any new packets to send? if so, store and send exactly one.
if((info = get_next_packet_info(sequence)) != NULL) {
int packet_next_index = packet_next % WINDOW_SIZE;
packet_buffer[packet_next_index] = *info;
send_packet_info(sock, out, info);
packet_next++;
sequence += info->data_len;
} else {
// if there are no more packets and we have no packets in buffer, break from loop
if(packet_next == packet_unackd)
break;
}
}
// Check for acks
FD_ZERO(&socks);
FD_SET(sock, &socks);
// wait to receive, or for a timeout
if (select(sock + 1, &socks, NULL, NULL, &t)) {
unsigned char buf[10000];
int buf_len = sizeof(buf);
int received;
if ((received = recvfrom(sock, &buf, buf_len, 0, (struct sockaddr *) &in, (socklen_t *) &in_len)) < 0) {
perror("recvfrom");
exit(1);
}
// mylog("\nRECEIVED PACKET: ");
header *myheader = get_header(buf);
// mylog("sequence: %d, ack: %d, magic: %d\n", myheader->sequence, myheader->ack, myheader->magic);
// mylog("packet_unackd: %d, looking for - sequence: %d, magic: %d\n", packet_unackd,
// packet_buffer[packet_unackd % WINDOW_SIZE].sequence,
// get_header(packet_buffer[packet_unackd % WINDOW_SIZE].packet)->magic);
// sleep(1);
if ((myheader->magic == MAGIC) && (myheader->ack == 1)) {
packet_info unackd = packet_buffer[packet_unackd % WINDOW_SIZE];
if(myheader->sequence == unackd.sequence + unackd.data_len) {
mylog("[recv ack] %d\n", myheader->sequence);
packet_unackd++;
} else {
mylog("[recv sequence mismatch]\n");
}
} else {
mylog("[recv corrupted ack] %x %d\n", MAGIC, sequence);
}
}
// sleep(1);
}
// Eventually we'll want to make sure that the final packet was ack'd before exiting.
send_final_packet(sock, out);
//dump_packet(packet_buffer[0].packet, packet_buffer[0].data_len + sizeof(header));
//dump_packet(packet, packet_len);
/* while (send_next_packet(sock, out)) {
int done = 0; // fuck timeouts
while (!done) {
FD_ZERO(&socks);
FD_SET(sock, &socks);
// wait to receive, or for a timeout
if (select(sock + 1, &socks, NULL, NULL, &t)) {
unsigned char buf[10000];
int buf_len = sizeof(buf);
int received;
if ((received = recvfrom(sock, &buf, buf_len, 0, (struct sockaddr *) &in, (socklen_t *) &in_len)) < 0) {
perror("recvfrom");
exit(1);
}
header *myheader = get_header(buf);
if ((myheader->magic == MAGIC) && (myheader->sequence >= sequence) && (myheader->ack == 1)) {
mylog("[recv ack] %d\n", myheader->sequence);
sequence = myheader->sequence;
done = 1;
} else {
mylog("[recv corrupted ack] %x %d\n", MAGIC, sequence);
}
} else {
mylog("[error] timeout occurred\n");
}
}
}
*/
// send_final_packet(sock, out);
mylog("[completed]\n");
return 0;
}