Fix a UDP bug

This commit is contained in:
Quentin Dufour 2019-02-14 17:15:13 +01:00
parent ab568befcf
commit 8165b4a907
3 changed files with 28 additions and 17 deletions

View file

@ -4,8 +4,7 @@ struct naive_ctx {
int ref_count;
struct buffer_packet tcp_to_udp;
struct buffer_packet udp_to_tcp;
struct sockaddr_in udp_addr;
socklen_t udp_addrlen;
struct udp_target udp_t;
};
void free_nothing(void* app_ctx) {}
@ -62,9 +61,7 @@ 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,
(struct sockaddr*) &(app_ctx->udp_addr),
app_ctx->udp_addrlen);
write_res = write_packet_to_udp(udp_fd, bp, &(app_ctx->udp_t));
if (read_res == FDS_ERR) goto co_error;
}
}
@ -93,15 +90,13 @@ void udp_to_tcp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
// 1. Read packet from UDP socket
if (bp->mode == BP_READING) {
read_res = read_packet_from_udp (fd, bp,
(struct sockaddr*) &(app_ctx->udp_addr),
&(app_ctx->udp_addrlen));
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(fd, bp);
write_res = write_packet_to_tcp(tcp_fd, bp);
if (write_res == FDS_ERR) goto co_error;
}
}
@ -117,7 +112,6 @@ 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->udp_addrlen = sizeof(struct sockaddr_in);
as->on_tcp_co.name = "tcp-listen";
as->on_tcp_co.flags = EPOLLIN;

View file

@ -43,9 +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 sockaddr* addr, socklen_t addrlen) {
ssize_t nwrite, bytes_to_send;
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_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;
}
if (bp->mode == BP_READING) return FDS_ERR;
@ -56,6 +65,7 @@ enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct socka
0,
addr,
addrlen);
if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nwrite != bytes_to_send) return FDS_ERR;
@ -65,21 +75,22 @@ enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct socka
return FDS_READY;
}
enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct sockaddr* addr, socklen_t* addrlen) {
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_WRITING) return FDS_ERR;
*addrlen = sizeof(struct sockaddr_in);
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;
nread = recvfrom(fd, &(bp->ip.ap.str.payload), udp_packet_size, MSG_TRUNC,
addr, addrlen);
(struct sockaddr*)&(udp_t->addr), &(udp_t->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;
bp->ip.ap.str.size = nread + pkt_size_size;
bp->mode = BP_WRITING;

View file

@ -47,7 +47,13 @@ struct buffer_packet {
struct internet_packet ip;
};
struct udp_target {
struct sockaddr_in addr;
socklen_t addrlen;
int set;
};
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 sockaddr* addr, socklen_t addrlen);
enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct sockaddr* addr, socklen_t* addrlen);
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);