From c80e5a0a7d5081ecd494efbbc9f42a22368d113a Mon Sep 17 00:00:00 2001 From: Bazsalanszky Date: Sat, 26 Oct 2019 23:00:15 +0200 Subject: [PATCH] Added map sorting and switched to binary search --- CMakeLists.txt | 6 ++-- main.c | 17 +++++++---- modules/map.c | 52 +++++++++++++++++++++++----------- modules/map.h | 1 + peer.c => modules/peer.c | 56 ++++++++++++++++++++----------------- peer.h => modules/peer.h | 4 +-- modules/{webio => }/webio.c | 0 modules/{webio => }/webio.h | 4 +-- 8 files changed, 86 insertions(+), 54 deletions(-) rename peer.c => modules/peer.c (89%) rename peer.h => modules/peer.h (96%) rename modules/{webio => }/webio.c (100%) rename modules/{webio => }/webio.h (93%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f49a9c6..1d2b532 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/main.c b/main.c index 2b3fd3c..480dca4 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,9 @@ #include #include -#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; diff --git a/modules/map.c b/modules/map.c index 1ef0b39..34bc693 100644 --- a/modules/map.c +++ b/modules/map.c @@ -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 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; + } + } + } +} + diff --git a/modules/map.h b/modules/map.h index 7d244c9..346c35c 100644 --- a/modules/map.h +++ b/modules/map.h @@ -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); \ No newline at end of file diff --git a/peer.c b/modules/peer.c similarity index 89% rename from peer.c rename to modules/peer.c index 39551e5..1edf232 100644 --- a/peer.c +++ b/modules/peer.c @@ -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); diff --git a/peer.h b/modules/peer.h similarity index 96% rename from peer.h rename to modules/peer.h index 5a5b1f3..7c056f7 100644 --- a/peer.h +++ b/modules/peer.h @@ -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 diff --git a/modules/webio/webio.c b/modules/webio.c similarity index 100% rename from modules/webio/webio.c rename to modules/webio.c diff --git a/modules/webio/webio.h b/modules/webio.h similarity index 93% rename from modules/webio/webio.h rename to modules/webio.h index d161cfa..a4008eb 100644 --- a/modules/webio/webio.h +++ b/modules/webio.h @@ -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{