controller-legacy/handlers/discovery.c

90 lines
2.5 KiB
C
Raw Normal View History

2020-04-13 22:50:55 +00:00
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <uuid/uuid.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <logger.h>
#include <handlers.h>
#include <helpers.h>
2020-04-23 23:33:48 +00:00
#include <mpack.h>
2020-04-13 22:50:55 +00:00
#include <enums.h>
void
handler_discovery(int fd, controller_t *controller)
{
ssize_t bytes_transferred;
uint16_t discovery_answer_port;
struct sockaddr_in si_other;
socklen_t slen = sizeof(si_other);
if((bytes_transferred = recvfrom(fd, &discovery_answer_port, sizeof(discovery_answer_port), 0, (struct sockaddr *) &si_other, &slen)) <= 0)
{
2020-04-24 13:08:26 +00:00
LOG_ERROR("received invalid discovery from %s\n", inet_ntoa(si_other.sin_addr));
2020-04-13 22:50:55 +00:00
return;
}
2020-04-24 13:08:26 +00:00
LOG_INFO("received discovery from %s:%d\n", inet_ntoa(si_other.sin_addr), discovery_answer_port);
2020-04-13 22:50:55 +00:00
if(discovery_answer_port == 0)
{
2020-04-24 13:08:26 +00:00
LOG_ERROR("invalid port received\n");
2020-04-13 22:50:55 +00:00
return;
}
2020-04-23 23:33:48 +00:00
char* payload;
size_t payload_size;
mpack_writer_t writer;
mpack_writer_init_growable(&writer, &payload, &payload_size);
2020-04-13 22:50:55 +00:00
2020-04-23 23:33:48 +00:00
mpack_start_map(&writer, 2);
mpack_write_uint(&writer, DISCOVERY_MAPPING_ID);
mpack_write_bin(&writer, (char*)controller->id, sizeof(uuid_t));
mpack_write_uint(&writer, DISCOVERY_MAPPING_COMMAND_PORT);
mpack_write_u16(&writer, controller->command_port);
mpack_write_uint(&writer, DISCOVERY_MAPPING_RELAY_COUNT);
mpack_write_u8(&writer, controller->relay_count);
mpack_write_uint(&writer, COMMAND_MAPPING_PERIODS_COUNT);
mpack_write_cstr(&writer, controller->name);
mpack_finish_map(&writer);
2020-04-13 22:50:55 +00:00
2020-04-23 23:33:48 +00:00
// finish writing
if(mpack_writer_destroy(&writer) != mpack_ok)
{
2020-04-24 13:08:26 +00:00
LOG_ERROR("error writing discovery answer payload\n");
2020-04-23 23:33:48 +00:00
return;
}
2020-04-13 22:50:55 +00:00
int fd_answer = helper_connect_tcp_server(inet_ntoa(si_other.sin_addr), discovery_answer_port);
if(fd_answer == -1)
{
2020-04-24 13:08:26 +00:00
LOG_ERROR("error during connecting\n");
2020-04-23 23:33:48 +00:00
free(payload);
2020-04-13 22:50:55 +00:00
return;
}
if((bytes_transferred = send(fd_answer, &payload_size, sizeof(payload_size), 0)) <= 0)
{
2020-04-24 13:08:26 +00:00
LOG_ERROR("error during sending\n");
2020-04-23 23:33:48 +00:00
free(payload);
close(fd_answer);
2020-04-13 22:50:55 +00:00
return;
}
if((bytes_transferred = send(fd_answer, payload, payload_size, 0)) <= 0)
{
2020-04-24 13:08:26 +00:00
LOG_ERROR("error during sending\n");
2020-04-23 23:33:48 +00:00
free(payload);
close(fd_answer);
2020-04-13 22:50:55 +00:00
return;
}
2020-04-23 23:33:48 +00:00
free(payload);
2020-04-13 22:50:55 +00:00
close(fd_answer);
}