#include "algo_skel.h" struct naive_ctx { int ref_count; GHashTable* packet_buffer; }; 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); } char* get_port(char* out, char* in) { sscanf(in, "%*[a-z]:%*[a-z]:%*[a-zA-Z0-9.]:%[0-9]", out); return out; } 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); } 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) { } 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); 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 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); } 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)); 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; 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 = on_tcp_read; 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 = on_udp_read; 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 = on_tcp_write; 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 = on_udp_write; as->on_udp_write.socklist = NULL; ctx->ref_count++; return; init_err: fprintf(stderr, "Failed to init algo naive\n"); exit(EXIT_FAILURE); }