From 638a12f96b1d9d0b9300ea26005c9d531cb58f49 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Fri, 15 Nov 2019 22:42:20 +0100 Subject: [PATCH] Added offline messages to home page --- README.md | 1 + lib/net.h | 8 ++++- modules/peer.c | 2 +- modules/server.c | 3 +- modules/webio.c | 86 ++++++++++++++++++++++++++++++++++++------------ modules/webio.h | 9 ++--- test.c | 13 +++++--- 7 files changed, 87 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 1d8a7e7..7bb8ae2 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # P2P-chat [![Build Status](https://travis-ci.com/Bazsalanszky/p2p-chat.svg?branch=master)](https://travis-ci.com/Bazsalanszky/p2p-chat) + A projektem célja (ahogy a neve is mondja) egy Peer to Peer chat alkalmazás létrehozása. A lénye az, hogy több gép egy hálózatot alkotva kommunikál egymással,azaz nincs egy központi szerver,ami az üzeneteket továbbítja a klienseknek,hanem minden üzenet közvetlenül megy a feladótól a címzettig. diff --git a/lib/net.h b/lib/net.h index d5677f4..a2bd5f8 100644 --- a/lib/net.h +++ b/lib/net.h @@ -3,7 +3,7 @@ // Datum: 2019. 11. 05. // #pragma once -#if defined(__linux__) || defined(__CYGWIN__) +#if defined(__linux__) && !defined(__CYGWIN__) #include #include #include @@ -17,6 +17,7 @@ //Mappákhoz #include #include +#include //Winsock2 kompatiblitás érdekében #ifndef SOCKET_ERROR #define SOCKET_ERROR -1 @@ -39,6 +40,11 @@ typedef int SOCKET; #elif defined(WIN32) || defined(__CYGWIN__) + +#ifdef __MINGW32__ +#include +#endif + #include #include #pragma comment(lib, "ws2_32.lib") diff --git a/modules/peer.c b/modules/peer.c index b5cf8f1..b414b8b 100644 --- a/modules/peer.c +++ b/modules/peer.c @@ -104,7 +104,7 @@ int peer_ConnetctTo(char* ip, int port, PeerList* peerList, Node_data my, fd_set } } //free(m.pairs); - //logger_log("Peer validated (%s->%s)!",inet_ntoa(hint.sin_addr),node.id); + logger_log("Peer validated (%s->%s)!",inet_ntoa(hint.sin_addr),node.id); return 0; } diff --git a/modules/server.c b/modules/server.c index 4482ec8..7a2b30c 100644 --- a/modules/server.c +++ b/modules/server.c @@ -1,5 +1,6 @@ // -// Created by balazs on 2019. 11. 07.. +// Készítette: Toldi Balázs Ádám +// Dátum: 2019. 11. 07. // #include "server.h" diff --git a/modules/webio.c b/modules/webio.c index c0e8e82..5c6c7c5 100644 --- a/modules/webio.c +++ b/modules/webio.c @@ -3,6 +3,7 @@ // Dátum: 2019. 10. 14.. // #include "webio.h" + int webio_create(Config config, struct Node_data myData, WebIO *webIo) { char *port = map_getValue(config, "interface-port"); if (port == NULL) @@ -46,13 +47,13 @@ int webio_handleRequest(WebIO wio, const PeerList *list) { return -1; } - if (strncmp(buf, "GET",3) == 0) { + if (strncmp(buf, "GET", 3) == 0) { char file[50]; sscanf(buf, "%*s %s", file); res = webio_handleGETrequest(client, wio, file, list); - } else if (strncmp(buf, "POST",4) == 0) { - int i =(int) strlen(buf) - 1; + } else if (strncmp(buf, "POST", 4) == 0) { + int i = (int) strlen(buf) - 1; while (buf[i] != '\n') { i--; } @@ -134,21 +135,20 @@ static int webio_handleGETrequest(SOCKET client, WebIO wio, char *file, const Pe strcat(path, file); //File küldés windows-on #ifdef _WIN32 - HANDLE file = CreateFileA(path,GENERIC_READ,FILE_SHARE_READ, + HANDLE file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - DWORD size = GetFileSize(file,NULL); - if(file == INVALID_HANDLE_VALUE || size< 0) + DWORD size = GetFileSize(file, NULL); + if (file == INVALID_HANDLE_VALUE || size < 0) webio_send404Page(client); else { - webio_sendOKHeader_wSize(client,path,size); + webio_sendOKHeader_wSize(client, path, size); TransmitFile(client, file, 0, 0, NULL, NULL, 0); CloseHandle(file); } #else - logger_log(path); int fd = open(path, O_RDONLY); if (fd == -1) webio_send404Page(client); @@ -167,17 +167,17 @@ static int webio_handleGETrequest(SOCKET client, WebIO wio, char *file, const Pe } } #endif - shutdown(client,SD_BOTH); + shutdown(client, SD_BOTH); } closesocket(client); - return 0; + return 0; } static int webio_handlePOSTrequest(SOCKET client, WebIO wio, const PeerList *list, Map post) { shutdown(client, SD_RECEIVE); char *response = "HTTP/1.1 304 Not Modified "; - int res = send(client, response,(int) strlen(response), 0); + int res = send(client, response, (int) strlen(response), 0); if (res == SOCKET_ERROR) { logger_log("Error with io"); return -1; @@ -214,7 +214,7 @@ static int webio_handlePOSTrequest(SOCKET client, WebIO wio, const PeerList *lis } logger_log("Message sent to %s", map_getValue(post, "id")); } else map_dump(post); - return 0; + return 0; } static void webio_getHeader(char *folder, char result[]) { @@ -251,8 +251,7 @@ static void webio_getIndex(char *folder, const PeerList *list, char *outputBuffe "", content, list->array[i].peerData.id); if (strcmp(list->array[i].peerData.nick, "") != 0) { - sprintf(content, "%s%s %d- ", content, list->array[i].peerData.nick, - (int) strlen(list->array[i].peerData.nick)); + sprintf(content, "%s%s - ", content, list->array[i].peerData.nick); } sprintf(content, "%s%s\n", content, list->array[i].peerData.id); } @@ -261,6 +260,50 @@ static void webio_getIndex(char *folder, const PeerList *list, char *outputBuffe sprintf(content, "%s
\n" " No peers connected!\n" "
\n", content); + strcat(content, "

Offline messages:

\n"); + char path[65]; + sprintf(path, "%s/peers/", folder); +#ifdef _DIRENT_H_ + DIR *d; + + d = opendir(path); + if (d != NULL){ + strcat(content, "
    \n"); + struct dirent *de; + while ((de = readdir(d)) != NULL) { + if(strcmp(de->d_name,".") == 0 || strcmp(de->d_name,"..") == 0) continue; + char peer[33]; + sscanf(de->d_name,"%[^.]",peer); + sprintf(content,"%s
  • %s
  • ",content, peer, peer); + } + closedir(d); + strcat(content, "
\n"); + }else{ + sprintf(content, "%s
\n" + " No offline messages!\n" + "
\n", content); + } +#else + HANDLE dir; + WIN32_FIND_DATA file_data; + strcat(path,"/*"); + if ((dir = FindFirstFile(path, &file_data)) == INVALID_HANDLE_VALUE) + sprintf(content, "%s
\n" + " No offline messages!\n" + "
\n", content); + else{ + strcat(content, "
    \n"); + do{ + if(strcmp(file_data.cFileName,".") == 0 || strcmp(file_data.cFileName,"..") == 0) continue; + char peer[33]; + sscanf(file_data.cFileName,"%[^.]",peer); + sprintf(content,"%s
  • %s
  • ",content, peer, peer); + }while(FindNextFile(dir,&file_data)); + FindClose(dir); + } +#endif + + sprintf(content, "%s\n" @@ -313,19 +356,20 @@ void webio_sendOKHeader(SOCKET socket, char *file) { "Content-Encoding: gzip\r\n" "Content-Language: en\r\n" "Content-Type: %s\r\n\r\n", webio_getMIMEtype(file)); - int res = send(socket, response, (int)strlen(response), 0); + int res = send(socket, response, (int) strlen(response), 0); if (res == SOCKET_ERROR) { logger_log("Error sending http ok header!"); } } -void webio_sendOKHeader_wSize(SOCKET socket, char *file,int size) { + +void webio_sendOKHeader_wSize(SOCKET socket, char *file, int size) { char response[8192]; sprintf(response, "HTTP/1.1 200 OK " "Content-Encoding: gzip\r\n" "Content-Language: en\r\n" "Content-Length: %d\r\n" - "Content-Type: %s\r\n\r\n",size, webio_getMIMEtype(file)); - int res = send(socket, response,(int) strlen(response), 0); + "Content-Type: %s\r\n\r\n", size, webio_getMIMEtype(file)); + int res = send(socket, response, (int) strlen(response), 0); if (res == SOCKET_ERROR) { logger_log("Error sending http ok header!"); } @@ -338,7 +382,7 @@ void webio_send404Page(SOCKET socket) { "Content-Language: en\r\n" "Content-Type: text/html\r\n\r\n" "

    Error 404 File not found!

    "; - int res = send(socket, response, (int)strlen(response), 0); + int res = send(socket, response, (int) strlen(response), 0); if (res == SOCKET_ERROR) { logger_log("Error sending 404 page!"); } @@ -347,9 +391,9 @@ void webio_send404Page(SOCKET socket) { void webio_sendPage(SOCKET socket, char *content) { char f[] = "index.html"; webio_sendOKHeader(socket, f); - int res = send(socket, content,(int) strlen(content), 0); + int res = send(socket, content, (int) strlen(content), 0); if (res == SOCKET_ERROR) { logger_log("Error sending page!"); } - shutdown(socket,SD_BOTH); + shutdown(socket, SD_BOTH); } diff --git a/modules/webio.h b/modules/webio.h index dd868a7..bbdd716 100644 --- a/modules/webio.h +++ b/modules/webio.h @@ -1,6 +1,6 @@ // // Készítette: Toldi Balázs Ádám -// Dátum: 2019. 10. 14.. +// Dátum: 2019. 10. 14. // #pragma once @@ -9,9 +9,6 @@ #include "../lib/tcp-listener.h" #include "peer.h" #include "config.h" -#ifdef __MINGW32__ -#include -#endif @@ -44,9 +41,9 @@ static void webio_getPeerPage(char* folder, char *id, bool online, char *outputB static bool webio_isPeerFound(char* folder,char* id); static void webio_sendOKHeader(SOCKET socket,char* file); + static void webio_sendOKHeader_wSize(SOCKET socket,char* file,int size); static void webio_sendPage(SOCKET socket, char* content); -static void webio_send404Page(SOCKET socket); - +static void webio_send404Page(SOCKET socket); \ No newline at end of file diff --git a/test.c b/test.c index f721c03..aac119a 100644 --- a/test.c +++ b/test.c @@ -5,12 +5,15 @@ //#include #include #include +#include "lib/map.h" +#include "lib/debugmalloc/debugmalloc.h" int main(int argc , char **argv) { - char buf[10] = "lma"; - char buf2[10] = "lma"; - - sprintf(buf,"%c%s",'a',buf2); - printf("%s",buf); + Map m; + map_init(&m); + map_addPair(&m,map_make_pair("Test","1")); + //map_dump(m); + free(m.pairs); + return 0; }