diff --git a/src/algo_naive.c b/src/algo_naive.c index b3f28a7..c794100 100644 --- a/src/algo_naive.c +++ b/src/algo_naive.c @@ -4,7 +4,6 @@ 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) {} @@ -34,7 +33,7 @@ void on_tcp_co(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) { 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:%d", &port); + sscanf(listen_info->url, "tcp:listen:127.0.0.1:%d", &port); fdinfo.fd = conn_sock1; fdinfo.cat->name = "tcp-read"; @@ -55,15 +54,17 @@ co_error: 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); +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; @@ -79,7 +80,10 @@ void tcp_to_udp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) { // 2. Write packet to UDP socket if (bp->mode == BP_WRITING) { - write_res = write_packet_to_udp(udp_fd, bp, &(app_ctx->udp_t)); + 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; } } diff --git a/src/donar_server.c b/src/donar_server.c index d599510..bcebeab 100644 --- a/src/donar_server.c +++ b/src/donar_server.c @@ -40,7 +40,7 @@ void init_tcp_servers(struct donar_server_ctx* ctx) { fdinfo.cat->name = "tcp-listen"; fdinfo.fd = sock; - sprintf(fdinfo.url, "tcp:listen:%d", ctx->ports[i]); + sprintf(fdinfo.url, "tcp:listen:127.0.0.1:%d", ctx->ports[i]); evt_core_add_fd(&(ctx->evts), &fdinfo); } return; diff --git a/src/evt_core.c b/src/evt_core.c index 885cd1d..37bc76d 100644 --- a/src/evt_core.c +++ b/src/evt_core.c @@ -174,3 +174,10 @@ void evt_core_loop(struct evt_core_ctx* ctx) { evt_core_free(ctx); } + +struct evt_core_fdinfo* evt_core_get_from_fd(struct evt_core_ctx* ctx, int fd) { + return g_hash_table_lookup (ctx->socklist, &fd); +} +struct evt_core_fdinfo* evt_core_get_from_url(struct evt_core_ctx* ctx, char* url) { + return g_hash_table_lookup (ctx->urltofd, url); +} diff --git a/src/evt_core.h b/src/evt_core.h index 25d99c1..63b1922 100644 --- a/src/evt_core.h +++ b/src/evt_core.h @@ -46,3 +46,5 @@ void evt_core_add_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* user_data struct evt_core_cat* evt_core_rm_fd(struct evt_core_ctx* ctx, int fd); void evt_core_free(struct evt_core_ctx* ctx); void evt_core_loop(struct evt_core_ctx* ctx); +struct evt_core_fdinfo* evt_core_get_from_fd(struct evt_core_ctx* ctx, int fd); +struct evt_core_fdinfo* evt_core_get_from_url(struct evt_core_ctx* ctx, char* url); diff --git a/src/packet.c b/src/packet.c index 0bd29f3..34bf579 100644 --- a/src/packet.c +++ b/src/packet.c @@ -43,28 +43,18 @@ enum FD_STATE write_packet_to_tcp(int fd, struct buffer_packet* bp) { return FDS_READY; } -enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct udp_target* udp_t) { +enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp) { ssize_t nwrite; size_t bytes_to_send; - size_t pkt_size_size = sizeof(bp->ip.ap.str.size); - struct sockaddr* addr = (struct sockaddr*)&(udp_t->addr); - socklen_t addrlen = udp_t->addrlen; - - if (!udp_t->set) { - printf("UDP address is not yet set. Hope we are a connected client...\n"); - addr = NULL; - addrlen = 0; - } + size_t pkt_header_size = sizeof(bp->ip.ap.str) - sizeof(char); if (bp->mode == BP_READING) return FDS_ERR; - bytes_to_send = bp->ip.ap.str.size - pkt_size_size; - nwrite = sendto(fd, + bytes_to_send = bp->ip.ap.str.size - pkt_header_size; + nwrite = send(fd, &(bp->ip.ap.str.payload), bytes_to_send, - 0, - addr, - addrlen); + 0); if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN; if (nwrite != bytes_to_send) return FDS_ERR; @@ -75,23 +65,20 @@ enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct udp_t return FDS_READY; } -enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct udp_target* udp_t) { +enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp) { ssize_t nread; if (bp->mode == BP_WRITING) return FDS_ERR; - udp_t->addrlen = sizeof(struct sockaddr_in); - size_t pkt_size_size = sizeof(bp->ip.ap.str.size); - size_t udp_packet_size = sizeof(struct internet_packet) - pkt_size_size; + size_t pkt_header_size = sizeof(bp->ip.ap.str) - sizeof(char); // We remove the payload + size_t udp_packet_size = sizeof(struct internet_packet) - pkt_header_size; - nread = recvfrom(fd, &(bp->ip.ap.str.payload), udp_packet_size, MSG_TRUNC, - (struct sockaddr*)&(udp_t->addr), &(udp_t->addrlen)); + nread = recv(fd, &(bp->ip.ap.str.payload), udp_packet_size, MSG_TRUNC); if ((int)nread > (int)udp_packet_size) return FDS_ERR; if (nread == -1 && errno == EAGAIN) return FDS_AGAIN; if (nread == -1) return FDS_ERR; - udp_t->set = 1; - bp->ip.ap.str.size = nread + pkt_size_size; + bp->ip.ap.str.size = nread + pkt_header_size; bp->mode = BP_WRITING; bp->awrite = 0; diff --git a/src/packet.h b/src/packet.h index 234de96..f8d1f8f 100644 --- a/src/packet.h +++ b/src/packet.h @@ -57,5 +57,5 @@ struct udp_target { enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp); enum FD_STATE write_packet_to_tcp(int fd, struct buffer_packet* bp); -enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct udp_target* udp_t); -enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct udp_target* udp_t); +enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp); +enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp);