tor_multipath_voip/src/algo_naive.c
2019-02-14 17:15:13 +01:00

160 lines
4.7 KiB
C

#include "algo_skel.h"
struct naive_ctx {
int ref_count;
struct buffer_packet tcp_to_udp;
struct buffer_packet udp_to_tcp;
struct udp_target udp_t;
};
void free_nothing(void* app_ctx) {}
void free_naive(void* app_ctx) {
struct naive_ctx* ctx = (struct naive_ctx*) app_ctx;
ctx->ref_count--;
if (ctx->ref_count <= 0) free(ctx);
}
void on_tcp_co(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
int conn_sock1, conn_sock2;
struct sockaddr addr;
socklen_t in_len;
struct epoll_event current_event;
in_len = sizeof(addr);
conn_sock1 = accept(fd, &addr, &in_len);
if (conn_sock1 == -1) goto co_error;
conn_sock2 = dup(conn_sock1);
if (conn_sock2 == -1) goto co_error;
//printf("fd=%d accepts, creating fds=%d,%d\n", fd, conn_sock1, conn_sock2);
evt_core_add_fd (ctx, "tcp-read", conn_sock1);
evt_core_add_fd (ctx, "tcp-write", conn_sock2);
return;
co_error:
perror("Failed to handle new connection");
exit(EXIT_FAILURE);
}
void tcp_to_udp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
// Get target file descriptor
struct evt_core_cat* udp = g_hash_table_lookup (ctx->catlist, "udp-write");
if (udp == NULL || udp->socklist->len < 1) goto co_error;
int udp_fd = g_array_index(udp->socklist, int, 0);
// Init data structures for the transfer
struct naive_ctx* app_ctx = cat->app_ctx;
struct buffer_packet* bp = &(app_ctx->tcp_to_udp);
int read_res = FDS_READY;
int write_res = FDS_READY;
// Either we can read something, either we can write something
while ((read_res != FDS_AGAIN && bp->mode == BP_READING)
|| (write_res != FDS_AGAIN && bp->mode == BP_WRITING)) {
// 1. Read packet from TCP socket
if (bp->mode == BP_READING) {
read_res = read_packet_from_tcp (fd, bp);
if (read_res == FDS_ERR) goto co_error;
}
// 2. Write packet to UDP socket
if (bp->mode == BP_WRITING) {
write_res = write_packet_to_udp(udp_fd, bp, &(app_ctx->udp_t));
if (read_res == FDS_ERR) goto co_error;
}
}
return;
co_error:
perror("Failed to handle read write for tcp_to_udp");
exit(EXIT_FAILURE);
}
void udp_to_tcp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
// Get target file descriptor
struct evt_core_cat* tcp = g_hash_table_lookup (ctx->catlist, "tcp-write");
if (tcp == NULL || tcp->socklist->len < 1) goto co_error;
int tcp_fd = g_array_index(tcp->socklist, int, 0);
// Init data structures for the transfer
struct naive_ctx* app_ctx = cat->app_ctx;
struct buffer_packet* bp = &(app_ctx->udp_to_tcp);
int read_res = FDS_READY;
int write_res = FDS_READY;
// Either we can read something, either we can write something
while ((read_res != FDS_AGAIN && bp->mode == BP_READING)
|| (write_res != FDS_AGAIN && bp->mode == BP_WRITING)) {
// 1. Read packet from UDP socket
if (bp->mode == BP_READING) {
read_res = read_packet_from_udp (fd, bp, &(app_ctx->udp_t));
if (read_res == FDS_ERR) goto co_error;
}
// 2. Write packet to TCP socket
if (bp->mode == BP_WRITING) {
write_res = write_packet_to_tcp(tcp_fd, bp);
if (write_res == FDS_ERR) goto co_error;
}
}
return;
co_error:
perror("Failed to handle read write for udp_to_tcp");
exit(EXIT_FAILURE);
}
void algo_naive(struct algo_skel* as) {
struct naive_ctx* ctx = malloc(sizeof(struct naive_ctx));
if (ctx == NULL) goto init_err;
memset(ctx, 0, sizeof(struct naive_ctx));
as->on_tcp_co.name = "tcp-listen";
as->on_tcp_co.flags = EPOLLIN;
as->on_tcp_co.app_ctx = NULL;
as->on_tcp_co.free_app_ctx = free_nothing;
as->on_tcp_co.cb = on_tcp_co;
as->on_tcp_co.socklist = NULL;
as->on_tcp_read.name = "tcp-read";
as->on_tcp_read.flags = EPOLLIN | EPOLLET | EPOLLRDHUP;
as->on_tcp_read.app_ctx = ctx;
as->on_tcp_read.free_app_ctx = free_naive;
as->on_tcp_read.cb = tcp_to_udp;
as->on_tcp_read.socklist = NULL;
ctx->ref_count++;
as->on_udp_read.name = "udp-read";
as->on_udp_read.flags = EPOLLIN | EPOLLET;
as->on_udp_read.app_ctx = ctx;
as->on_udp_read.free_app_ctx = free_naive;
as->on_udp_read.cb = udp_to_tcp;
as->on_udp_read.socklist = NULL;
ctx->ref_count++;
as->on_tcp_write.name = "tcp-write";
as->on_tcp_write.flags = EPOLLOUT | EPOLLET | EPOLLRDHUP;
as->on_tcp_write.app_ctx = ctx;
as->on_tcp_write.free_app_ctx = free_naive;
as->on_tcp_write.cb = udp_to_tcp;
as->on_tcp_write.socklist = NULL;
ctx->ref_count++;
as->on_udp_write.name = "udp-write";
as->on_udp_write.flags = EPOLLOUT | EPOLLET;
as->on_udp_write.app_ctx = ctx;
as->on_udp_write.free_app_ctx = free_naive;
as->on_udp_write.cb = tcp_to_udp;
as->on_udp_write.socklist = NULL;
ctx->ref_count++;
return;
init_err:
fprintf(stderr, "Failed to init algo naive\n");
exit(EXIT_FAILURE);
}