Fixed some bugs
This commit is contained in:
parent
8ddfc5fbd0
commit
95fa44feb5
11 changed files with 387 additions and 164 deletions
|
@ -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 webio.h webio.c)
|
||||
add_executable(p2p-2 main.c peer.h peer.c utility.c utility.h webio.h webio.c)
|
||||
add_executable(p2p-3 main.c peer.h peer.c utility.c utility.h webio.h webio.c)
|
||||
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)
|
||||
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
|
||||
|
|
50
main.c
50
main.c
|
@ -1,7 +1,8 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "peer.h"
|
||||
#include "webio.h"
|
||||
#include "modules/webio/webio.h"
|
||||
|
||||
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
//#include "webinterface.h"
|
||||
|
@ -16,17 +17,17 @@
|
|||
|
||||
|
||||
int main(void) {
|
||||
logger_log("%s","Goood");
|
||||
FILE *seed_file;
|
||||
seed_file = fopen("seed.txt", "r");
|
||||
char seed[513];
|
||||
if (seed_file == NULL) {
|
||||
printf("Seed not found! Generating a new one...\n");
|
||||
logger_log("Seed not found! Generating a new one...");
|
||||
strcpy(seed, generateSeed(512));
|
||||
seed_file = fopen("seed.txt", "w");
|
||||
fprintf(seed_file, "%s", seed);
|
||||
|
||||
} else {
|
||||
printf("Seed found!\n");
|
||||
fgets(seed, 512, seed_file);
|
||||
}
|
||||
fclose(seed_file);
|
||||
|
@ -37,11 +38,11 @@ int main(void) {
|
|||
strcpy(mynode.nick, "");
|
||||
mynode.port = atoi(DEFAULT_PORT);
|
||||
|
||||
printf("Initialising core...\n");
|
||||
logger_log("Initialising core...");
|
||||
WSADATA ws;
|
||||
int res = WSAStartup(MAKEWORD(2, 2), &ws);
|
||||
if (res != 0) {
|
||||
printf("Error at startup! Error code: %d", WSAGetLastError());
|
||||
logger_log("Error at startup! Error code: %d", WSAGetLastError());
|
||||
WSACleanup();
|
||||
}
|
||||
|
||||
|
@ -61,7 +62,7 @@ int main(void) {
|
|||
//TODO: Use config to determine port
|
||||
res = getaddrinfo(NULL, DEFAULT_PORT, &hint, &result);
|
||||
if (res != 0) {
|
||||
printf("Error creating address information! Error code: %d", WSAGetLastError());
|
||||
logger_log("Error creating address information! Error code: %d", WSAGetLastError());
|
||||
WSACleanup();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
@ -69,7 +70,7 @@ int main(void) {
|
|||
//Creating listening socket
|
||||
SOCKET listening = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||
if (listening == INVALID_SOCKET) {
|
||||
printf("Error creating socket! Error: %d", WSAGetLastError());
|
||||
logger_log("Error creating socket! Error: %d", WSAGetLastError());
|
||||
WSACleanup();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
@ -77,7 +78,7 @@ int main(void) {
|
|||
//Binding the socket
|
||||
res = bind(listening, result->ai_addr, result->ai_addrlen);
|
||||
if (res == SOCKET_ERROR) {
|
||||
printf("Error binding socket! Error: %d", WSAGetLastError());
|
||||
logger_log("Error binding socket! Error: %d", WSAGetLastError());
|
||||
freeaddrinfo(result);
|
||||
WSACleanup();
|
||||
return EXIT_FAILURE;
|
||||
|
@ -88,7 +89,7 @@ int main(void) {
|
|||
//TODO: Set max connection count in config
|
||||
res = listen(listening, SOMAXCONN);
|
||||
if (res == -1) {
|
||||
printf("Error starting listening! Error: %d", WSAGetLastError());
|
||||
logger_log("Error starting listening! Error: %d", WSAGetLastError());
|
||||
closesocket(listening);
|
||||
WSACleanup();
|
||||
return EXIT_FAILURE;
|
||||
|
@ -97,26 +98,27 @@ int main(void) {
|
|||
struct sockaddr_in sin;
|
||||
socklen_t len = sizeof(sin);
|
||||
if (getsockname(listening, (struct sockaddr *) &sin, &len) == -1) {
|
||||
printf("Error at getsockname!Error code: %d", WSAGetLastError());
|
||||
logger_log("Error at getsockname!Error code: %d", WSAGetLastError());
|
||||
closesocket(listening);
|
||||
WSACleanup();
|
||||
return 1;
|
||||
}
|
||||
mynode.port = ntohs(sin.sin_port);
|
||||
printf("Started listening on port %d\n", mynode.port);
|
||||
|
||||
logger_log("Started listening on port %d", mynode.port);
|
||||
|
||||
fd_set master;
|
||||
FD_ZERO(&master);
|
||||
FD_SET(listening, &master);
|
||||
|
||||
//Connecting to peers
|
||||
printf("Checking peers.txt for peers...\n");
|
||||
logger_log("Checking peers.txt for peers...");
|
||||
Peer *peerList = (Peer *) malloc(sizeof(Peer) * DEFAULT_MAX_PEER_COUNT);
|
||||
int peerCount = 0;
|
||||
|
||||
FILE *peer_file;
|
||||
peer_file = fopen("peers.txt", "r");
|
||||
if (peer_file == NULL) {
|
||||
printf("peers.txt not found!\n");
|
||||
logger_log("peers.txt not found!");
|
||||
peer_file = fopen("peers.txt", "w");
|
||||
fprintf(peer_file, "");
|
||||
|
||||
|
@ -125,20 +127,24 @@ int main(void) {
|
|||
int port;
|
||||
while (fscanf(peer_file, "%[^:]:%d", ip, &port) != EOF) {
|
||||
if(peer_ConnetctTo(ip, port, peerList, &peerCount, mynode,&master) != 0)
|
||||
printf("Error while connecting to peer...\n");
|
||||
logger_log("Error while connecting to peer...");
|
||||
}
|
||||
|
||||
}
|
||||
fclose(peer_file);
|
||||
WebIO webIo;
|
||||
webio_create(atoi(DEFAULT_INTERFACE_PORT),DEFAULT_WWW_FOLDER,&webIo);
|
||||
webio_create(atoi(DEFAULT_INTERFACE_PORT),DEFAULT_WWW_FOLDER,mynode,&webIo);
|
||||
FD_SET(webIo.socket,&master);
|
||||
printf("Started web interface at http://127.0.0.1:%d\n",ntohs(webIo.sockaddr.sin_port));
|
||||
|
||||
logger_log("Started web interface at http://127.0.0.1:%d",ntohs(webIo.sockaddr.sin_port));
|
||||
|
||||
|
||||
char *command =(char*) malloc(64);
|
||||
sprintf(command,"start http://127.0.0.1:%d",ntohs(webIo.sockaddr.sin_port));
|
||||
//system(command);
|
||||
system(command);
|
||||
free(command);
|
||||
|
||||
printf("Starting main loop...\n");
|
||||
logger_log("Starting main loop...");
|
||||
while (1) {
|
||||
fd_set copy = master;
|
||||
int count = select(0, ©, NULL, NULL, NULL);
|
||||
|
@ -147,9 +153,9 @@ int main(void) {
|
|||
SOCKET sock = copy.fd_array[i];
|
||||
if (sock == listening) {
|
||||
if(peer_HandleConnection(listening, peerList, &peerCount, mynode,&master) != 0)
|
||||
printf("Error while receiving connection...\n");
|
||||
logger_log("Error while receiving connection...");
|
||||
}else if(sock == webIo.socket ){
|
||||
webio_handleRequest(webIo);
|
||||
webio_handleRequest(webIo,peerList,peerCount);
|
||||
} else {
|
||||
char buf[DEFAULT_BUFLEN];
|
||||
ZeroMemory(buf, DEFAULT_BUFLEN);
|
||||
|
@ -158,7 +164,7 @@ int main(void) {
|
|||
//Peer disconnect
|
||||
int k = peer_getPeer(peerList, peerCount, sock);
|
||||
if (k != -1) {
|
||||
printf("Peer disconnected(%s->%s)\n", inet_ntoa(peerList[k].sockaddr.sin_addr),peerList[k].peerData.id);
|
||||
logger_log("Peer disconnected(%s->%s)", inet_ntoa(peerList[k].sockaddr.sin_addr),peerList[k].peerData.id);
|
||||
peer_removeFromList(peerList, &peerCount, k);
|
||||
closesocket(sock);
|
||||
FD_CLR(sock, &master);
|
||||
|
|
5
modules/config.c
Normal file
5
modules/config.c
Normal file
|
@ -0,0 +1,5 @@
|
|||
//
|
||||
// Készítette: Toldi Balázs Ádám
|
||||
// Dátum: 2019. 10. 16.
|
||||
//
|
||||
|
13
modules/config.h
Normal file
13
modules/config.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
//
|
||||
// Készítette: Toldi Balázs Ádám
|
||||
// Dátum: 2019. 10. 16.
|
||||
//
|
||||
|
||||
#ifndef P2P_CONFIG_H
|
||||
#define P2P_CONFIG_H
|
||||
|
||||
#endif //P2P_CONFIG_H
|
||||
#ifndef P2P_UTILITY_H
|
||||
|
||||
#include "../utility.h"
|
||||
#endif
|
257
modules/webio/webio.c
Normal file
257
modules/webio/webio.c
Normal file
|
@ -0,0 +1,257 @@
|
|||
//
|
||||
// Készítette: Toldi Balázs Ádám
|
||||
// Dátum: 2019. 10. 14..
|
||||
//
|
||||
#include "webio.h"
|
||||
|
||||
#ifdef RANDOM_PORT
|
||||
#define DEFAULT_INTERFACE_PORT "0"
|
||||
#else
|
||||
#define DEFAULT_INTERFACE_PORT "5081"
|
||||
#endif
|
||||
|
||||
|
||||
int webio_create(int port,char* folder,struct Node_data myData,WebIO *webIo){
|
||||
struct addrinfo hint = {};
|
||||
struct addrinfo *result = NULL;
|
||||
WebIO wio;
|
||||
memset(&hint, 0, sizeof(struct addrinfo));
|
||||
hint.ai_protocol = IPPROTO_TCP;
|
||||
hint.ai_socktype = SOCK_STREAM;
|
||||
hint.ai_family = AF_INET;
|
||||
//TODO: Disable this in local modeó
|
||||
hint.ai_canonname = NULL;
|
||||
hint.ai_addr = NULL;
|
||||
hint.ai_next = NULL;
|
||||
|
||||
//TODO: Use config to determine port
|
||||
int res = getaddrinfo(NULL, DEFAULT_INTERFACE_PORT, &hint, &result);
|
||||
if (res != 0) {
|
||||
char error[129];
|
||||
sprintf(error,"Error creating address information!Error code: %d", WSAGetLastError());
|
||||
logger_log(error);
|
||||
WSACleanup();
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Creating listening socket
|
||||
SOCKET listening = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||
if (listening == INVALID_SOCKET) {
|
||||
char error[129];
|
||||
sprintf(error,"Error creating socket!Error code: %d", WSAGetLastError());
|
||||
logger_log(error);
|
||||
WSACleanup();
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Binding the socket
|
||||
res = bind(listening, result->ai_addr, result->ai_addrlen);
|
||||
if (res == SOCKET_ERROR) {
|
||||
char error[129];
|
||||
sprintf(error,"Error at binding!Error code: %d", WSAGetLastError());
|
||||
logger_log(error);
|
||||
freeaddrinfo(result);
|
||||
WSACleanup();
|
||||
return -1;
|
||||
}
|
||||
|
||||
freeaddrinfo(result);
|
||||
|
||||
//TODO: Set max connection count in config
|
||||
res = listen(listening, SOMAXCONN);
|
||||
if (res == -1) {
|
||||
char error[129];
|
||||
sprintf(error,"Error at starting listening!Error code: %d", WSAGetLastError());
|
||||
logger_log(error);
|
||||
WSACleanup();
|
||||
return -1;
|
||||
}
|
||||
//Ez alapvetően akkor hasznos amikor a port 0-ra van állítva, azaz akkor amikor a rendszer random választ egyet.
|
||||
struct sockaddr_in sin;
|
||||
socklen_t len = sizeof(sin);
|
||||
if (getsockname(listening, (struct sockaddr *) &sin, &len) == -1) {
|
||||
char error[129];
|
||||
sprintf(error,"Error at getsockname!Error code: %d", WSAGetLastError());
|
||||
logger_log(error);
|
||||
return -1;
|
||||
}
|
||||
wio.sockaddr = sin;
|
||||
wio.socket= listening;
|
||||
wio.nodeData = myData;
|
||||
strcpy(wio.folder,folder);
|
||||
*webIo = wio;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int webio_handleRequest(WebIO wio,Peer list[],int peerCount){
|
||||
SOCKET client = accept(wio.socket,NULL,NULL);
|
||||
char buf[8192];
|
||||
int res = recv(client,buf,8192,0);
|
||||
if(res <=0){
|
||||
logger_log("Error with web interface!");
|
||||
closesocket(client);
|
||||
return -1;
|
||||
}
|
||||
char req[10];
|
||||
sscanf(buf,"%s",req);
|
||||
|
||||
if(strcmp(req,"GET") == 0) {
|
||||
char file[50];
|
||||
sscanf(buf, "%*s %s", file);
|
||||
res = webio_handleGETrequest(client,wio,file, list, peerCount);
|
||||
}else if(strcmp(req,"POST") == 0)
|
||||
res = webio_handlePOSTrequest(client,wio, list,peerCount);
|
||||
else
|
||||
res = -1;
|
||||
return res;
|
||||
|
||||
}
|
||||
char* webio_getMIMEtype(char* filename){
|
||||
char* ext = webio_getFiletype(filename);
|
||||
if (ext == "html")
|
||||
return "text/html";
|
||||
else if (ext == "json")
|
||||
return "application/json";
|
||||
else if (ext == "js")
|
||||
return "application/javascript";
|
||||
else if (ext == "css")
|
||||
return "text/css";
|
||||
else if (ext == "jpg")
|
||||
return "image/jpeg";
|
||||
else if (ext == "png")
|
||||
return "image/png";
|
||||
else if (ext == "ico")
|
||||
return "image/ico";
|
||||
else if (ext == "xml")
|
||||
return "text/xml";
|
||||
else if (ext == "zip")
|
||||
return "application/zip";
|
||||
else return "text/plain";
|
||||
}
|
||||
|
||||
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 webio_handleGETrequest(SOCKET client,WebIO wio,char* file,Peer list[],int peerCount){
|
||||
|
||||
char buf[8192];
|
||||
sscanf(buf,"%*s %s",file);
|
||||
|
||||
char path[129];
|
||||
|
||||
ZeroMemory(path,sizeof(path));
|
||||
strcat(path,wio.folder);
|
||||
char *response = (char *) malloc(sizeof(char) * 8192);
|
||||
if(strcmp(file,"/") == 0) {
|
||||
strcat(response, "HTTP/1.1 200 OK "
|
||||
"Content-Encoding: gzip\r\n"
|
||||
"Content-Language: en\r\n"
|
||||
"Content-Type: text/html\r\n\r\n"
|
||||
);
|
||||
strcat(response,getIndex(wio.folder,list,peerCount));
|
||||
}else {
|
||||
strcat(path, file);
|
||||
|
||||
FILE *fp;
|
||||
fp = fopen(path, "r");
|
||||
|
||||
if (fp != NULL) {
|
||||
char *content = (char *) malloc(sizeof(char) * 16384);
|
||||
ZeroMemory(content, 16384);
|
||||
char buf[4096];
|
||||
int counter = 0;
|
||||
//Ez így működik, de nagyon lassú.
|
||||
//TODO: Ki kéne ezt javítani
|
||||
while (fgets(buf, 4096, fp) != NULL) {
|
||||
int len = strlen(content);
|
||||
strcat(content, buf);
|
||||
counter += 4096;
|
||||
if (counter >= len) {
|
||||
content = realloc(content, counter + 16384);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
response = (char *) malloc(sizeof(char) * counter + 1024);
|
||||
ZeroMemory(response, counter);
|
||||
strcat(response, "HTTP/1.1 200 OK "
|
||||
"Content-Encoding: gzip\r\n"
|
||||
"Content-Language: en\r\n"
|
||||
"Content-Type: text/html\r\n\r\n"
|
||||
);
|
||||
|
||||
strcat(response, content);
|
||||
} else {
|
||||
|
||||
strcat(response, "HTTP/1.1 404 Not Found "
|
||||
"Content-Encoding: gzip\r\n"
|
||||
"Content-Language: en\r\n"
|
||||
"Content-Type: text/html\r\n\r\n"
|
||||
"<h1>Error 404 File not found!</h1>");
|
||||
}
|
||||
}
|
||||
int res = send(client,response,strlen(response),0);
|
||||
if(res == SOCKET_ERROR){
|
||||
logger_log("Sending failed!");
|
||||
return -1;
|
||||
}
|
||||
shutdown(client,SD_SEND);
|
||||
closesocket(client);
|
||||
}
|
||||
|
||||
int webio_handlePOSTrequest(SOCKET client,WebIO wio,Peer list[],int peerCount){
|
||||
|
||||
}
|
||||
char* webio_getHeader(char* folder) {
|
||||
FILE *fp;
|
||||
char *path = (char*) calloc(65,1);
|
||||
strcat(path, folder);
|
||||
strcat(path, "/header.html");
|
||||
|
||||
fp = fopen(path, "r");
|
||||
|
||||
|
||||
if (fp != NULL) {
|
||||
char *content = (char *) calloc(sizeof(char) * 8192,1);
|
||||
char buf[1024];
|
||||
while (fgets(buf, 1024, fp) != NULL) {
|
||||
strcat(content, buf);
|
||||
// printf("%s",buf);
|
||||
}
|
||||
fclose(fp);
|
||||
return content;
|
||||
}else return "<html>";
|
||||
}
|
||||
|
||||
char* getIndex(char* folder,Peer list[],int count){
|
||||
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) {
|
||||
strcat(content, "<ul>\n");
|
||||
for (int i = 0; i < count; ++i) {
|
||||
strcat(content, "<li>");
|
||||
strcat(content, list[i].peerData.id);
|
||||
strcat(content, "</li>\n");
|
||||
}
|
||||
strcat(content, "</ul>\n");
|
||||
}else
|
||||
strcat(content,"<div class=\"alert alert-warning\" role=\"alert\">\n"
|
||||
" No peers connected!\n"
|
||||
"</div>\n");
|
||||
strcat(content,"<script>setTimeout(function(){\n"
|
||||
" window.location.reload(1);\n"
|
||||
"}, 5000);</script>\n");
|
||||
|
||||
strcat(content,"</div>\n");
|
||||
strcat(content,"</body>\n");
|
||||
strcat(content,"</html>\n");
|
||||
return content;
|
||||
}
|
37
modules/webio/webio.h
Normal file
37
modules/webio/webio.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
//
|
||||
// Készítette: Toldi Balázs Ádám
|
||||
// Dátum: 2019. 10. 14..
|
||||
//
|
||||
#ifndef P2P_UTILITY_H
|
||||
#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;
|
||||
struct sockaddr_in sockaddr;
|
||||
char folder[65];
|
||||
struct Node_data nodeData;
|
||||
} 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_handleGETrequest(SOCKET client,WebIO wio,char * file,Peer list[],int peerCount);
|
||||
|
||||
int webio_handlePOSTrequest(SOCKET client,WebIO wio,Peer list[],int peerCount);
|
||||
|
||||
char* webio_getMIMEtype(char* filename);
|
||||
|
||||
char* webio_getFiletype(char* filename);
|
||||
|
||||
char* webio_getHeader(char* folder);
|
||||
|
||||
char* getIndex(char* folder,Peer list[],int count);
|
53
peer.c
53
peer.c
|
@ -7,7 +7,7 @@
|
|||
//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){
|
||||
printf("Couldn't connect,because the max peer count is reached!\n");
|
||||
logger_log("Couldn't connect,because the max peer count is reached!");
|
||||
return -1;
|
||||
}
|
||||
struct sockaddr_in hint;
|
||||
|
@ -23,27 +23,27 @@ int peer_ConnetctTo(char* ip,int port,Peer peerList[],int* peerCount, node_data
|
|||
if (res == SOCKET_ERROR) {
|
||||
return -1;
|
||||
}
|
||||
printf("Connected to peer!Sending handshake...\n");
|
||||
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);
|
||||
res = send(sock,handshake,strlen(handshake),0);
|
||||
if (res == SOCKET_ERROR) {
|
||||
printf("Error sending peer list!Disconnecting...\n" );
|
||||
logger_log("Error sending peer list!Disconnecting..." );
|
||||
closesocket(sock);
|
||||
return -1 ;
|
||||
}
|
||||
printf("Sent!Waiting for response...\n");
|
||||
logger_log("Sent!Waiting for response...");
|
||||
char buf[DEFAULT_BUFLEN];
|
||||
int inBytes = recv(sock, buf, DEFAULT_BUFLEN, 0);
|
||||
if (inBytes <= 0) {
|
||||
printf("Error: Invalid response!\n");
|
||||
logger_log("Error: Invalid response!");
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
if(buf[0] != '@'){
|
||||
printf("Error: Invalid response!\n");
|
||||
logger_log("Error: Invalid response!");
|
||||
return -1;
|
||||
}
|
||||
int len;
|
||||
|
@ -52,7 +52,9 @@ int peer_ConnetctTo(char* ip,int port,Peer peerList[],int* peerCount, node_data
|
|||
strcpy(node.ip,ip);
|
||||
|
||||
if(map_isFound(m,len,"valid") && strcmp("false", map_getValue(m, len, "valid")) == 0) {
|
||||
printf("Peer closed connection! Error: %s\n",map_getValue(m,len,"error"));
|
||||
char error[129];
|
||||
sprintf(error,"Peer closed connection! Error: %s\n",map_getValue(m,len,"error"));
|
||||
logger_log(error);
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
|
@ -61,14 +63,14 @@ int peer_ConnetctTo(char* ip,int port,Peer peerList[],int* peerCount, node_data
|
|||
if(map_isFound(m,len,"id")) {
|
||||
strcpy(node.id, map_getValue(m, len, "id"));
|
||||
} else {
|
||||
printf("Error: Invalid response!ID not found in handshake.\n");
|
||||
return -1;
|
||||
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"));
|
||||
} else {
|
||||
printf("Error: Invalid response!Port not found in handshake.\n");
|
||||
return -1;
|
||||
logger_log("Error: Invalid response!Port not found in handshake.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(map_isFound(m,len,"nickname")) {
|
||||
|
@ -85,7 +87,6 @@ int peer_ConnetctTo(char* ip,int port,Peer peerList[],int* peerCount, node_data
|
|||
if(map_isFound(m,len,"peers")) {
|
||||
char* tmp = strtok(map_getValue(m,len,"peers"),",");
|
||||
while(tmp != NULL){
|
||||
printf("%s\t%d\n",tmp,*peerCount);
|
||||
char ip[NI_MAXHOST];
|
||||
int port;
|
||||
sscanf(tmp, "%[^:]:%d", ip, &port);
|
||||
|
@ -94,7 +95,9 @@ int peer_ConnetctTo(char* ip,int port,Peer peerList[],int* peerCount, node_data
|
|||
tmp = strtok(NULL,",");
|
||||
}
|
||||
}
|
||||
printf("Peer validated (%s->%s)!\n",node.ip,node.id);
|
||||
char*msg = (char*) calloc(sizeof(char)*129, sizeof(char));
|
||||
sprintf(msg,"Peer validated (%s->%s)!",node.ip,node.id);
|
||||
logger_log(msg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -127,18 +130,19 @@ int peer_HandleConnection(SOCKET listening,Peer peerList[],int* peerCount, node_
|
|||
|
||||
int len;
|
||||
map *m = getHandshakeData(buf,&len);
|
||||
|
||||
node_data node;
|
||||
strcpy(node.ip,ip);
|
||||
if(map_isFound(m,len,"id")) {
|
||||
strcpy(node.id, map_getValue(m, len, "id"));
|
||||
} else {
|
||||
printf("Error: Invalid response!ID not found in handshake.\n");
|
||||
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"));
|
||||
} else {
|
||||
printf("Error: Invalid response!Port not found in handshake.\n");
|
||||
logger_log("Error: Invalid response!Port not found in handshake.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -146,18 +150,18 @@ int peer_HandleConnection(SOCKET listening,Peer peerList[],int* peerCount, node_
|
|||
strcpy(node.nick, map_getValue(m, len, "nickname"));
|
||||
}
|
||||
if(peer_isFoundInList(peerList,*peerCount,node.id)){
|
||||
printf("Handshake received, but the id sent is taken! Dropping peer...\n");
|
||||
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);
|
||||
if (res == SOCKET_ERROR) {
|
||||
printf("Error sending error message!Disconnecting...\n");
|
||||
logger_log("Error sending error message!Disconnecting...");
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
printf("Handshake recived! Sending response!\n");
|
||||
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);
|
||||
|
@ -181,7 +185,7 @@ int peer_HandleConnection(SOCKET listening,Peer peerList[],int* peerCount, node_
|
|||
}
|
||||
int res = send(sock, handshake, strlen(handshake), 0);
|
||||
if (res == SOCKET_ERROR) {
|
||||
printf("Error sending handshake!Disconnecting...\n");
|
||||
logger_log("Error sending handshake!Disconnecting...");
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
|
@ -191,7 +195,9 @@ int peer_HandleConnection(SOCKET listening,Peer peerList[],int* peerCount, node_
|
|||
p.sockaddr = client;
|
||||
peerList = peer_addTolist(peerList,peerCount,p);
|
||||
FD_SET(sock,fdSet);
|
||||
printf("Peer successfully connected from %s whit id %s\n",inet_ntoa(client.sin_addr),node.id);
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -205,7 +211,7 @@ bool peer_isFoundInList(struct peer list[],int peerCount,char* id){
|
|||
}
|
||||
bool peer_isIPfoundInList(struct peer list[],int peerCount,char* ip,int port){
|
||||
for(int i=0;i < peerCount;++i){
|
||||
if(list[i].peerData.ip == ip && list[i].peerData.port == port) {
|
||||
if(strcmp(list[i].peerData.ip,ip) == 0 && list[i].peerData.port == port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -214,10 +220,7 @@ 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*) malloc(sizeof(Peer)*((*peerCount)+1));
|
||||
for (int j = 0; j < *peerCount; ++j) {
|
||||
peers[j] = list[j];
|
||||
}
|
||||
Peer *peers = (Peer*) realloc(list,sizeof(Peer)*((*peerCount)+1));
|
||||
list[*peerCount] = p;
|
||||
*peerCount += 1;
|
||||
return peers;
|
||||
|
|
5
peer.h
5
peer.h
|
@ -1,8 +1,13 @@
|
|||
//
|
||||
// Created by Balazs Tolid on 2019. 10. 09..
|
||||
//
|
||||
#ifndef P2P_UTILITY_H
|
||||
#include "utility.h"
|
||||
|
||||
#endif
|
||||
#ifndef P2P_PEER_H
|
||||
#define P2P_PEER_H
|
||||
#endif
|
||||
#define DEFAULT_MAX_PEER_COUNT 64
|
||||
|
||||
typedef struct Node_data {
|
||||
|
|
11
test.c
11
test.c
|
@ -4,6 +4,15 @@
|
|||
//
|
||||
#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){
|
||||
|
@ -13,5 +22,7 @@ int main(void){
|
|||
md5(contents, buffer);
|
||||
|
||||
printf("%s %d\n", buffer,strlen(buffer));
|
||||
char txt[] = "Hello.world.txt";
|
||||
printf("%s",webio_getFiletype(txt));
|
||||
return 0;
|
||||
}
|
98
webio.c
98
webio.c
|
@ -1,98 +0,0 @@
|
|||
//
|
||||
// Készítette: Toldi Balázs Ádám
|
||||
// Dátum: 2019. 10. 14..
|
||||
//
|
||||
#include "webio.h"
|
||||
|
||||
#ifdef RANDOM_PORT;
|
||||
#define DEFAULT_INTERFACE_PORT "0"
|
||||
#else
|
||||
#define DEFAULT_INTERFACE_PORT "5081"
|
||||
#endif
|
||||
|
||||
|
||||
int webio_create(int port,char* folder,WebIO *webIo){
|
||||
struct addrinfo hint = {};
|
||||
struct addrinfo *result = NULL;
|
||||
WebIO wio;
|
||||
memset(&hint, 0, sizeof(struct addrinfo));
|
||||
hint.ai_protocol = IPPROTO_TCP;
|
||||
hint.ai_socktype = SOCK_STREAM;
|
||||
hint.ai_family = AF_INET;
|
||||
//TODO: Disable this in local modeó
|
||||
hint.ai_canonname = NULL;
|
||||
hint.ai_addr = NULL;
|
||||
hint.ai_next = NULL;
|
||||
|
||||
//TODO: Use config to determine port
|
||||
int res = getaddrinfo(NULL, DEFAULT_INTERFACE_PORT, &hint, &result);
|
||||
if (res != 0) {
|
||||
printf("Error creating address information! Error code: %d", WSAGetLastError());
|
||||
WSACleanup();
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Creating listening socket
|
||||
SOCKET listening = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||
if (listening == INVALID_SOCKET) {
|
||||
printf("Error creating socket! Error: %d", WSAGetLastError());
|
||||
WSACleanup();
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Binding the socket
|
||||
res = bind(listening, result->ai_addr, result->ai_addrlen);
|
||||
if (res == SOCKET_ERROR) {
|
||||
printf("Error binding socket! Error: %d", WSAGetLastError());
|
||||
freeaddrinfo(result);
|
||||
WSACleanup();
|
||||
return -1;
|
||||
}
|
||||
|
||||
freeaddrinfo(result);
|
||||
|
||||
//TODO: Set max connection count in config
|
||||
res = listen(listening, SOMAXCONN);
|
||||
if (res == -1) {
|
||||
printf("Error starting listening! Error: %d", WSAGetLastError());
|
||||
closesocket(listening);
|
||||
WSACleanup();
|
||||
return -1;
|
||||
}
|
||||
//Ez alapvetően akkor hasznos amikor a port 0-ra van állítva, azaz akkor amikor a rendszer random választ egyet.
|
||||
struct sockaddr_in sin;
|
||||
socklen_t len = sizeof(sin);
|
||||
if (getsockname(listening, (struct sockaddr *) &sin, &len) == -1) {
|
||||
printf("Error at getsockname!Error code: %d", WSAGetLastError());
|
||||
return -1;
|
||||
}
|
||||
wio.sockaddr = sin;
|
||||
wio.socket= listening;
|
||||
strcpy(wio.folder,folder);
|
||||
*webIo = wio;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int webio_handleRequest(WebIO wio){
|
||||
SOCKET client = accept(wio.socket,NULL,NULL);
|
||||
char buf[8192];
|
||||
int res = recv(client,buf,8192,0);
|
||||
if(res <=0){
|
||||
printf("Error with web interface!\n");
|
||||
closesocket(client);
|
||||
return -1;
|
||||
}
|
||||
//printf("%s\n",buf);
|
||||
char response[] ="HTTP/1.1 200 OK "
|
||||
"Content-Encoding: gzip\r\n"
|
||||
"Content-Language: en\r\n"
|
||||
"Content-Type: text/html\r\n\r\n"
|
||||
"<h1>Hello there!</h1>";
|
||||
res = send(client,response,strlen(response),0);
|
||||
if(res == SOCKET_ERROR){
|
||||
printf("Sending failed!");
|
||||
return -1;
|
||||
}
|
||||
shutdown(client,SD_SEND);
|
||||
|
||||
}
|
16
webio.h
16
webio.h
|
@ -1,16 +0,0 @@
|
|||
//
|
||||
// Készítette: Toldi Balázs Ádám
|
||||
// Dátum: 2019. 10. 14..
|
||||
//
|
||||
#ifndef P2P_UTILITY_H
|
||||
#include "utility.h"
|
||||
#endif
|
||||
typedef struct webio{
|
||||
SOCKET socket;
|
||||
struct sockaddr_in sockaddr;
|
||||
char folder[65];
|
||||
} WebIO;
|
||||
|
||||
int webio_create(int port,char* folder,WebIO *webIo);
|
||||
|
||||
int webio_handleRequest(WebIO wio);
|
Loading…
Reference in a new issue