mirror of
https://github.com/Hizenberg469/btClient.git
synced 2026-04-19 17:52:24 +03:00
first commit
This commit is contained in:
57
include/BProtocol.hpp
Normal file
57
include/BProtocol.hpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#ifndef __B_PROTOCOL
|
||||
#define __B_PROTOCOL
|
||||
|
||||
#include "torrent_structure.hpp"
|
||||
#include <Torrent.hpp>
|
||||
|
||||
|
||||
/*************************B_Protocol literals*********************/
|
||||
|
||||
|
||||
|
||||
#define DictionaryStart 'd'
|
||||
#define DictionaryEnd 'e'
|
||||
#define ListStart 'l'
|
||||
#define ListEnd 'e'
|
||||
#define NumberStart 'i'
|
||||
#define NumberEnd 'e'
|
||||
#define ByteArrayDivider ':'
|
||||
|
||||
|
||||
/*************************B_Protocol literals*********************/
|
||||
|
||||
|
||||
/*************************Decode Functions Implementation*************************/
|
||||
|
||||
/*Implemented*/
|
||||
long long _read_file(const std::string& torrentFile, char*& raw_data);
|
||||
|
||||
/*Implemented*/
|
||||
bNode* b_decode(char*& raw_data, long long &size);
|
||||
|
||||
/*Implemented*/
|
||||
long long int integer_decode(char*& raw_data, long long int &size);
|
||||
|
||||
/*Implemented*/
|
||||
std::string string_decode(char*& raw_data, long long int& size);
|
||||
|
||||
|
||||
|
||||
/*************************Decode Functions Implementation*************************/
|
||||
|
||||
|
||||
/*************************Encode Functions Implementation*************************/
|
||||
|
||||
|
||||
/* Always decrease the return size of e_data by 1, after processing*/
|
||||
int b_encode(bNode* tFile, char*& e_data, long long& size);
|
||||
|
||||
|
||||
|
||||
/*************************Encode Functions Implementation*************************/
|
||||
|
||||
|
||||
|
||||
//void TorrentInfoDump(bNode* res, TorrentInfo* &torrentInfo);
|
||||
|
||||
#endif
|
||||
86
include/Network.hpp
Normal file
86
include/Network.hpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#ifndef __NETWORK__
|
||||
|
||||
|
||||
#define __NETWORK__
|
||||
|
||||
//Networking Feature;
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <memory.h>
|
||||
#include <assert.h>
|
||||
#include <Torrent.hpp>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
|
||||
//178.17.4.234:51413
|
||||
//78.159.131.21:42363
|
||||
//136.54.172.182:50413
|
||||
|
||||
//91.103.253.141:1
|
||||
|
||||
//84.17.63.56:44320
|
||||
|
||||
//122.162.144.112:32927
|
||||
|
||||
//122.162.144.112:32927 helloworld.txt.to
|
||||
//182.69.177.39::9001
|
||||
|
||||
#define DEST_PORT 9001
|
||||
#define SERVER_IP_ADDRESS "182.69.183.180"
|
||||
|
||||
|
||||
#define BT_CHOKE 0
|
||||
#define BT_UNCHOKE 1
|
||||
#define BT_INTERSTED 2
|
||||
#define BT_NOT_INTERESTED 3
|
||||
#define BT_HAVE 4
|
||||
#define BT_BITFIELD 5
|
||||
#define BT_REQUEST 6
|
||||
#define BT_PIECE 7
|
||||
#define BT_CANCEL 8
|
||||
|
||||
|
||||
typedef struct {
|
||||
char* bitfield;
|
||||
size_t size;
|
||||
}bt_bitfield_t;
|
||||
|
||||
typedef struct {
|
||||
int index;
|
||||
int begin;
|
||||
int length;
|
||||
}bt_request_t;
|
||||
|
||||
typedef struct {
|
||||
int index;
|
||||
int begin;
|
||||
char piece[0];
|
||||
}bt_piece_t;
|
||||
|
||||
typedef struct bt_msg {
|
||||
int length;
|
||||
unsigned char bt_type;
|
||||
|
||||
union {
|
||||
bt_bitfield_t bitfield;
|
||||
int have;
|
||||
bt_piece_t piece;
|
||||
bt_request_t request;
|
||||
bt_request_t cancel;
|
||||
char data[0];
|
||||
}payload;
|
||||
|
||||
|
||||
}bt_msg_t;
|
||||
|
||||
void start_client_handshake(int& sockfd,char handshake[],Torrent *torrent);
|
||||
char* buildHandShake(Torrent *torrent);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
124
include/Torrent.hpp
Normal file
124
include/Torrent.hpp
Normal file
@@ -0,0 +1,124 @@
|
||||
#ifndef __TORRENT_H
|
||||
#define __TORRENT_H
|
||||
|
||||
//To use POSIX TIMER library
|
||||
#define __USE_POSIX199309 1
|
||||
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <stdlib.h>
|
||||
#include <curl/curl.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <Tracker.hpp>
|
||||
#include <BProtocol.hpp>
|
||||
#include <torrent_structure.hpp>
|
||||
#include <eventpp/eventdispatcher.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define DIR_SEPARATOR '\\'
|
||||
#else
|
||||
#define DIR_SEPARATOR '/'
|
||||
#endif
|
||||
|
||||
class FileItem {
|
||||
public:
|
||||
|
||||
std::string Path;
|
||||
long long int Size;
|
||||
long long int Offset;
|
||||
|
||||
std::string FormattedSize;
|
||||
|
||||
FileItem();
|
||||
void set_FormattedSize();
|
||||
|
||||
};
|
||||
|
||||
class Tracker;
|
||||
class Torrent {
|
||||
public:
|
||||
|
||||
eventpp::EventDispatcher <std::string, void(
|
||||
std::vector<std::string>&)> PeerListUpdated;
|
||||
|
||||
long long int torrentFileSize;
|
||||
|
||||
std::string Name;
|
||||
int IsPrivate;
|
||||
std::vector<FileItem> Files;
|
||||
std::string FileDirectory();
|
||||
std::string DownloadDirectory;
|
||||
|
||||
std::vector<Tracker*> Trackers;
|
||||
std::string Comment;
|
||||
std::string CreatedBy;
|
||||
time_t CreationDate;
|
||||
std::string Encoding;
|
||||
|
||||
int BlockSize;
|
||||
int PieceSize;
|
||||
long long int TotalSize();
|
||||
|
||||
std::string FormattedPieceSize();
|
||||
std::string FormattedTotalSize();
|
||||
|
||||
int PieceCount();
|
||||
|
||||
std::vector<std::vector<unsigned char>> PieceHashes;
|
||||
std::vector<bool> IsPieceVerified;
|
||||
std::vector<std::vector<bool>> IsBlockAcquired;
|
||||
|
||||
std::string VerifiedPiecesString();
|
||||
int VerifiedPieceCount();
|
||||
double VerifiedRatio();
|
||||
bool IsCompleted();
|
||||
bool IsStarted();
|
||||
|
||||
long long int Uploaded;
|
||||
long long int Downloaded();
|
||||
long long int Left();
|
||||
|
||||
|
||||
char Infohash[20];
|
||||
std::string HexStringInfoHash(); //Not Implemented...
|
||||
std::string UrlSafeStringInfoHash();
|
||||
|
||||
//Locking...
|
||||
|
||||
Torrent();
|
||||
Torrent(std::string name, std::string location,
|
||||
std::vector<FileItem> files,
|
||||
std::vector<std::string> trackers,
|
||||
int pieceSize, char* pieceHashes,
|
||||
int blockSize,
|
||||
int isPrivate);
|
||||
|
||||
void UpdateTrackers(TrackerEvent ev,
|
||||
std::string id, int port);
|
||||
void ResetTrackerLastRequest();
|
||||
|
||||
bNode* TorrentToBEncodingObject(Torrent torrent);
|
||||
bDictionary* TorrentInfoToBEncodingObject(Torrent torrent);
|
||||
Torrent* BEncodingObjectToTorrent(bNode* bencoding,
|
||||
std::string name, std::string downloadPath);
|
||||
//Encoding and creationdate left to implement...
|
||||
|
||||
void Verify(int piece);
|
||||
int GetPieceSize(int piece);
|
||||
int GetBlockCount(int piece);
|
||||
std::vector<unsigned char> GetHash(int piece);
|
||||
std::string ReadPiece(int piece);
|
||||
std::string Read(long long int start, int length);
|
||||
|
||||
Torrent* LoadFromFile(std::string filePath, std::string downloadPath);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
54
include/Tracker.hpp
Normal file
54
include/Tracker.hpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#ifndef __TRACKER_H
|
||||
#define __TRACKER_H
|
||||
|
||||
//To use POSIX TIMER library
|
||||
#define __USE_POSIX199309 1
|
||||
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <list>
|
||||
#include <curl/curl.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <Torrent.hpp>
|
||||
#include <stdint.h>
|
||||
#include <eventpp/eventdispatcher.h>
|
||||
|
||||
|
||||
typedef enum {
|
||||
Started,
|
||||
Paused,
|
||||
Stopped
|
||||
} TrackerEvent;
|
||||
|
||||
|
||||
class Torrent;
|
||||
class Tracker {
|
||||
public:
|
||||
|
||||
eventpp::EventDispatcher<std::string,
|
||||
void(std::vector<std::string>&)> PeerListUpdated;
|
||||
|
||||
std::string Address;
|
||||
struct timespec LastPeerRequested;
|
||||
long long int PeerRequestInterval; //In seconds
|
||||
//The Response from tracker...
|
||||
std::string httpWebRequest;
|
||||
|
||||
Tracker(std::string address);
|
||||
|
||||
void Request(char* url);
|
||||
|
||||
void Update(Torrent torrent, TrackerEvent ev, std::string id,
|
||||
int port);
|
||||
|
||||
|
||||
//To Implment response from tracker....
|
||||
|
||||
void Handle_response();
|
||||
void ResetLastRequest();
|
||||
|
||||
std::string returnTracker();
|
||||
};
|
||||
#endif
|
||||
71
include/debug.hpp
Normal file
71
include/debug.hpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#ifndef __DEBUG__
|
||||
#define __DEBUG__
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <torrent_structure.hpp>
|
||||
|
||||
inline void error_msg(std::string msg,const char* function_name) {
|
||||
std::cout << "Error:\n";
|
||||
std::cout << msg << '\n';
|
||||
std::cout << "Error in Function : " <<
|
||||
std::string(function_name) << '\n';
|
||||
}
|
||||
|
||||
|
||||
inline void _dump_parsed_torrent_file_data(bNode* tFile) {
|
||||
|
||||
b_type ty = tFile->type;
|
||||
|
||||
switch (ty) {
|
||||
case B_DICTIONARY: {
|
||||
|
||||
int d_size = tFile->value.dict->count;
|
||||
|
||||
bDictionaryNode* curr = tFile->value.dict->head;
|
||||
|
||||
while (curr != NULL) {
|
||||
|
||||
std::cout << curr->key << ' ';
|
||||
|
||||
_dump_parsed_torrent_file_data(curr->value);
|
||||
std::cout << '\n';
|
||||
|
||||
curr = curr->next;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case B_LIST: {
|
||||
|
||||
int l_size = tFile->value.list->count;
|
||||
|
||||
bListNode* curr = tFile->value.list->head;
|
||||
|
||||
while (curr != NULL) {
|
||||
|
||||
_dump_parsed_torrent_file_data(curr->value);
|
||||
std::cout << '\n';
|
||||
|
||||
curr = curr->next;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case B_STRING: {
|
||||
|
||||
std::cout << tFile->value.str << '\n';
|
||||
break;
|
||||
}
|
||||
case B_INTEGER: {
|
||||
|
||||
std::cout << tFile->value.number << '\n';
|
||||
break;
|
||||
}
|
||||
case B_UNKNOWN: {
|
||||
|
||||
error_msg("Data-type doesn't belong to .torrent file \
|
||||
syntax.\n\
|
||||
.torrent file is broken.\n", __FUNCTION__);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
139
include/torrent_structure.hpp
Normal file
139
include/torrent_structure.hpp
Normal file
@@ -0,0 +1,139 @@
|
||||
#ifndef __TORRENT_STRUCT_H
|
||||
#define __TORRENT_STRUCT_H
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include<cmath>
|
||||
#include <list>
|
||||
|
||||
/*************************** (.torrent) content structure for parsing*************************/
|
||||
|
||||
typedef enum {
|
||||
|
||||
B_STRING,
|
||||
B_INTEGER,
|
||||
B_DICTIONARY,
|
||||
B_LIST,
|
||||
B_UNKNOWN
|
||||
|
||||
}b_type;
|
||||
|
||||
class bDictionary;
|
||||
class bList;
|
||||
class bListNode;
|
||||
class bDictionaryNode;
|
||||
|
||||
typedef bDictionary TorrentFile;
|
||||
|
||||
class bNode {
|
||||
|
||||
public:
|
||||
|
||||
b_type type;
|
||||
long long int size;
|
||||
|
||||
class val {
|
||||
public:
|
||||
|
||||
std::string str;
|
||||
long long number;
|
||||
bDictionary* dict;
|
||||
bList* list; //Pointer to array of pointer of bNode...
|
||||
uint64_t start;
|
||||
uint64_t end;
|
||||
|
||||
val();
|
||||
};
|
||||
|
||||
val value;
|
||||
|
||||
bNode();
|
||||
};
|
||||
|
||||
class bList {
|
||||
public:
|
||||
bListNode* head;
|
||||
bListNode* tail;
|
||||
int count;
|
||||
|
||||
bList();
|
||||
|
||||
std::vector<bNode*> to_Stl_list();
|
||||
};
|
||||
|
||||
class bListNode {
|
||||
public:
|
||||
|
||||
bNode* value;
|
||||
bListNode* next;
|
||||
|
||||
bListNode();
|
||||
};
|
||||
|
||||
|
||||
class bDictionary {
|
||||
public:
|
||||
bDictionaryNode* head;
|
||||
bDictionaryNode* tail;
|
||||
int count;
|
||||
|
||||
bDictionary();
|
||||
|
||||
std::map<std::string,bNode*> to_Stl_map();
|
||||
};
|
||||
|
||||
|
||||
|
||||
class bDictionaryNode {
|
||||
|
||||
public:
|
||||
std::string key;
|
||||
bNode* value;
|
||||
bDictionaryNode* next;
|
||||
|
||||
bDictionaryNode();
|
||||
};
|
||||
|
||||
/*************************** (.torrent) content structure for parsing*************************/
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
//class TorrentInfo {
|
||||
//
|
||||
//public:
|
||||
// std::string announce;
|
||||
// std::string comment;
|
||||
// std::string created_by;
|
||||
// long long int creation_date;
|
||||
// std::string encoding;
|
||||
//
|
||||
//
|
||||
// uint64_t start;
|
||||
// uint64_t end;
|
||||
//
|
||||
// long long int length;
|
||||
// std::string name;
|
||||
// long long int blockSize;
|
||||
// long long int pieceSize;
|
||||
//
|
||||
//
|
||||
// char** pieceHashes;
|
||||
//
|
||||
// std::string DownloadDirectory;
|
||||
// std::string FileDirectory;
|
||||
//
|
||||
// //std::list<FileItem>Files;
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user