/
server.c
415 lines (295 loc) · 9.57 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* server receives request messages and sends control/data messages */
#include "common.h"
void
GetClientAddress(char *buf, char *address)
{
int i;
int begin;
char ret[CADDRESS_LEN + 1];
int j = 0;
begin = MSGTYPE_LEN + FILENAME_LEN + CHUNK_LEN;
for(i = begin; i < begin + CADDRESS_LEN; i++)
{
if(buf[i] != ' ') ret[j++] = buf[i];
}
ret[j] = '\0';
strcpy(address, ret);
}
void
GetClientPort(char *buf, unsigned short *port)
{
char ret[CPORT_LEN + 1];
int begin;
int i;
int j = 0;
begin = MSGTYPE_LEN + FILENAME_LEN + CHUNK_LEN + CADDRESS_LEN;
for(i = begin; i < begin + CPORT_LEN; i++)
{
if(buf[i] != ' ') ret[j++] = buf[i];
}
ret[j] = '\0';
*port = (unsigned short)atoi(ret); /* NEEDED? */
}
void
GetRequestedFileName(char *buf, char *filename)
{
int i;
int j = 0;
for(i = MSGTYPE_LEN; i < FILENAME_LEN + 1; i++)
{
if(buf[i] != ' ') filename[j++] = buf[i];
}
filename[j] = '\0';
}
int
IsValidFileName(char *filename)
{
if ( (filename[0] == '/') || (filename[0] == '.') ) return 0;
else return 1;
}
void
GetRequestedChunk(char *buf, int *chunk)
{
int i;
int j = 0;
char requested_chunk[CHUNK_LEN + 1];
for(i = FILENAME_LEN + 1; i < FILENAME_LEN + CHUNK_LEN + 2; i++)
{
if(buf[i] != ' ') requested_chunk[j++] = buf[i];
}
requested_chunk[j] = '\0';
*chunk = atoi(requested_chunk);
}
void
PrintRequestMessage(char *msg)
{
char fname[FILENAME_LEN + 1];
int chunk_num;
char client_address[CADDRESS_LEN + 1];
unsigned short client_port;
GetRequestedFileName(msg, fname);
GetRequestedChunk(msg, &chunk_num);
GetClientAddress(msg, client_address);
GetClientPort(msg, &client_port);
fprintf(stderr, "[Request Message - %d]\n", strlen(msg));
fprintf(stderr, "\t File Name : %s\n", fname);
fprintf(stderr, "\t Chunk Number : %d\n", chunk_num);
fprintf(stderr, "\tClient Address : %s\n", client_address);
fprintf(stderr, "\t Client Port : %d\n", client_port);
return;
}
void
GetResponseMessage(char *inmsg, char *outmsg)
{
char filename[FILENAME_LEN + 1]; /* requested file name */
int requested_chunk; /* requested chunk number */
char client_address[CADDRESS_LEN + 1]; /* address of client */
unsigned short client_port; /* port of client */
int total_chunks; /* total # of chunks in file */
char temp[CHUNK_LEN + 1]; /* requested chunk - string */
int i; /* loops et al */
char data[DATA_LEN + 1]; /* file chunk */
int status; /* function returns */
int datsize; /* size of data chunk */
/* extract info from request message */
GetRequestedFileName(inmsg, filename);
GetRequestedChunk(inmsg, &requested_chunk);
GetClientAddress(inmsg, client_address);
GetClientPort(inmsg, &client_port);
/* get total number of chunks in file */
total_chunks = GetTotalChunks(filename);
/* check for errors - construct CTL messages */
if(!IsValidFileName(filename)) strcpy(outmsg, "CERRORUNAUTHORIZED REQUEST");
else if(inmsg[0] != 'R') strcpy(outmsg, "CERRORBAD REQUEST FORMAT");
else if(requested_chunk < 0) strcpy(outmsg, "CERRORBAD CHUNK REQUEST");
else if(requested_chunk == 0) /* number of chunks requested */
{
if(total_chunks == -1) strcpy(outmsg, "CERRORCANNOT STAT FILE");
else if(total_chunks == 0) strcpy(outmsg, "CERRORFILE SIZE NULL");
else if(total_chunks > 99999) strcpy(outmsg, "CERRORTOO MANY CHUNKS");
else
{
sprintf(temp, "%d", total_chunks);
strcpy(outmsg, "CCHNKS");
strcat(outmsg, temp);
}
}
else /* construct data message */
{
status = GetFileChunk(filename, requested_chunk, data, &datsize);
if(status != 0)
{
if(status == -1) strcpy(outmsg, "CERRORFILE SIZE NULL");
else if (status == -2) strcpy(outmsg, "CERRORCANNOT STAT FILE");
else if(status == -3) strcpy(outmsg, "CERRORCHUNK OUT OF BOUND");
else strcpy(outmsg, "CERRORUNKNOWN ERROR");
}
else
{
/* MSGTYPE */
strcpy(outmsg, "D");
/* CHUNK */
sprintf(temp, "%d", requested_chunk);
for(i = 0; i < CHUNK_LEN - strlen(temp); i++) strcat(outmsg, " ");
strcat(outmsg, temp);
/* data */
strcat(outmsg, data);
outmsg[datsize + MSGTYPE_LEN + CHUNK_LEN] = '\0';
}
}
}
/* returns 0 if successful */
/* returns -1 if file size null */
/* returns -2 if stat failed */
/* returns -3 if chunk out of bounds */
int
GetFileChunk(char *filename, int req_chunk, char *buf, int *size)
{
FILE *fp;
int total_chunks;
int i;
long long skipsize;
char *skipdata;
total_chunks = GetTotalChunks(filename);
if(total_chunks == 0) return -1;
if(total_chunks == -1) return -2;
if(req_chunk > total_chunks) return -3;
fp = fopen(filename, "rb");
if(fp == NULL) return -2;
/*
for(i = 0; i < req_chunk; i++)
{
//*size = fread(buf, 1, DATA_LEN, fp);
*size = fread(buf, DATA_LEN, 1, fp);
}
*/
skipsize = DATA_LEN * (req_chunk - 1);
fseek(fp, skipsize, SEEK_SET);
//i = fread(skipdata, skipsize, 1, fp); printf("i = %d\n", i);
*size = fread(buf, 1, DATA_LEN, fp);
fclose(fp);
return 0;
}
/* returns 0 if file size null */
/* returns -1 if stat failed */
int
GetTotalChunks(char *filename)
{
struct stat statbuf;
int statret;
statret = stat(filename, &statbuf);
if(statret == 0)
{
if(statbuf.st_size != 0) return statbuf.st_size / DATA_LEN + 1;
else return 0;
}
else return -1;
}
int
CreateRecvSocket()
{
int recv_sock;
int length;
struct sockaddr_in recv_name;
struct timeval tv;
unsigned short port;
port = SERVER_RECV_PORT;
/* create a socket to read from */
recv_sock = socket(AF_INET, SOCK_DGRAM, 0);
if(recv_sock < 0)
{
perror("Opening receiving socket");
exit(1);
}
/* create name with wildcards */
recv_name.sin_family = AF_INET;
recv_name.sin_addr.s_addr = INADDR_ANY;
recv_name.sin_port = port;
if(bind(recv_sock, (struct sockaddr *)&recv_name, sizeof(recv_name)))
{
perror("Binding receiving socket");
exit(1);
}
/* acquire and print port number */
length = sizeof(recv_name);
if(getsockname(recv_sock, (struct sockaddr *)&recv_name, &length))
{
perror("Getting receiving socket name");
exit(1);
}
fprintf(stderr, "Listening on port : %d\n", ntohs(recv_name.sin_port));
return recv_sock;
}
int CreateSendSocket()
{
int send_sock;
send_sock = socket(AF_INET, SOCK_DGRAM, 0);
if(send_sock < 0)
{
perror("Opening sending socket");
exit(1);
}
return send_sock;
}
/* ************************************************************************** */
int main(int argc, char *argv[])
{
/* messages */
char recvd_msg[RMSGSIZE];
char *response_msg;
/* sockets */
int recv_sock;
int send_sock;
/* send related */
struct sockaddr_in send_name;
struct hostent *hp, *gethostbyname();
/* client related */
char client_address[CADDRESS_LEN + 1];
unsigned short client_port;
/* create socket to listen on - no timeout */
recv_sock = CreateRecvSocket();
/* create socket to send on */
send_sock = CreateSendSocket();
/* ** start server ** */
while(1)
{
/* read from socket */
if(read(recv_sock, recvd_msg, RMSGSIZE+1) < 0)
{
perror("Reading from socket");
}
else
{
GetClientAddress(recvd_msg, client_address);
GetClientPort(recvd_msg, &client_port);
/* construct socket to send to */
hp = gethostbyname(client_address);
if(hp == 0)
{
fprintf(stderr, "Unknown host : %s\n", client_address);
continue;
}
bcopy(hp->h_addr, &send_name.sin_addr, hp->h_length);
send_name.sin_family = AF_INET;
send_name.sin_port = htons(client_port);
/* send reponse */
GetResponseMessage(recvd_msg, response_msg);
if(response_msg[0] == 'C')
{
if(sendto(send_sock, response_msg, CMSGSIZE, 0, (struct sockaddr *)&send_name, sizeof(send_name)) < 0)
{
perror("Sending message");
}
}
if(response_msg[0] == 'D')
{
if(sendto(send_sock, response_msg, DMSGSIZE, 0, (struct sockaddr *)&send_name, sizeof(send_name)) < 0)
{
perror("Sending message");
}
}
}
}
return 0;
}
/* ************************************************************************** */