Non tested but complete refactor of algo niave

This commit is contained in:
Quentin Dufour 2019-02-14 13:50:43 +01:00
parent 3b3c5b6dbd
commit b42170d6a5
5 changed files with 165 additions and 108 deletions

View file

@ -21,6 +21,8 @@ list(APPEND CSOURCES
src/algo_naive.c src/algo_naive.c
src/utils.h src/utils.h
src/utils.c src/utils.c
src/packet.h
src/packet.c
) )
add_executable(donar ${CSOURCES} src/donar.c) add_executable(donar ${CSOURCES} src/donar.c)

View file

@ -1,12 +1,9 @@
#include "algo_skel.h" #include "algo_skel.h"
#define NAIVE_BUFFER 128
struct naive_ctx { struct naive_ctx {
int ref_count; int ref_count;
struct buffer_packet tcp_to_udp; struct buffer_packet tcp_to_udp;
struct buffer_packet udp_to_tcp; struct buffer_packet udp_to_tcp;
struct ring_buffer tcp_to_udp_rb;
struct ring_buffer udp_to_tcp_rb;
struct sockaddr_in udp_addr; struct sockaddr_in udp_addr;
socklen_t udp_addrlen; socklen_t udp_addrlen;
}; };
@ -41,50 +38,6 @@ co_error:
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp) {
int nread;
size_t pkt_size_size = sizeof(bp->ip.ap.str.size);
if (bp->mode == BP_WRITING) return FDS_ERR;
while (bp->aread < pkt_size_size) {
nread = read(fd, &(bp->ip.ap.raw) + bp->aread, pkt_size_size - bp->aread);
if (nread == 0) return FDS_AGAIN;
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nread == -1) return FDS_ERR;
bp->aread += nread;
}
while (bp->aread < bp->ip.ap.str.size) {
nread = read(fd, &(bp->ip.ap.raw) + bp->aread, bp->ip.ap.str.size - bp->aread);
if (nread == 0) return FDS_AGAIN;
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nread == -1) return FDS_ERR;
bp->aread += nread;
}
bp->mode = BP_WRITING;
return FDS_READY;
}
enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct sockaddr* addr, socklen_t addrlen) {
int nwrite, bytes_to_send;
size_t pkt_size_size = sizeof(bp->ip.ap.str.size);
if (bp->mode == BP_READING) return FDS_ERR;
bytes_to_send = bp->ip.ap.str.size - pkt_size_size;
nwrite = sendto(fd,
&(bp->ip.ap.str.payload),
bytes_to_send,
0,
addr,
addrlen);
if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nwrite != bytes_to_send) return FDS_ERR;
return FDS_READY;
}
void tcp_to_udp(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) {
// Get target file descriptor // Get target file descriptor
struct evt_core_cat* udp = g_hash_table_lookup (ctx->catlist, "udp-write"); struct evt_core_cat* udp = g_hash_table_lookup (ctx->catlist, "udp-write");
@ -109,8 +62,7 @@ void tcp_to_udp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
// 2. Write packet to UDP socket // 2. Write packet to UDP socket
if (bp->mode == BP_WRITING) { if (bp->mode == BP_WRITING) {
write_res = write_packet_to_udp(udp_fd, write_res = write_packet_to_udp(udp_fd, bp,
bp,
(struct sockaddr*) &(app_ctx->udp_addr), (struct sockaddr*) &(app_ctx->udp_addr),
app_ctx->udp_addrlen); app_ctx->udp_addrlen);
if (read_res == FDS_ERR) goto co_error; if (read_res == FDS_ERR) goto co_error;
@ -131,29 +83,27 @@ void udp_to_tcp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
// Init data structures for the transfer // Init data structures for the transfer
struct naive_ctx* app_ctx = cat->app_ctx; struct naive_ctx* app_ctx = cat->app_ctx;
struct ring_buffer* rb = &(app_ctx->udp_to_tcp_rb); struct buffer_packet* bp = &(app_ctx->udp_to_tcp);
char buffer[NET_BUFFER_SIZE]; int read_res = FDS_READY;
int nread, nwrite, rb_free_space; int write_res = FDS_READY;
while (1) { // Either we can read something, either we can write something
rb_free_space = ring_buffer_free_space (rb); // We can't afford to read more while ((read_res != FDS_AGAIN && bp->mode == BP_READING)
app_ctx->udp_addrlen = sizeof(struct sockaddr_in); || (write_res != FDS_AGAIN && bp->mode == BP_WRITING)) {
nread = recvfrom(fd, buffer, rb_free_space, MSG_TRUNC, (struct sockaddr*)&(app_ctx->udp_addr), &(app_ctx->udp_addrlen)); // Effective read
if (nread == 0) return; // End of file // 1. Read packet from UDP socket
if (nread == -1 && errno == EAGAIN) return; // No more data to read if (bp->mode == BP_READING) {
if (nread == -1) goto co_error; // A bad error read_res = read_packet_from_udp (fd, bp,
if (nread > rb_free_space) { (struct sockaddr*) &(app_ctx->udp_addr),
fprintf(stderr, "[warn!] we partially read a UDP packet\n"); &(app_ctx->udp_addrlen));
} else { if (read_res == FDS_ERR) goto co_error;
ring_buffer_write(rb, buffer, nread); // Persist read data in our buffer
} }
nread = ring_buffer_read(rb, buffer, RING_BUFFER_SIZE); // 2. Write packet to TCP socket
nwrite = write(tcp_fd, buffer, nread); if (bp->mode == BP_WRITING) {
if (nwrite == -1 && errno == EAGAIN) return; write_res = write_packet_to_tcp(fd, bp);
if (nwrite == -1) goto co_error; if (write_res == FDS_ERR) goto co_error;
printf("written to tcp_fd=%d\n", nwrite); }
ring_buffer_ack_read (rb, nwrite);
} }
return; return;

View file

@ -3,48 +3,10 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "packet.h"
#include "evt_core.h" #include "evt_core.h"
#include "utils.h" #include "utils.h"
/*
* man 7 udp about receive operation on UDP sockets:
*
* > All receive operations return only one packet. When the packet is smaller than the passed
* > buffer, only that much data is returned; when it is bigger, the packet is truncated and the
* > MSG_TRUNC flag is set. MSG_WAITALL is not supported.
*/
enum FD_STATE {
FDS_READY,
FDS_AGAIN,
FDS_ERR
};
enum BP_MODE {
BP_READING,
BP_WRITING
};
union abstract_packet {
char raw;
struct {
uint16_t size;
char payload;
} str;
};
struct internet_packet {
union abstract_packet ap;
char rest[1499]; // MTU = 1500, 1 byte in the union
};
struct buffer_packet {
uint8_t mode;
uint16_t aread;
uint16_t awrite;
struct internet_packet ip;
};
struct algo_skel { struct algo_skel {
struct evt_core_cat on_udp_read; struct evt_core_cat on_udp_read;
struct evt_core_cat on_tcp_read; struct evt_core_cat on_tcp_read;

90
src/packet.c Normal file
View file

@ -0,0 +1,90 @@
#include "packet.h"
enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp) {
int nread;
size_t pkt_size_size = sizeof(bp->ip.ap.str.size);
if (bp->mode == BP_WRITING) return FDS_ERR;
while (bp->aread < pkt_size_size) {
nread = read(fd, &(bp->ip.ap.raw) + bp->aread, pkt_size_size - bp->aread);
if (nread == 0) return FDS_AGAIN;
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nread == -1) return FDS_ERR;
bp->aread += nread;
}
while (bp->aread < bp->ip.ap.str.size) {
nread = read(fd, &(bp->ip.ap.raw) + bp->aread, bp->ip.ap.str.size - bp->aread);
if (nread == 0) return FDS_AGAIN;
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nread == -1) return FDS_ERR;
bp->aread += nread;
}
bp->mode = BP_WRITING;
bp->awrite = 0;
return FDS_READY;
}
enum FD_STATE write_packet_to_tcp(int fd, struct buffer_packet* bp) {
int nwrite;
while (bp->awrite < bp->ip.ap.str.size) {
nwrite = send(fd, &(bp->ip.ap.raw), bp->ip.ap.str.size, 0);
if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nwrite == -1) return FDS_ERR;
bp->awrite += nwrite;
}
bp->mode = BP_READING;
bp->aread = 0;
return FDS_READY;
}
enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct sockaddr* addr, socklen_t addrlen) {
int nwrite, bytes_to_send;
size_t pkt_size_size = sizeof(bp->ip.ap.str.size);
if (bp->mode == BP_READING) return FDS_ERR;
bytes_to_send = bp->ip.ap.str.size - pkt_size_size;
nwrite = sendto(fd,
&(bp->ip.ap.str.payload),
bytes_to_send,
0,
addr,
addrlen);
if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nwrite != bytes_to_send) return FDS_ERR;
bp->mode = BP_READING;
bp->aread = 0;
return FDS_READY;
}
enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct sockaddr* addr, socklen_t* addrlen) {
int nread;
if (bp->mode == BP_WRITING) return FDS_ERR;
*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);
if (nread > udp_packet_size) return FDS_ERR;
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nread == -1) return FDS_ERR;
bp->ip.ap.str.size = nread + pkt_size_size;
bp->mode = BP_WRITING;
bp->awrite = 0;
return FDS_READY;
}

53
src/packet.h Normal file
View file

@ -0,0 +1,53 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
/*
* man 7 udp about receive operation on UDP sockets:
*
* > All receive operations return only one packet. When the packet is smaller than the passed
* > buffer, only that much data is returned; when it is bigger, the packet is truncated and the
* > MSG_TRUNC flag is set. MSG_WAITALL is not supported.
*/
enum FD_STATE {
FDS_READY,
FDS_AGAIN,
FDS_ERR
};
enum BP_MODE {
BP_READING,
BP_WRITING
};
union abstract_packet {
char raw;
struct {
uint16_t size;
char payload;
} str;
};
struct internet_packet {
union abstract_packet ap;
char rest[1499]; // MTU = 1500, 1 byte in the union
};
struct buffer_packet {
uint8_t mode;
uint16_t aread;
uint16_t awrite;
struct internet_packet ip;
};
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);