cool stuff

This commit is contained in:
NikolajDanger
2021-11-04 22:39:39 +01:00
parent 28e8fced8e
commit 26ad1f8725
2 changed files with 13 additions and 29 deletions

Binary file not shown.

View File

@ -109,17 +109,11 @@ void download_only_peer(char *cascade_file)
queue = malloc(casc_file->blockcount * sizeof(csc_block_t*));
for (unsigned long long i = 0;i<casc_file->blockcount;i++) {
if ((&casc_file->blocks[i])->completed == 0) {
queue[uncomp_count]0 = &casc_file->blocks[i];
queue[uncomp_count] = &casc_file->blocks[i];
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];
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;
uint8_t rio_buf[MAX_LINE];
for (int i = 0; i < MAX_LINE ; i++) {
rio_buf[i] = 0;
}
int tracker_socket;
/*
TODO Setup a connection to the tracker
HINT: Remember that as well as making a connection, you'll also need to initialise a buffer for messages
*/
tracker_socket = Open_clientfd(tracker_ip, tracker_port);
Rio_readinitb(&rio, tracker_socket);
struct RequestHeader request_header;
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;
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);
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);
@ -478,14 +460,16 @@ int get_peers_list(csc_peer_t** peers, uint8_t hash[32])
Close(tracker_socket);
return NULL;
}
int peercount = msglen/12;
peers = malloc(peercount * sizeof(csc_peer_t));
/*
TODO Parse the body of the response to get a list of peers
HINT Some of the later provided code expects the peers to be stored in the ''peers' variable, which
is an array of 'csc_peer's, as defined in cascade.h
*/
int peercount;
for (int i = 0;i<peercount;i++) {
csc_peer_t peer;
uint8_t* peer_data = &rio_buf[REPLY_HEADER_SIZE + 12*i];
struct in_addr ip = {};
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])));
}
Close(tracker_socket);
return peercount;