-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.c
360 lines (302 loc) · 10.2 KB
/
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
#define _GNU_SOURCE // asks stdio.h to include asprintf
/* Load in the correct networking libraries for the OS */
#include "networking.h"
#include <stdio.h>
#include <stdlib.h> //memcpy
#include <string.h>
#include <inttypes.h>
#include <time.h> //time
#include <unistd.h> //getopt
#include <getopt.h> //getopt_long
#include <sys/stat.h>
/* Windows doesn't seem to provide asprintf.h... */
#ifdef _WIN32
# include "asprintf.h"
#endif
#include "errors.h"
#include "fountain.h"
#include "dbg.h"
#include "fountainprotocol.h" // msg definitions
#include "mapping.h" // map_file unmap_file
#define LISTEN_PORT 2534
#define LISTEN_IP "0.0.0.0"
#define BUF_LEN 512
#define BURST_SIZE 1000
// ------ types ------
typedef struct client_s {
struct sockaddr_in address;
} client_s;
// ------ Forward declarations ------
static int create_connection(const char* ip_address);
static int receive_request(client_s * new_client, const char * filename, const char* mapping, size_t len);
static void close_connection();
static int send_fountain(client_s * client, fountain_s* ftn);
static int send_block_burst(client_s * client, const char * mapping, size_t len, wait_signal_s* signal);
static int send_info(client_s * client, const char * filename);
static int filesize_in_bytes(const char * filename);
// TODO: test use of long options on windows
struct option long_options[] = {
{ "blocksize", required_argument, NULL, 'b' },
{ "help", no_argument, NULL, 'h' },
{ "ip", required_argument, NULL, 'i' },
{ "latency", required_argument, NULL, 'L' },
{ "port", required_argument, NULL, 'p' },
{ "sectionsize",required_argument, NULL, 's' },
{ 0, 0, 0, 0 }
};
// ------ static variables ------
static SOCKET s;
#ifdef _WIN32
static WSADATA w;
#endif /* _WIN32 */
static int listen_port = LISTEN_PORT;
static char* listen_ip = LISTEN_IP;
static char const * program_name;
static int blk_size = -1; /* better to set this based on filesize */
static int section_size = 20;
static int dbg_add_response_latency = 0;
// ------ functions ------
static void print_usage_and_exit(int status) {
FILE* out = (status == 0) ? stdout : stderr;
fprintf(out, "Usage: %s [OPTION]... FILE\n", program_name);
fputs("\
\n\
-b, --blocksize=BYTES manually set the blocksize in bytes\n\
-h, --help display this help message\n\
-i, --ip=IPADDRESS set the ip address to listen on, the default is \n\
0.0.0.0\n\
-L, --latency=LATENCY debug setting: adds response latency to the server\n\
-p, --port=PORT set the UDP port to listen on, default is 2534\n\
-s, --sectionsize=BLOCKS the number of sections of blocks the file is\n\
sub-divided into\n\
", out);
exit(status);
}
int main(int argc, char** argv) {
/* deal with options */
program_name = argv[0];
int c;
while ( (c = getopt_long(argc, argv, "b:hi:L:p:s:", long_options, NULL)) != -1) {
switch (c) {
case 'b':
blk_size = atoi(optarg);
break;
case 'h':
print_usage_and_exit(0);
break;
case 'i':
listen_ip = optarg;
break;
case 'L':
dbg_add_response_latency = atoi(optarg);
break;
case 'p':
listen_port = atoi(optarg);
break;
case 's':
section_size = atoi(optarg);
break;
case '?':
print_usage_and_exit(1);
break;
default: // Shouldn't happen
fprintf(stderr, "bad option: %c\n", optopt);
print_usage_and_exit(1);
break;
}
}
char const * filename = NULL;
if (optind < argc) {
filename = argv[optind];
} else {
print_usage_and_exit(1);
}
/* seed random number generation */
srand(time(NULL));
// Check that the file exists
FILE* f = fopen(filename, "rb");
if (f == NULL) handle_error(ERR_FOPEN, &filename);
fclose(f);
int filesize = filesize_in_bytes(filename);
if (blk_size <= 0) {
blk_size = 128; // Should probably start at 1024
if (filesize < 0)
return -1;
while (filesize / blk_size > INT16_MAX) {
blk_size <<= 1;
}
odebug("%d", blk_size);
} else if (filesize / blk_size > INT16_MAX) {
/* The user provided a blk_size... better check they haven't done *
* a silly */
log_err("Block size is too small. Cannot divide file "
"into %d pieces or more", INT16_MAX);
return -1;
} else if (blk_size > MAX_BLOCK_SIZE) {
log_err("Maximum block size is %d", MAX_BLOCK_SIZE);
return -1;
}
int error;
if ((error = create_connection(listen_ip)) < 0) {
log_err("Unable to bind to socket");
close_connection();
return -1;
}
printf("Listening on %s:%d ...\n" ,listen_ip, listen_port);
char* mapping = map_file_read(filename);
if (!mapping) {
log_err("Error mapping file: %s", filename);
return -1;
}
client_s client;
int request_type;
while ((request_type = receive_request(&client, filename, mapping, filesize)) >= 0) {
//
}
unmap_file(mapping);
close_connection();
return 0;
}
int create_connection(const char* ip_address) {
struct sockaddr_in addr;
#ifdef _WIN32
if (WSAStartup(0x0202, &w))
return -10;
if (w.wVersion != 0x0202)
return -20;
#endif
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == INVALID_SOCKET)
return -30;
memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(listen_port);
addr.sin_addr.s_addr = inet_addr(ip_address);
if (bind(s, (struct sockaddr*)&addr, sizeof addr) < 0)
return -40;
return 0;
}
void close_connection() {
if (s)
closesocket(s);
#ifdef _WIN32
WSACleanup();
#endif
}
static void wait_signal_order_from_network(wait_signal_s* wait_signal) {
fp_from(wait_signal->magic);
fp_from(wait_signal->num_sections);
int n = wait_signal->num_sections;
for (int i = 0; i < n; i++) {
fp_from(wait_signal->sections[i].section);
fp_from(wait_signal->sections[i].capacity);
}
}
//
// Translate the message sent to us
int receive_request(client_s * new_client, const char * filename, const char* mapping, size_t len) {
char buf[BUF_LEN];
struct sockaddr_in remote_addr;
socklen_t remote_addr_size = sizeof remote_addr;
memset(buf, '\0', BUF_LEN);
if (recvfrom(s, buf, BUF_LEN, 0, (struct sockaddr*)&remote_addr,
&remote_addr_size) < 0)
return -1;
new_client->address = remote_addr;
debug("Received msg: %s", buf);
#ifndef NDEBUG
// This doesn't actually simulate latency
// Really we need a receive buffer
if (dbg_add_response_latency) {
debug("Sleeping for %d seconds...", dbg_add_response_latency);
sleep(dbg_add_response_latency);
}
#endif
// Lookup the message in the table
packet_s* packet = (packet_s*)buf;
int magic = ntohl(packet->magic);
int error;
switch (magic) {
case MAGIC_REQUEST_INFO:
error = send_info(new_client, filename);
break;
case MAGIC_WAITING:
{
wait_signal_s* signal = (wait_signal_s*)buf;
wait_signal_order_from_network(signal);
error = send_block_burst(new_client, mapping, len, signal);
}
break;
default:
return -1;
}
if (error < 0)
handle_error(error, NULL);
return 0;
}
static void file_info_order_for_network(file_info_s* info) {
fp_to(info->magic);
fp_to(info->section_size);
fp_to(info->blk_size);
fp_to(info->filesize);
}
int filesize_in_bytes(const char * filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
log_err("Error getting filesize");
return -1;
}
int send_info(client_s * client, const char * filename) {
debug("Sending info for file %s", filename);
file_info_s info = {
.magic = MAGIC_INFO,
.section_size = section_size,
.blk_size = blk_size,
.filesize = filesize_in_bytes(filename),
};
strncpy(info.filename, filename, sizeof info.filename - 1);
#ifdef _WIN32
/* Make the path cross platform by replacing \ path separator with / */
char* bs_pos = info.filename;
while ((bs_pos = strchr(bs_pos, '\\')) != NULL) {
*bs_pos++ = '/';
}
#endif
odebug("%"PRId16, info.blk_size);
odebug("%"PRId32, info.filesize);
file_info_order_for_network(&info);
int bytes_sent = sendto(s, (char*)&info, sizeof info, 0,
(struct sockaddr*)&client->address,
sizeof client->address);
if (bytes_sent == SOCKET_ERROR) return ERR_SEND;
return 0;
}
int send_fountain(client_s * client, fountain_s* ftn) {
buffer_s packet = pack_fountain(ftn);
if (packet.length == 0) return ERR_PACKING;
int bytes_sent = sendto(s, packet.buffer, packet.length, 0,
(struct sockaddr*)&client->address,
sizeof client->address);
free(packet.buffer);
if (bytes_sent == SOCKET_ERROR)
return ERR_SEND;
return 0;
}
int send_block_burst(client_s* client, const char* mapping, size_t len, wait_signal_s* signal) {
for (int i = 0; i < signal->num_sections; i++) {
int capacity = signal->sections[i].capacity;
int section = signal->sections[i].section;
for (int j = 0; j < capacity; j++) {
// make a fountain
// send it across the air
fountain_s* ftn = make_fountain(mapping, blk_size, len, section, section_size);
if (ftn == NULL) return ERR_MEM;
int error = send_fountain(client, ftn);
if (error < 0) handle_error(error, NULL);
free_fountain(ftn);
}
log_info("Sent packet burst of size %d for section %d", capacity, section);
}
return 0;
}