From beb0a4eb9829637902940962f81750d2ca443a91 Mon Sep 17 00:00:00 2001 From: Bazsalanszky Date: Mon, 28 Oct 2019 16:58:09 +0100 Subject: [PATCH] Added default config file --- README.md | 2 +- config.example.ini | 12 +++++ docs/specs.md | 2 +- modules/config.c | 30 ++++++++++-- modules/crypto.c | 108 +++++++++++++++++++---------------------- modules/tcp-listener.c | 30 +++++++++++- modules/webio.c | 2 +- 7 files changed, 118 insertions(+), 68 deletions(-) create mode 100644 config.example.ini diff --git a/README.md b/README.md index 576de67..55d41dd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # P2P-chat -A projekem 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. +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. ## Szükséges könyvtárak diff --git a/config.example.ini b/config.example.ini new file mode 100644 index 0000000..ec6c7b7 --- /dev/null +++ b/config.example.ini @@ -0,0 +1,12 @@ +#Becenév +#nickname=Pelda +#A program által használt port (Alapértelmezett: 6381) +port=6327 +#Letiltja a külső csatlakozást (Alapértelmezett: false) +localmode=false +#Ezne a porton lesz elérhető a felhasználói felület (Alapértelmezett: 5381) +interface-port=5381 +#Ebben a mappában vannak tárolva a html fájlok a felhasználói felülethez (Alapértelmezett: htdocs/) +interface-folder=htdocs/ +#A felhasználói felület csak ezen a gépen érhető elő (Alapértelmezett: true;Ajánlott) +interface-local=true \ No newline at end of file diff --git a/docs/specs.md b/docs/specs.md index 7cf60d1..cec1c6a 100644 --- a/docs/specs.md +++ b/docs/specs.md @@ -23,7 +23,7 @@ A program ezekben az esetekben ír ki a képernyőre (és mindezt elmenti a `log ## Konfigurációs fájl -A konfigurációs fájl arra szolgál, hogy a felhasználó igényei szerint beállíthassa,hogy hogyan működjön a program.Ez a fájl a `config.txt`. Itt a különböző értékek soronként `kulcs=érték` formátumban vannak. +A konfigurációs fájl arra szolgál, hogy a felhasználó igényei szerint beállíthassa,hogy hogyan működjön a program.Ez a fájl a `config.ini`. Itt a különböző értékek soronként `kulcs=érték` formátumban vannak. Ha egy sor kettős kereszttel(`#`) kezdődik,akkor azt a program kommentnek veszi. diff --git a/modules/config.c b/modules/config.c index fdc74ce..5af2601 100644 --- a/modules/config.c +++ b/modules/config.c @@ -8,16 +8,36 @@ config config_load(){ config cfg; map_init(&cfg); FILE * f; - f = fopen("config.txt","r"); + f = fopen("config.ini","r"); if(f != NULL) { - char key[65], value[65]; - while (fscanf(f, "%[^=]=%s\n", key, value) != EOF) { - //Comment in config - if (key[0] == '#') continue; + char buf[256], key[65], value[65]; + while (fgets(buf, sizeof(buf), f) != NULL) + { + if (buf[0] == '#') continue; + sscanf(buf, "%[^=]=%s\n", key, value); map_addPair(&cfg, map_make_pair(key, value)); ZeroMemory(key, 65); ZeroMemory(value, 65); + ZeroMemory(buf, 256); } + }else{ + + char* exampleConf= "#Becenév\n" + "#nickname=Pelda\n" + "#A program által használt port (Alapértelmezett: 6381)\n" + "port=6327\n" + "#Letiltja a külső csatlakozást (Alapértelmezett: false)\n" + "localmode=false\n" + "#Ezne a porton lesz elérhető a felhasználói felület (Alapértelmezett: 5381)\n" + "interface-port=5381\n" + "#Ebben a mappában vannak tárolva a html fájlok a felhasználói felülethez (Alapértelmezett: htdocs/)\n" + "interface-folder=htdocs/\n" + "#A felhasználói felület csak ezen a gépen érhető elő (Alapértelmezett: true;Ajánlott)\n" + "interface-local=true" ; + f = fopen("config.ini","w"); + fprintf(f,exampleConf); + fclose(f); } + return cfg; } \ No newline at end of file diff --git a/modules/crypto.c b/modules/crypto.c index 1193fb1..66ac387 100644 --- a/modules/crypto.c +++ b/modules/crypto.c @@ -5,68 +5,58 @@ #include "crypto.h" #undef APPMACROS_ONLY + #include -RSA * createRSA(unsigned char * key,int public) -{ - RSA *rsa= NULL; - BIO *keybio ; +RSA *createRSA(unsigned char *key, int public) { + RSA *rsa = NULL; + BIO *keybio; keybio = BIO_new_mem_buf(key, -1); - if (keybio==NULL) - { - printf( "Failed to create key BIO"); + if (keybio == NULL) { + logger_log("Failed to create key BIO"); return 0; - } - if(public) - { - rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa,NULL, NULL); } - else - { - rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL); - } - if(rsa == NULL) - { - printf( "Failed to create RSA"); + if (public) { + rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL); + } else { + rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL); + } + if (rsa == NULL) { + logger_log("Failed to create RSA"); } return rsa; } RSA *createRSAfromFile(char *file, int pub) { - FILE * fp = fopen(file,"rb"); + FILE *fp = fopen(file, "rb"); - if(fp == NULL) - { + if (fp == NULL) { return NULL; } - RSA *rsa= RSA_new() ; - if(pub) - { - rsa = PEM_read_RSA_PUBKEY(fp, &rsa,NULL, NULL); - } - else - { - rsa = PEM_read_RSAPrivateKey(fp, &rsa,NULL, NULL); + RSA *rsa = RSA_new(); + if (pub) { + rsa = PEM_read_RSA_PUBKEY(fp, &rsa, NULL, NULL); + } else { + rsa = PEM_read_RSAPrivateKey(fp, &rsa, NULL, NULL); } return rsa; } - RSA *generate_key() { - int ret = 0; - RSA *r = NULL; - BIGNUM *bne = NULL; - BIO *bp_public = NULL, *bp_private = NULL; + int ret = 0; + RSA *r = NULL; + BIGNUM *bne = NULL; + BIO *bp_public = NULL, *bp_private = NULL; - int bits = 2048; - unsigned long e = RSA_F4; + int bits = 2048; + unsigned long e = RSA_F4; bne = BN_new(); - ret = BN_set_word(bne,e); - if(ret != 1){ + ret = BN_set_word(bne, e); + if (ret != 1) { BIO_free_all(bp_public); BIO_free_all(bp_private); RSA_free(r); @@ -76,7 +66,7 @@ RSA *generate_key() { r = RSA_new(); ret = RSA_generate_key_ex(r, bits, bne, NULL); - if(ret != 1){ + if (ret != 1) { BIO_free_all(bp_public); BIO_free_all(bp_private); RSA_free(r); @@ -85,12 +75,12 @@ RSA *generate_key() { } bp_public = BIO_new_file("public.pem", "w+"); ret = PEM_write_bio_RSA_PUBKEY(bp_public, r); - if(ret != 1){ + if (ret != 1) { BIO_free_all(bp_public); BIO_free_all(bp_private); RSA_free(r); BN_free(bne); - return NULL; + return NULL; } bp_private = BIO_new_file("private.pem", "w+"); @@ -104,11 +94,11 @@ RSA *generate_key() { } -void RSA_getPublicKey(RSA*r,char* pubkey){ +void RSA_getPublicKey(RSA *r, char *pubkey) { int keylen; BIO *bio = BIO_new(BIO_s_mem()); - int res = PEM_write_bio_RSA_PUBKEY(bio,r); + int res = PEM_write_bio_RSA_PUBKEY(bio, r); keylen = BIO_pending(bio); int re = BIO_read(bio, pubkey, keylen); @@ -119,25 +109,26 @@ void RSA_getPrivateKey(RSA *r, char *privkey) { int keylen; BIO *bio = BIO_new(BIO_s_mem()); - int res = PEM_write_bio_RSAPrivateKey(bio,r,NULL,NULL,0,NULL,NULL); + int res = PEM_write_bio_RSAPrivateKey(bio, r, NULL, NULL, 0, NULL, NULL); keylen = BIO_pending(bio); int re = BIO_read(bio, privkey, keylen); BIO_free_all(bio); } + int public_encrypt(unsigned char *data, int data_len, unsigned char *key, unsigned char *encrypted) { - RSA * rsa = createRSA(key,1); - int result = RSA_public_encrypt(data_len,data,encrypted,rsa,RSA_PKCS1_PADDING); + RSA *rsa = createRSA(key, 1); + int result = RSA_public_encrypt(data_len, data, encrypted, rsa, RSA_PKCS1_PADDING); return result; } int private_decrypt(unsigned char *enc_data, int data_len, unsigned char *key, unsigned char *decrypted) { - RSA * rsa = createRSA(key,0); - int result = RSA_private_decrypt(data_len,enc_data,decrypted,rsa,RSA_PKCS1_PADDING); + RSA *rsa = createRSA(key, 0); + int result = RSA_private_decrypt(data_len, enc_data, decrypted, rsa, RSA_PKCS1_PADDING); return result; } -int base64Encode(const unsigned char* input ,size_t len, char** output) { +int base64Encode(const unsigned char *input, size_t len, char **output) { BIO *bio, *b64; BUF_MEM *bufferPtr; @@ -152,28 +143,29 @@ int base64Encode(const unsigned char* input ,size_t len, char** output) { BIO_set_close(bio, BIO_NOCLOSE); BIO_free_all(bio); - *output=bufferPtr->data; + *output = bufferPtr->data; return 0; } -size_t calcDecodeLength(const char* b64input) { //Calculates the length of a decoded string + +size_t calcDecodeLength(const char *b64input) { //Calculates the length of a decoded string size_t len = strlen(b64input), padding = 0; - if (b64input[len-1] == '=' && b64input[len-2] == '=') //last two chars are = + if (b64input[len - 1] == '=' && b64input[len - 2] == '=') //last two chars are = padding = 2; - else if (b64input[len-1] == '=') //last char is = + else if (b64input[len - 1] == '=') //last char is = padding = 1; - return (len*3)/4 - padding; + return (len * 3) / 4 - padding; } -int base64Decode(const char* input, unsigned char**buffer,size_t* len) { //Decodes a base64 encoded string +int base64Decode(const char *input, unsigned char **buffer, size_t *len) { //Decodes a base64 encoded string BIO *bio, *b64; int decodeLen = calcDecodeLength(input); - *buffer = (unsigned char*)malloc(decodeLen + 1); + *buffer = (unsigned char *) malloc(decodeLen + 1); (*buffer)[decodeLen] = '\0'; bio = BIO_new_mem_buf(input, -1); @@ -189,10 +181,10 @@ int base64Decode(const char* input, unsigned char**buffer,size_t* len) { //Decod } void printOpenSSLError(char *msg) { - char * err = malloc(130);; + char *err = malloc(130);; ERR_load_crypto_strings(); ERR_error_string(ERR_get_error(), err); - logger_log("%s ERROR: %s\n",msg, err); + logger_log("%s ERROR: %s\n", msg, err); free(err); } diff --git a/modules/tcp-listener.c b/modules/tcp-listener.c index 3736169..11e33da 100644 --- a/modules/tcp-listener.c +++ b/modules/tcp-listener.c @@ -42,7 +42,33 @@ struct addrinfo* tcp_createIPv4Socket(SOCKET *s,int port,bool wildcard){ int tcp_bindnlisten(SOCKET s,struct addrinfo* addr,int conn_count){ int res = bind(s, addr->ai_addr, addr->ai_addrlen); if (res == SOCKET_ERROR) { - logger_log("Error binding socket! Error: %d", WSAGetLastError()); + logger_log("Error binding socket!"); + switch(WSAGetLastError()){ + case WSAENETDOWN: + logger_log("The network subsystem has failed."); + break; + case WSAEADDRINUSE: + logger_log("Port already in use."); + break; + case WSAEACCES: + logger_log("An attempt was made to access a socket in a way forbidden by its access permissions."); + break; + case WSAEADDRNOTAVAIL: + logger_log("The requested address is not valid in its context."); + break; + case WSAEFAULT: + logger_log("The system detected an invalid pointer address in attempting to use a pointer argument in a call."); + break; + case WSAEINPROGRESS: + logger_log("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function."); + break; + case WSAEINVAL: + logger_log("This socket is already bound to an address."); + break; + case WSAENOBUFS: + logger_log("Not enough buffers are available or there are too many connections!"); + break; + } freeaddrinfo(addr); WSACleanup(); return 1; @@ -73,4 +99,4 @@ struct sockaddr_in tcp_getAddr_in(SOCKET s) { int tcp_getSockPort(SOCKET s){ struct sockaddr_in sin = tcp_getAddr_in(s); return ntohs(sin.sin_port); -} +} \ No newline at end of file diff --git a/modules/webio.c b/modules/webio.c index 6c3f2a1..69fe11e 100644 --- a/modules/webio.c +++ b/modules/webio.c @@ -284,4 +284,4 @@ char *getPeerPage(char *folder, Peer p) { "",header,p.peerData.id,p.peerData.id,p.peerData.id); free(header); return content; -} +} \ No newline at end of file