From e346230c60d697a12cb7d2b319302e9a0b68be20 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 19 Feb 2019 14:49:44 +0100 Subject: [PATCH] Review UDP management --- src/algo_naive.c | 4 ++-- src/donar_client.c | 7 +++++++ src/donar_server.c | 5 +++++ src/evt_core.c | 4 ++++ src/evt_core.h | 1 + src/packet.c | 23 +++++++++-------------- src/packet.h | 4 ++-- 7 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/algo_naive.c b/src/algo_naive.c index ecd6f55..2d69b8a 100644 --- a/src/algo_naive.c +++ b/src/algo_naive.c @@ -219,7 +219,7 @@ void on_udp_read(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { // 2. Read packet from socket bp->ip.ap.str.port = url_get_port_int (fdinfo->url); - read_res = read_packet_from_udp (fdinfo->fd, bp); + read_res = read_packet_from_udp (fdinfo->fd, bp, fdinfo->other); if (read_res == FDS_ERR) goto co_error; if (bp->mode != BP_WRITING) return; printf("A full UDP packet has been read from UDP\n"); @@ -249,7 +249,7 @@ void on_udp_write (struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { if ((bp = get_write_buffer(app_ctx, fdinfo)) == NULL) return; // 2. Write buffer - write_res = write_packet_to_udp(fdinfo->fd, bp); + write_res = write_packet_to_udp(fdinfo->fd, bp, fdinfo->other); if (write_res == FDS_ERR) goto co_error; if (bp->mode != BP_READING) return; printf("A full UDP packet has been written to UDP\n"); diff --git a/src/donar_client.c b/src/donar_client.c index 3151fc5..23ba560 100644 --- a/src/donar_client.c +++ b/src/donar_client.c @@ -117,6 +117,7 @@ void init_udp_socket(char* port, struct donar_client_ctx* ctx) { struct evt_core_fdinfo fdinfo = {0}; fdinfo.cat = &cat; fdinfo.url = url; + fdinfo.free_other = evt_core_free_app_ctx_simple; sock1 = create_udp_server (port); if (sock1 < 0) goto socket_failed; @@ -125,11 +126,17 @@ void init_udp_socket(char* port, struct donar_client_ctx* ctx) { fdinfo.cat->name = "udp-read"; fdinfo.fd = sock1; + fdinfo.other = malloc(sizeof(struct udp_target)); + if (fdinfo.other == NULL) goto socket_failed; + memset(fdinfo.other, 0, sizeof(struct udp_target)); sprintf(fdinfo.url, "udp:read:127.0.0.1:%s", port); evt_core_add_fd (&(ctx->evts), &fdinfo); fdinfo.cat->name = "udp-write"; fdinfo.fd = sock2; + fdinfo.other = malloc(sizeof(struct udp_target)); + if (fdinfo.other == NULL) goto socket_failed; + memset(fdinfo.other, 0, sizeof(struct udp_target)); sprintf(fdinfo.url, "udp:write:127.0.0.1:%s", port); evt_core_add_fd (&(ctx->evts), &fdinfo); return; diff --git a/src/donar_server.c b/src/donar_server.c index bcebeab..18a3a69 100644 --- a/src/donar_server.c +++ b/src/donar_server.c @@ -57,6 +57,7 @@ void serv_init_udp_socket(char* port, struct donar_server_ctx* ctx) { struct evt_core_fdinfo fdinfo = {0}; fdinfo.cat = &cat; fdinfo.url = url; + fdinfo.free_other = evt_core_free_app_ctx_simple; sock1 = create_udp_client ("127.0.0.1", port); if (sock1 < 0) goto socket_failed; @@ -65,11 +66,15 @@ void serv_init_udp_socket(char* port, struct donar_server_ctx* ctx) { fdinfo.fd = sock1; fdinfo.cat->name = "udp-read"; + fdinfo.other = malloc(sizeof(struct udp_target)); + if (fdinfo.other == NULL) goto socket_failed; sprintf(fdinfo.url, "udp:read:127.0.0.1:%s", port); evt_core_add_fd (&(ctx->evts), &fdinfo); fdinfo.fd = sock2; fdinfo.cat->name = "udp-write"; + fdinfo.other = malloc(sizeof(struct udp_target)); + if (fdinfo.other == NULL) goto socket_failed; sprintf(fdinfo.url, "udp:write:127.0.0.1:%s", port); evt_core_add_fd (&(ctx->evts), &fdinfo); diff --git a/src/evt_core.c b/src/evt_core.c index 2354b9b..b60f55e 100644 --- a/src/evt_core.c +++ b/src/evt_core.c @@ -184,3 +184,7 @@ 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) { return g_hash_table_lookup (ctx->urltofd, url); } + +void evt_core_free_app_ctx_simple(void* v) { + free(v); +} diff --git a/src/evt_core.h b/src/evt_core.h index 523617c..cce94cd 100644 --- a/src/evt_core.h +++ b/src/evt_core.h @@ -50,3 +50,4 @@ 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); +void evt_core_free_app_ctx_simple(void* v); diff --git a/src/packet.c b/src/packet.c index 86a7852..bb798e3 100644 --- a/src/packet.c +++ b/src/packet.c @@ -44,16 +44,10 @@ 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) { +enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct udp_target* udp_t) { ssize_t nwrite; size_t bytes_to_send; size_t pkt_header_size = sizeof(bp->ip.ap.str) - sizeof(char); - struct sockaddr_in addr = { - .sin_family = AF_INET, - .sin_port = htons(bp->ip.ap.str.port), - }; - inet_aton("127.0.0.1", &addr.sin_addr); - socklen_t addr_len = sizeof(addr); if (bp->mode != BP_WRITING) return FDS_ERR; @@ -62,8 +56,8 @@ enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp) { &(bp->ip.ap.str.payload), bytes_to_send, 0, - (struct sockaddr*)&addr, - addr_len); + (struct sockaddr*)&udp_t->addr, + udp_t->addrlen); if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN; if (nwrite != bytes_to_send) return FDS_ERR; @@ -74,26 +68,27 @@ enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp) { return FDS_READY; } -enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp) { +enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct udp_target* udp_t) { ssize_t nread; if (bp->mode != BP_READING) return FDS_ERR; - struct sockaddr_in addr = {0}; - socklen_t addr_len = sizeof(addr); 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; + socklen_t addrlen = sizeof(struct sockaddr_in); nread = recvfrom(fd, &(bp->ip.ap.str.payload), udp_packet_size, MSG_TRUNC, - (struct sockaddr*)&addr, - &addr_len); + (struct sockaddr*)&udp_t->addr, + &addrlen); 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; + udp_t->addrlen = addrlen; bp->ip.ap.str.size = nread + pkt_header_size; bp->mode = BP_WRITING; diff --git a/src/packet.h b/src/packet.h index 49044a5..77a487c 100644 --- a/src/packet.h +++ b/src/packet.h @@ -58,5 +58,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); -enum FD_STATE read_packet_from_udp (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);