2019-02-11 09:23:38 +00:00
|
|
|
#include "donar_server.h"
|
|
|
|
|
2019-02-11 15:23:20 +00:00
|
|
|
void create_onion_services(struct tor_os_str* tos, struct tor_ctl* tctl, uint16_t* ports, int ports_count) {
|
2019-02-11 09:23:38 +00:00
|
|
|
tor_os_create (tos, "onion_services.pub", "onion_services.txt", ports_count);
|
|
|
|
tor_os_read (tos);
|
|
|
|
|
|
|
|
int err = 0;
|
|
|
|
err = tor_ctl_connect (tctl, "127.0.0.1", "9051");
|
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "Unable to open Tor Socket\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
err = tor_ctl_add_onion (tctl, tos, ports);
|
|
|
|
if (err != 0) {
|
|
|
|
fprintf(stderr, "Unable to create Onion Services (error: %d)\n", err);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy_resources(struct tor_os_str* tos, struct tor_ctl* tctl) {
|
|
|
|
tor_ctl_close (tctl);
|
|
|
|
tor_os_free (tos);
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:23:20 +00:00
|
|
|
void init_tcp_servers(struct donar_server_ctx* ctx) {
|
2019-02-18 13:35:09 +00:00
|
|
|
char url[1024];
|
|
|
|
struct evt_core_cat cat = {0};
|
|
|
|
struct evt_core_fdinfo fdinfo = {0};
|
|
|
|
fdinfo.cat = &cat;
|
|
|
|
fdinfo.url = url;
|
|
|
|
|
2019-02-11 15:23:20 +00:00
|
|
|
char buffer[6];
|
2019-02-11 21:40:00 +00:00
|
|
|
int err, sock = 0;
|
|
|
|
for (int i = 0; i < PORT_SIZE; i++) {
|
2019-02-11 15:23:20 +00:00
|
|
|
sprintf (buffer, "%d", ctx->ports[i]);
|
2019-02-11 21:40:00 +00:00
|
|
|
sock = create_tcp_server (buffer);
|
|
|
|
if (sock < 0) goto socket_create_err;
|
|
|
|
err = listen(sock, SOMAXCONN);
|
2019-02-11 15:23:20 +00:00
|
|
|
if (err != 0) goto socket_create_err;
|
2019-02-18 13:35:09 +00:00
|
|
|
|
|
|
|
fdinfo.cat->name = "tcp-listen";
|
|
|
|
fdinfo.fd = sock;
|
|
|
|
sprintf(fdinfo.url, "tcp:listen:%d", ctx->ports[i]);
|
|
|
|
evt_core_add_fd(&(ctx->evts), &fdinfo);
|
2019-02-11 15:23:20 +00:00
|
|
|
}
|
|
|
|
return;
|
2019-02-11 09:23:38 +00:00
|
|
|
|
2019-02-11 15:23:20 +00:00
|
|
|
socket_create_err:
|
|
|
|
fprintf(stderr, "Unable to create a TCP socket\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-02-15 17:09:51 +00:00
|
|
|
void serv_init_udp_socket(char* port, struct donar_server_ctx* ctx) {
|
|
|
|
int sock1, sock2;
|
2019-02-18 13:35:09 +00:00
|
|
|
char url[1024];
|
|
|
|
struct evt_core_cat cat = {0};
|
|
|
|
struct evt_core_fdinfo fdinfo = {0};
|
|
|
|
fdinfo.cat = &cat;
|
|
|
|
fdinfo.url = url;
|
|
|
|
|
2019-02-15 17:09:51 +00:00
|
|
|
sock1 = create_udp_client ("127.0.0.1", port);
|
|
|
|
if (sock1 < 0) goto socket_failed;
|
|
|
|
sock2 = dup(sock1);
|
|
|
|
if (sock2 < 0) goto socket_failed;
|
2019-02-18 13:35:09 +00:00
|
|
|
|
|
|
|
fdinfo.fd = sock1;
|
|
|
|
fdinfo.cat->name = "udp-read";
|
|
|
|
sprintf(fdinfo.url, "udp:read:127.0.0.1:%s", port);
|
|
|
|
evt_core_add_fd (&(ctx->evts), &fdinfo);
|
|
|
|
|
|
|
|
fdinfo.fd = sock2;
|
|
|
|
fdinfo.cat->name = "udp-write";
|
|
|
|
sprintf(fdinfo.url, "udp:write:127.0.0.1:%s", port);
|
|
|
|
evt_core_add_fd (&(ctx->evts), &fdinfo);
|
|
|
|
|
2019-02-15 17:09:51 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
socket_failed:
|
|
|
|
fprintf(stderr, "UDP socket init failed\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2019-02-11 15:23:20 +00:00
|
|
|
|
2019-02-15 17:09:51 +00:00
|
|
|
void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, GPtrArray* ports) {
|
2019-02-11 21:40:00 +00:00
|
|
|
evt_core_init (&(ctx->evts));
|
|
|
|
evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_co));
|
2019-02-12 10:17:37 +00:00
|
|
|
evt_core_add_cat (&(ctx->evts), &(algo->on_udp_read));
|
|
|
|
evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_read));
|
|
|
|
evt_core_add_cat (&(ctx->evts), &(algo->on_udp_write));
|
|
|
|
evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_write));
|
2019-02-11 21:40:00 +00:00
|
|
|
|
2019-02-12 16:12:20 +00:00
|
|
|
printf("--- Categories created\n");
|
2019-02-11 22:40:37 +00:00
|
|
|
|
2019-02-11 21:40:00 +00:00
|
|
|
for (uint16_t i = 0; i < PORT_SIZE ; i++) {
|
2019-02-11 15:23:20 +00:00
|
|
|
ctx->ports[i] = 7500 + i;
|
|
|
|
}
|
2019-02-11 21:40:00 +00:00
|
|
|
create_onion_services (&(ctx->tos), &(ctx->tctl), ctx->ports, PORT_SIZE);
|
2019-02-11 15:23:20 +00:00
|
|
|
printf("--- Onion services created\n");
|
|
|
|
init_tcp_servers(ctx);
|
2019-02-12 16:12:20 +00:00
|
|
|
printf("--- TCP servers are listening\n");
|
2019-02-11 15:23:20 +00:00
|
|
|
|
2019-02-15 17:09:51 +00:00
|
|
|
g_ptr_array_foreach (ports, (void(*)(void*, void*))serv_init_udp_socket, ctx);
|
|
|
|
printf("--- UDP Sockets are configured\n");
|
|
|
|
|
2019-02-11 21:40:00 +00:00
|
|
|
evt_core_loop (&(ctx->evts));
|
2019-02-11 15:23:20 +00:00
|
|
|
|
|
|
|
destroy_resources (&(ctx->tos), &(ctx->tctl));
|
2019-02-11 09:23:38 +00:00
|
|
|
}
|