tor_multipath_voip/src/evt_core.c

264 lines
8.6 KiB
C
Raw Normal View History

2019-02-11 17:56:52 +00:00
#include "evt_core.h"
2019-02-18 13:35:09 +00:00
void free_fdinfo(void* v) {
struct evt_core_fdinfo* fdinfo = (struct evt_core_fdinfo*)v;
close(fdinfo->fd); // We close the file descriptor here
if (fdinfo->url != NULL) free(fdinfo->url); // We free the URL here;
2019-03-19 17:27:14 +00:00
if (fdinfo->free_other != NULL) fdinfo->free_other(fdinfo->other);
2019-02-11 17:56:52 +00:00
free(v);
}
2019-02-18 13:35:09 +00:00
void free_simple(void* s) {
free(s);
2019-02-11 17:56:52 +00:00
}
void free_cat(void* vcat) {
struct evt_core_cat* cat = (struct evt_core_cat*) vcat;
2019-02-14 17:08:20 +00:00
if (cat->free_app_ctx != NULL) cat->free_app_ctx(cat->app_ctx);
2019-02-11 22:40:37 +00:00
g_array_free(cat->socklist, TRUE);
2019-02-11 18:14:07 +00:00
free(cat->name);
free(cat);
2019-02-11 17:56:52 +00:00
}
void evt_core_init(struct evt_core_ctx* ctx) {
ctx->epollfd = epoll_create1(0);
if (ctx->epollfd == -1) {
perror("Failed to create epoll file descriptor epoll_create1");
exit(EXIT_FAILURE);
}
2019-02-18 13:35:09 +00:00
ctx->catlist = g_hash_table_new_full(g_str_hash, g_str_equal,NULL, free_cat);
ctx->socklist = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, free_fdinfo);
ctx->urltofd = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL);
2019-02-11 17:56:52 +00:00
}
2019-02-11 18:14:07 +00:00
void evt_core_add_cat(struct evt_core_ctx* ctx, struct evt_core_cat* cat) {
2019-02-11 22:40:37 +00:00
if (cat->socklist != NULL) {
fprintf(stderr, "cat->socklist must be null. What have you done?\n");
exit(EXIT_FAILURE);
}
2019-02-18 13:35:09 +00:00
// 1. Create category structure
2019-02-11 17:56:52 +00:00
struct evt_core_cat* dyn = NULL;
dyn = malloc(sizeof(struct evt_core_cat));
if (dyn == NULL) {
2019-02-11 18:14:07 +00:00
fprintf(stderr, "Failed to alloc memory\n");
2019-02-11 17:56:52 +00:00
exit(EXIT_FAILURE);
}
2019-02-18 13:35:09 +00:00
// 2. Populate category structure
2019-02-11 17:56:52 +00:00
dyn->app_ctx = cat->app_ctx;
dyn->free_app_ctx = cat->free_app_ctx;
dyn->cb = cat->cb;
dyn->name = strdup(cat->name);
dyn->flags = cat->flags;
2019-02-18 20:55:53 +00:00
dyn->err_cb = cat->err_cb;
2019-02-18 13:35:09 +00:00
dyn->socklist = g_array_new (FALSE, FALSE, sizeof(struct evt_core_fdinfo*));
2019-02-11 17:56:52 +00:00
if (dyn->name == NULL) {
perror("Unable to allocate memory for category name via strdup");
exit(EXIT_FAILURE);
}
2019-02-18 13:35:09 +00:00
// 3. Insert category structure in our context
g_hash_table_insert (ctx->catlist, dyn->name, dyn);
2019-02-11 18:14:07 +00:00
}
2019-03-21 17:43:38 +00:00
void evt_core_mv_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct evt_core_cat* to_cat) {
2019-03-27 17:11:05 +00:00
//printf("Moving fd=%d from cat=%s to cat=%s\n",fdinfo->fd, fdinfo->cat->name, to_cat->name);
2019-03-25 17:26:00 +00:00
2019-03-21 17:43:38 +00:00
// 1. Update old category
for (int i = 0; i < fdinfo->cat->socklist->len; i++) {
if (g_array_index(fdinfo->cat->socklist, struct evt_core_fdinfo*, i) == fdinfo) {
g_array_remove_index(fdinfo->cat->socklist, i);
}
}
// 2. Set new cat for fdinfo
fdinfo->cat = to_cat;
// 3. Update new category
g_array_append_val (fdinfo->cat->socklist, fdinfo);
2019-03-22 16:14:35 +00:00
// 4. Update epoll flags
update_fd_epoll (ctx->epollfd, fdinfo->fd, fdinfo->cat->flags);
// 5. Handle cases where data arrived before registering the file descriptor
fdinfo->cat->cb(ctx, fdinfo);
2019-03-21 17:43:38 +00:00
}
void evt_core_mv_fd2(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, char* to_cat) {
struct evt_core_cat* cat = evt_core_get_from_cat (ctx, to_cat);
if (cat == NULL) {
fprintf(stderr, "Category %s does not exist\n", to_cat);
exit(EXIT_FAILURE);
}
evt_core_mv_fd (ctx, fdinfo, cat);
}
2019-03-22 16:14:35 +00:00
struct evt_core_fdinfo* evt_core_add_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* user_data) {
2019-02-18 13:35:09 +00:00
// 1. Fetch fd category
struct evt_core_cat* cat = g_hash_table_lookup(ctx->catlist, user_data->cat->name);
if (cat == NULL) {
fprintf(stderr, "Category %s should be defined before inserting a file descriptor in it.\n", user_data->cat->name);
exit(EXIT_FAILURE);
}
2019-02-11 18:14:07 +00:00
2019-02-18 13:35:09 +00:00
// 2. Create fdinfo struct
struct evt_core_fdinfo* fdinfo;
if ((fdinfo = malloc(sizeof (struct evt_core_fdinfo))) == NULL) {
perror("Unable to allocate memory for fdinfo via malloc");
2019-02-11 18:14:07 +00:00
exit(EXIT_FAILURE);
}
2019-02-18 13:35:09 +00:00
// 3. Populate fdinfo struct
fdinfo->fd = user_data->fd;
fdinfo->cat = cat;
fdinfo->url = strdup(user_data->url);
fdinfo->other = user_data->other;
fdinfo->free_other = user_data->free_other;
if (fdinfo->url == NULL) {
perror("Unable to allocate memory via malloc for fdinfo->url");
2019-02-11 18:14:07 +00:00
exit(EXIT_FAILURE);
}
2019-02-11 17:56:52 +00:00
2019-02-18 13:35:09 +00:00
// 4. Insert structure in our context
g_array_append_val (cat->socklist, fdinfo);
g_hash_table_insert(ctx->socklist, &(fdinfo->fd), fdinfo);
g_hash_table_insert(ctx->urltofd, fdinfo->url, fdinfo);
// 5. Add file descriptor to epoll
add_fd_to_epoll(ctx->epollfd, user_data->fd, cat->flags);
2019-03-27 17:11:05 +00:00
//printf("Added fd=%d with url=%s in cat=%s\n", fdinfo->fd, fdinfo->url, fdinfo->cat->name);
2019-03-22 16:14:35 +00:00
// 6. Ensure that events arrived before epoll registering are handled
fdinfo->cat->cb(ctx, fdinfo);
2019-03-22 16:14:35 +00:00
return fdinfo;
2019-02-11 21:40:00 +00:00
}
2019-02-15 14:45:56 +00:00
struct evt_core_cat* evt_core_rm_fd(struct evt_core_ctx* ctx, int fd) {
2019-02-18 13:35:09 +00:00
struct evt_core_cat* cat;
2019-02-15 14:45:56 +00:00
2019-02-18 13:35:09 +00:00
// 1. Fetch fdinfo structure
struct evt_core_fdinfo* fdinfo = g_hash_table_lookup (ctx->socklist, &fd);
if (fdinfo == NULL) return NULL;
cat = fdinfo->cat;
2019-03-27 17:11:05 +00:00
//printf("Closing fd=%d from cat=%s\n",fdinfo->fd, fdinfo->cat->name);
2019-02-18 13:35:09 +00:00
2019-03-05 15:13:08 +00:00
// 2. Update category
2019-02-15 14:45:56 +00:00
for (int i = 0; i < cat->socklist->len; i++) {
2019-03-05 15:13:08 +00:00
if (g_array_index(cat->socklist, struct evt_core_fdinfo*, i) == fdinfo) {
2019-02-15 14:45:56 +00:00
g_array_remove_index(cat->socklist, i);
}
}
2019-02-18 13:35:09 +00:00
2019-03-05 15:13:08 +00:00
// 3. Remove structure from urltofd and socklist
g_hash_table_remove(ctx->urltofd, fdinfo->url);
g_hash_table_remove(ctx->socklist, &fd); // Will be free here
// 4. Close and remove file descriptor
epoll_ctl(ctx->epollfd, EPOLL_CTL_DEL, fd, NULL);
close(fd);
// 5. Return file descriptor's category
2019-02-15 14:45:56 +00:00
return cat;
}
2019-02-11 21:40:00 +00:00
void evt_core_free(struct evt_core_ctx* ctx) {
g_hash_table_destroy(ctx->socklist);
g_hash_table_destroy(ctx->catlist);
2019-02-18 13:35:09 +00:00
g_hash_table_destroy (ctx->urltofd);
2019-02-11 21:40:00 +00:00
}
void evt_core_loop(struct evt_core_ctx* ctx) {
struct epoll_event current_event, events[EVT_CORE_MAX_EVENTS];
2019-02-18 13:35:09 +00:00
struct evt_core_fdinfo* fdinfo;
2019-02-11 21:40:00 +00:00
struct evt_core_cat* cat;
printf("--- Start main loop\n");
int num_fd, n = 0;
while(1) {
num_fd = epoll_wait(ctx->epollfd, events, EVT_CORE_MAX_EVENTS, -1);
if (num_fd == -1) {
perror("Failed to epoll_wait");
exit(EXIT_FAILURE);
}
for (n = 0 ; n < num_fd; n++) {
2019-02-18 13:35:09 +00:00
// 1. Handle errors
2019-02-12 18:29:00 +00:00
if (events[n].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) {
2019-02-12 16:38:11 +00:00
int err_fd = events[n].data.fd;
2019-02-12 18:29:00 +00:00
int evt = events[n].events;
if (evt & EPOLLRDHUP) fprintf(stderr, "Epoll Read Hup Event. ");
if (evt & EPOLLHUP) fprintf(stderr, "Epoll Hup Event. ");
2019-02-19 15:40:20 +00:00
if (evt & EPOLLERR) {
int error = 0;
socklen_t errlen = sizeof(error);
if (getsockopt(err_fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == 0) {
2019-03-05 15:13:08 +00:00
fprintf(stderr, "Socket %d error = %s\n", err_fd, strerror(error));
} else if (errno == EBADF) {
fprintf(stderr, "fd=%d is invalid. Probably closed\n", err_fd);
continue;
} else {
fprintf(stderr, "Using fd=%d as socket produced error: ", err_fd);
perror("getsockopt");
2019-02-19 15:40:20 +00:00
}
fprintf(stderr, "Epoll Err Event. ");
}
2019-02-15 14:45:56 +00:00
2019-02-18 20:55:53 +00:00
fdinfo = evt_core_get_from_fd(ctx, err_fd);
if (fdinfo != NULL) {
2019-02-20 16:46:58 +00:00
if (fdinfo->cat->err_cb != NULL) {
2019-03-05 15:21:25 +00:00
if (fdinfo->cat->err_cb(ctx, fdinfo) == 1) {
fprintf(stderr, "Error on fd=%d on cat=%s is handled by app, not clearing it\n", err_fd, fdinfo->cat->name);
2019-03-05 15:21:25 +00:00
continue;
}
2019-02-20 16:46:58 +00:00
}
2019-03-05 15:21:25 +00:00
fprintf(stderr, "Clearing fd=%d on cat=%s\n", err_fd, fdinfo->cat->name);
2019-02-18 20:55:53 +00:00
evt_core_rm_fd (ctx, err_fd);
2019-02-12 16:38:11 +00:00
} else {
2019-03-05 15:13:08 +00:00
fprintf(stderr, "The file descriptor %d is not registered in a category, this is probably a logic error\n", err_fd);
2019-02-15 14:45:56 +00:00
close (err_fd);
2019-02-12 16:38:11 +00:00
}
2019-02-11 21:40:00 +00:00
continue;
}
2019-02-18 13:35:09 +00:00
// 2. Fetch info and call appropriate function
fdinfo = g_hash_table_lookup(ctx->socklist, &(events[n].data.fd));
if (fdinfo == NULL) {
2019-02-11 21:40:00 +00:00
fprintf(stderr, "Ignoring file descriptor %d as it is not registered. This is a bug.\n", events[n].data.fd);
continue;
}
2019-02-20 16:46:58 +00:00
while(fdinfo->cat->cb(ctx, fdinfo) == 0);
2019-02-11 21:40:00 +00:00
}
}
evt_core_free(ctx);
2019-02-11 17:56:52 +00:00
}
2019-02-18 14:11:12 +00:00
struct evt_core_fdinfo* evt_core_get_from_fd(struct evt_core_ctx* ctx, int fd) {
return g_hash_table_lookup (ctx->socklist, &fd);
}
2019-03-05 15:13:08 +00:00
2019-02-18 14:11:12 +00:00
struct evt_core_fdinfo* evt_core_get_from_url(struct evt_core_ctx* ctx, char* url) {
return g_hash_table_lookup (ctx->urltofd, url);
}
2019-02-19 13:49:44 +00:00
2019-03-05 15:13:08 +00:00
struct evt_core_cat* evt_core_get_from_cat(struct evt_core_ctx* ctx, char* name) {
return g_hash_table_lookup (ctx->catlist, name);
}
struct evt_core_fdinfo* evt_core_get_first_from_cat(struct evt_core_ctx* ctx, char* name) {
struct evt_core_cat* cat = g_hash_table_lookup (ctx->catlist, name);
if (cat == NULL) return NULL;
if (cat->socklist == NULL || cat->socklist->len <= 0) return NULL;
return g_array_index(cat->socklist, struct evt_core_fdinfo*, 0);
}
2019-02-19 13:49:44 +00:00
void evt_core_free_app_ctx_simple(void* v) {
free(v);
}