2019-07-14 22:39:37 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
2020-02-23 00:12:39 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <uuid/uuid.h>
|
|
|
|
|
2019-07-14 22:39:37 +00:00
|
|
|
#include <config.h>
|
|
|
|
#include <helpers.h>
|
2019-07-22 20:06:13 +00:00
|
|
|
#include <models/controller_dbo.h>
|
2020-02-23 00:12:39 +00:00
|
|
|
#include <binn.h>
|
2019-07-22 20:06:13 +00:00
|
|
|
#include "api_v1_controllers.h"
|
2019-07-29 20:02:38 +00:00
|
|
|
|
2019-07-14 22:39:37 +00:00
|
|
|
using namespace api::v1;
|
|
|
|
|
2020-02-23 19:06:14 +00:00
|
|
|
enum DISCOVERY_MAPPING
|
|
|
|
{
|
|
|
|
DISCOVERY_MAPPING_ID = 0,
|
|
|
|
DISCOVERY_MAPPING_NAME = 1,
|
|
|
|
DISCOVERY_MAPPING_COMMAND_PORT = 2,
|
|
|
|
DISCOVERY_MAPPING_RELAY_COUNT = 3,
|
|
|
|
};
|
|
|
|
|
2019-07-22 20:06:13 +00:00
|
|
|
void controllers::post_discover(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
|
2019-07-14 22:39:37 +00:00
|
|
|
{
|
|
|
|
|
2019-07-19 09:41:39 +00:00
|
|
|
int discover_server_socket = helpers::bind_tcp_server("0.0.0.0", "0", config::discover_max_client_backlog);
|
|
|
|
int discover_server_port = helpers::get_server_port(discover_server_socket);
|
2019-07-14 22:39:37 +00:00
|
|
|
|
|
|
|
if(discover_server_port == -1)
|
|
|
|
{
|
2019-07-19 12:42:36 +00:00
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
2019-07-14 22:39:37 +00:00
|
|
|
resp->setStatusCode(k500InternalServerError);
|
|
|
|
callback(resp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-23 00:12:39 +00:00
|
|
|
int16_t payload[1];
|
|
|
|
payload[0] = discover_server_port;
|
2019-07-14 22:39:37 +00:00
|
|
|
|
2020-02-23 00:12:39 +00:00
|
|
|
if(helpers::send_udp_broadcast("255.255.255.255", config::discover_port_dev, payload, sizeof(payload)) < 0)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
2019-07-19 12:42:36 +00:00
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
2019-07-19 09:41:39 +00:00
|
|
|
resp->setStatusCode(k500InternalServerError);
|
|
|
|
callback(resp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-20 12:51:45 +00:00
|
|
|
struct sockaddr_storage their_addr{};
|
2019-07-19 09:41:39 +00:00
|
|
|
socklen_t addr_size;
|
|
|
|
int client_fd, s_ret;
|
|
|
|
fd_set accept_fds;
|
2019-07-20 12:51:45 +00:00
|
|
|
struct timeval timeout{};
|
2019-07-19 09:41:39 +00:00
|
|
|
|
|
|
|
uint8_t discover_answer_buf[1];
|
|
|
|
|
2019-07-22 20:06:13 +00:00
|
|
|
controller_dbo **known_controllers = controller_dbo::get_all();
|
2019-07-19 09:41:39 +00:00
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
addr_size = sizeof(their_addr);
|
|
|
|
|
|
|
|
FD_ZERO(&accept_fds);
|
2019-07-20 12:51:45 +00:00
|
|
|
FD_SET(discover_server_socket, &accept_fds); // NOLINT(hicpp-signed-bitwise)
|
2019-07-19 09:41:39 +00:00
|
|
|
|
2019-07-20 12:51:45 +00:00
|
|
|
timeout.tv_sec = config::discover_timeout_ms / 1000;
|
2019-07-19 09:41:39 +00:00
|
|
|
timeout.tv_usec = (config::discover_timeout_ms % 1000) * 1000;
|
|
|
|
|
2019-07-20 12:51:45 +00:00
|
|
|
s_ret = select(discover_server_socket + 1, &accept_fds, nullptr, nullptr, &timeout);
|
2019-07-19 09:41:39 +00:00
|
|
|
if(s_ret == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if((client_fd = accept(discover_server_socket, (struct sockaddr *) &their_addr, &addr_size)) < 0)
|
|
|
|
{
|
|
|
|
LOG_ERROR << "Error Accepting client " << strerror(errno);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-23 00:12:39 +00:00
|
|
|
size_t payload_length;
|
|
|
|
|
|
|
|
if(recv(client_fd, &payload_length, sizeof(payload_length), 0) <= 0)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
|
|
|
LOG_ERROR << "Error Receiving header from client";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-23 00:12:39 +00:00
|
|
|
void *answer_payload = malloc((payload_length + 1));
|
|
|
|
ssize_t bytes_transferred;
|
2019-07-19 09:41:39 +00:00
|
|
|
|
2020-02-23 00:12:39 +00:00
|
|
|
if((bytes_transferred = recv(client_fd, answer_payload, payload_length, 0)) <= 0)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
|
|
|
LOG_ERROR << "Error Receiving payload from client";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-20 12:51:45 +00:00
|
|
|
struct sockaddr_in addr{};
|
|
|
|
socklen_t client_addr_size = sizeof(struct sockaddr_in);
|
|
|
|
if(getpeername(client_fd, (struct sockaddr *)&addr, &client_addr_size) != 0)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
LOG_ERROR << "Error Receiving payload from client";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:06:14 +00:00
|
|
|
uuid_t discovered_id;
|
|
|
|
int id_size = sizeof(uuid_t);
|
|
|
|
memcpy(&discovered_id, binn_map_blob(answer_payload, DISCOVERY_MAPPING_ID, &id_size), id_size);
|
2019-07-29 20:02:38 +00:00
|
|
|
|
2020-03-11 23:36:06 +00:00
|
|
|
char *discovered_name = binn_map_str(answer_payload, DISCOVERY_MAPPING_NAME);
|
|
|
|
uint32_t discovered_command_port = binn_map_uint32(answer_payload, DISCOVERY_MAPPING_COMMAND_PORT);
|
|
|
|
uint8_t discovered_relay_count = binn_map_uint8(answer_payload, DISCOVERY_MAPPING_RELAY_COUNT);
|
2020-02-23 00:12:39 +00:00
|
|
|
|
2019-07-19 09:41:39 +00:00
|
|
|
bool found_discovered_in_list = false;
|
|
|
|
|
2019-07-22 20:06:13 +00:00
|
|
|
for(int i = 0; known_controllers[i] != nullptr; i++)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
|
|
|
if(!found_discovered_in_list)
|
|
|
|
{
|
2020-02-23 19:06:14 +00:00
|
|
|
if(uuid_compare(known_controllers[i]->id, discovered_id) == 0)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
2019-07-22 20:06:13 +00:00
|
|
|
known_controllers[i]->active = true;
|
2020-04-18 19:29:52 +00:00
|
|
|
strncpy(known_controllers[i]->name, discovered_name, 128);
|
|
|
|
known_controllers[i]->name[127] = '\0';
|
|
|
|
known_controllers[i]->port = discovered_command_port;
|
|
|
|
known_controllers[i]->relay_count = discovered_relay_count;
|
2019-07-22 20:06:13 +00:00
|
|
|
known_controllers[i]->update();
|
2019-07-29 20:02:38 +00:00
|
|
|
delete known_controllers[i];
|
2019-07-19 09:41:39 +00:00
|
|
|
found_discovered_in_list = true;
|
2019-07-22 20:06:13 +00:00
|
|
|
known_controllers[i] = known_controllers[i + 1];
|
2019-07-19 09:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-22 20:06:13 +00:00
|
|
|
known_controllers[i] = known_controllers[i + 1];
|
2019-07-19 09:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found_discovered_in_list)
|
|
|
|
{
|
2019-07-22 20:06:13 +00:00
|
|
|
controller_dbo discovered_controller{};
|
|
|
|
strcpy(discovered_controller.ip, inet_ntoa(addr.sin_addr));
|
2020-02-23 19:06:14 +00:00
|
|
|
memcpy(discovered_controller.id, discovered_id, sizeof(uuid_t));
|
2020-03-11 23:36:06 +00:00
|
|
|
strcpy(discovered_controller.name, discovered_name);
|
|
|
|
discovered_controller.relay_count = discovered_relay_count;
|
|
|
|
discovered_controller.port = discovered_command_port;
|
2019-07-22 20:06:13 +00:00
|
|
|
discovered_controller.active = true;
|
|
|
|
|
|
|
|
discovered_controller.insert();
|
2019-07-19 09:41:39 +00:00
|
|
|
}
|
2020-03-11 23:36:06 +00:00
|
|
|
free(answer_payload);
|
2019-07-19 09:41:39 +00:00
|
|
|
|
|
|
|
discover_answer_buf[0] = config::discover_code_accept;
|
|
|
|
send(client_fd, discover_answer_buf, sizeof(uint8_t), 0);
|
|
|
|
close(client_fd);
|
|
|
|
}
|
|
|
|
}
|
2019-07-22 20:06:13 +00:00
|
|
|
for(int i = 0; known_controllers[i] != nullptr; i++)
|
2019-07-19 12:42:36 +00:00
|
|
|
{
|
2019-07-22 20:06:13 +00:00
|
|
|
known_controllers[i]->active = false;
|
|
|
|
known_controllers[i]->update();
|
|
|
|
LOG_DEBUG << "Lost: " << known_controllers[i]->name;
|
2019-07-19 12:42:36 +00:00
|
|
|
}
|
2019-07-22 20:06:13 +00:00
|
|
|
controller_dbo::free_list(known_controllers);
|
2019-07-19 12:42:36 +00:00
|
|
|
|
2019-07-22 20:06:13 +00:00
|
|
|
controller_dbo **all_controllers = controller_dbo::get_all();
|
|
|
|
Json::Value all_controllers_json(Json::arrayValue);
|
2019-07-19 12:42:36 +00:00
|
|
|
|
2019-07-22 20:06:13 +00:00
|
|
|
for(int i = 0; all_controllers[i] != nullptr; i++)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
2019-07-22 20:06:13 +00:00
|
|
|
all_controllers_json.append(all_controllers[i]->to_json());
|
2019-07-19 09:41:39 +00:00
|
|
|
}
|
2019-07-14 22:39:37 +00:00
|
|
|
|
2019-07-22 20:06:13 +00:00
|
|
|
auto resp = HttpResponse::newHttpJsonResponse(all_controllers_json);
|
2019-07-14 22:39:37 +00:00
|
|
|
|
|
|
|
callback(resp);
|
2019-07-19 12:42:36 +00:00
|
|
|
|
2019-07-22 20:06:13 +00:00
|
|
|
controller_dbo::free_list(all_controllers);
|
2019-07-19 12:42:36 +00:00
|
|
|
|
2019-07-14 22:39:37 +00:00
|
|
|
}
|
|
|
|
|