-
Notifications
You must be signed in to change notification settings - Fork 1
/
torrent_utils.cpp
368 lines (253 loc) · 7.19 KB
/
torrent_utils.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
361
362
363
364
365
366
367
368
#include <iostream>
#include <ctime>
#include <string.h>
#include <openssl/sha.h>
#include <fstream>
#include <sys/stat.h>
#include "mtorrent_file.h"
#include "torrent_utils.h"
#define MAX 524288 //512Bytes
/*#define MAX 10*/
using namespace std;
vector<string> get_tokens(char buffer[1024]) {
vector<string> tokens;
string temp;
char ch;
for(int i =0; i < strlen(buffer); i++) {
ch = buffer[i];
if(ch == ' ' || ch == '\n' || ch == '\0') {
tokens.push_back(temp);
temp = "";
} else {
temp = temp + ch;
}
}
tokens.push_back(temp);
return tokens;
}
vector<string> get_tokens(string buffer) {
vector<string> tokens;
string temp;
char ch;
for(int i =0; i < buffer.size(); i++) {
ch = buffer[i];
if(ch == ' ' || ch == '\n' || ch == '\0') {
tokens.push_back(temp);
temp = "";
} else {
temp = temp + ch;
}
}
tokens.push_back(temp);
return tokens;
}
vector<string> parse_seeder_list(char buffer[1024]) {
vector<string> tokens;
string temp;
char ch;
for(int i =0; i < strlen(buffer); i++) {
ch = buffer[i];
if(ch == '\n' || ch == '\0') {
tokens.push_back(temp);
temp = "";
} else {
temp = temp + ch;
}
}
tokens.push_back(temp);
return tokens;
}
void get_ip_port(string ip_port, string &ip, string &port) {
int i=0;
for(i=0;i<ip_port.size();i++) {
if(ip_port[i] == ':')
break;
}
ip = ip_port.substr(0, i);
port = ip_port.substr(i+1, ip_port.size()-i-1);
}
string get_current_time(){
time_t now = time(0);
string time;
tm *ltm = localtime(&now);
time = time + to_string(ltm->tm_mday) + "/" + to_string(1 + ltm->tm_mon)
+ "/" + to_string(1900 + ltm->tm_year)
+ " | " + to_string(ltm->tm_hour) + ":" + to_string(ltm->tm_min)
+ ":" + to_string(ltm->tm_sec);
return time;
}
mtorrent_file create_mtorrent_object(string mt_file_path) {
ifstream file_read;
file_read.open(mt_file_path);
char buffer[1024];
mtorrent_file mt_file;
string tracker_1_ip, tracker_1_port;
string tracker_2_ip, tracker_2_port;
bzero(buffer, 1024);
file_read.getline(buffer, 1024);
string tracker_url_1 = buffer;
get_ip_port(tracker_url_1, tracker_1_ip, tracker_1_port);
mt_file.tracker_1_ip = tracker_1_ip;
mt_file.tracker_1_port = tracker_1_port;
bzero(buffer, 1024);
file_read.getline(buffer, 1024);
string tracker_url_2 = buffer;
get_ip_port(tracker_url_2, tracker_2_ip, tracker_2_port);
mt_file.tracker_2_ip = tracker_2_ip;
mt_file.tracker_2_port = tracker_2_port;
bzero(buffer, 1024);
file_read.getline(buffer, 1024);
mt_file.file_name = buffer;
bzero(buffer, 1024);
file_read.getline(buffer, 1024);
mt_file.file_size = atoi(buffer);
bzero(buffer, 1024);
file_read.getline(buffer, 1024);
mt_file.hash_string = buffer;
//mt_file.show();
return mt_file;
}
string compute_sha_of_hash_string(string hash_string) {
char input[MAX];
strcpy(input, hash_string.c_str());
char buffer[SHA_DIGEST_LENGTH*2];
unsigned char output[SHA_DIGEST_LENGTH];
memset(buffer, 0x0, SHA_DIGEST_LENGTH*2);
memset(output, 0x0, SHA_DIGEST_LENGTH);
SHA1((unsigned char *) input, strlen(input), output);
for (int i=0; i < SHA_DIGEST_LENGTH; i++) {
sprintf((char*)&(buffer[i*2]), "%02x", output[i]);
}
string sha(buffer, 20);
return sha;
}
string get_sha_from_mtorrent_file(string mt_file) {
char buffer[1024] = {0};
ifstream file_read;
file_read.open(mt_file);
for(int i=0;i<5;i++) {
bzero(buffer, 1024);
cin.getline(buffer, 1024);
}
string sha(buffer);
return sha;
}
string file_name_from_path(string path) {
string file_name;
int i;
for(i = path.length() - 1; i >= 0; i--) {
//cout << path[i] << endl;
if(path[i] == '/') {
break;
}
}
file_name = path.substr(i+1, path.length() - i - 1);
for(i = file_name.length() - 1; i >= 0; i--) {
if(file_name[i] == '.') {
break;
}
}
file_name = file_name.substr(0, i);
return file_name;
}
string get_file_name_from_path(string path) {
string file_name;
int i;
for(i = path.length() - 1; i >= 0; i--) {
//cout << path[i] << endl;
if(path[i] == '/') {
break;
}
}
file_name = path.substr(i+1, path.length() - i - 1);
for(i = file_name.length() - 1; i >= 0; i--) {
if(file_name[i] == '.') {
break;
}
}
file_name = file_name.substr(0, i);
return file_name;
}
mtorrent_file create_mtorrent_file(string original_file, string mt_file_name, mtorrent_file mt_file) {
ifstream file_read;
ofstream file_write;
file_read.open(original_file, ios::binary);
file_write.open("temp.txt", ios::trunc);
file_write.close();
char input[MAX];
char ch;
long int counter = 0;
char buffer[SHA_DIGEST_LENGTH*2];
unsigned char output[SHA_DIGEST_LENGTH];
while(file_read >> ch) {
if(counter == MAX) {
memset(buffer, 0x0, SHA_DIGEST_LENGTH*2);
memset(output, 0x0, SHA_DIGEST_LENGTH);
SHA1((unsigned char *) input, counter, output);
for (int i=0; i < SHA_DIGEST_LENGTH; i++) {
sprintf((char*)&(buffer[i*2]), "%02x", output[i]);
}
file_write.open("temp.txt", ios::app);
file_write.write(buffer, 20);
file_write.close();
//cout << endl;
int i;
for (i = 0; i < 20; i++) {
printf("%02x", output[i]);
}
counter = 0;
input[counter] = ch;
counter++;
} else {
input[counter] = ch;
counter++;
}
}
if(counter != 0) {
memset(buffer, 0x0, SHA_DIGEST_LENGTH*2);
memset(output, 0x0, SHA_DIGEST_LENGTH);
if(input[counter-1] == '\n') {
//cout << "Putting zero";
input[counter-1] = '\0';
}
SHA1((unsigned char *) input, counter, output);
for (int i=0; i < SHA_DIGEST_LENGTH; i++) {
sprintf((char*)&(buffer[i*2]), "%02x", output[i]);
}
file_write.open("temp.txt", ios::app);
file_write.write(buffer, 20);
file_write.close();
/*cout << endl;
int i;
for (i = 0; i < 20; i++) {
printf("%02x", output[i]);
}*/
}
file_read.close();
/**********************************************************
Finally read the temp file and create its SHA
***********************************************************/
//cout << endl << "Final :" << endl;
file_read.open("temp.txt");
counter = 0;
memset(input, 0x0, MAX);
while(file_read >> ch) {
input[counter++] = ch;
}
mt_file.file_name = original_file;
struct stat results;
stat(original_file.c_str(), &results);
mt_file.file_size = results.st_size;
mt_file.hash_string = input;
//string file_name = get_file_name_from_path(original_file);
//string m_torrent_file = file_name + ".mtorrent";
file_write.open(mt_file_name);
file_write << mt_file.tracker_1_ip << ":" << mt_file.tracker_1_port << endl;
file_write << mt_file.tracker_2_ip << ":" << mt_file.tracker_2_port << endl;
file_write << mt_file.file_name << endl;
file_write << mt_file.file_size << endl;
file_write.write(buffer, 20);
file_write.close();
file_read.close();
return mt_file;
}