tor_multipath_voip/src/evt_core.c

360 lines
12 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;
2020-01-23 16:08:04 +00:00
fprintf(stdout, "[%s][evt_core] Freeing fdinfo url=%s fd=%d\n", current_human_datetime (), fdinfo->url, fdinfo->fd);
if (close(fdinfo->fd) != 0) { // We close the file descriptor here
fprintf(stderr, "[%s][evt_core] Failed to close fd for url=%s fd=%d. ", current_human_datetime (), fdinfo->url, fdinfo->fd);
perror("Error");
}
2019-04-03 07:54:52 +00:00
if (fdinfo->free_other != NULL) {
2019-05-10 08:39:15 +00:00
//fprintf(stderr, "Freeing fdinfo->other for %s\n", fdinfo->url);
2019-04-03 07:54:52 +00:00
fdinfo->free_other(fdinfo->other);
}
if (fdinfo->url != NULL) {
2019-05-10 08:39:15 +00:00
//fprintf(stderr, "Freeing fdinfo->url for %s\n", fdinfo->url);
2019-04-03 07:54:52 +00:00
free(fdinfo->url); // We free the URL here;
}
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
}
2019-04-01 12:16:41 +00:00
void evt_core_init(struct evt_core_ctx* ctx, uint8_t verbose) {
2019-02-11 17:56:52 +00:00
ctx->epollfd = epoll_create1(0);
if (ctx->epollfd == -1) {
perror("Failed to create epoll file descriptor epoll_create1");
exit(EXIT_FAILURE);
}
2019-04-01 12:16:41 +00:00
ctx->verbose = verbose;
2019-05-09 09:24:05 +00:00
ctx->loop = 1;
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-04-01 13:48:11 +00:00
if (ctx->verbose) fprintf(stderr, " 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-04-01 13:48:11 +00:00
if (ctx->verbose) fprintf(stderr, " 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-04-01 13:53:47 +00:00
if (ctx->verbose) fprintf(stderr, " Closing url=%s, fd=%d from cat=%s\n", fdinfo->url, 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
2019-04-03 07:54:52 +00:00
// Done in free_fdinfo
//epoll_ctl(ctx->epollfd, EPOLL_CTL_DEL, fd, NULL);
//close(fd);
2019-03-05 15:13:08 +00:00
// 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
}
2019-04-01 17:27:40 +00:00
enum timing_config {
TIMING_ACTIVATED = 1 << 0,
TIMING_DISPLAY_START = 1 << 1,
TIMING_DISPLAY_END = 1 << 2,
TIMING_DISPLAY_BOTH = 1 << 3
};
struct timing_fx {
struct timespec start;
enum timing_config config;
uint8_t activated_start;
char start_template[255], end_template[255];
};
void timing_fx_init(struct timing_fx* tfx, enum timing_config conf, char* startt, char* endt) {
tfx->config = conf;
strncpy (tfx->start_template, startt, sizeof(tfx->start_template) - 1);
strncpy (tfx->end_template, endt, sizeof(tfx->end_template) - 1);
tfx->start_template[sizeof(tfx->start_template) - 1] = 0; // Enforce null terminated string
tfx->end_template[sizeof(tfx->end_template) - 1] = 0;
}
void timing_fx_start(struct timing_fx* tfx, ...) {
va_list args;
if (!(tfx->config & TIMING_ACTIVATED)) return;
if (tfx->config & (TIMING_DISPLAY_START | TIMING_DISPLAY_BOTH)) {
va_start(args, tfx);
vfprintf(stderr, tfx->start_template, args);
va_end(args);
}
2020-02-01 07:42:48 +00:00
if (clock_gettime(CLOCK_MONOTONIC, &tfx->start) == -1) {
2019-04-01 17:27:40 +00:00
perror("clock_gettime");
exit(EXIT_FAILURE);
}
}
double timing_fx_stop(struct timing_fx* tfx, ...) {
va_list args;
struct timespec stop;
double elapsed_in_cb;
if (!(tfx->config & TIMING_ACTIVATED)) return 0.;
2020-02-01 07:42:48 +00:00
if (clock_gettime(CLOCK_MONOTONIC, &stop) == -1) {
2019-04-01 17:27:40 +00:00
perror("clock_gettime");
exit(EXIT_FAILURE);
}
elapsed_in_cb = (double)elapsed_micros (&tfx->start, &stop) / 1000000.;
if (tfx->config & (TIMING_DISPLAY_END | TIMING_DISPLAY_BOTH)) {
va_start(args, tfx);
vfprintf(stderr, tfx->end_template, args);
fprintf(stderr, ": done in %f sec\n", elapsed_in_cb);
va_end(args);
}
return elapsed_in_cb;
}
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;
2019-04-01 17:27:40 +00:00
int return_code;
clock_t start_timing, end_timing;
2019-04-01 12:16:41 +00:00
double elapsed_in_cb;
2019-04-01 17:27:40 +00:00
struct timing_fx tfx_epoll, tfx_err, tfx_cb, tfx_loop;
enum timing_config base = ctx->verbose ? TIMING_ACTIVATED : 0;
timing_fx_init (&tfx_epoll, base | TIMING_DISPLAY_END, "", "[SLEPT]()");
timing_fx_init (&tfx_cb, base | TIMING_DISPLAY_BOTH, "[BEGIN CB](name=%s, url=%s, fd=%d)\n", "[END CB]()");
timing_fx_init (&tfx_err, base | TIMING_DISPLAY_BOTH, "[BEGIN ERR](name=%s, url=%s, fd=%d)\n", "[END ERR]()");
timing_fx_init (&tfx_loop, base | TIMING_DISPLAY_END, "", "[LOOP]()");
2019-02-11 21:40:00 +00:00
printf("--- Start main loop\n");
int num_fd, n = 0;
2019-05-09 09:24:05 +00:00
while(ctx->loop) {
2019-04-01 17:27:40 +00:00
timing_fx_start(&tfx_epoll);
2019-02-11 21:40:00 +00:00
num_fd = epoll_wait(ctx->epollfd, events, EVT_CORE_MAX_EVENTS, -1);
2019-04-01 17:27:40 +00:00
timing_fx_stop(&tfx_epoll);
timing_fx_start(&tfx_loop);
2019-02-11 21:40:00 +00:00
if (num_fd == -1) {
perror("Failed to epoll_wait");
2020-01-23 18:52:11 +00:00
continue;
2019-02-11 21:40:00 +00:00
}
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;
2020-01-23 16:08:04 +00:00
if (evt & EPOLLRDHUP) fprintf(stderr, "Epoll Read Hup Event fd=%d.\n", err_fd);
if (evt & EPOLLHUP) fprintf(stderr, "Epoll Hup Event fd=%d.\n", err_fd);
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-04-01 17:27:40 +00:00
timing_fx_start (&tfx_err, fdinfo->cat->name, fdinfo->url, fdinfo->fd);
return_code = fdinfo->cat->err_cb(ctx, fdinfo);
timing_fx_stop(&tfx_err);
if (return_code == 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-04-01 17:27:40 +00:00
timing_fx_start(&tfx_cb, fdinfo->cat->name, fdinfo->url, fdinfo->fd);
2019-02-20 16:46:58 +00:00
while(fdinfo->cat->cb(ctx, fdinfo) == 0);
2019-04-01 17:27:40 +00:00
timing_fx_stop(&tfx_cb);
2019-02-11 21:40:00 +00:00
}
2019-04-01 17:27:40 +00:00
timing_fx_stop(&tfx_loop);
2019-02-11 21:40:00 +00:00
}
evt_core_free(ctx);
2019-04-01 12:16:41 +00:00
return;
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);
}