From 4f66f0cc96b046afddd52a104ce2e2a13fb14844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega?= Date: Sat, 1 Jul 2017 18:46:22 +0200 Subject: [PATCH] Started adding node functionality. --- CMakeLists.txt | 53 ++++++++++++++++++++++++++++ include/nodes.h | 66 +++++++++++++++++++++++++++++++++++ src/nodes.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 include/nodes.h create mode 100644 src/nodes.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..20cbdea --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,53 @@ +# Copyright (C) 2017 Ortega Froysa, Nicolás +# Author: Ortega Froysa, Nicolás +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation, either +# version 3 of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program. If not, see +# . + +cmake_minimum_required(VERSION 2.8) +project(NeoComm) + +set(TARGET_NAME "neocomm") +set(TARGET_VERSION_MAJOR 0) +set(TARGET_VERSION_MINOR 1) + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "release") +endif() +string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE) +message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}") + +option(BUILD_SHARED_LIBS + "Whether to build a shared object instead of a static." OFF) + +include_directories( + "include/") + +set(SRCS + src/nodes.c) + +set(CMAKE_C_FLAGS "-std=c99 -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors") +set(CMAKE_C_FLAGS_DEBUG "-g -O0") +set(CMAKE_C_FLAGS_RELEASE "-O3") +set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O3") +set(CMAKE_C_FLAGS_MINSEZEREL "-Os") + +if(NOT CMAKE_BUILD_TYPE MATCHES "debug" AND + NOT CMAKE_BUILD_TYPE MATCHES "relwithdebinfo") + add_definitions("-DNDEBUG") +else() + add_definitions("-DDEBUG") +endif() + +add_library(${TARGET_NAME} ${SRCS}) diff --git a/include/nodes.h b/include/nodes.h new file mode 100644 index 0000000..cc1f67d --- /dev/null +++ b/include/nodes.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2017 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + */ + +#pragma once + +/** + * @brief Initialize the node list. + * + * @param max_nodes Maximum number of nodes available. + * + * @return If the operation failed then it will return 0, else it will + * return 1. + */ +int init_nodes(unsigned int max_nodes); + +/** + * @brief Enlarge the list. + * + * @param new_max_nodes New number of maximum nodes. + * + * @return If the new size is less than or equal to the current size, or + * the list failed to be reallocated, then it returns 0, else it returns 1. + */ +int resize_node_list(unsigned int new_max_nodes); + +/** + * @brief Get the maximum number of nodes that can be used. + * + * @return The maximum number of nodes that can be used.. + */ +unsigned int get_node_max(); + +/** + * @brief Get the number of nodes there currently are. + * + * @return The number of nodes remembered. + */ +unsigned int get_node_count(); + +/** + * @brief Add a new node to the list with an address (normally IP) and port. + * + * @param addr Address of the new node (normally an IP, but also DNS). + * @param port Port number for new node. + * + * @return If the node list is full it will return 0, else 1. + */ +int add_node(char *addr, unsigned short port); + +int remove_node(char *addr, unsigned short port); diff --git a/src/nodes.c b/src/nodes.c new file mode 100644 index 0000000..3c92b30 --- /dev/null +++ b/src/nodes.c @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2017 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + */ + +#include "nodes.h" + +#include +#include + +struct node { + // The address of the node + char *address; + unsigned short port; + // Whether or not it is a directory node/server + int directory; + // Useful variables for passing on connections of new nodes + unsigned int connections; + unsigned int max_connections; +}; + +// List of nodes +static struct node *node_list; +// Maximum number of nodes +static unsigned int node_max; +// Number of nodes in the list +static unsigned int node_count; + +int init_nodes(unsigned int max_nodes) { + node_list = calloc(max_nodes, sizeof(struct node)); + if(!node_list) + return 0; + + node_max = max_nodes; + node_count = 0; + + return 1; +} + +int resize_node_list(unsigned int new_max_nodes) { + if(new_max_nodes <= node_max) + return 0; + + void* tmp_list = realloc(node_list, new_max_nodes * sizeof(struct node)); + if(!tmp_list) + return 0; + + node_list = tmp_list; + + return 1; +} + +unsigned int get_node_max() { + return node_max; +} + +unsigned int get_node_count() { + return node_count; +} + +int add_node(char *addr, unsigned short port) { + if(node_count >= node_max) + return 0; + for(unsigned int i = 0; i < node_count; ++i) + { + if(strcmp(node_list[i].address, addr) == 0 && + node_list[i].port == port) + return 1; + } + node_list[node_count].address = addr; + node_list[node_count].port = port; + node_list[node_count].directory = 0; + node_list[node_count].connections = 0; + node_list[node_count].max_connections = 0; + + node_count++; + + return 1; +}