Fix file descriptor bug

This commit is contained in:
Quentin Dufour 2019-02-12 17:12:20 +01:00
parent debe0feb56
commit 0d41b47ff9
5 changed files with 23 additions and 11 deletions

View file

@ -22,7 +22,7 @@ void on_tcp_co(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
conn_sock = accept(fd, &in_addr, &in_len); conn_sock = accept(fd, &in_addr, &in_len);
if (conn_sock == -1) goto co_error; if (conn_sock == -1) goto co_error;
evt_core_add_fd (ctx, "tcp-read", fd); evt_core_add_fd (ctx, "tcp-read", fd);
evt_core_add_fd (ctx, "tcp-write", fd); evt_core_add_fd (ctx, "tcp-write", dup(fd));
return; return;

View file

@ -49,9 +49,15 @@ void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, char* ud
evt_core_add_cat (&(ctx->evts), &(algo->on_udp_write)); evt_core_add_cat (&(ctx->evts), &(algo->on_udp_write));
evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_write)); evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_write));
printf("--- Categories created\n");
int sock = create_udp_client (udp_host, udp_port); int sock = create_udp_client (udp_host, udp_port);
if (sock < 0) {
fprintf(stderr, "Unable to create a UDP client socket\n");
exit(EXIT_FAILURE);
}
evt_core_add_fd (&(ctx->evts), "udp-read", sock); evt_core_add_fd (&(ctx->evts), "udp-read", sock);
evt_core_add_fd (&(ctx->evts), "udp-write", sock); evt_core_add_fd (&(ctx->evts), "udp-write", dup(sock));
printf("--- UDP client is connected\n");
for (uint16_t i = 0; i < PORT_SIZE ; i++) { for (uint16_t i = 0; i < PORT_SIZE ; i++) {
ctx->ports[i] = 7500 + i; ctx->ports[i] = 7500 + i;
@ -59,6 +65,7 @@ void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, char* ud
create_onion_services (&(ctx->tos), &(ctx->tctl), ctx->ports, PORT_SIZE); create_onion_services (&(ctx->tos), &(ctx->tctl), ctx->ports, PORT_SIZE);
printf("--- Onion services created\n"); printf("--- Onion services created\n");
init_tcp_servers(ctx); init_tcp_servers(ctx);
printf("--- TCP servers are listening\n");
evt_core_loop (&(ctx->evts)); evt_core_loop (&(ctx->evts));

View file

@ -34,7 +34,6 @@ void evt_core_add_cat(struct evt_core_ctx* ctx, struct evt_core_cat* cat) {
fprintf(stderr, "cat->socklist must be null. What have you done?\n"); fprintf(stderr, "cat->socklist must be null. What have you done?\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
cat->socklist = g_array_new (FALSE, FALSE, sizeof(int));
struct evt_core_cat* dyn = NULL; struct evt_core_cat* dyn = NULL;
dyn = malloc(sizeof(struct evt_core_cat)); dyn = malloc(sizeof(struct evt_core_cat));
@ -47,6 +46,7 @@ void evt_core_add_cat(struct evt_core_ctx* ctx, struct evt_core_cat* cat) {
dyn->cb = cat->cb; dyn->cb = cat->cb;
dyn->name = strdup(cat->name); dyn->name = strdup(cat->name);
dyn->flags = cat->flags; dyn->flags = cat->flags;
dyn->socklist = g_array_new (FALSE, FALSE, sizeof(int));
if (dyn->name == NULL) { if (dyn->name == NULL) {
perror("Unable to allocate memory for category name via strdup"); perror("Unable to allocate memory for category name via strdup");
@ -71,7 +71,7 @@ void evt_core_add_fd(struct evt_core_ctx* ctx, char* name, int fd) {
perror("Unable to allocate memory for key via malloc"); perror("Unable to allocate memory for key via malloc");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
memcpy(key, &fd, sizeof(int)); *key = fd;
struct evt_core_cat* cat = g_hash_table_lookup(ctx->catlist, name); struct evt_core_cat* cat = g_hash_table_lookup(ctx->catlist, name);
if (cat == NULL) { if (cat == NULL) {
@ -80,7 +80,7 @@ void evt_core_add_fd(struct evt_core_ctx* ctx, char* name, int fd) {
} }
g_array_append_val (cat->socklist, fd); g_array_append_val (cat->socklist, fd);
g_hash_table_insert(ctx->socklist, key, cat); g_hash_table_insert(ctx->socklist, key, cat); // broken
add_fd_to_epoll(ctx->epollfd, fd, cat->flags); add_fd_to_epoll(ctx->epollfd, fd, cat->flags);
} }
@ -103,10 +103,11 @@ void evt_core_loop(struct evt_core_ctx* ctx) {
} }
for (n = 0 ; n < num_fd; n++) { for (n = 0 ; n < num_fd; n++) {
if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (!(events[n].events & EPOLLIN))) { if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (!(events[n].events & EPOLLIN))) {
/* An error has occured on this fd, or the socket is not /* An error has occured on this fd, or the socket is not
ready for reading (why were we notified then?) */ ready for reading (why were we notified then?) */
fprintf (stderr, "epoll error\n");
fprintf (stderr, "An epoll error occured with fd=%d\n", events[n].data.fd);
close (events[n].data.fd); close (events[n].data.fd);
continue; continue;
} }

View file

@ -128,10 +128,14 @@ void fill_buffer(size_t* written, char* dest, void *src, size_t n) {
* You need to read everything before going back to epoll * You need to read everything before going back to epoll
* Which means keeping state too * Which means keeping state too
*/ */
void add_fd_to_epoll(int epollfd, int fd, int flags) { void add_fd_to_epoll(int epollfd, int fd, uint32_t flags) {
make_socket_non_blocking (fd); int err = make_socket_non_blocking (fd);
if (err == -1) {
fprintf(stderr, "Unable to set fd=%d to non blocking\n", fd);
exit(EXIT_FAILURE);
}
struct epoll_event current_event; struct epoll_event current_event = {0};
//current_event.events = EPOLLIN | EPOLLET; //current_event.events = EPOLLIN | EPOLLET;
current_event.events = flags; current_event.events = flags;
current_event.data.fd = fd; current_event.data.fd = fd;

View file

@ -12,6 +12,6 @@ int create_tcp_client(char* host, char* service);
int create_udp_client(char* host, char* service); int create_udp_client(char* host, char* service);
int create_tcp_server(char* service); int create_tcp_server(char* service);
int make_socket_non_blocking(int fd); int make_socket_non_blocking(int fd);
void add_fd_to_epoll(int epollfd, int fd, int flags); void add_fd_to_epoll(int epollfd, int fd, uint32_t flags);
int read_entity(int fd, void* entity, int size); int read_entity(int fd, void* entity, int size);
void fill_buffer(size_t* written, char* dest, void *src, size_t n); void fill_buffer(size_t* written, char* dest, void *src, size_t n);