tor_multipath_voip/src/donar_init.c
2019-03-28 11:47:14 +01:00

105 lines
2.6 KiB
C

#include "donar_init.h"
void free_udp_t(void* v) {
struct udp_target* udp_t = v;
udp_t->ref_count--;
if (udp_t <= 0) {
free(udp_t);
}
}
void init_udp_remote(char* port, struct evt_core_ctx* evts) {
int sock1, sock2;
char url[1024];
struct evt_core_cat cat = {0};
struct evt_core_fdinfo fdinfo = {0};
// 1. Init shared parameters for the fdinfo structure
struct udp_target* udp_t = malloc(sizeof(struct udp_target));
if (udp_t == NULL) goto socket_failed;
memset(udp_t, 0, sizeof(struct udp_target));
udp_t->ref_count = 2;
fdinfo.cat = &cat;
fdinfo.url = url;
fdinfo.free_other = free_udp_t;
fdinfo.other = udp_t;
// 2. Duplicate sockets
sock1 = create_udp_server ("127.13.3.7", port);
if (sock1 < 0) goto socket_failed;
sock2 = dup(sock1);
if (sock2 < 0) goto socket_failed;
// 3. Register them
fdinfo.cat->name = "udp-read";
fdinfo.fd = sock1;
sprintf(fdinfo.url, "udp:read:127.0.0.1:%s", port);
evt_core_add_fd (evts, &fdinfo);
fdinfo.cat->name = "udp-write";
fdinfo.fd = sock2;
sprintf(fdinfo.url, "udp:write:127.0.0.1:%s", port);
evt_core_add_fd (evts, &fdinfo);
return;
socket_failed:
fprintf(stderr, "UDP socket init failed\n");
exit(EXIT_FAILURE);
}
void init_udp_exposed(char* port, struct evt_core_ctx* evts) {
int sock1, sock2;
char url[1024];
struct evt_core_cat cat = {0};
struct evt_core_fdinfo fdinfo = {0};
// 1. Init shared parameters for the fdinfo structure
struct udp_target* udp_t = malloc(sizeof(struct udp_target));
if (udp_t == NULL) goto socket_failed;
memset(udp_t, 0, sizeof(struct udp_target));
udp_t->ref_count = 2;
fdinfo.cat = &cat;
fdinfo.url = url;
fdinfo.free_other = free_udp_t;
fdinfo.other = udp_t;
sock1 = create_udp_client ("127.0.0.1", port);
if (sock1 < 0) goto socket_failed;
sock2 = dup(sock1);
if (sock2 < 0) goto socket_failed;
fdinfo.fd = sock1;
fdinfo.cat->name = "udp-read";
sprintf(fdinfo.url, "udp:read:127.0.0.1:%s", port);
evt_core_add_fd (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 (evts, &fdinfo);
return;
socket_failed:
fprintf(stderr, "UDP socket init failed\n");
exit(EXIT_FAILURE);
}
void free_port (void* ptr) {
free(ptr);
}
void donar_init_params(struct donar_params* dp) {
dp->onion_file = NULL;
dp->algo = NULL;
dp->is_server = 0;
dp->is_client = 0;
dp->is_healing = 0;
dp->is_waiting_bootstrap = 0;
dp->errored = 0;
dp->remote_ports = g_ptr_array_new_with_free_func (free_port);
dp->exposed_ports = g_ptr_array_new_with_free_func (free_port);
}