Beginning migration.

This commit is contained in:
Nicolás Ortega Froysa
2017-08-12 13:59:18 -05:00
parent e4de99086a
commit 48e9006220
11 changed files with 664 additions and 737 deletions

View File

@ -1,306 +0,0 @@
/*
* 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 "neocomm/connectivity.h"
#include "iconnectivity.h"
#include "ierror.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
static struct NeoComm_directory_node *node_list = NULL;
static unsigned int max_nodes;
static unsigned int num_nodes;
static int run;
static int sockfd;
static pthread_t accept_thread;
/*
* This is a protected bind function that is not accessible as a public
* interface.
*/
int NeoComm_bind(const unsigned short port) {
struct sockaddr_in serv_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
/*
* This declaration is here because if you put it at `default:' then
* it complains about the line being a declaration and not a
* statement.
*/
char error_msg[128];
switch(errno) {
case EACCES:
NeoComm_error("Insufficient privileges");
break;
case ENOBUFS:
NeoComm_error("Insufficient resources available");
break;
case ENOMEM:
NeoComm_error("Insufficient memory to open socket");
break;
default:
snprintf(error_msg, 128,
"Failed to initiate socket with errno `%d'", errno);
NeoComm_error(error_msg);
break;
}
return 0;
}
bzero((char*) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = INADDR_ANY;
if(bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)
{
char error_msg[128]; // same as before
switch(errno) {
case EADDRINUSE:
NeoComm_error("Address in use");
break;
case EADDRNOTAVAIL:
NeoComm_error("Address unavailable on system");
break;
case EAFNOSUPPORT:
NeoComm_error("Unsupported address family for socket");
break;
case EALREADY:
NeoComm_error("Assignment request already exists for socket");
break;
case EINVAL:
NeoComm_error("Socket already bound");
break;
case ENOBUFS:
NeoComm_error("Insufficient resources");
break;
case EACCES:
NeoComm_error("Insufficient privileges");
break;
default:
snprintf(error_msg, 128,
"Failed to bind to socket with errno `%d'", errno);
NeoComm_error(error_msg);
break;
}
return 0;
}
listen(sockfd, 5);
return 1;
}
void *NeoComm_connect_manager() {
// Allow for thread to be cancelled asynchronously
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
while(run)
{
int cli_sockfd;
struct sockaddr_in cli_addr;
socklen_t size_cli_addr = sizeof(cli_addr);
cli_sockfd = accept(sockfd, (struct sockaddr*) &cli_addr,
&size_cli_addr);
if(cli_sockfd < 0)
{
NeoComm_error("Error on accept");
continue;
}
++num_nodes;
// TODO: Add the new connection and have it listen on a new thread.
}
pthread_exit(NULL);
}
int NeoComm_init_directory(const unsigned int max_num_nodes,
const unsigned short portnum) {
if(node_list)
{
NeoComm_error("Already running");
return 0;
}
if(max_num_nodes == 0)
{
NeoComm_error("Insufficient number of nodes");
return 0;
}
if(!NeoComm_bind(portnum))
return 0;
node_list = calloc(max_num_nodes, sizeof(struct NeoComm_directory_node));
if(!node_list)
{
NeoComm_error("Failed to allocate memory to directory node list");
return 0;
}
max_nodes = max_num_nodes;
num_nodes = 0;
for(unsigned int i = 0; i < max_nodes; ++i)
node_list[i].in_use = 0;
run = 1;
int err = pthread_create(&accept_thread, NULL,
NeoComm_connect_manager, NULL);
if(err != 0)
{
NeoComm_error("Insufficient resources to create thread");
return 0;
}
return 1;
}
void NeoComm_shutdown_directory() {
if(!node_list)
return;
run = 0;
pthread_cancel(accept_thread);
for(unsigned int i = 0; i < num_nodes; ++i)
{
pthread_cancel(node_list[i].thread);
close(node_list[i].sockfd);
}
num_nodes = 0;
close(sockfd);
free(node_list);
node_list = NULL;
max_nodes = 0;
pthread_exit(NULL);
}
int NeoComm_resize_directory(const unsigned int new_max_num_nodes) {
if(!node_list)
{
NeoComm_error("Directory not initialized");
return 0;
}
if(new_max_num_nodes == max_nodes)
return 1;
else if(new_max_num_nodes < max_nodes)
{
NeoComm_error("Cannot downsize directory size");
return 0;
}
void *tmp_list = realloc(node_list,
new_max_num_nodes * sizeof(struct NeoComm_directory_node));
if(!tmp_list)
{
NeoComm_error("Failed to allocate more memory");
return 0;
}
for(unsigned int i = max_nodes; i < new_max_num_nodes; ++i)
node_list[i].in_use = 0;
max_nodes = new_max_num_nodes;
return 1;
}
unsigned int NeoComm_get_directory_size() {
return max_nodes;
}
unsigned int NeoComm_get_num_nodes() {
return num_nodes;
}
int NeoComm_add_node(const struct NeoComm_address addr) {
if(num_nodes == max_nodes)
{
NeoComm_error("Directory full");
return 0;
}
struct hostent *host = gethostbyname(addr.address);
if(!host)
{
NeoComm_error("Error getting host");
return 0;
}
unsigned int i;
for(i = 0; i < max_nodes; ++i)
{
if(!node_list[i].in_use)
break;
}
bzero((char*) &node_list[i].addr, sizeof(node_list[i].addr));
node_list[i].addr.sin_family = AF_INET;
bcopy((char*) host->h_addr_list[0],
(char*) &node_list[i].addr.sin_addr.s_addr,
host->h_length);
node_list[i].addr.sin_port = htons(addr.port);
if(connect(node_list[i].sockfd,
(struct sockaddr*) &node_list[i].addr,
sizeof(node_list[i].addr)) < 0)
{
NeoComm_error("Failed to connect to new node");
return 0;
}
node_list[i].in_use = 1;
++num_nodes;
// TODO: listen to node on new thread
return 1;
}
void NeoComm_remove_node(const struct NeoComm_address addr) {
struct hostent *host = gethostbyname(addr.address);
if(!host)
return;
unsigned int i;
for(i = 0; i < max_nodes; ++i)
{
if(strncmp((char*) host->h_addr_list[0],
(char*) &node_list[i].addr.sin_addr.s_addr,
host->h_length) == 0)
break;
if(i == max_nodes - 1)
++i;
}
if(i == max_nodes)
return;
pthread_cancel(node_list[i].thread);
close(node_list[i].sockfd);
node_list[i].in_use = 0;
--num_nodes;
}

View File

@ -1,34 +0,0 @@
/*
* 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 "neocomm/error.h"
#include "ierror.h"
#include <assert.h>
static const char *neocomm_error;
void NeoComm_error(const char *error) {
assert(error);
neocomm_error = error;
}
const char* NeoComm_get_last_error() {
return neocomm_error;
}

View File

@ -1,40 +0,0 @@
/*
* 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/>.
*/
#pragma once
#include <netinet/in.h>
#include <pthread.h>
/*
* This is an internal structure for nodes.
*/
struct NeoComm_directory_node {
int in_use;
struct sockaddr_in addr;
int sockfd;
pthread_t thread;
int open_directory;
unsigned int connections;
unsigned int max_connections;
};
int NeoComm_bind(unsigned short portnum);
void *NeoComm_connect_manager();

View File

@ -1,22 +0,0 @@
/*
* 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/>.
*/
#pragma once
void NeoComm_error(const char* error);