Added map sorting and switched to binary search

This commit is contained in:
Bazsalanszky 2019-10-26 23:00:15 +02:00
parent 748f095cb5
commit c80e5a0a7d
Signed by: Bazsalanszky
GPG key ID: 0998CF5510B134D9
8 changed files with 86 additions and 54 deletions

View file

@ -4,9 +4,9 @@ project(p2p C)
set(CMAKE_C_STANDARD 99)
set(GCC_COVERAGE_COMPILE_FLAGS "-Werror")
add_executable(p2p main.c peer.h peer.c utility.c utility.h modules/webio/webio.h modules/webio/webio.c modules/config.h modules/config.c modules/map.h modules/map.c modules/crypto.c modules/crypto.h)
add_executable(p2p-2 main.c peer.h peer.c utility.c utility.h modules/webio/webio.h modules/webio/webio.c modules/config.h modules/config.c modules/map.h modules/map.c modules/crypto.c modules/crypto.h)
add_executable(p2p-3 main.c peer.h peer.c utility.c utility.h modules/webio/webio.h modules/webio/webio.c modules/config.h modules/config.c modules/map.h modules/map.c modules/crypto.c modules/crypto.h)
add_executable(p2p main.c modules/peer.h modules/peer.c utility.c utility.h modules/webio.h modules/webio.c modules/config.h modules/config.c modules/map.h modules/map.c modules/crypto.c modules/crypto.h)
add_executable(p2p-2 main.c modules/peer.h modules/peer.c utility.c utility.h modules/webio.h modules/webio.c modules/config.h modules/config.c modules/map.h modules/map.c modules/crypto.c modules/crypto.h)
add_executable(p2p-3 main.c modules/peer.h modules/peer.c utility.c utility.h modules/webio.h modules/webio.c modules/config.h modules/config.c modules/map.h modules/map.c modules/crypto.c modules/crypto.h)
set_target_properties( p2p-2 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/p2p-2 )
set_target_properties( p2p-3 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/p2p-3 )
# Függvények kipróbálására hoztam létre

17
main.c
View file

@ -1,9 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include "peer.h"
#include "modules/peer.h"
#include "modules/crypto.h"
#include "modules/webio/webio.h"
#include "modules/webio.h"
#include "modules/config.h"
#pragma comment(lib, "ws2_32.lib")
@ -43,13 +43,18 @@ int main(void) {
base64Encode((unsigned char*)pub,strlen(pub),&base64Key);
mynode.pubkey = createRSA((unsigned char*)mynode.pubkey_str,1);
if(map_isFound(config,"nickname")) {
strcpy(mynode.nick, map_getValue(config, "nickname"));
char * nickname = map_getValue(config,"nickname");
if(nickname != NULL) {
strcpy(mynode.nick, nickname);
}
if(map_isFound(config,"port"))
mynode.port = atoi(map_getValue(config,"port"));
char * port = map_getValue(config,"port");
if(port != NULL)
mynode.port = atoi(port);
else
mynode.port = atoi(DEFAULT_PORT);
logger_log("Initialising core...");
//TODO: Ezt a részt külön függvénybe tenni egy külön file-ban
WSADATA ws;

View file

@ -11,45 +11,65 @@ void map_init(map *m) {
m->pairs = 0;
}
bool map_isFound(map map, char* key){
bool map_isFound(map map, char *key) {
for (int i = 0; i < map.length; ++i) {
if(strcmp(map.pairs[i].key,key) == 0)
if (strcmp(map.pairs[i].key, key) == 0)
return true;
}
return false;
}
char* map_getValue(map m, char* key){
for (int i = 0; i < m.length; ++i) {
if(strcmp(m.pairs[i].key,key) == 0)
return m.pairs[i].value;
char *map_getValue(map m, char *key) {
size_t min = 0;
size_t max = m.length-1;
size_t kp = (min+max)/2;
while(min <= max && strcmp(m.pairs[kp].key,key) != 0){
if(strcmp(m.pairs[kp].key,key) < 0)
min = kp+1;
else
max = kp-1;
kp = (min+max)/2;
}
return "UNDEFINED";
return min <= max ? m.pairs[kp].value : NULL;
}
void map_dump(map m) {
for (int i = 0; i <m.length ; ++i) {
printf("%s %s\n",m.pairs[i].key,m.pairs[i].value);
for (int i = 0; i < m.length; ++i) {
printf("%s %s\n", m.pairs[i].key, m.pairs[i].value);
}
}
void map_addPair(map* m,pair p) {
if (m->length >= m->size)
{
void map_addPair(map *m, pair p) {
if (m->length >= m->size) {
assert(m->length == m->size);
size_t new_size = (m->size + 2) * 2;
pair *new_list = realloc(m->pairs, new_size * sizeof(pair));
if (new_list == 0)
printf("OUT OF MEMORY!");
m->size = new_size;
m->pairs = new_list;
m->pairs = new_list;
}
m->pairs[m->length++] = p;
map_sort(m);
}
pair map_make_pair(char *key,char *value) {
pair map_make_pair(char *key, char *value) {
pair result;
strcpy(result.key,key);
strcpy(result.value,value);
strcpy(result.key, key);
strcpy(result.value, value);
return result;
}
void map_sort(map* m) {
for (int i = m->length - 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (strcmp(m->pairs[j].key, m->pairs[j + 1].key) > 0) {
pair tmp = m->pairs[j];
m->pairs[j] = m->pairs[j + 1];
m->pairs[j + 1] = tmp;
}
}
}
}

View file

@ -32,5 +32,6 @@ void map_addPair(map *m, pair p);
pair map_make_pair(char *key,char *value);
void map_sort(map* m);
//Debug-hoz hasznos
void map_dump(map m);

View file

@ -62,19 +62,19 @@ int peer_ConnetctTo(char* ip,int port,peerList* peerList, node_data my,fd_set* f
return -1;
}
if(map_isFound(m,"id")) {
strcpy(node.id, map_getValue(m, "id"));
char * id = map_getValue(m, "id");
if(id != NULL) {
strcpy(node.id, id);
} else {
logger_log("Error: Invalid response!ID not found in handshake.");
return -1;
}
if(map_isFound(m,"pubkey")) {
char* pbk_str = map_getValue(m, "pubkey");
if(pbk_str != NULL) {
unsigned char* pubkey;
char* base64Key = map_getValue(m, "pubkey");
size_t len;
base64Decode(base64Key,&pubkey,&len);
base64Decode(pbk_str,&pubkey,&len);
strcpy(node.pubkey_str,(const char*)pubkey);
node.pubkey = createRSA(pubkey,1);
} else {
@ -82,15 +82,16 @@ int peer_ConnetctTo(char* ip,int port,peerList* peerList, node_data my,fd_set* f
return -1;
}
if(map_isFound(m,"port")) {
node.port = atoi(map_getValue(m, "port"));
char * port_str = map_getValue(m,"port");
if(port_str != NULL) {
node.port = atoi(port_str);
} else {
logger_log("Error: Invalid response!Port not found in handshake.");
return -1;
}
if(map_isFound(m,"nickname")) {
strcpy(node.nick, map_getValue(m, "nickname"));
char * nickname = map_getValue(m,"nickname");
if(nickname != NULL) {
strcpy(node.nick, nickname);
}
Peer p;
@ -99,9 +100,10 @@ int peer_ConnetctTo(char* ip,int port,peerList* peerList, node_data my,fd_set* f
p.sockaddr = hint;
FD_SET(sock,fdSet);
peer_addTolist(peerList,p);
//TODO: Connect to recived peers
if(map_isFound(m,"peers")) {
char* tmp = strtok(map_getValue(m,"peers"),",");
char* peers =map_getValue(m,"peers");
if(peers != NULL) {
char* tmp = strtok(peers,",");
while(tmp != NULL){
char ip[NI_MAXHOST];
int port;
@ -148,15 +150,19 @@ int peer_HandleConnection(SOCKET listening,peerList *peerList, node_data my,fd_s
map_dump(m);
node_data node;
strcpy(node.ip,ip);
if(map_isFound(m,"id")) {
strcpy(node.id, map_getValue(m, "id"));
char* id = map_getValue(m, "id");
if(id != NULL) {
strcpy(node.id, id);
} else {
logger_log("Error: Invalid response!ID not found in handshake.");
return -1;
}
if(map_isFound(m,"pubkey")) {
char* base64Key = map_getValue(m, "pubkey");
if(base64Key != NULL) {
unsigned char* pubkey;
char* base64Key = map_getValue(m, "pubkey");
size_t len;
base64Decode(base64Key,&pubkey,&len);
strcpy(node.pubkey_str,(const char*)pubkey);
@ -165,16 +171,16 @@ int peer_HandleConnection(SOCKET listening,peerList *peerList, node_data my,fd_s
logger_log("Error: Invalid response!RSA public key not found in handshake.");
return -1;
}
if(map_isFound(m,"port")) {
node.port = atoi(map_getValue(m, "port"));
char * port = map_getValue(m, "port");
if(port != NULL) {
node.port = atoi(port);
} else {
logger_log("Error: Invalid response!Port not found in handshake.");
return -1;
}
char * nickname = map_getValue(m, "nickname");
if(map_isFound(m,"nickname")) {
strcpy(node.nick, map_getValue(m, "nickname"));
strcpy(node.nick, nickname);
}
if(peer_isFoundInList(*peerList,node.id)){
logger_log("Handshake received, but the id sent is taken! Dropping peer...");
@ -190,7 +196,7 @@ int peer_HandleConnection(SOCKET listening,peerList *peerList, node_data my,fd_s
}
free(m.pairs);
logger_log("Handshake recived! Sending response!");
char handshake[DEFAULT_BUFLEN],*base64Key;
char handshake[DEFAULT_BUFLEN];
base64Encode((unsigned char*)my.pubkey_str,strlen(my.pubkey_str),&base64Key);
sprintf(handshake,"@id=%s&port=%d&pubkey=%s",my.id,my.port,base64Key);

View file

@ -3,8 +3,8 @@
// Dátum: 2019. 10. 09.
//
#pragma once
#include "utility.h"
#include "modules/crypto.h"
#include "../utility.h"
#include "crypto.h"
#define DEFAULT_MAX_PEER_COUNT 64

View file

@ -3,8 +3,8 @@
// Dátum: 2019. 10. 14..
//
#pragma once
#include "../../utility.h"
#include "../../peer.h"
#include "../utility.h"
#include "peer.h"
typedef struct webio{