cool stuff
This commit is contained in:
BIN
A3/src/cascade
BIN
A3/src/cascade
Binary file not shown.
@ -109,17 +109,11 @@ void download_only_peer(char *cascade_file)
|
|||||||
queue = malloc(casc_file->blockcount * sizeof(csc_block_t*));
|
queue = malloc(casc_file->blockcount * sizeof(csc_block_t*));
|
||||||
for (unsigned long long i = 0;i<casc_file->blockcount;i++) {
|
for (unsigned long long i = 0;i<casc_file->blockcount;i++) {
|
||||||
if ((&casc_file->blocks[i])->completed == 0) {
|
if ((&casc_file->blocks[i])->completed == 0) {
|
||||||
queue[uncomp_count]0 = &casc_file->blocks[i];
|
queue[uncomp_count] = &casc_file->blocks[i];
|
||||||
uncomp_count++;
|
uncomp_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
queue = malloc(sizeof(csc_block_t*) * uncomp_count);
|
|
||||||
for (int i = 0;i<uncomp_count;i++) {
|
|
||||||
queue[i] = uncomp[i];
|
|
||||||
}
|
|
||||||
free(uncomp);
|
|
||||||
|
|
||||||
uint8_t hash_buf[32];
|
uint8_t hash_buf[32];
|
||||||
get_file_sha(cascade_file, hash_buf, 32);
|
get_file_sha(cascade_file, hash_buf, 32);
|
||||||
|
|
||||||
@ -410,17 +404,11 @@ int get_peers_list(csc_peer_t** peers, uint8_t hash[32])
|
|||||||
{
|
{
|
||||||
rio_t rio;
|
rio_t rio;
|
||||||
uint8_t rio_buf[MAX_LINE];
|
uint8_t rio_buf[MAX_LINE];
|
||||||
for (int i = 0; i < MAX_LINE ; i++) {
|
|
||||||
rio_buf[i] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int tracker_socket;
|
int tracker_socket;
|
||||||
|
|
||||||
/*
|
tracker_socket = Open_clientfd(tracker_ip, tracker_port);
|
||||||
TODO Setup a connection to the tracker
|
Rio_readinitb(&rio, tracker_socket);
|
||||||
|
|
||||||
HINT: Remember that as well as making a connection, you'll also need to initialise a buffer for messages
|
|
||||||
*/
|
|
||||||
|
|
||||||
struct RequestHeader request_header;
|
struct RequestHeader request_header;
|
||||||
strncpy(request_header.protocol, "CASC", 4);
|
strncpy(request_header.protocol, "CASC", 4);
|
||||||
@ -431,15 +419,9 @@ int get_peers_list(csc_peer_t** peers, uint8_t hash[32])
|
|||||||
|
|
||||||
struct RequestBody request_body;
|
struct RequestBody request_body;
|
||||||
strncpy(request_body.hash, hash, 32);
|
strncpy(request_body.hash, hash, 32);
|
||||||
inet_pton(AF_INET, my_ip, &request_body.ip);
|
inet_aton(my_ip, &request_body.ip);
|
||||||
request_body.port = atol(my_port);
|
request_body.port = atol(my_port);
|
||||||
memcpy(&rio_buf[HEADER_SIZE], &request_body, BODY_SIZE);
|
memcpy(&rio_buf[HEADER_SIZE], &request_body, BODY_SIZE);
|
||||||
for (int i = 0;i<128;i++) {
|
|
||||||
printf("%02x ",rio_buf[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
return 0; // TODO REMOVE
|
|
||||||
|
|
||||||
Rio_writen(tracker_socket, rio_buf, MESSAGE_SIZE);
|
Rio_writen(tracker_socket, rio_buf, MESSAGE_SIZE);
|
||||||
|
|
||||||
@ -478,14 +460,16 @@ int get_peers_list(csc_peer_t** peers, uint8_t hash[32])
|
|||||||
Close(tracker_socket);
|
Close(tracker_socket);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
int peercount = msglen/12;
|
||||||
|
peers = malloc(peercount * sizeof(csc_peer_t));
|
||||||
|
|
||||||
/*
|
for (int i = 0;i<peercount;i++) {
|
||||||
TODO Parse the body of the response to get a list of peers
|
csc_peer_t peer;
|
||||||
|
uint8_t* peer_data = &rio_buf[REPLY_HEADER_SIZE + 12*i];
|
||||||
HINT Some of the later provided code expects the peers to be stored in the ''peers' variable, which
|
struct in_addr ip = {};
|
||||||
is an array of 'csc_peer's, as defined in cascade.h
|
sprintf(peer.ip, "%u.%u.%u.%u", peer_data[0], peer_data[1], peer_data[2], peer_data[3]);
|
||||||
*/
|
sprintf(peer.port, "%u", be16toh(*((uint16_t*)&peer_data[4])));
|
||||||
int peercount;
|
}
|
||||||
|
|
||||||
Close(tracker_socket);
|
Close(tracker_socket);
|
||||||
return peercount;
|
return peercount;
|
||||||
|
Reference in New Issue
Block a user