-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
360 lines (302 loc) · 8.19 KB
/
main.cpp
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
#include <stdlib.h>
#include <string.h>
#include <string>
#include <openssl/sha.h>
#include <iostream>
#include <fstream>
#include <cmath>
#include <time.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <iomanip>
#include <vector>
#include <sys/ioctl.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <bits/stdc++.h>
using namespace std;
#define __STDC_WANT_LIB_EXT1__ 1
#define CHUNKSIZE (512*1024)
char *tok;
vector<string> tokens;
vector<string> tracker_tokens;
char inp[100];
char buffer[1024] = {0};
fstream logfile,torrent,gettorrent,getfile;
bool flag = false;
struct winsize w;
string get_time()
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
// Must be static, otherwise won't work
static char _retval[20];
strftime(_retval, sizeof(_retval),"[%H:%M:%S]", timeinfo);
return _retval;
}
void blank()
{
printf("\033c" );
}
void command_prompt()
{
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
printf("\033[1;1H");
printf("%c[2K", 27);
printf("\033[30;");
printf("47m");
printf("kaushik@hitesh:~$ ");
printf("\033[0m");
printf(" ");
fflush(stdin);
}
void create_mtorrent(string argument)
{
torrent<<argument<<"\n";
}
string gethash()
{
gettorrent.open(tokens[1].c_str(),ios::in);
string get;
if(!gettorrent)
{
logfile<<get_time()<<" Error opening mtorrent file for downloading\n";
}
else
{
logfile<<get_time()<<" opening mtorrent file for downloading\n";
for(int i = 0;i < 6;i++)
{
getline(gettorrent,get);
}
return get;
}
}
int create_connection(string ip,int port)
{
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
if(inet_pton(AF_INET,ip.c_str(), &serv_addr.sin_addr)<=0)
{
logfile<<get_time()<<"Invalid IP address\n";
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
logfile<<get_time()<<" Connection attempt to tracker Failed \n";
return -1;
}
return sock;
}
/* Function to get seeders of the file */
int file_get(string torrent)
{
string ip;
int port;
gettorrent.open(torrent.c_str(),ios::in);
string get;
if(!gettorrent)
{
logfile<<get_time()<<" Error opening mtorrent file for downloading\n";
return -1;
}
else
{
logfile<<get_time()<<" Opening mtorrent file for downloading\n";
for(int i = 0;i < 2;i++) // skipping client ip
{
getline(gettorrent,get);
}
int pos = get.find(':');
ip = get.substr(0,pos);
port = stoi(get.substr(pos+1));
gettorrent.close();
return create_connection(ip,port);
}
}
string file_read(const char *filename)
{
char hashchar[3] = "\0";
unsigned char hash[20];
unsigned char chunk[CHUNKSIZE];
fstream input;
size_t current_read;
string temp;
input.open(filename,ios::binary | ios::in | ios::ate);
if(!input)
{
logfile<<get_time()<<" error opening input file\n";
logfile<<"\n";
}
else
{
double size = input.tellg();
string str = to_string(size);
create_mtorrent(str);
long int num = ceil(size/CHUNKSIZE);
input.seekg(0,ios::beg);
while(num--)
{
input.read((char *)chunk, CHUNKSIZE);
current_read = input.gcount();
SHA1(chunk,current_read,hash);
for(int i = 0;i<10;++i)
{
snprintf(hashchar,sizeof(hashchar),"%02x",hash[i]);
temp += hashchar;
}
}
SHA1((unsigned char *)temp.c_str(),strlen(temp.c_str()),hash);
temp = "";
for(int i = 0;i<10;++i)
{
snprintf(hashchar,sizeof(hashchar),"%02x",hash[i]);
temp += hashchar;
}
}
//cout<<"\n\n<------newhash---->"<<temp<<"\n\n";
return temp;
}
string makepath(char *path)
{
char *tok;
vector<string>split;
tok = strtok(path, " ");
while(tok != NULL)
{
split.push_back(tok);
tok = strtok(NULL,"/");
}
}
void download(string ip,int port,string hash,int index)
{
int sock = create_connection(ip,port);
if(sock < 0)
logfile<<get_time()<< "Thread failed to create socket\n";
else
{
string _send = "id$"+hash;
char buff[24];
strcpy(buff,_send.c_str());
send(sock,buff,strlen(buff),0);
logfile<<get_time()<<" Request for chunk IDs sent to PEER: "<<index;
}
}
int main(int argc,char *argv[])
{
fflush(stdin);
string client(argv[1]);
string tracker1(argv[2]);
string tracker2(argv[3]);
string delim = ":";
vector<string>ip;
string hash;
char hello[1024];
logfile.open(argv[4],ios::out | ios::app);
if(!logfile)
cout<<"error creating log file\n";
else
{
logfile<<get_time()<<" Log file creation successful\n";
blank();
command_prompt();
fflush(stdin);
cin.getline(inp,sizeof(inp));
tok = strtok(inp, " ");
tokens.clear();
while(tok != NULL)
{
tokens.push_back(tok);
tok = strtok(NULL," ");
}
if(tokens[0] == "share")
{
torrent.open(tokens[2].c_str(),ios::out | ios::out);
if(!torrent)
{
logfile<<get_time()<<" Torrent file creation failed.\n";
}
else
{
logfile<<get_time()<<" Client trying to initiate share.\n";
logfile<<get_time()<<" Torrent file creation successful.\n";
create_mtorrent(client);
create_mtorrent(tracker1);
create_mtorrent(tracker2);
create_mtorrent(tokens[1]);
//string path = makepath(tokens[1].c_str());
hash = file_read((tokens[1]).c_str());
create_mtorrent(hash);
int sock = create_connection(tracker1.substr(0,tracker1.find(':')),stoi(tracker1.substr(tracker1.find(':')+1)));
if(sock < 0)
{
cout<<"Connection attemptfor sharing failed\n";
}
else
{
strcpy(hello,tokens[0].c_str());
strcat(hello,(delim).c_str());
strcat(hello,client.c_str());
strcat(hello,(delim).c_str());
strcat(hello,(hash).c_str());
send(sock , hello , strlen(hello) , 0 );
printf("Share request sent successfully\n");
close(sock);
}
}
}
else if(tokens[0] == "get")
{
char buffer[1024];
int sock = file_get(tokens[1]);
if(sock < 0)
{
logfile<<get_time()<<" Connection attempt for downloading failed \n";
}
else
{
getfile.open(tokens[2].c_str(),ios::out);
string hash = gethash();
strcpy(hello,tokens[0].c_str());
strcat(hello,(delim).c_str());
strcat(hello,hash.c_str());
send(sock , hello , strlen(hello) , 0 );
logfile<<get_time()<<" GET request sent to tracker successfully\n";
int bytesread = read(sock, buffer,sizeof(buffer));
buffer[bytesread] = '\0';
/* separate out the IPs and PORTs recieved from tracker */
tracker_tokens.clear();
tok = strtok(buffer, ":");
while(tok != NULL)
{
tracker_tokens.push_back(tok);
tok = strtok(NULL,":");
}
}
}
else
{
char ch;
cout<<"\nClient Exiting!!! Press any key to continue ";
cin>>ch;
exit(0);
}
}
return 0;
}