tor_multipath_voip/src/donar_client.c

81 lines
2.6 KiB
C
Raw Normal View History

#include "donar_client.h"
void load_onion_services(struct donar_client_ctx* ctx, char* onion_file, int ports_count) {
tor_os_create (&(ctx->tos), onion_file, NULL, ports_count);
tor_os_read (&(ctx->tos));
}
void init_tcp_clients(struct donar_client_ctx* ctx) {
2019-02-12 20:45:15 +00:00
for (uint16_t i = 0; i < CLIENT_PORT_SIZE ; i++) {
ctx->ports[i] = 7500 + i;
}
int sock1, sock2, err;
char target_host[255];
for (int i = 0; i < CLIENT_PORT_SIZE; ) {
sock1 = create_tcp_client("127.0.0.1", "9050");
err = socks5_handshake(sock1);
if (err < 0) goto failed_socks5;
if (strlen(ctx->tos.keys[i].pub) > 254) {
fprintf(stderr, "Domain name is too long\n");
exit(EXIT_FAILURE);
}
sprintf(target_host, "%s.onion", ctx->tos.keys[i].pub);
err = socks5_connect_dns(sock1, target_host, ctx->ports[i]);
if (err < 0) goto failed_socks5;
err = socks5_reply(sock1);
if (err < 0) goto failed_socks5;
sock2 = dup(sock1);
if (sock2 < 0) goto failed_socks5;
evt_core_add_fd (&(ctx->evts), "tcp-read", sock1);
evt_core_add_fd (&(ctx->evts), "tcp-write", sock2);
printf("Socket %d/%d %s:%d has been added to the pool\n", i+1, CLIENT_PORT_SIZE, target_host, ctx->ports[i]);
i++;
continue;
failed_socks5:
fprintf(stderr, "Failed connection for socket %d/%d. Sleeping 2 seconds...\n",i+1, CLIENT_PORT_SIZE);
close(sock1);
sleep(2);
}
}
void init_udp_server(struct donar_client_ctx* ctx, char* port) {
int sock1, sock2;
sock1 = create_udp_server(port);
if (sock1 < 0) goto socket_failed;
sock2 = dup(sock1);
if (sock2 < 0) goto socket_failed;
evt_core_add_fd (&(ctx->evts), "udp-read", sock1);
evt_core_add_fd (&(ctx->evts), "udp-write", sock2);
return;
socket_failed:
fprintf(stderr, "Socket initialization for the UDP server failed\n");
exit(EXIT_FAILURE);
}
void donar_client(struct donar_client_ctx* ctx, struct algo_skel* algo, char* onion_file, char* 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");
load_onion_services (ctx, onion_file, CLIENT_PORT_SIZE);
printf("--- Onion services loaded\n");
2019-02-14 14:40:05 +00:00
init_tcp_clients(ctx);
2019-02-12 20:45:15 +00:00
printf("--- TCP Clients Connected\n");
2019-02-14 14:40:05 +00:00
init_udp_server (ctx, port);
printf("--- UDP Server is listening\n");
evt_core_loop(&(ctx->evts));
tor_os_free (&(ctx->tos));
}