Review UDP management

This commit is contained in:
Quentin Dufour 2019-02-19 14:49:44 +01:00
parent 837633e15e
commit e346230c60
7 changed files with 30 additions and 18 deletions

View file

@ -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");

View file

@ -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;

View file

@ -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);

View file

@ -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);
}

View file

@ -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);

View file

@ -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;

View file

@ -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);