Added configuration and fixed the peerList
This commit is contained in:
parent
8dce51a0cf
commit
a609cdea7e
12 changed files with 171 additions and 155 deletions
|
@ -4,13 +4,13 @@ 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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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
|
||||
add_executable(test test.c utility.h utility.c)
|
||||
add_executable(test test.c utility.h utility.c modules/map.h modules/map.c)
|
||||
|
||||
target_compile_definitions(p2p-2 PUBLIC RANDOM_PORT)
|
||||
target_compile_definitions(p2p-3 PUBLIC RANDOM_PORT)
|
||||
|
|
39
main.c
39
main.c
|
@ -1,8 +1,9 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "peer.h"
|
||||
#include "modules/webio/webio.h"
|
||||
|
||||
#include "modules/webio/webio.h"
|
||||
#include "modules/config.h"
|
||||
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
//#include "webinterface.h"
|
||||
|
@ -17,7 +18,8 @@
|
|||
|
||||
|
||||
int main(void) {
|
||||
logger_log("%s","Goood");
|
||||
map config = config_load();
|
||||
|
||||
FILE *seed_file;
|
||||
seed_file = fopen("seed.txt", "r");
|
||||
char seed[513];
|
||||
|
@ -34,10 +36,13 @@ int main(void) {
|
|||
char id[MD5_DIGEST_LENGTH];
|
||||
md5(seed, id);
|
||||
node_data mynode;
|
||||
strcpy(mynode.id, id);
|
||||
strcpy(mynode.nick, "");
|
||||
strncpy(mynode.id, id,MD5_DIGEST_LENGTH);
|
||||
if(map_isFound(config,"nickname"))
|
||||
strcpy(mynode.nick,map_getValue(config,"nickname"));
|
||||
if(map_isFound(config,"port"))
|
||||
mynode.port = atoi(map_getValue(config,"port"));
|
||||
else
|
||||
mynode.port = atoi(DEFAULT_PORT);
|
||||
|
||||
logger_log("Initialising core...");
|
||||
WSADATA ws;
|
||||
int res = WSAStartup(MAKEWORD(2, 2), &ws);
|
||||
|
@ -60,7 +65,9 @@ int main(void) {
|
|||
hint.ai_next = NULL;
|
||||
|
||||
//TODO: Use config to determine port
|
||||
res = getaddrinfo(NULL, DEFAULT_PORT, &hint, &result);
|
||||
char sport[10];
|
||||
sprintf( sport, "%d", mynode.port);
|
||||
res = getaddrinfo(NULL, sport, &hint, &result);
|
||||
if (res != 0) {
|
||||
logger_log("Error creating address information! Error code: %d", WSAGetLastError());
|
||||
WSACleanup();
|
||||
|
@ -112,8 +119,8 @@ int main(void) {
|
|||
|
||||
//Connecting to peers
|
||||
logger_log("Checking peers.txt for peers...");
|
||||
Peer *peerList = (Peer *) malloc(sizeof(Peer) * DEFAULT_MAX_PEER_COUNT);
|
||||
int peerCount = 0;
|
||||
peerList peerList1;
|
||||
peer_initList(&peerList1);
|
||||
|
||||
FILE *peer_file;
|
||||
peer_file = fopen("peers.txt", "r");
|
||||
|
@ -126,7 +133,7 @@ int main(void) {
|
|||
char ip[NI_MAXHOST];
|
||||
int port;
|
||||
while (fscanf(peer_file, "%[^:]:%d", ip, &port) != EOF) {
|
||||
if(peer_ConnetctTo(ip, port, peerList, &peerCount, mynode,&master) != 0)
|
||||
if(peer_ConnetctTo(ip, port, &peerList1, mynode,&master) != 0)
|
||||
logger_log("Error while connecting to peer...");
|
||||
}
|
||||
|
||||
|
@ -152,20 +159,20 @@ int main(void) {
|
|||
for (int i = 0; i < count; i++) {
|
||||
SOCKET sock = copy.fd_array[i];
|
||||
if (sock == listening) {
|
||||
if(peer_HandleConnection(listening, peerList, &peerCount, mynode,&master) != 0)
|
||||
if(peer_HandleConnection(listening, &peerList1, mynode,&master) != 0)
|
||||
logger_log("Error while receiving connection...");
|
||||
}else if(sock == webIo.socket ){
|
||||
webio_handleRequest(webIo,peerList,peerCount);
|
||||
webio_handleRequest(webIo,peerList1);
|
||||
} else {
|
||||
char buf[DEFAULT_BUFLEN];
|
||||
ZeroMemory(buf, DEFAULT_BUFLEN);
|
||||
int inBytes = recv(sock, buf, DEFAULT_BUFLEN, 0);
|
||||
if (inBytes <= 0) {
|
||||
//Peer disconnect
|
||||
int k = peer_getPeer(peerList, peerCount, sock);
|
||||
int k = peer_getPeer(peerList1, sock);
|
||||
if (k != -1) {
|
||||
logger_log("Peer disconnected(%s->%s)", inet_ntoa(peerList[k].sockaddr.sin_addr),peerList[k].peerData.id);
|
||||
peer_removeFromList(peerList, &peerCount, k);
|
||||
logger_log("Peer disconnected(%s->%s)", inet_ntoa(peerList1.array[k].sockaddr.sin_addr),peerList1.array[k].peerData.id);
|
||||
peer_removeFromList(&peerList1, k);
|
||||
closesocket(sock);
|
||||
FD_CLR(sock, &master);
|
||||
}
|
||||
|
@ -174,8 +181,8 @@ int main(void) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
free(peerList);
|
||||
free(&config);
|
||||
free(&peerList1);
|
||||
closesocket(listening);
|
||||
WSACleanup();
|
||||
return 0;
|
||||
|
|
|
@ -2,4 +2,21 @@
|
|||
// Készítette: Toldi Balázs Ádám
|
||||
// Dátum: 2019. 10. 16.
|
||||
//
|
||||
#include "config.h"
|
||||
|
||||
config config_load(){
|
||||
config cfg;
|
||||
map_init(&cfg);
|
||||
FILE * f;
|
||||
f = fopen("config.txt","r");
|
||||
char key[65],value[65];
|
||||
while(fscanf(f,"%[^=]=%s\n",key,value) != EOF){
|
||||
//Comment in config
|
||||
if(key[0] == '#') continue;
|
||||
map_addPair(&cfg,map_make_pair(key,value));
|
||||
ZeroMemory(key,65);
|
||||
ZeroMemory(value,65);
|
||||
}
|
||||
|
||||
return cfg;
|
||||
}
|
|
@ -3,11 +3,10 @@
|
|||
// Dátum: 2019. 10. 16.
|
||||
//
|
||||
|
||||
#ifndef P2P_CONFIG_H
|
||||
#define P2P_CONFIG_H
|
||||
|
||||
#endif //P2P_CONFIG_H
|
||||
#ifndef P2P_UTILITY_H
|
||||
|
||||
#pragma once
|
||||
#include "../utility.h"
|
||||
#endif
|
||||
#include "map.h"
|
||||
|
||||
|
||||
config config_load();
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
//
|
||||
|
||||
#pragma once
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
|
@ -23,7 +22,7 @@ typedef struct Map
|
|||
pair *pairs;
|
||||
} map,config;
|
||||
|
||||
void initMap(map * m);
|
||||
void map_init(map *m);
|
||||
|
||||
bool map_isFound(map m, char* key);
|
||||
|
||||
|
@ -33,7 +32,7 @@ void map_addPair(map *m, pair p);
|
|||
|
||||
pair map_make_pair(char *key,char *value);
|
||||
|
||||
void map_init(map *m);
|
||||
|
||||
|
||||
//Debug-hoz hasznos
|
||||
void map_dump(map m);
|
|
@ -83,7 +83,7 @@ int webio_create(int port,char* folder,struct Node_data myData,WebIO *webIo){
|
|||
return 0;
|
||||
}
|
||||
|
||||
int webio_handleRequest(WebIO wio,Peer list[],int peerCount){
|
||||
int webio_handleRequest(WebIO wio,peerList list){
|
||||
SOCKET client = accept(wio.socket,NULL,NULL);
|
||||
char buf[8192];
|
||||
int res = recv(client,buf,8192,0);
|
||||
|
@ -98,9 +98,9 @@ int webio_handleRequest(WebIO wio,Peer list[],int peerCount){
|
|||
if(strcmp(req,"GET") == 0) {
|
||||
char file[50];
|
||||
sscanf(buf, "%*s %s", file);
|
||||
res = webio_handleGETrequest(client,wio,file, list, peerCount);
|
||||
res = webio_handleGETrequest(client,wio,file, list);
|
||||
}else if(strcmp(req,"POST") == 0)
|
||||
res = webio_handlePOSTrequest(client,wio, list,peerCount);
|
||||
res = webio_handlePOSTrequest(client,wio, list);
|
||||
else
|
||||
res = -1;
|
||||
return res;
|
||||
|
@ -138,7 +138,7 @@ char* webio_getFiletype(char* filename){
|
|||
}
|
||||
return type;
|
||||
}
|
||||
int webio_handleGETrequest(SOCKET client,WebIO wio,char* file,Peer list[],int peerCount){
|
||||
int webio_handleGETrequest(SOCKET client,WebIO wio,char* file,peerList list){
|
||||
|
||||
char buf[8192];
|
||||
sscanf(buf,"%*s %s",file);
|
||||
|
@ -154,7 +154,7 @@ int webio_handleGETrequest(SOCKET client,WebIO wio,char* file,Peer list[],int pe
|
|||
"Content-Language: en\r\n"
|
||||
"Content-Type: text/html\r\n\r\n"
|
||||
);
|
||||
strcat(response,getIndex(wio.folder,list,peerCount));
|
||||
strcat(response,getIndex(wio.folder,list));
|
||||
}else {
|
||||
strcat(path, file);
|
||||
|
||||
|
@ -204,7 +204,7 @@ int webio_handleGETrequest(SOCKET client,WebIO wio,char* file,Peer list[],int pe
|
|||
closesocket(client);
|
||||
}
|
||||
|
||||
int webio_handlePOSTrequest(SOCKET client,WebIO wio,Peer list[],int peerCount){
|
||||
int webio_handlePOSTrequest(SOCKET client,WebIO wio,peerList list){
|
||||
|
||||
}
|
||||
char* webio_getHeader(char* folder) {
|
||||
|
@ -228,17 +228,21 @@ char* webio_getHeader(char* folder) {
|
|||
}else return "<html>";
|
||||
}
|
||||
|
||||
char* getIndex(char* folder,Peer list[],int count){
|
||||
char* getIndex(char* folder,peerList list){
|
||||
char* content = (char*) calloc(sizeof(char*)*8192,1);
|
||||
char * header = webio_getHeader(folder);
|
||||
|
||||
strcat(content,header);
|
||||
strcat(content,"<h1>Peers:</h1>\n");
|
||||
if(count > 0) {
|
||||
if(list.length > 0) {
|
||||
strcat(content, "<ul>\n");
|
||||
for (int i = 0; i < count; ++i) {
|
||||
for (int i = 0; i < list.length; ++i) {
|
||||
strcat(content, "<li>");
|
||||
strcat(content, list[i].peerData.id);
|
||||
if(strlen(list.array[i].peerData.nick) != 0){
|
||||
strcat(content, list.array[i].peerData.nick);
|
||||
strcat(content, " - ");
|
||||
}
|
||||
strcat(content, list.array[i].peerData.id);
|
||||
strcat(content, "</li>\n");
|
||||
}
|
||||
strcat(content, "</ul>\n");
|
||||
|
|
|
@ -2,16 +2,10 @@
|
|||
// Készítette: Toldi Balázs Ádám
|
||||
// Dátum: 2019. 10. 14..
|
||||
//
|
||||
#ifndef P2P_UTILITY_H
|
||||
#pragma once
|
||||
#include "../../utility.h"
|
||||
#endif
|
||||
#ifndef P2P_PEER_H
|
||||
#include "../../peer.h"
|
||||
#endif
|
||||
|
||||
#ifndef P2P_WEBIO_H
|
||||
#define P2P_WEBIO_H
|
||||
#endif
|
||||
|
||||
typedef struct webio{
|
||||
SOCKET socket;
|
||||
|
@ -22,11 +16,11 @@ typedef struct webio{
|
|||
|
||||
int webio_create(int port,char* folder,struct Node_data myData,WebIO *webIo);
|
||||
|
||||
int webio_handleRequest(WebIO wio,Peer list[],int peerCount);
|
||||
int webio_handleRequest(WebIO wio,peerList list);
|
||||
|
||||
int webio_handleGETrequest(SOCKET client,WebIO wio,char * file,Peer list[],int peerCount);
|
||||
int webio_handleGETrequest(SOCKET client,WebIO wio,char * file,peerList list);
|
||||
|
||||
int webio_handlePOSTrequest(SOCKET client,WebIO wio,Peer list[],int peerCount);
|
||||
int webio_handlePOSTrequest(SOCKET client,WebIO wio,peerList list);
|
||||
|
||||
char* webio_getMIMEtype(char* filename);
|
||||
|
||||
|
@ -34,4 +28,4 @@ char* webio_getFiletype(char* filename);
|
|||
|
||||
char* webio_getHeader(char* folder);
|
||||
|
||||
char* getIndex(char* folder,Peer list[],int count);
|
||||
char* getIndex(char* folder,peerList list);
|
148
peer.c
148
peer.c
|
@ -5,11 +5,7 @@
|
|||
|
||||
//Kouhai peer
|
||||
//Amikor mi csatlakozunk egy peerhez
|
||||
int peer_ConnetctTo(char* ip,int port,Peer peerList[],int* peerCount, node_data my,fd_set* fdSet){
|
||||
if(*peerCount+1 >= DEFAULT_MAX_PEER_COUNT){
|
||||
logger_log("Couldn't connect,because the max peer count is reached!");
|
||||
return -1;
|
||||
}
|
||||
int peer_ConnetctTo(char* ip,int port,peerList* peerList, node_data my,fd_set* fdSet){
|
||||
struct sockaddr_in hint;
|
||||
hint.sin_family = AF_INET;
|
||||
hint.sin_port = htons(port);
|
||||
|
@ -25,9 +21,13 @@ int peer_ConnetctTo(char* ip,int port,Peer peerList[],int* peerCount, node_data
|
|||
}
|
||||
logger_log("Connected to peer!Sending handshake...");
|
||||
char handshake[DEFAULT_BUFLEN];
|
||||
if(strlen(my.nick) == 0)
|
||||
sprintf(handshake,"@id=%s&port=%d",my.id,my.port);
|
||||
else sprintf(handshake,"@id=%s&port=%d&nickname=",my.id,my.port,my.nick);
|
||||
sprintf(handshake,"@id=%s&port=%d",my.id,my.port);
|
||||
if(strlen(my.nick) != 0) {
|
||||
char buf[DEFAULT_BUFLEN];
|
||||
ZeroMemory(buf,DEFAULT_BUFLEN);
|
||||
sprintf(buf, "&nickname=%s",my.nick);
|
||||
strcat(handshake,buf);
|
||||
}
|
||||
res = send(sock,handshake,strlen(handshake),0);
|
||||
if (res == SOCKET_ERROR) {
|
||||
logger_log("Error sending peer list!Disconnecting..." );
|
||||
|
@ -47,34 +47,34 @@ int peer_ConnetctTo(char* ip,int port,Peer peerList[],int* peerCount, node_data
|
|||
return -1;
|
||||
}
|
||||
int len;
|
||||
map *m = getHandshakeData(buf,&len);
|
||||
map m = getHandshakeData(buf);
|
||||
node_data node;
|
||||
strcpy(node.ip,ip);
|
||||
|
||||
if(map_isFound(m,len,"valid") && strcmp("false", map_getValue(m, len, "valid")) == 0) {
|
||||
if(map_isFound(m,"valid") && strcmp("false", map_getValue(m, "valid")) == 0) {
|
||||
char error[129];
|
||||
sprintf(error,"Peer closed connection! Error: %s\n",map_getValue(m,len,"error"));
|
||||
sprintf(error,"Peer closed connection! Error: %s\n",map_getValue(m,"error"));
|
||||
logger_log(error);
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
if(map_isFound(m,len,"id")) {
|
||||
strcpy(node.id, map_getValue(m, len, "id"));
|
||||
if(map_isFound(m,"id")) {
|
||||
strcpy(node.id, map_getValue(m, "id"));
|
||||
} else {
|
||||
logger_log("Error: Invalid response!ID not found in handshake.");
|
||||
return -1;
|
||||
}
|
||||
if(map_isFound(m,len,"port")) {
|
||||
node.port = atoi(map_getValue(m, len, "port"));
|
||||
if(map_isFound(m,"port")) {
|
||||
node.port = atoi(map_getValue(m, "port"));
|
||||
} else {
|
||||
logger_log("Error: Invalid response!Port not found in handshake.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(map_isFound(m,len,"nickname")) {
|
||||
strcpy(node.nick, map_getValue(m, len, "nickname"));
|
||||
if(map_isFound(m,"nickname")) {
|
||||
strcpy(node.nick, map_getValue(m, "nickname"));
|
||||
}
|
||||
|
||||
Peer p;
|
||||
|
@ -82,28 +82,26 @@ int peer_ConnetctTo(char* ip,int port,Peer peerList[],int* peerCount, node_data
|
|||
p.socket = sock;
|
||||
p.sockaddr = hint;
|
||||
FD_SET(sock,fdSet);
|
||||
peerList = peer_addTolist(peerList,peerCount,p);
|
||||
peer_addTolist(peerList,p);
|
||||
//TODO: Connect to recived peers
|
||||
if(map_isFound(m,len,"peers")) {
|
||||
char* tmp = strtok(map_getValue(m,len,"peers"),",");
|
||||
if(map_isFound(m,"peers")) {
|
||||
char* tmp = strtok(map_getValue(m,"peers"),",");
|
||||
while(tmp != NULL){
|
||||
char ip[NI_MAXHOST];
|
||||
int port;
|
||||
sscanf(tmp, "%[^:]:%d", ip, &port);
|
||||
if(!peer_isIPfoundInList(peerList,*peerCount,ip,port))
|
||||
peer_ConnetctTo(ip,port,peerList,peerCount,my,fdSet);
|
||||
if(!peer_isIPfoundInList(*peerList,ip,port))
|
||||
peer_ConnetctTo(ip,port,peerList,my,fdSet);
|
||||
tmp = strtok(NULL,",");
|
||||
}
|
||||
}
|
||||
char*msg = (char*) calloc(sizeof(char)*129, sizeof(char));
|
||||
sprintf(msg,"Peer validated (%s->%s)!",node.ip,node.id);
|
||||
logger_log(msg);
|
||||
logger_log("Peer validated (%s->%s)!",node.ip,node.id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Senpai peer
|
||||
//Amikor a egy peer csatlakozik hozzánk
|
||||
int peer_HandleConnection(SOCKET listening,Peer peerList[],int* peerCount, node_data my,fd_set* fdSet){
|
||||
int peer_HandleConnection(SOCKET listening,peerList *peerList, node_data my,fd_set* fdSet){
|
||||
struct sockaddr_in client;
|
||||
int clientSize = sizeof(client);
|
||||
SOCKET sock = accept(listening, (struct sockaddr*)& client, &clientSize);
|
||||
|
@ -126,30 +124,30 @@ int peer_HandleConnection(SOCKET listening,Peer peerList[],int* peerCount, node_
|
|||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%s\n",buf);
|
||||
|
||||
int len;
|
||||
map *m = getHandshakeData(buf,&len);
|
||||
map m = getHandshakeData(buf);
|
||||
|
||||
node_data node;
|
||||
strcpy(node.ip,ip);
|
||||
if(map_isFound(m,len,"id")) {
|
||||
strcpy(node.id, map_getValue(m, len, "id"));
|
||||
if(map_isFound(m,"id")) {
|
||||
strcpy(node.id, map_getValue(m, "id"));
|
||||
} else {
|
||||
logger_log("Error: Invalid response!ID not found in handshake.");
|
||||
return -1;
|
||||
}
|
||||
if(map_isFound(m,len,"port")) {
|
||||
node.port = atoi(map_getValue(m, len, "port"));
|
||||
if(map_isFound(m,"port")) {
|
||||
node.port = atoi(map_getValue(m, "port"));
|
||||
} else {
|
||||
logger_log("Error: Invalid response!Port not found in handshake.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(map_isFound(m,len,"nickname")) {
|
||||
strcpy(node.nick, map_getValue(m, len, "nickname"));
|
||||
if(map_isFound(m,"nickname")) {
|
||||
strcpy(node.nick, map_getValue(m, "nickname"));
|
||||
}
|
||||
if(peer_isFoundInList(peerList,*peerCount,node.id)){
|
||||
if(peer_isFoundInList(*peerList,node.id)){
|
||||
logger_log("Handshake received, but the id sent is taken! Dropping peer...");
|
||||
char handshake[1024] = "@valid=false&error=ID_TAKEN";
|
||||
int res = send(sock, handshake, strlen(handshake), 0);
|
||||
|
@ -162,18 +160,20 @@ int peer_HandleConnection(SOCKET listening,Peer peerList[],int* peerCount, node_
|
|||
return -1;
|
||||
}
|
||||
logger_log("Handshake recived! Sending response!");
|
||||
char handshake[DEFAULT_BUFLEN];
|
||||
if(strlen(my.nick) == 0)
|
||||
sprintf(handshake,"@id=%s&port=%d",my.id,my.port);
|
||||
else sprintf(handshake,"@id=%s&port=%d&nickname=",my.id,my.port,my.nick);
|
||||
|
||||
char* handshake = (char*) calloc(DEFAULT_BUFLEN, sizeof(char));
|
||||
sprintf(handshake,"@id=%s&port=%d",my.id,my.port);
|
||||
if(strlen(my.nick) != 0) {
|
||||
ZeroMemory(buf,DEFAULT_BUFLEN);
|
||||
sprintf(buf, "&nickname=%s",my.nick);
|
||||
strcat(handshake,buf);
|
||||
}
|
||||
char peers[DEFAULT_BUFLEN] = "&peers=";
|
||||
for (int i = 0; i < *peerCount; ++i) {
|
||||
strcat(peers,peerList[i].peerData.ip);
|
||||
for (int i = 0; i < peerList->length; ++i) {
|
||||
strcat(peers,peerList->array[i].peerData.ip);
|
||||
strcat(peers,":");
|
||||
|
||||
char port[10];
|
||||
sprintf(port, "%d", peerList[i].peerData.port);
|
||||
sprintf(port, "%d", peerList->array[i].peerData.port);
|
||||
|
||||
strcat(peers,port);
|
||||
strcat(peers,",");
|
||||
|
@ -193,25 +193,31 @@ int peer_HandleConnection(SOCKET listening,Peer peerList[],int* peerCount, node_
|
|||
p.peerData = node;
|
||||
p.socket = sock;
|
||||
p.sockaddr = client;
|
||||
peerList = peer_addTolist(peerList,peerCount,p);
|
||||
peer_addTolist(peerList,p);
|
||||
FD_SET(sock,fdSet);
|
||||
char*msg = (char*) calloc(sizeof(char)*129, sizeof(char));
|
||||
sprintf(msg,"Peer successfully connected from %s with id %s",inet_ntoa(client.sin_addr),node.id);
|
||||
logger_log(msg);
|
||||
|
||||
logger_log("Peer successfully connected from %s with id %s",inet_ntoa(client.sin_addr),node.id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool peer_isFoundInList(struct peer list[],int peerCount,char* id){
|
||||
for(int i=0;i < peerCount;++i){
|
||||
if(strcmp(list[i].peerData.id,id)==0) {
|
||||
void peer_initList(peerList *list){
|
||||
list->size = 0;
|
||||
list->length = 0;
|
||||
list->array = 0;
|
||||
}
|
||||
|
||||
|
||||
bool peer_isFoundInList(peerList list,char* id){
|
||||
for(int i=0;i < list.length;++i){
|
||||
if(strcmp(list.array[i].peerData.id,id)==0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool peer_isIPfoundInList(struct peer list[],int peerCount,char* ip,int port){
|
||||
for(int i=0;i < peerCount;++i){
|
||||
if(strcmp(list[i].peerData.ip,ip) == 0 && list[i].peerData.port == port) {
|
||||
bool peer_isIPfoundInList(struct peerList list,char* ip,int port){
|
||||
for(int i=0;i < list.length;++i){
|
||||
if(strcmp(list.array[i].peerData.ip,ip) == 0 && list.array[i].peerData.port == port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -219,27 +225,29 @@ bool peer_isIPfoundInList(struct peer list[],int peerCount,char* ip,int port){
|
|||
}
|
||||
|
||||
|
||||
Peer* peer_addTolist(struct peer list[],int *peerCount, struct peer p){
|
||||
Peer *peers = (Peer*) realloc(list,sizeof(Peer)*((*peerCount)+1));
|
||||
list[*peerCount] = p;
|
||||
*peerCount += 1;
|
||||
return peers;
|
||||
}
|
||||
Peer* peer_removeFromList(struct peer list[],int *peerCount, int i){
|
||||
Peer *peers = (Peer*) malloc(sizeof(Peer)*((*peerCount)-1));
|
||||
for (int k=0,j = 0; j < *peerCount; ++j) {
|
||||
if(j == i) continue;
|
||||
peers[k] = list[j];
|
||||
k++;
|
||||
void peer_addTolist(peerList *list, struct peer p){
|
||||
if (list->length >= list->size)
|
||||
{
|
||||
assert(list->length == list->size);
|
||||
size_t new_size = (list->size + 2) * 2;
|
||||
Peer *new_list = realloc(list->array, new_size * sizeof(Peer));
|
||||
if (new_list == 0)
|
||||
printf("OUT OF MEMORY!");
|
||||
list->size = new_size;
|
||||
list->array = new_list;
|
||||
}
|
||||
list->array[list->length++] = p;
|
||||
}
|
||||
void peer_removeFromList(struct peerList* list, int i){
|
||||
|
||||
*peerCount -=1;
|
||||
return peers;
|
||||
for (int k=i; k < list->length-1; ++k)
|
||||
list->array[k] =list->array[k+1];
|
||||
list->length--;
|
||||
}
|
||||
|
||||
int peer_getPeer(struct peer list[],int peerCount,SOCKET socket){
|
||||
for (int i = 0; i < peerCount; ++i) {
|
||||
if(list[i].socket == socket)
|
||||
int peer_getPeer(struct peerList list,SOCKET socket){
|
||||
for (int i = 0; i < list.length; ++i) {
|
||||
if(list.array[i].socket == socket)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
|
|
31
peer.h
31
peer.h
|
@ -1,18 +1,14 @@
|
|||
//
|
||||
// Created by Balazs Tolid on 2019. 10. 09..
|
||||
//
|
||||
#ifndef P2P_UTILITY_H
|
||||
#pragma once
|
||||
#include "utility.h"
|
||||
|
||||
#endif
|
||||
#ifndef P2P_PEER_H
|
||||
#define P2P_PEER_H
|
||||
#endif
|
||||
#define DEFAULT_MAX_PEER_COUNT 64
|
||||
|
||||
typedef struct Node_data {
|
||||
char ip[NI_MAXHOST];
|
||||
char id[MD5_DIGEST_LENGTH];
|
||||
char id[MD5_DIGEST_LENGTH+1];
|
||||
char nick[30];
|
||||
int port;
|
||||
} node_data;
|
||||
|
@ -23,19 +19,24 @@ typedef struct peer{
|
|||
struct sockaddr_in sockaddr;
|
||||
} Peer;
|
||||
|
||||
|
||||
//TODO: Create peerlist
|
||||
typedef struct peerList{
|
||||
size_t size; // A lista által lefoglalt hely
|
||||
size_t length; // A listán található elemek száma
|
||||
Peer * array; // A lista
|
||||
}peerList;
|
||||
|
||||
//Kouhai peer
|
||||
int peer_ConnetctTo(char* ip,int port,Peer peerList[],int *peerCount, node_data my,fd_set* fdSet);
|
||||
int peer_ConnetctTo(char* ip,int port,peerList* peerList, node_data my,fd_set* fdSet);
|
||||
//Senpai peer
|
||||
int peer_HandleConnection(SOCKET listening,Peer peerList[],int *peerCount, node_data my,fd_set* fdSet);
|
||||
int peer_HandleConnection(SOCKET listening,peerList* peerList, node_data my,fd_set* fdSet);
|
||||
|
||||
bool peer_isFoundInList(struct peer list[],int peerCount,char* id);
|
||||
bool peer_isIPfoundInList(struct peer list[],int peerCount,char* ip,int port);
|
||||
Peer* peer_addTolist(struct peer list[],int *peerCount, struct peer p);
|
||||
void peer_initList(peerList *list);
|
||||
|
||||
Peer* peer_removeFromList(struct peer list[],int *peerCount, int i);
|
||||
bool peer_isFoundInList(struct peerList list,char* id);
|
||||
bool peer_isIPfoundInList(struct peerList list,char* ip,int port);
|
||||
void peer_addTolist(struct peerList* list, struct peer p);
|
||||
|
||||
int peer_getPeer(struct peer list[],int peerCount,SOCKET socket);
|
||||
void peer_removeFromList(struct peerList *list, int i);
|
||||
|
||||
int peer_getPeer(struct peerList list,SOCKET socket);
|
||||
|
||||
|
|
17
test.c
17
test.c
|
@ -4,25 +4,10 @@
|
|||
//
|
||||
#include "utility.h"
|
||||
|
||||
char* webio_getFiletype(char* filename){
|
||||
char *type = (char*) malloc(10);
|
||||
char* tmp = strtok(filename,".");
|
||||
while(tmp != NULL){
|
||||
type =tmp;
|
||||
tmp = strtok(NULL,".");
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
int main(void){
|
||||
char contents[] = "The quick brown fox jumps over the lazy dog";
|
||||
static unsigned char buffer[33];
|
||||
|
||||
md5(contents, buffer);
|
||||
|
||||
printf("%s %d\n", buffer,strlen(buffer));
|
||||
char txt[] = "Hello.world.txt";
|
||||
printf("%s",webio_getFiletype(txt));
|
||||
printf("ok");
|
||||
return 0;
|
||||
}
|
|
@ -20,6 +20,7 @@ char* generateSeed(int len){
|
|||
return result;
|
||||
}
|
||||
map getHandshakeData(char* text){
|
||||
|
||||
map result;
|
||||
map_init(&result);
|
||||
if (text[0] == '@')
|
||||
|
@ -30,9 +31,8 @@ map getHandshakeData(char* text){
|
|||
count++;
|
||||
}
|
||||
int i =0;
|
||||
const char c[2] = "&";
|
||||
char *tmp;
|
||||
tmp = strtok (text,c);
|
||||
tmp = strtok (text,"&");
|
||||
|
||||
while (tmp != NULL && i <count+1)
|
||||
{
|
||||
|
|
|
@ -2,7 +2,9 @@
|
|||
// Készítette: Toldi Balázs Ádám
|
||||
// Dátum: 2019. 10. 11.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "modules/map.h"
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
@ -12,10 +14,10 @@
|
|||
#include <assert.h>
|
||||
#include <openssl/md5.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include "modules/map.h"
|
||||
|
||||
|
||||
#define DEFAULT_BUFLEN 1024
|
||||
|
||||
#define DEFAULT_BUFLEN 4096
|
||||
|
||||
char* generateSeed(int len);
|
||||
|
||||
|
|
Loading…
Reference in a new issue