tor_multipath_voip/src/algo_naive.c

286 lines
8.3 KiB
C
Raw Normal View History

2019-02-11 21:40:00 +00:00
#include "algo_skel.h"
2019-02-12 10:17:37 +00:00
struct naive_ctx {
2019-02-12 21:38:42 +00:00
int ref_count;
2019-02-18 16:51:13 +00:00
GHashTable* packet_buffer;
2019-02-12 10:17:37 +00:00
};
void free_nothing(void* app_ctx) {}
2019-02-11 21:40:00 +00:00
void free_naive(void* app_ctx) {
2019-02-12 21:38:42 +00:00
struct naive_ctx* ctx = (struct naive_ctx*) app_ctx;
ctx->ref_count--;
if (ctx->ref_count <= 0) free(ctx);
2019-02-11 21:40:00 +00:00
}
2019-02-18 16:51:13 +00:00
char* get_port(char* out, char* in) {
sscanf(in, "%*[a-z]:%*[a-z]:%*[a-zA-Z0-9.]:%[0-9]", out);
return out;
}
2019-02-11 21:40:00 +00:00
void on_tcp_co(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
2019-02-18 13:35:09 +00:00
int conn_sock1, conn_sock2, port;
struct sockaddr_in addr;
2019-02-11 21:40:00 +00:00
socklen_t in_len;
2019-02-18 13:35:09 +00:00
char url[1024];
struct evt_core_cat local_cat = {0};
struct evt_core_fdinfo fdinfo = {0};
fdinfo.cat = &local_cat;
fdinfo.url = url;
2019-02-11 21:40:00 +00:00
2019-02-12 16:38:11 +00:00
in_len = sizeof(addr);
2019-02-18 13:35:09 +00:00
conn_sock1 = accept(fd, (struct sockaddr*)&addr, &in_len);
2019-02-11 21:40:00 +00:00
2019-02-12 16:38:11 +00:00
if (conn_sock1 == -1) goto co_error;
conn_sock2 = dup(conn_sock1);
if (conn_sock2 == -1) goto co_error;
2019-02-12 18:29:00 +00:00
//printf("fd=%d accepts, creating fds=%d,%d\n", fd, conn_sock1, conn_sock2);
2019-02-18 13:35:09 +00:00
struct evt_core_fdinfo* listen_info = g_hash_table_lookup(ctx->socklist, &fd);
if (listen_info == NULL) goto co_error;
2019-02-18 14:11:12 +00:00
sscanf(listen_info->url, "tcp:listen:127.0.0.1:%d", &port);
2019-02-18 13:35:09 +00:00
fdinfo.fd = conn_sock1;
fdinfo.cat->name = "tcp-read";
sprintf(fdinfo.url, "tcp:read:127.0.0.1:%d", port);
evt_core_add_fd (ctx, &fdinfo);
fdinfo.fd = conn_sock2;
fdinfo.cat->name = "tcp-write";
sprintf(fdinfo.url, "tcp:write:127.0.0.1:%d", port);
evt_core_add_fd (ctx, &fdinfo);
printf("Selected port: %d\n", port);
2019-02-11 21:40:00 +00:00
return;
co_error:
perror("Failed to handle new connection");
exit(EXIT_FAILURE);
}
2019-02-18 16:51:13 +00:00
struct buffer_packet* get_bp(struct evt_core_cat* cat, int fd, int is_read_buffer) {
struct buffer_packet* bp;
struct naive_ctx* ctx = cat->app_ctx;
bp = g_hash_table_lookup (ctx->packet_buffer, &fd);
if (bp != NULL) return bp;
if (!is_read_buffer) goto alloc_error;
bp = malloc(sizeof(struct buffer_packet));
if (bp == NULL) goto alloc_error;
memset(bp, 0, sizeof(struct buffer_packet));
bp->fdread = fd;
bp->fdwrite = -1;
bp->ref_count++;
g_hash_table_insert(ctx->packet_buffer, &(bp->fdread), bp);
return bp;
alloc_error:
perror("alloc error");
exit(EXIT_FAILURE);
}
void on_tcp_read(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd);
void on_tcp_write(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd);
void on_udp_read(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd);
void on_udp_write (struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd);
void on_tcp_read(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
struct buffer_packet* bp;
struct evt_core_fdinfo *to_fdinfo = NULL;
struct naive_ctx* app_ctx = cat->app_ctx;
int read_res = FDS_READY;
char url[255];
// 1. Read in our buffer packet
bp = get_bp(cat, fd, TRUE);
while (read_res != FDS_AGAIN && bp->mode == BP_READING) {
read_res = read_packet_from_tcp (fd, bp);
if (read_res == FDS_ERR) goto co_error;
}
if (bp->mode != BP_SWITCH_WRITE) return;
// 2. Packet has just been read, choose a destination
sprintf(url, "udp:write:127.0.0.1:%d", bp->ip.ap.str.port);
to_fdinfo = evt_core_get_from_url (ctx, url);
if (to_fdinfo == NULL) goto co_error;
// 3. Configure packet buffer for the destination
bp->mode = BP_WRITING;
bp->fdwrite = to_fdinfo->fd;
bp->ref_count++;
g_hash_table_insert(app_ctx->packet_buffer, &(bp->fdwrite), bp);
on_udp_write(ctx, to_fdinfo->cat, to_fdinfo->fd);
return;
co_error:
perror("Failed to TCP read");
exit(EXIT_FAILURE);
}
void on_tcp_write(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
struct buffer_packet* bp;
struct evt_core_fdinfo *to_fdinfo;
int write_res = FDS_READY;
char url[255];
bp = get_bp(cat, fd, FALSE);
if (bp == NULL) return;
while (write_res != FDS_AGAIN && bp->mode == BP_WRITING) {
write_res = write_packet_to_tcp(fd, bp);
if (write_res == FDS_ERR) goto co_error;
}
co_error:
perror("Failed to TCP write");
exit(EXIT_FAILURE);
}
void on_udp_read(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
}
void on_udp_write (struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
2019-02-18 14:11:12 +00:00
}
2019-02-11 22:40:37 +00:00
2019-02-18 14:11:12 +00:00
void tcp_to_udp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
2019-02-12 10:17:37 +00:00
// Init data structures for the transfer
2019-02-18 14:11:12 +00:00
char url[255];
2019-02-12 10:17:37 +00:00
struct naive_ctx* app_ctx = cat->app_ctx;
2019-02-14 10:16:38 +00:00
struct buffer_packet* bp = &(app_ctx->tcp_to_udp);
2019-02-18 14:11:12 +00:00
struct evt_core_fdinfo* fdinfo;
2019-02-14 10:16:38 +00:00
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) {
2019-02-18 14:11:12 +00:00
sprintf(url, "udp:write:127.0.0.1:%d", bp->ip.ap.str.port);
fdinfo = evt_core_get_from_url(ctx, url);
if (fdinfo == NULL) goto co_error;
write_res = write_packet_to_udp(fdinfo->fd, bp);
2019-02-14 10:16:38 +00:00
if (read_res == FDS_ERR) goto co_error;
}
2019-02-11 21:40:00 +00:00
}
return;
co_error:
2019-02-12 18:29:00 +00:00
perror("Failed to handle read write for tcp_to_udp");
2019-02-11 21:40:00 +00:00
exit(EXIT_FAILURE);
}
2019-02-12 10:17:37 +00:00
void udp_to_tcp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
// Get target file descriptor
2019-02-12 16:38:11 +00:00
struct evt_core_cat* tcp = g_hash_table_lookup (ctx->catlist, "tcp-write");
2019-02-11 22:40:37 +00:00
if (tcp == NULL || tcp->socklist->len < 1) goto co_error;
int tcp_fd = g_array_index(tcp->socklist, int, 0);
2019-02-11 21:40:00 +00:00
2019-02-12 10:17:37 +00:00
// 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) {
2019-02-18 16:51:13 +00:00
read_res = read_packet_from_udp (fd, bp);
if (read_res == FDS_ERR) goto co_error;
2019-02-14 10:16:38 +00:00
}
2019-02-12 10:17:37 +00:00
// 2. Write packet to TCP socket
if (bp->mode == BP_WRITING) {
2019-02-14 16:15:13 +00:00
write_res = write_packet_to_tcp(tcp_fd, bp);
if (write_res == FDS_ERR) goto co_error;
}
2019-02-11 22:40:37 +00:00
}
return;
co_error:
2019-02-12 18:29:00 +00:00
perror("Failed to handle read write for udp_to_tcp");
2019-02-11 22:40:37 +00:00
exit(EXIT_FAILURE);
2019-02-11 21:40:00 +00:00
}
2019-02-18 16:51:13 +00:00
void free_buffer_packet(void *v) {
struct buffer_packet* bp = (struct buffer_packet*) v;
bp->ref_count--;
if (bp->ref_count == 0) free(v);
}
void free_simple(void* v) {
free(v);
}
2019-02-11 21:40:00 +00:00
void algo_naive(struct algo_skel* as) {
2019-02-12 21:38:42 +00:00
struct naive_ctx* ctx = malloc(sizeof(struct naive_ctx));
if (ctx == NULL) goto init_err;
memset(ctx, 0, sizeof(struct naive_ctx));
2019-02-18 16:51:13 +00:00
ctx->packet_buffer = g_hash_table_new_full (g_int_hash, g_int_equal, NULL, free_buffer_packet);
as->on_init.name = "init";
as->on_init.flags = 0;
as->on_init.app_ctx = NULL;
as->on_init.free_app_ctx = NULL;
as->on_init.cb = NULL;
as->on_init.socklist = NULL;
2019-02-12 21:38:42 +00:00
2019-02-11 21:40:00 +00:00
as->on_tcp_co.name = "tcp-listen";
2019-02-12 10:17:37 +00:00
as->on_tcp_co.flags = EPOLLIN;
2019-02-11 21:40:00 +00:00
as->on_tcp_co.app_ctx = NULL;
2019-02-12 21:38:42 +00:00
as->on_tcp_co.free_app_ctx = free_nothing;
2019-02-11 21:40:00 +00:00
as->on_tcp_co.cb = on_tcp_co;
as->on_tcp_co.socklist = NULL;
2019-02-12 10:17:37 +00:00
as->on_tcp_read.name = "tcp-read";
2019-02-12 18:29:00 +00:00
as->on_tcp_read.flags = EPOLLIN | EPOLLET | EPOLLRDHUP;
2019-02-12 21:38:42 +00:00
as->on_tcp_read.app_ctx = ctx;
2019-02-12 10:17:37 +00:00
as->on_tcp_read.free_app_ctx = free_naive;
2019-02-18 16:51:13 +00:00
as->on_tcp_read.cb = on_tcp_read;
2019-02-12 10:17:37 +00:00
as->on_tcp_read.socklist = NULL;
2019-02-12 21:38:42 +00:00
ctx->ref_count++;
2019-02-12 10:17:37 +00:00
as->on_udp_read.name = "udp-read";
as->on_udp_read.flags = EPOLLIN | EPOLLET;
2019-02-12 21:38:42 +00:00
as->on_udp_read.app_ctx = ctx;
2019-02-12 10:17:37 +00:00
as->on_udp_read.free_app_ctx = free_naive;
2019-02-18 16:51:13 +00:00
as->on_udp_read.cb = on_udp_read;
2019-02-12 10:17:37 +00:00
as->on_udp_read.socklist = NULL;
2019-02-12 21:38:42 +00:00
ctx->ref_count++;
2019-02-12 10:17:37 +00:00
as->on_tcp_write.name = "tcp-write";
2019-02-12 18:29:00 +00:00
as->on_tcp_write.flags = EPOLLOUT | EPOLLET | EPOLLRDHUP;
2019-02-12 21:38:42 +00:00
as->on_tcp_write.app_ctx = ctx;
as->on_tcp_write.free_app_ctx = free_naive;
2019-02-18 16:51:13 +00:00
as->on_tcp_write.cb = on_tcp_write;
2019-02-12 10:17:37 +00:00
as->on_tcp_write.socklist = NULL;
2019-02-12 21:38:42 +00:00
ctx->ref_count++;
2019-02-12 10:17:37 +00:00
as->on_udp_write.name = "udp-write";
as->on_udp_write.flags = EPOLLOUT | EPOLLET;
2019-02-12 21:38:42 +00:00
as->on_udp_write.app_ctx = ctx;
as->on_udp_write.free_app_ctx = free_naive;
2019-02-18 16:51:13 +00:00
as->on_udp_write.cb = on_udp_write;
2019-02-12 10:17:37 +00:00
as->on_udp_write.socklist = NULL;
2019-02-12 21:38:42 +00:00
ctx->ref_count++;
2019-02-12 10:17:37 +00:00
2019-02-12 16:47:14 +00:00
return;
init_err:
fprintf(stderr, "Failed to init algo naive\n");
exit(EXIT_FAILURE);
2019-02-11 21:40:00 +00:00
}