Adding a node returns a pointer to the new node.
This should help to edit aspects of it, such as whether or not it is a directory and such.
This commit is contained in:
parent
605a91bc41
commit
cafe6a7243
@ -19,6 +19,24 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @brief Simple address structure providing IP/DNS and port information.
|
||||
*/
|
||||
struct address {
|
||||
char *address; ///< IP/DNS string.
|
||||
unsigned short port; ///< Port of address.
|
||||
};
|
||||
|
||||
struct node {
|
||||
// The address of the node
|
||||
struct address address;
|
||||
// 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;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Initialize the node list.
|
||||
* @details This function allocates memory and prepares the nodes list, you
|
||||
@ -56,21 +74,30 @@ unsigned int NeoComm_get_node_max();
|
||||
unsigned int NeoComm_get_node_count();
|
||||
|
||||
/**
|
||||
* @brief Add a new node to the list with an address (normally IP) and port.
|
||||
* @brief Add a new node where only address is given (all other variables
|
||||
* are set to 0).
|
||||
*
|
||||
* @param addr Address of the new node (normally an IP, but also DNS).
|
||||
* @param port Port number for new node.
|
||||
* @param addr Address of the new node.
|
||||
*
|
||||
* @return If the node list is full it will return 0, else 1.
|
||||
* @return If the list is full then it will return NULL, else it will return
|
||||
* a pointer to the new (or old, if already existing) node.
|
||||
*/
|
||||
int NeoComm_add_node(char *addr, unsigned short port);
|
||||
struct node *NeoComm_add_node(struct address addr);
|
||||
|
||||
/**
|
||||
* @brief Remove a node from the list.
|
||||
*
|
||||
* @param addr The address of the node.
|
||||
* @param port The public port of the node.
|
||||
*
|
||||
* @return If the node was not found it returns 0, else it returns 1.
|
||||
*/
|
||||
int NeoComm_remove_node(char *addr, unsigned short port);
|
||||
int NeoComm_remove_node(struct address addr);
|
||||
|
||||
/**
|
||||
* @brief Get a pointer to the node with a given address.
|
||||
*
|
||||
* @param addr The address of the node.
|
||||
*
|
||||
* @return A modifiable pointer to the node or NULL if not found.
|
||||
*/
|
||||
struct node *NeoComm_get_node(struct address addr);
|
||||
|
63
src/nodes.c
63
src/nodes.c
@ -22,17 +22,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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
|
||||
@ -46,7 +35,7 @@ int NeoComm_init_nodes(unsigned int max_nodes) {
|
||||
return 0;
|
||||
|
||||
for(unsigned int i = 0; i < max_nodes; ++i)
|
||||
node_list[i].address = NULL;
|
||||
node_list[i].address.address = NULL;
|
||||
|
||||
node_max = max_nodes;
|
||||
node_count = 0;
|
||||
@ -75,42 +64,41 @@ unsigned int NeoComm_get_node_count() {
|
||||
return node_count;
|
||||
}
|
||||
|
||||
int NeoComm_add_node(char *addr, unsigned short port) {
|
||||
struct node *NeoComm_add_node(struct address addr) {
|
||||
if(node_count >= node_max)
|
||||
return 0;
|
||||
for(unsigned int i = 0; i < node_count; ++i)
|
||||
return NULL;
|
||||
for(unsigned int i = 0; i < node_max; ++i)
|
||||
{
|
||||
if(strcmp(node_list[i].address, addr) == 0 &&
|
||||
node_list[i].port == port)
|
||||
return 1;
|
||||
if(strcmp(node_list[i].address.address, addr.address) == 0 &&
|
||||
node_list[i].address.port == addr.port)
|
||||
return &node_list[i];
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < node_max; ++i)
|
||||
{
|
||||
if(!node_list[i].address)
|
||||
if(!node_list[i].address.address)
|
||||
{
|
||||
node_list[i].address = addr;
|
||||
node_list[i].port = port;
|
||||
node_list[i].address.address = addr.address;
|
||||
node_list[i].address.port = addr.port;
|
||||
node_list[i].directory = 0;
|
||||
node_list[i].connections = 0;
|
||||
node_list[i].max_connections = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
node_count++;
|
||||
|
||||
return 1;
|
||||
return &node_list[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int NeoComm_remove_node(char *addr, unsigned short port) {
|
||||
int NeoComm_remove_node(struct address addr) {
|
||||
int removed = 0;
|
||||
for(unsigned int i = 0; i < node_count; ++i)
|
||||
for(unsigned int i = 0; i < node_max; ++i)
|
||||
{
|
||||
if(!removed && strcmp(node_list[i].address, addr) == 0 &&
|
||||
node_list[i].port == port)
|
||||
if(!removed && strcmp(node_list[i].address.address,
|
||||
addr.address) == 0 &&
|
||||
node_list[i].address.port == addr.port)
|
||||
{
|
||||
node_list[i].address = NULL;
|
||||
node_list[i].address.address = NULL;
|
||||
node_count--;
|
||||
removed = 1;
|
||||
break;
|
||||
@ -120,3 +108,14 @@ int NeoComm_remove_node(char *addr, unsigned short port) {
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct node *NeoComm_get_node(struct address addr) {
|
||||
for(unsigned int i = 0; i < node_max; ++i)
|
||||
{
|
||||
if(strcmp(node_list[i].address.address, addr.address) == 0 &&
|
||||
node_list[i].address.port == addr.port) {
|
||||
return &node_list[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user