forked from ali-z-can/SummerSeedProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreciever.c
524 lines (408 loc) · 13.1 KB
/
reciever.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//
// Created by alican on 17.07.2018.
//
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdbool.h>
#include <errno.h>
#include <libgen.h>
#include <time.h>
#include <netpacket/packet.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netinet/if_ether.h>
#include "publisher.h"
#include "packet_headers.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <poll.h>
#include "myether.h"
#include <time.h>
#include <unistd.h>
#define TOPORT 14950
#define MYPORT 1043
#define LINE_SIZE 1024
#define REC_BUFF_SIZE 1500
#define MAX_IFNAMSIZ 32
#define FRAG_SIZE 1500
struct special_hdr{
struct reciever_to_publisher_packet_hdr hdrr;
struct ether_header eth;
}__attribute__((packed));
void my_ip(char *myniccard, char *myipaddr) {
int fd;
struct ifreq ifr;
myipaddr[0] = 0;
fd = socket(AF_INET, SOCK_DGRAM, 0);
/* I want to get an IPv4 IP address */
ifr.ifr_addr.sa_family = AF_INET;
/* I want IP address attached to "eth0" */
//strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
strncpy(ifr.ifr_name, myniccard, IFNAMSIZ - 1);
ioctl(fd, SIOCGIFADDR, &ifr);
close(fd);
/* display result */
sprintf(myipaddr, "%s", inet_ntoa(((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr));
printf("MY IP address:%s: on port: %d\n", myipaddr, MYPORT);
}
int createSocket(struct sockaddr_in *sockaddr) {
int fd;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
printf("Error opening socket\n");
return -1;
}
sockaddr.sin_port = htons(PORT);
sockaddr.sin_addr.s_addr = 0;
sockaddr.sin_addr.s_addr = INADDR_ANY;
sockaddr.sin_family = AF_INET;
if (bind(fd, (struct sockaddr *) &sockaddr, sizeof(struct sockaddr_in)) == -1) {
printf("Error binding socket\n");
return -1;
}
return fd;
}
int closeSocket(/*struct sockaddr_in* sockaddr,*/ int fd) {
close(fd);
}
int requestHash(char *fileName) {
FILE *file = fopen("files/TrustedSourceFile.txt", "r");
char myipaddressm[22];
char *myniccardm = "eth0";
struct sockaddr_in localaddr;
char line[LINE_SIZE];
int sockfd;
struct sockaddr_in their_addr;
struct reciever_to_publisher_packet_hdr hdr1;
struct hostent *he;
assert(file != NULL);
my_ip(myniccardm, myipaddressm);
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Sender: Client-socket() error lol!");
exit(1);
}
localaddr.sin_family = AF_INET;
localaddr.sin_addr.s_addr = inet_addr(myipaddressm);
localaddr.sin_port = htons(MYPORT);
bind(sockfd, (struct sockaddr *) &localaddr, sizeof(localaddr));
while (fgets(line, sizeof(line), file)) {
if ((he = gethostbyname(line)) == NULL) {
perror("Sender: Client-gethostbyname() error!");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(TOPORT);
their_addr.sin_addr = *((struct in_addr *) he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);
hdr1.startingIndex = 0;
hdr1.finishIndex = 0;
hdr1.fragsize = 0;
hdr1.isFinished = '0';
hdr1.data = fileName;
char *toSent = (char *) (hdr1);
if ((numbytes = sendto(sockfd, toSent,
strlen(toSent),
0,
(struct sockaddr *) &their_addr,
sizeof(struct sockaddr))) == -1) {
perror("Sender: Client-sendto() error lol!");
exit(1);
} else
printf("Sender: Client-sendto() is OK...\n");
}
if (close(sockfd) != 0)
printf("Sender: Client-sockfd closing is failed!\n");
else
printf("Sender: Client-sockfd successfully closed!\n");
}
char *recieveHashFromTS(char *fileName, int *size) {
FILE *file = fopen("files/TrustedSourceFile.txt", "r");
struct sockaddr_in recaddr;
int *sfd;
char ifname[MAX_IFNAMSIZ] = {0};
int ret;
char *buffer;
char *arg_ifname;
struct publisher_to_reciever_packet_hdr *hdr1;
int lines;
int ch;
char *toRet;
ch = 0;
lines = 0;
int i;
char *toSize;
if (fp == NULL)
return 0;
while (!feof(fp)) {
ch = fgetc(fp);
if (ch == '\n') {
lines++;
}
}
struct pollfd ufds[lines];
for (i = 0; i < lines; i++) {
sfd[i] = createSocket(recaddr);
ufds[i].fd = sfd[i];
ufds[i].events = POLLIN;
}
int recieved;
recieved = 0;
while (recieved < lines) {
int pp = poll(ufds, lines, POLL_TIMEOUT);
if (pp == -1) {
perror("poll");
continue;
}
for (i = 0; i < lines; i++) {
if (ufds[i].revents & POLLIN) {
ret = recv(sfd, hdr1, REC_BUFF_SIZE, 0);
if (ret <= 0) {
fprintf(stderr, "ERROR: recv failed ret: %d, errno: %d\n", ret, errno);
exit(1);
}
if (hdr1->data[0] == '1') {
int x;
for (x = 0; x < 32; x++) {
toRet[x] = hdr1->data[x + 1];
}
for (x = 0; x < (strlen(hdr1->data)) - 32; x++) {
toSize[x] = hdr1->data[x + 33];
}
size = (int) (toSize);
recieved = lines;
}
}
}
recieved++;
}
if(toRet == NULL)
return "file not found";
return toRet;
}
int broadCastMD5(char* hashFromTS, struct publisher* publisherlist){
int sfd;
char ifname[MAX_IFNAMSIZ] = {0};
int ret;
int size;
char *buffer;
char *arg_data;
char *arg_ifname;
char *myniccardm = "eth0";
char myipaddressm[22];
struct sockaddr_in addrout;
time_t endwait;
time_t start = time(NULL);
time_t seconds = 10;
endwait = start + seconds;
sfd = net_create_raw_socket(ifname, ETHER_TYPE, 0);
if (sfd == -1) {
fprintf(stderr, "failed to init socket\n");
exit(1);
}
struct special_hdr *hdr1;
arg_data = hashFromTS + myipaddressm;
size = strlen(arg_data) + 1 + sizeof(*hdr1);
buffer = malloc(size);
if (!buffer){
fprintf(stderr, "memory allocation error!\n");
exit(1);
}
hdr1 = (struct special_hdr*)buffer;
net_get_iface_mac(ifname, (char *)hdr1->eth.ether_shost);
hdr1->eth.ether_type = htons(ETHER_TYPE);
memcpy(hdr1->eth.ether_dhost, ETH_ADD_BCAST, ETH_ALEN);
hdr1->hdrr.startingIndex = 0;
hdr1->hdrr.finishIndex = 0;
hdr1->hdrr.fragsize = 0;
hdr1->hdrr.isFinished = 'c';
strcpy(hdr1->hdrr.data, arg_data);
endwait = start + seconds;
while(start < endwait){
ret = send(sfd, hdr1, size, 0);
if (ret != size) {
fprintf(stderr, "ERROR: send failed ret: %d, errno: %d\n", ret, errno);
exit(1);
}
fprintf(stderr, "%d bytes sent\n", ret);
start = time(NULL);
}
closeSocket(sfd);
}
int recievePublishers(struct publisher* publisherList,int* numberOfPublishers){
int sfd;
char ifname[MAX_IFNAMSIZ] = {0};
int ret;
struct publisher_to_reciever_packet_hdr *hdr1;
char* buffer;
char *arg_ifname;
struct sockaddr_in inaddr;
numberOfPublishers = 0;
sfd = createSocket(inaddr);
buffer = malloc(REC_BUFF_SIZE);
hdr1 = (struct publisher_to_reciever_packet_hdr*) buffer;
time_t endwait;
time_t start = time(NULL);
time_t seconds = 10;
endwait = start + seconds;
int i;
i = 0;
while(start < endwait) {
ret = recv(sfd, hdr1, REC_BUFF_SIZE, 0);
if(hdr1->packetType =='r') {
publisherList[i] = (struct publisher *) calloc(1, sizeof(struct publisher));
memcpy(publisherList[i]->name, hdr1->data, 23);
createSocket(publisherList[i]->sockaddr_in);
i++;
}
start = time(NULL);
}
numberOfPublishers = i;
}
int partition(int size,int numberOfPublishers, struct publisher* publisherList){
int sizePerPublisher;
sizePerPublisher = size/numberOfPublishers;
int remaining;
if(sizePerPublisher != size){
remaining = size - (sizePerPublisher*numberOfPublishers);
}
int i;
for(i = 0; i < numberOfPublishers; i++){
publisherList[i]->startingIndex = i*sizePerPublisher;
publisherList[i]->finishIndex = (i+1)*sizePerPublisher;
if(publisherList[i]->finishIndex > size/FRAG_SIZE){
publisherList[i]->finishIndex = size/FRAG_SIZE ;
}
}
}
int askForPartitions(int numberOfPublishers,struct publisher* publisherlist){
struct reciever_to_publisher_packet_hdr *hdrr;
int sockfd;
struct sockaddr_in their_addr;
int i;
struct hostent *he;
for(i = 0; i<numberOfPublishers;i++){
hdrr->startingIndex = publisherlist[i]->startingIndex;
hdrr->finishIndex = publisherlist[i]->finishIndex;
hdrr->isFinished = '0';
char *toSent = (char *) (hdrr);
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Sender: Client-socket() error lol!");
exit(1);
}
localaddr.sin_family = AF_INET;
localaddr.sin_addr.s_addr = inet_addr(myipaddressm);
localaddr.sin_port = htons(MYPORT);
bind(sockfd, (struct sockaddr *) &localaddr, sizeof(localaddr));
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(TOPORT);
their_addr.sin_addr = *((struct in_addr *) he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);
if ((numbytes = sendto(sockfd, toSent,
strlen(toSent),
0,
(struct sockaddr *) &their_addr,
sizeof(struct sockaddr))) == -1) {
perror("Sender: Client-sendto() error lol!");
exit(1);
}
if (close(sockfd) != 0)
printf("Sender: Client-sockfd closing is failed!\n");
}
}
int evaluateHash(char* hashFromTS, struct publisher* p){
}
void recieveHashPacket(int numberOfPublishers,struct publisher* publisherlist){
int sfd;
char ifname[MAX_IFNAMSIZ] = {0};
int ret;
struct publisher_to_reciever_packet_hdr *hdr1;
char* buffer;
char *arg_ifname;
struct sockaddr_in inaddr;
numberOfPublishers = 0;
sfd = createSocket(inaddr);
buffer = malloc(REC_BUFF_SIZE);
hdr1 = (struct publisher_to_reciever_packet_hdr*) buffer;
int i,y,z,t;
for(i = 0; i<numberOfPublishers;i++){
ret = recv(sfd, hdr1, REC_BUFF_SIZE, 0);
for(y=0;y<numberOfPublishers;y++){
char checker[22];
for(z=0;z<22;z++){
checker[i] = hdr1->data[i];
}
if(strcmp(checker,publisherlist[i]->name)){
for(z = 0; hdr1->data[0];z++){
for(t=0;t<22;t++){
publisherlist[z]->md5Array[z][t] = hdr1->data[1+z*22+t];
}
}
}
}
}
}
int recievePackets(struct publisher* p, int fragmentCount,char* hashFromTS,char* filename){
int fd;
struct sockaddr_in insock;
fd = createSocket(insock);
struct publisher_to_reciever_packet_hdr hdr;
if (fd == -1) {
fprintf(stderr, "failed to init socket\n");
exit(1);
}
int ret;
char* buffer;
buffer = malloc(RX_BUF_SIZE);
if (!buffer) {
fprintf(stderr, "memory allocation error!\n");
exit(1);
}
hdr = (struct publisher_to_reciever_packet_hdr*)buffer;
char **indexing;
int *arr;
arr = calloc(hdr->fragmentcount,sizeof(int));
indexing = calloc(hdr->fragmentcount,sizeof(char *));
int i;
for(i=0;i<fragmentCount;i++){
ret = recv(fd, hdr, RX_BUF_SIZE, 0);
if (ret <= 0) {
fprintf(stderr, "ERROR: recv failed ret: %d, errno: %d\n", ret, errno);
exit(1);
}
indexing[hdr.frindex - 1] = (char *) calloc(hdr->size,sizeof(char));
memcpy(indexing[hdr.frindex -1],hdr->data,hdr->size);
arr[hdr.frindex-1] =hdr->size;
}
if((i = evaluateHash(hashFromTS, p,indexing)) == 0){
write_to_file2(p,generalArr,indexing,arr,filename,generalSizes,fragmentCount);
}
free(hdr2);
}
int evaluateHash(char* hashFromTS, struct publisher* p){
}
void write_to_file(int *sizes, char **fragments, char *fname, int fragsize){
FILE* outp;
outp = fopen(fname,"w");
for (int i = 0; i < fragsize; ++i) {
fwrite(fragments[i],sizes[i],1,outp);
free(fragments[i]);
}
free(sizes);
free(fragments);
fclose(outp);
}
void write_to_file2(struct publiser* p,char **generalArr,char **indexing,int *sizesOfPart,int *generalSizes,int fragSize){
for(int i = 0; i<fragSize;i++){
memcpy(frags[i+p->startingIndex],indexing[i],sizesOfPart[i]);
memcpy(generalSizes[i+p->startingIndex],sizesOfPart[i], sizeof(int));
}
free(sizesOfPart);
free(indexing);
}