tor_multipath_voip/src/donar_server.c

109 lines
3.1 KiB
C
Raw Normal View History

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) {
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-11 21:40:00 +00:00
evt_core_add_fd(&(ctx->evts), "tcp-listen", sock);
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-11 21:40:00 +00:00
/*
2019-02-11 15:23:20 +00:00
void handle_new_tcp_client_connection(struct donar_server_ctx* ctx, struct epoll_event* evt) {
int conn_sock, err;
struct sockaddr in_addr;
socklen_t in_len;
struct epoll_event current_event;
in_len = sizeof(in_addr);
conn_sock = accept(evt->data.fd, &in_addr, &in_len);
if (conn_sock == -1) goto co_error;
err = make_socket_non_blocking (conn_sock);
if (err == -1) goto co_error;
current_event.events = EPOLLIN | EPOLLET;
current_event.data.fd = conn_sock;
if (epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, conn_sock, &current_event) == -1)
goto co_error;
return;
co_error:
perror("Failed to handle new connection");
exit(EXIT_FAILURE);
}
void handle_new_tcp_client_rw(struct donar_server_ctx* ctx, struct epoll_event* evt) {
char buffer[128];
int nread, nwrite;
while(1) {
nread = read(evt->data.fd, buffer, sizeof(buffer));
if (nread == 0) break;
if (nread == -1 && errno == EAGAIN) break;
if (nread == -1) goto co_error;
nwrite = write(evt->data.fd, buffer, nread);
if (nwrite == -1 && errno == EAGAIN) {
printf("Lost data EAGAIN\n");
break;
}
if (nwrite == -1) goto co_error;
if (nwrite != nread) {
printf("Lost data not everything has been written\n");
break;
}
}
return;
co_error:
perror("Failed to handle read write");
exit(EXIT_FAILURE);
2019-02-11 21:40:00 +00:00
}*/
2019-02-11 15:23:20 +00:00
2019-02-11 21:40:00 +00:00
void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo) {
evt_core_init (&(ctx->evts));
evt_core_add_cat (&(ctx->evts), &(algo->on_udp_data));
evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_data));
evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_co));
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-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
}