Added net.h

This commit is contained in:
Bazsalanszky 2019-11-06 21:50:03 +01:00
parent dbad87a94e
commit 1828c99094
Signed by: Bazsalanszky
GPG key ID: 214701A3BD4B06F2
7 changed files with 54 additions and 52 deletions

View file

@ -9,9 +9,9 @@ set(SOURCE_FILES modules/peer.h modules/peer.c utility.c utility.h modules/webio
option(BUILD_DOC "Build Documentation" ON) option(BUILD_DOC "Build Documentation" ON)
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 lib/map.h lib/map.c lib/tcp-listener.c lib/tcp-listener.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 lib/map.h lib/map.c lib/tcp-listener.c lib/tcp-listener.h lib/net.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 lib/map.h lib/map.c lib/tcp-listener.c lib/tcp-listener.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 lib/map.h lib/map.c lib/tcp-listener.c lib/tcp-listener.h lib/net.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 lib/map.h lib/map.c lib/tcp-listener.c lib/tcp-listener.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 lib/map.h lib/map.c lib/tcp-listener.c lib/tcp-listener.h lib/net.h)
set_target_properties( p2p-2 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/p2p-2 ) 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 ) 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 # Függvények kipróbálására hoztam létre

27
lib/net.h Normal file
View file

@ -0,0 +1,27 @@
//
// Keszitette: Toldi Balázs Ádám
// Datum: 2019. 11. 05.
//
#pragma once
#if defined(__linux__) || defined(__CYGWIN__)
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
//Winsock2 kompatiblitás érdekében
#ifndef SOCKET_ERROR
#define SOCKET_ERROR -1
#endif
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif
//Ez winsock2-ben definiálva van (ott elvileg unsigned long long int-ként)
typedef int SOCKET;
#elif _WIN32
#include <ws2tcpip.h>
#endif

16
main.c
View file

@ -3,7 +3,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "modules/peer.h" #include "modules/peer.h"
#include "modules/webio.h" #include "modules/webio.h"
#include "modules/config.h" #include "modules/config.h"
@ -11,15 +10,6 @@
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
#ifdef RANDOM_PORT
#define DEFAULT_PORT "0"
#else
#define DEFAULT_PORT "6327"
#endif
#define DEFAULT_INTERFACE_PORT "5081"
#define DEFAULT_WWW_FOLDER "htdocs/"
int main(void) { int main(void) {
Map config = config_load(); Map config = config_load();
@ -121,12 +111,6 @@ int main(void) {
logger_log("Started web interface at http://127.0.0.1:%d", tcp_getSockPort(webIo.socket)); logger_log("Started web interface at http://127.0.0.1:%d", tcp_getSockPort(webIo.socket));
char *command = (char *) malloc(64);
sprintf(command, "start http://127.0.0.1:%d/", tcp_getSockPort(webIo.socket));
system(command);
free(command);
logger_log("Starting main loop..."); logger_log("Starting main loop...");
bool run = true; bool run = true;
while (run) { while (run) {

View file

@ -8,7 +8,7 @@ Config config_load(){
Config cfg; Config cfg;
map_init(&cfg); map_init(&cfg);
FILE * f; FILE * f;
f = fopen("Config.ini","r"); f = fopen("config.ini","r");
if(f != NULL) { if(f != NULL) {
char buf[256], key[65], value[65]; char buf[256], key[65], value[65];
while (fgets(buf, sizeof(buf), f) != NULL) while (fgets(buf, sizeof(buf), f) != NULL)
@ -22,20 +22,20 @@ Config config_load(){
} }
}else{ }else{
char* exampleConf= "#Becenév\n"
f = fopen("config.ini","w");
fprintf(f,"#Becenév\n"
"#nickname=Pelda\n" "#nickname=Pelda\n"
"#A program által használt port (Alapértelmezett: 6381)\n" "#A program által használt port (Alapértelmezett: %s)\n"
"port=6327\n" "port=%s\n"
"#Letiltja a külső csatlakozást (Alapértelmezett: false)\n" "#Letiltja a külső csatlakozást (Alapértelmezett: false)\n"
"localmode=false\n" "localmode=false\n"
"#Ezne a porton lesz elérhető a felhasználói felület (Alapértelmezett: 5381)\n" "#Ezne a porton lesz elérhető a felhasználói felület (Alapértelmezett: %s)\n"
"interface-port=5381\n" "interface-port=%s\n"
"#Ebben a mappában vannak tárolva a html fájlok a felhasználói felülethez (Alapértelmezett: htdocs/)\n" "#Ebben a mappában vannak tárolva a html fájlok a felhasználói felülethez (Alapértelmezett: %s)\n"
"interface-folder=htdocs/\n" "interface-folder=%s\n"
"#A felhasználói felület csak ezen a gépen érhető elő (Alapértelmezett: true;Ajánlott)\n" "#A felhasználói felület csak ezen a gépen érhető elő (Alapértelmezett: true;Ajánlott)\n"
"interface-local=true" ; "interface-local=true",DEFAULT_PORT,DEFAULT_PORT,DEFAULT_INTERFACE_PORT,DEFAULT_INTERFACE_PORT,DEFAULT_WWW_FOLDER,DEFAULT_WWW_FOLDER);
f = fopen("Config.ini","w");
fprintf(f,exampleConf);
fclose(f); fclose(f);
} }

View file

@ -7,6 +7,12 @@
#pragma once #pragma once
#include "../utility.h" #include "../utility.h"
#include "../lib/map.h" #include "../lib/map.h"
#ifdef RANDOM_PORT
#define DEFAULT_PORT "0"
#else
#define DEFAULT_PORT "6327"
#endif
#define DEFAULT_INTERFACE_PORT "5081"
#define DEFAULT_WWW_FOLDER "htdocs/"
Config config_load(); Config config_load();

View file

@ -80,7 +80,7 @@ bool peer_IP_isFound(struct PeerList list, char* ip, int port);
void peer_addTolist(struct PeerList* list, struct peer peer); void peer_addTolist(struct PeerList* list, struct peer peer);
/*! /*!
* @brief Levesz egy elemet egy PeerList -ről. * @brief Levesz egy elemet egy PeerList -ről.
* @param[in,out] list Mutató a PeerList struktúrára,amiről le a karunk venni elemet * @param[in,out] list Mutató a PeerList struktúrára,amiről le akarunk venni elemet
* @param[in] index A peer indexe a listában * @param[in] index A peer indexe a listában
*/ */
void peer_removeFromList(struct PeerList *list, int index); void peer_removeFromList(struct PeerList *list, int index);

View file

@ -5,6 +5,7 @@
#pragma once #pragma once
#include "lib/map.h" #include "lib/map.h"
#include "lib/net.h"
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -12,23 +13,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#ifndef SOCKET_ERROR
#define SOCKET_ERROR -1
#endif
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif
typedef int SOCKET;
//! (MAXIMUM) Ekkora mérető csomagokat küld el a program alapértelmezetten. //! (MAXIMUM) Ekkora mérető csomagokat küld el a program alapértelmezetten.
#define DEFAULT_BUFLEN 4096 #define DEFAULT_BUFLEN 4096