-
Notifications
You must be signed in to change notification settings - Fork 3
/
receiver.c
190 lines (173 loc) · 4.52 KB
/
receiver.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
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include "receiver.h"
#include "list.h"
#include "cache.h"
#include "p2p.h"
extern int sock;
void ReceiverInit(int port)
{
struct sockaddr_in host;
if ((sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) {
fprintf(stderr, "Error Creating Socket\n");
exit(1);
}
memset((char *) &host, 0, sizeof(host));
host.sin_family = AF_INET;
host.sin_port = htons(port);
host.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *)&host, sizeof(host))==-1) {
fprintf(stderr, "Error Binding Socket\n");
exit(1);
}
}
void *Receiver(void *args)
{
int i;
unsigned int len = sizeof(struct sockaddr_in);
unsigned char buf[BUFLEN], *packet;
struct sockaddr_in host, dest;
struct Message *message;
pthread_t *thread = malloc(sizeof(pthread_t));
// Receive Messages and spawn worker threads.
while (1) {
if (recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *)&dest, &len)==-1)
exit(1);
packet = malloc(BUFLEN);
message = malloc(sizeof(struct Message));
memcpy(packet, buf, BUFLEN);
memcpy(&(message->address), &dest, len);
message->packet = packet;
pthread_create(thread, NULL, ParseMessages, (void *)message);
}
close(sock);
return;
}
void * ParseMessages(void *args)
{
unsigned char cont, code;
struct Message *message = (struct Message *)args;
unsigned char *packet = message->packet;
cont = (unsigned char)packet[0];
code = (unsigned char)packet[1];
switch(cont) {
case CONTROL:
switch (code) {
case LISTING:
ParseListing(message);
break;
case REQUEST:
ParseRequest(message);
break;
case TRY:
ParseTry(message);
break;
}
break;
case DATA:
ParseData(message);
break;
}
free(message->packet);
free(message);
}
void ParseListing(struct Message *message)
{
int i, j, k;
short count;
char buf[20], *ptr;
struct ListingMessage *list = (struct ListingMessage *)message->packet;
count = ntohs(list->count);
ptr = (char *) message->packet + 4; // Point to the files in the packet
j = 0;
for (i = 0; i < count; i++) { // Iterate through the files
k = 0;
while(ptr[j] != '\0') { // By copying each bit
buf[k] = ptr[j];
j++; k++;
}
buf[k] = '\0'; j++; // Skip over the NULL bit
DirectoryAdd(buf, message->address);
}
}
void ParseRequest(struct Message *message)
{
char buf[20], datagram[1200], *ptr;
unsigned short len, buflen, size, data = 0;
struct sockaddr_in list[20];
FILE *file = NULL;
ptr = message->packet + 2;
strcpy(buf, ptr);
// If we have the file send it.
if ((ListSearch(buf) == 1)) {
SendFile(buf, message->address);
return;
}
// if in cache
else if (CacheGetFile(buf, datagram, &size) == OK) {
SendBuffer(buf, datagram, size, message->address);
return;
}
// We do not have it but we know a place to get it
else {
size = DirectoryGetListBySearch(buf, list);
if (size > 0) {
SendTry(buf, size, list, message->address);
SendRequest(buf);
}
}
}
void ParseData(struct Message *message)
{
char buf[20], *packet;
unsigned short len, buflen, ptr;
FILE *file = NULL;
packet = (char *)message->packet;
strcpy(buf, packet + 1); ptr++;
buflen = strlen(buf) + 1; ptr += buflen;
len = packet[ptr] << 8 | (unsigned char)packet[ptr + 1]; ptr += 2;
if (RdsRemove(buf) != OK) {
return;
}
CacheAdd(buf, packet + ptr, len);
file = fopen(buf, "w");
if (file == NULL) {
printf("Could not open file %s\n", buf);
return;
}
fwrite(packet + ptr, sizeof(char), len, file);
fclose(file);
printf("Saved file %s\n", buf);
}
void ParseTry(struct Message *message)
{
int i;
char buf[20], *packet, ptr;
unsigned short len, count, port;
struct sockaddr_in peer;
struct sockaddr_in addresses[20];
packet = (char *)message->packet;
strcpy(buf, packet + 2); ptr += 2;
len = strlen(buf) + 1; ptr += len;
count = ntohs( packet[ptr] << 8 | (unsigned char)packet[ptr+1]);
ptr +=2;
peer.sin_family = AF_INET;
for (i = 0; i < count; i++) {
memcpy(&peer.sin_addr, packet + ptr, 4); ptr += 4;
port = packet[ptr] << 8 | (unsigned char)packet[ptr+1];
peer.sin_port = htons(port);
ptr += 2;
addresses[i] = peer;
DirectoryAdd(buf, peer);
}
if (RdsSearch(buf) == OK)
SendRequestTo(buf, count, addresses);
}