181 lines
5.3 KiB
C
181 lines
5.3 KiB
C
#include "algo_skel.h"
|
|
|
|
struct naive_ctx {
|
|
int ref_count;
|
|
struct buffer_packet tcp_to_udp;
|
|
struct buffer_packet udp_to_tcp;
|
|
};
|
|
|
|
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, port;
|
|
struct sockaddr_in addr;
|
|
socklen_t in_len;
|
|
char url[1024];
|
|
struct evt_core_cat local_cat = {0};
|
|
struct evt_core_fdinfo fdinfo = {0};
|
|
fdinfo.cat = &local_cat;
|
|
fdinfo.url = url;
|
|
|
|
in_len = sizeof(addr);
|
|
conn_sock1 = accept(fd, (struct sockaddr*)&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);
|
|
|
|
struct evt_core_fdinfo* listen_info = g_hash_table_lookup(ctx->socklist, &fd);
|
|
if (listen_info == NULL) goto co_error;
|
|
sscanf(listen_info->url, "tcp:listen:127.0.0.1:%d", &port);
|
|
|
|
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);
|
|
|
|
return;
|
|
|
|
co_error:
|
|
perror("Failed to handle new connection");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
char* get_port(char* out, char* in) {
|
|
sscanf(in, "%*[a-z]:%*[a-z]:%*[a-zA-Z0-9.]:%[0-9]", out);
|
|
return out;
|
|
}
|
|
|
|
void tcp_to_udp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
|
|
// Init data structures for the transfer
|
|
char url[255];
|
|
struct naive_ctx* app_ctx = cat->app_ctx;
|
|
struct buffer_packet* bp = &(app_ctx->tcp_to_udp);
|
|
struct evt_core_fdinfo* fdinfo;
|
|
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) {
|
|
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);
|
|
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);
|
|
}
|