core-legacy/controllers/api_v1_devices_discover.cc

169 lines
5.2 KiB
C++
Raw Normal View History

2019-07-14 22:39:37 +00:00
#include <netdb.h>
#include <unistd.h>
#include <config.h>
#include <helpers.h>
2019-07-19 09:41:39 +00:00
#include <cmath>
#include <models/device_dbo.h>
2019-07-14 22:39:37 +00:00
#include "api_v1_devices.h"
using namespace api::v1;
void devices::post_discover(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{
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;
}
Json::Value payload;
payload["port"] = discover_server_port;
2019-07-19 09:41:39 +00:00
Json::StreamWriterBuilder json_writer;
2019-07-14 22:39:37 +00:00
2019-07-19 09:41:39 +00:00
if(helpers::send_udp_broadcast("255.255.255.255", config::discover_port_dev, Json::writeString(json_writer, payload).c_str()) < 0)
{
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;
}
struct sockaddr_storage their_addr;
socklen_t addr_size;
int client_fd, s_ret;
fd_set accept_fds;
struct timeval timeout;
uint8_t discover_answer_buf[1];
uint8_t discover_header_buf[1];
2019-07-19 12:42:36 +00:00
device_dbo **known_devices = device_dbo::get_all();
2019-07-19 09:41:39 +00:00
while(true)
{
addr_size = sizeof(their_addr);
FD_ZERO(&accept_fds);
FD_SET(discover_server_socket, &accept_fds);
timeout.tv_sec = floor(config::discover_timeout_ms / 1000);
timeout.tv_usec = (config::discover_timeout_ms % 1000) * 1000;
s_ret = select(discover_server_socket + 1, &accept_fds, NULL, NULL, &timeout);
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;
}
if(recv(client_fd, discover_header_buf, 1, 0) < 0)
{
LOG_ERROR << "Error Receiving header from client";
continue;
}
int payload_length = discover_header_buf[0];
char *answer_payload = (char*)malloc((payload_length + 1) * sizeof(*answer_payload));
if(recv(client_fd, answer_payload, payload_length, 0) < 0)
{
LOG_ERROR << "Error Receiving payload from client";
continue;
}
struct sockaddr_in addr;
socklen_t addr_size = sizeof(struct sockaddr_in);
if(getpeername(client_fd, (struct sockaddr *)&addr, &addr_size) != 0)
{
LOG_ERROR << "Error Receiving payload from client";
continue;
}
answer_payload[payload_length] = '\0';
Json::Reader json_reader;
Json::Value client_info;
json_reader.parse(answer_payload, &answer_payload[payload_length], client_info, false);
2019-07-19 12:42:36 +00:00
free(answer_payload);
2019-07-19 09:41:39 +00:00
const char *discovered_id = client_info["id"].asCString();
bool found_discovered_in_list = false;
2019-07-19 12:42:36 +00:00
for(int i = 0; known_devices[i] != nullptr; i++)
2019-07-19 09:41:39 +00:00
{
if(!found_discovered_in_list)
{
2019-07-19 12:42:36 +00:00
if(strcmp(known_devices[i]->id, discovered_id) == 0)
2019-07-19 09:41:39 +00:00
{
2019-07-19 12:42:36 +00:00
known_devices[i]->active = true;
known_devices[i]->update();
free(known_devices[i]);
2019-07-19 09:41:39 +00:00
found_discovered_in_list = true;
2019-07-19 12:42:36 +00:00
known_devices[i] = known_devices[i + 1];
2019-07-19 09:41:39 +00:00
}
}
else
{
2019-07-19 12:42:36 +00:00
known_devices[i] = known_devices[i + 1];
2019-07-19 09:41:39 +00:00
}
}
if(!found_discovered_in_list)
{
device_dbo discovered_device;
strcpy(discovered_device.ip, inet_ntoa(addr.sin_addr));
strcpy(discovered_device.id, discovered_id);
strcpy(discovered_device.name, client_info["name"].asCString());
discovered_device.relay_count = client_info["relay_count"].asInt();
discovered_device.port = client_info["port"].asInt();
discovered_device.active = true;
discovered_device.insert();
}
discover_answer_buf[0] = config::discover_code_accept;
send(client_fd, discover_answer_buf, sizeof(uint8_t), 0);
close(client_fd);
}
}
2019-07-19 12:42:36 +00:00
for(int i = 0; known_devices[i] != nullptr; i++)
{
known_devices[i]->active = false;
known_devices[i]->update();
LOG_DEBUG << "Lost: " << known_devices[i]->name;
}
device_dbo::free_list(known_devices);
device_dbo **all_devices = device_dbo::get_all();
Json::Value all_devices_json(Json::arrayValue);
2019-07-19 09:41:39 +00:00
for(int i = 0; all_devices[i] != nullptr; i++)
{
2019-07-19 12:42:36 +00:00
all_devices_json.append(all_devices[i]->to_json());
2019-07-19 09:41:39 +00:00
}
2019-07-14 22:39:37 +00:00
2019-07-19 12:42:36 +00:00
auto resp = HttpResponse::newHttpJsonResponse(all_devices_json);
2019-07-14 22:39:37 +00:00
callback(resp);
2019-07-19 12:42:36 +00:00
device_dbo::free_list(all_devices);
2019-07-14 22:39:37 +00:00
}