-
Notifications
You must be signed in to change notification settings - Fork 3
/
sender.c
179 lines (160 loc) · 4.79 KB
/
sender.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <sys/socket.h>
#include "p2p.h"
#include "rds.h"
#include "list.h"
#include "sender.h"
#include "directory.h"
extern int sock;
/* Every 240 seconds send out a message to all peers
* with the files that we are serving. This is started
* on its own thread.
* End when the program dies.
*/
void *SendListing(void *args)
{
int i, len, ptr = 0;
struct sockaddr_in dest[20];
struct Packet packet;
struct timespec sleep, counter;
struct ListingMessage message = ListGetMessage();
// Setup the packet
packet.count = ListMessageSize();
packet.data = malloc(sizeof(char) * packet.count);
memcpy(packet.data, &(message.cont), 1); ptr++;
memcpy(packet.data + ptr, &(message.code), 1); ptr++;
memcpy(packet.data + ptr, &(message.count), 2); ptr += 2;
memcpy(packet.data + ptr, message.entries, packet.count -4);
// Setup the timers
sleep.tv_sec = 240;
sleep.tv_nsec = 0;
while (1) {
len = DirectoryGetList(dest);
for (i = 0; i < len; i++) {
Send(&dest[i], &packet);
}
nanosleep(&sleep, &counter);
}
free(packet.data);
}
/* Send a UDP Packet to the Destination.
* dest is the destination ip/port.
* packet is the packet you wish to send.
*/
int Send(struct sockaddr_in *dest, struct Packet *packet)
{
socklen_t slen = sizeof(struct sockaddr_in);
if (sendto(sock, packet->data, packet->count, 0,
(struct sockaddr*)dest, slen)==-1) {
return -1;
}
}
void SendRequest(char *request)
{
struct sockaddr_in dest[20];
struct Packet packet;
int len = strlen(request) + 1, ptr = 0, i;
unsigned char cont = 0xCC;
unsigned char code = 0x52;
packet.count = 2 + len;
packet.data = malloc(packet.count);
// Create the packet
memcpy(packet.data, &cont, 1); ptr++;
memcpy(packet.data + ptr, &code, 1); ptr++;
memcpy(packet.data + ptr, request, len);
RdsAdd(request);
// Send to everyone that I know
len = DirectoryGetList(dest);
for (i = 0; i < len; i++) {
Send(&dest[i], &packet);
}
}
void SendRequestTo(char *request, short count, struct sockaddr_in *addresses)
{
struct sockaddr_in dest[20];
struct Packet packet;
int len = strlen(request) + 1, ptr = 0, i;
unsigned char cont = 0xCC;
unsigned char code = 0x52;
packet.count = 2 + len;
packet.data = malloc(packet.count);
// Create the packet
memcpy(packet.data, &cont, 1); ptr++;
memcpy(packet.data + ptr, &code, 1); ptr++;
memcpy(packet.data + ptr, request, len);
for (i = 0; i < count; i++) {
Send(&addresses[i], &packet);
}
}
void SendFile(char *str, struct sockaddr_in address)
{
unsigned char cont = 0xDD;
struct Packet packet;
short buflen, size, ptr = 0;
FILE *file = NULL;
file = fopen(str, "r");
if (file == NULL) {
fprintf(stderr, "Could not open file %s\n", str);
return;
}
fseek (file , 0 , SEEK_END);
size = ftell(file);
rewind(file);
buflen = strlen(str) + 1;
packet.count = buflen + size + 3;
packet.data = malloc(packet.count);
memcpy(packet.data, &cont, 1); ptr++;
memcpy(packet.data + ptr, str, buflen); ptr += buflen;
((char *)packet.data)[ptr] = size >> 8;
((char *)packet.data)[ptr+1] = (size & 0xFF) % 256; ptr += 2;
fread(packet.data + ptr, sizeof(char), size, file);
Send(&address, &packet);
fclose(file);
free(packet.data);
}
void SendBuffer(char *str, char *buffer, short size, struct sockaddr_in address)
{
short buflen, ptr = 0;
struct Packet packet;
unsigned char cont = 0xDD;
buflen = strlen(str) + 1;
packet.count = buflen + size + 3;
packet.data = malloc(packet.count);
memcpy(packet.data, &cont, 1); ptr++;
memcpy(packet.data + ptr, str, buflen); ptr += buflen;
((char *)packet.data)[ptr] = size >> 8;
((char *)packet.data)[ptr+1] = (size & 0xFF) % 256; ptr += 2;
memcpy(packet.data + ptr, buffer, size);
Send(&address, &packet);
free(packet.data);
}
void SendTry(char *file, short count, struct sockaddr_in *addresses, struct sockaddr_in dest)
{
int i;
unsigned char *buf;
unsigned char cont = 0xCC;
unsigned char cond = 0x54;
struct Packet packet;
unsigned short buflen, ptr = 0, port;
buflen = strlen(file) + 1;
packet.count = 4 + buflen + (count * 6); // 4 bytes (cont, cond, count) + filename + each address
packet.data = malloc(packet.count);
memcpy(packet.data, &cont, 1); ptr++;
memcpy(packet.data + ptr, &cond, 1); ptr++;
memcpy(packet.data + ptr, file, buflen); ptr += buflen;
memcpy(packet.data + ptr, &count, 2); ptr += 2;
// Copy addresses
for (i = 0; i < count; i++) {
memcpy(packet.data + ptr, &(addresses[i].sin_addr), 4); ptr += 4;
port = ntohs(addresses[i].sin_port);
((char *)packet.data)[ptr] = port >> 8;
((char *)packet.data)[ptr + 1] = (port & 0xFF) % 256;
ptr += 2;
}
Send(&dest, &packet);
free(packet.data);
}