| 
									
										
										
										
											2017-07-01 18:46:22 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (C) 2017  Ortega Froysa, Nicolás <deathsbreed@themusicinnoise.net> | 
					
						
							|  |  |  |  * Author: Ortega Froysa, Nicolás <deathsbreed@themusicinnoise.net> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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 | 
					
						
							|  |  |  |  * <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "nodes.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #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
 | 
					
						
							|  |  |  | static unsigned int node_max; | 
					
						
							|  |  |  | // Number of nodes in the list
 | 
					
						
							|  |  |  | static unsigned int node_count; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-02 12:09:49 +02:00
										 |  |  | int NeoComm_init_nodes(unsigned int max_nodes) { | 
					
						
							| 
									
										
										
										
											2017-07-01 18:46:22 +02:00
										 |  |  | 	node_list = calloc(max_nodes, sizeof(struct node)); | 
					
						
							|  |  |  | 	if(!node_list) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-02 12:09:49 +02:00
										 |  |  | 	for(unsigned int i = 0; i < max_nodes; ++i) | 
					
						
							|  |  |  | 		node_list[i].address = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 18:46:22 +02:00
										 |  |  | 	node_max = max_nodes; | 
					
						
							|  |  |  | 	node_count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-02 12:09:49 +02:00
										 |  |  | int NeoComm_resize_node_list(unsigned int new_max_nodes) { | 
					
						
							| 
									
										
										
										
											2017-07-01 18:46:22 +02:00
										 |  |  | 	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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-02 12:09:49 +02:00
										 |  |  | unsigned int NeoComm_get_node_max() { | 
					
						
							| 
									
										
										
										
											2017-07-01 18:46:22 +02:00
										 |  |  | 	return node_max; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-02 12:09:49 +02:00
										 |  |  | unsigned int NeoComm_get_node_count() { | 
					
						
							| 
									
										
										
										
											2017-07-01 18:46:22 +02:00
										 |  |  | 	return node_count; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-02 12:09:49 +02:00
										 |  |  | int NeoComm_add_node(char *addr, unsigned short port) { | 
					
						
							| 
									
										
										
										
											2017-07-01 18:46:22 +02:00
										 |  |  | 	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; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-07-02 12:09:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for(unsigned int i = 0; i < node_max; ++i) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if(!node_list[i].address) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			node_list[i].address = addr; | 
					
						
							|  |  |  | 			node_list[i].port = port; | 
					
						
							|  |  |  | 			node_list[i].directory = 0; | 
					
						
							|  |  |  | 			node_list[i].connections = 0; | 
					
						
							|  |  |  | 			node_list[i].max_connections = 0; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-07-01 18:46:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	node_count++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-07-02 12:09:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | int NeoComm_remove_node(char *addr, unsigned short port) { | 
					
						
							|  |  |  | 	int removed = 0; | 
					
						
							|  |  |  | 	for(unsigned int i = 0; i < node_count; ++i) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if(!removed && strcmp(node_list[i].address, addr) == 0 && | 
					
						
							|  |  |  | 				node_list[i].port == port) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			node_list[i].address = NULL; | 
					
						
							|  |  |  | 			node_count--; | 
					
						
							|  |  |  | 			removed = 1; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(!removed) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } |