tor_multipath_voip/src/donar_server.c
2019-02-12 17:12:20 +01:00

73 lines
2.3 KiB
C

#include "donar_server.h"
void create_onion_services(struct tor_os_str* tos, struct tor_ctl* tctl, uint16_t* ports, int ports_count) {
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);
}
void init_tcp_servers(struct donar_server_ctx* ctx) {
char buffer[6];
int err, sock = 0;
for (int i = 0; i < PORT_SIZE; i++) {
sprintf (buffer, "%d", ctx->ports[i]);
sock = create_tcp_server (buffer);
if (sock < 0) goto socket_create_err;
err = listen(sock, SOMAXCONN);
if (err != 0) goto socket_create_err;
evt_core_add_fd(&(ctx->evts), "tcp-listen", sock);
}
return;
socket_create_err:
fprintf(stderr, "Unable to create a TCP socket\n");
exit(EXIT_FAILURE);
}
void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, char* udp_host, char* udp_port) {
evt_core_init (&(ctx->evts));
evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_co));
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));
printf("--- Categories created\n");
int sock = create_udp_client (udp_host, udp_port);
if (sock < 0) {
fprintf(stderr, "Unable to create a UDP client socket\n");
exit(EXIT_FAILURE);
}
evt_core_add_fd (&(ctx->evts), "udp-read", sock);
evt_core_add_fd (&(ctx->evts), "udp-write", dup(sock));
printf("--- UDP client is connected\n");
for (uint16_t i = 0; i < PORT_SIZE ; i++) {
ctx->ports[i] = 7500 + i;
}
create_onion_services (&(ctx->tos), &(ctx->tctl), ctx->ports, PORT_SIZE);
printf("--- Onion services created\n");
init_tcp_servers(ctx);
printf("--- TCP servers are listening\n");
evt_core_loop (&(ctx->evts));
destroy_resources (&(ctx->tos), &(ctx->tctl));
}