tor_multipath_voip/src/algo_utils.c

285 lines
9.2 KiB
C
Raw Normal View History

2019-03-07 15:57:02 +00:00
#include "algo_utils.h"
2019-03-20 09:28:52 +00:00
void iterate(int* fd, GQueue* q, int* waiting_count) {
fprintf(stderr, "Queue for fd=%d has length=%d\n", *fd, q->length);
2019-05-24 13:23:21 +00:00
*waiting_count += q->length;
2019-03-20 09:28:52 +00:00
}
void iterate2(int* fd, struct buffer_packet *bp, gpointer user_data) {
fprintf(stderr, "fd=%d has a used_buffer entry\n", *fd);
}
2019-05-27 15:32:00 +00:00
void naive_free_simple(void* v) {
GQueue* g = v;
g_queue_free (g);
}
2019-08-12 13:28:22 +00:00
void __push_to_free(struct buffer_resources *app_ctx, struct buffer_packet* bp) {
memset(bp, 0, sizeof(struct buffer_packet));
g_queue_push_tail (app_ctx->free_buffer, bp);
}
2019-05-27 15:32:00 +00:00
void debug_buffer(struct buffer_resources *app_ctx, struct evt_core_fdinfo *fdinfo) {
2019-03-27 15:24:39 +00:00
fprintf(stderr, "No more free buffer for fd=%d.\n", fdinfo->fd);
int waiting_count = 0;
g_hash_table_foreach(app_ctx->write_waiting, (GHFunc)iterate, &waiting_count);
g_hash_table_foreach(app_ctx->used_buffer, (GHFunc)iterate2, NULL);
fprintf(stderr, "total_buffers=%d, free_buffer=%d, used_buffers=%d, app_buffer=%d, write_buffer=%d.\n",
PACKET_BUFFER_SIZE,
app_ctx->free_buffer->length,
g_hash_table_size(app_ctx->used_buffer),
g_hash_table_size(app_ctx->application_waiting),
waiting_count);
}
2019-05-27 15:32:00 +00:00
void init_buffer_management(struct buffer_resources* ctx) {
ctx->free_buffer = g_queue_new ();
ctx->read_waiting = g_queue_new ();
ctx->application_waiting = g_hash_table_new (NULL, NULL);
ctx->used_buffer = g_hash_table_new(g_int_hash, g_int_equal);
ctx->write_waiting = g_hash_table_new_full (g_int_hash, g_int_equal, NULL, naive_free_simple);
for (int i = 0; i < sizeof(ctx->bps) / sizeof(ctx->bps[0]); i++) {
2019-08-12 13:28:22 +00:00
__push_to_free (ctx, &(ctx->bps[i]));
2019-05-27 15:32:00 +00:00
}
}
void destroy_buffer_management(struct buffer_resources* ctx) {
g_queue_free(ctx->free_buffer);
g_queue_free(ctx->read_waiting);
g_hash_table_destroy (ctx->application_waiting);
g_hash_table_destroy (ctx->used_buffer);
g_hash_table_destroy (ctx->write_waiting);
}
2019-03-07 15:57:02 +00:00
/**
* Returns a buffer if available, NULL otherwise
*/
2019-05-27 15:32:00 +00:00
struct buffer_packet* get_read_buffer(struct buffer_resources *app_ctx, struct evt_core_fdinfo *fdinfo) {
2019-03-07 15:57:02 +00:00
struct buffer_packet* bp;
// 1. Check if we don't have a buffer
2019-05-13 16:37:49 +00:00
bp = fdinfo == NULL ? NULL : g_hash_table_lookup (app_ctx->used_buffer, &fdinfo->fd);
2019-03-07 15:57:02 +00:00
if (bp != NULL) return bp;
// 2. Get a new buffer otherwise
bp = g_queue_pop_head(app_ctx->free_buffer);
if (bp == NULL) {
2019-03-27 15:24:39 +00:00
debug_buffer(app_ctx, fdinfo);
2019-03-07 15:57:02 +00:00
// 2.1 If no buffer is available, we subscribe to be notified later
g_queue_push_tail (app_ctx->read_waiting, &(fdinfo->fd));
return NULL;
}
// 3. Update state
g_hash_table_insert(app_ctx->used_buffer, &(fdinfo->fd), bp);
return bp;
}
2019-05-27 15:32:00 +00:00
guint write_queue_len(struct buffer_resources *app_ctx, struct evt_core_fdinfo *fdinfo) {
2019-05-24 13:51:49 +00:00
GQueue* q;
if ((q = g_hash_table_lookup(app_ctx->write_waiting, &(fdinfo->fd))) == NULL) return 0; // No queue
return q->length;
}
2019-03-07 15:57:02 +00:00
/**
* Returns a buffer if available, NULL otherwise
*/
2019-05-27 15:32:00 +00:00
struct buffer_packet* get_write_buffer(struct buffer_resources *app_ctx, struct evt_core_fdinfo *fdinfo) {
2019-03-07 15:57:02 +00:00
struct buffer_packet* bp;
GQueue* q;
// 1. Check if we don't have a buffer
bp = g_hash_table_lookup (app_ctx->used_buffer, &fdinfo->fd);
if (bp != NULL) return bp;
// 2. Check our waiting queue otherwise
if ((q = g_hash_table_lookup(app_ctx->write_waiting, &(fdinfo->fd))) == NULL) return NULL;
bp = g_queue_pop_head(q);
if (bp == NULL) return NULL; // No packet to process
// 3. Update state
g_hash_table_insert(app_ctx->used_buffer, &(fdinfo->fd), bp);
return bp;
}
2019-05-27 15:32:00 +00:00
void mv_buffer_rtow(struct buffer_resources *app_ctx, struct evt_core_fdinfo* from, struct evt_core_fdinfo* to) {
2019-03-07 15:57:02 +00:00
GQueue* q;
2019-03-20 14:13:16 +00:00
struct buffer_packet* bp;
// 1. We get the packet buffer
bp = g_hash_table_lookup (app_ctx->used_buffer, &from->fd);
if (bp == NULL) {
2019-05-24 15:32:20 +00:00
fprintf(stderr, "Unable to find a buffer for fd=%d url=%s in rtow\n", from->fd, from->url);
2019-03-20 14:13:16 +00:00
exit(EXIT_FAILURE);
}
// 2. We get the target writing queue
2019-03-07 15:57:02 +00:00
q = g_hash_table_lookup(app_ctx->write_waiting, &(to->fd));
if (q == NULL) {
q = g_queue_new ();
g_hash_table_insert(app_ctx->write_waiting, &(to->fd), q);
}
2019-03-20 14:13:16 +00:00
// 3. We move the data
2019-03-07 15:57:02 +00:00
g_hash_table_remove(app_ctx->used_buffer, &from->fd);
g_queue_push_tail(q, bp);
}
2019-05-27 15:32:00 +00:00
void mv_buffer_rtof(struct buffer_resources *app_ctx, struct evt_core_fdinfo* from) {
2019-05-06 12:52:57 +00:00
struct buffer_packet* bp;
// 1. We get the packet buffer
bp = g_hash_table_lookup (app_ctx->used_buffer, &from->fd);
if (bp == NULL) {
2019-05-24 15:32:20 +00:00
fprintf(stderr, "Unable to find a buffer for fd=%d url=%s in rtof\n", from->fd, from->url);
2019-05-28 14:13:42 +00:00
return;
2019-05-06 12:52:57 +00:00
}
g_hash_table_remove(app_ctx->used_buffer, &(from->fd));
2019-05-24 12:05:42 +00:00
__push_to_free (app_ctx, bp);
2019-05-06 12:52:57 +00:00
}
2019-05-27 15:32:00 +00:00
void mv_buffer_wtof(struct buffer_resources *app_ctx, struct evt_core_fdinfo* fdinfo) {
2019-03-20 14:13:16 +00:00
struct buffer_packet* bp = g_hash_table_lookup (app_ctx->used_buffer, &(fdinfo->fd));
if (bp == NULL) {
2019-05-24 15:32:20 +00:00
fprintf(stderr, "Unable to find a buffer for fd=%d url=%s in wtof\n", fdinfo->fd, fdinfo->url);
2019-05-28 14:13:42 +00:00
return;
2019-03-20 14:13:16 +00:00
}
2019-03-07 15:57:02 +00:00
g_hash_table_remove(app_ctx->used_buffer, &(fdinfo->fd));
2019-05-24 12:05:42 +00:00
__push_to_free (app_ctx, bp);
2019-03-07 15:57:02 +00:00
}
2019-05-27 15:32:00 +00:00
void mv_buffer_rtoa(struct buffer_resources *app_ctx, struct evt_core_fdinfo* from, void* to) {
2019-03-20 14:13:16 +00:00
struct buffer_packet* bp;
bp = g_hash_table_lookup (app_ctx->used_buffer, &from->fd);
if (bp == NULL) {
fprintf(stderr, "Unable to find a buffer for fd=%d url=%s\n", from->fd, from->url);
}
g_hash_table_remove(app_ctx->used_buffer, &from->fd);
if (g_hash_table_contains(app_ctx->application_waiting, to)) {
2019-03-27 16:31:46 +00:00
fprintf(stderr, "Data already exists for this entry\n");
debug_buffer(app_ctx, from);
2019-03-20 14:13:16 +00:00
exit(EXIT_FAILURE);
}
g_hash_table_insert(app_ctx->application_waiting, to, bp);
}
2019-05-27 15:32:00 +00:00
void mv_buffer_atow(struct buffer_resources *app_ctx, void* from, struct evt_core_fdinfo* to) {
2019-03-20 14:13:16 +00:00
GQueue* q;
struct buffer_packet* bp;
// 1. We get the buffer
bp = g_hash_table_lookup (app_ctx->application_waiting, from);
if (bp == NULL) {
fprintf(stderr, "Unable to find this application buffer in atow. Doing nothing...\n");
return;
2019-03-20 14:13:16 +00:00
}
// 2. We get the target writing queue
q = g_hash_table_lookup(app_ctx->write_waiting, &(to->fd));
if (q == NULL) {
q = g_queue_new ();
g_hash_table_insert(app_ctx->write_waiting, &(to->fd), q);
}
// 3. We move the buffer
g_hash_table_remove (app_ctx->application_waiting, from);
g_queue_push_tail(q, bp);
}
2019-05-27 15:32:00 +00:00
void mv_buffer_atof(struct buffer_resources *app_ctx, void* from) {
2019-03-20 14:13:16 +00:00
struct buffer_packet* bp;
2019-05-24 12:05:42 +00:00
// 1. Remove the buffer
2019-03-20 14:13:16 +00:00
bp = g_hash_table_lookup (app_ctx->application_waiting, from);
if (bp == NULL) {
2019-05-24 15:32:20 +00:00
fprintf(stderr, "Unable to find this application buffer in atof\n");
2019-03-20 14:13:16 +00:00
exit(EXIT_FAILURE);
}
g_hash_table_remove (app_ctx->application_waiting, from);
2019-05-24 12:05:42 +00:00
// 2. Append it to free list
__push_to_free (app_ctx, bp);
2019-03-20 14:13:16 +00:00
}
2019-08-28 12:57:20 +00:00
struct buffer_packet* inject_buffer_tow(struct buffer_resources *app_ctx, struct evt_core_fdinfo* to) {
2019-03-27 15:24:39 +00:00
GQueue* q;
// 1. We get a free buffer
struct buffer_packet* bp_dest = g_queue_pop_head(app_ctx->free_buffer);
if (bp_dest == NULL) {
debug_buffer(app_ctx, to);
2019-05-13 16:37:49 +00:00
return NULL;
2019-03-27 15:24:39 +00:00
}
2019-08-28 12:57:20 +00:00
// 2. We get the target writing queue
2019-03-27 15:24:39 +00:00
q = g_hash_table_lookup(app_ctx->write_waiting, &(to->fd));
if (q == NULL) {
q = g_queue_new ();
g_hash_table_insert(app_ctx->write_waiting, &(to->fd), q);
}
2019-08-28 12:57:20 +00:00
// 3. We push the content to the appropriate destination
2019-03-27 15:24:39 +00:00
g_queue_push_tail(q, bp_dest);
2019-05-13 16:37:49 +00:00
return bp_dest;
2019-03-27 15:24:39 +00:00
}
2019-08-28 12:57:20 +00:00
struct buffer_packet* dup_buffer_tow(struct buffer_resources *app_ctx, struct buffer_packet* bp, struct evt_core_fdinfo* to) {
// 1. Inject a new buffer
struct buffer_packet* bp_dest = inject_buffer_tow (app_ctx, to);
// 2. We duplicate the data
memcpy(bp_dest, bp, sizeof(struct buffer_packet));
return bp_dest;
}
2019-08-26 15:48:22 +00:00
struct buffer_packet* dup_buffer_toa(struct buffer_resources *app_ctx, struct buffer_packet* bp, void* to) {
GQueue* q;
// 1. We get a free buffer
struct buffer_packet* bp_dest = g_queue_pop_head(app_ctx->free_buffer);
if (bp_dest == NULL) {
debug_buffer(app_ctx, to);
return NULL;
}
// 2. We duplicate the data
memcpy(bp_dest, bp, sizeof(struct buffer_packet));
// 3. We put the data
if (g_hash_table_contains(app_ctx->application_waiting, to)) {
fprintf(stderr, "Data already exists for this entry\n");
exit(EXIT_FAILURE);
}
g_hash_table_insert(app_ctx->application_waiting, to, bp_dest);
return bp_dest;
}
2019-05-27 15:32:00 +00:00
struct buffer_packet* get_app_buffer(struct buffer_resources *app_ctx, void* idx) {
2019-03-20 14:13:16 +00:00
return g_hash_table_lookup (app_ctx->application_waiting, idx);
}
2019-05-27 15:32:00 +00:00
void notify_read(struct evt_core_ctx* ctx, struct buffer_resources* app_ctx) {
2019-03-07 15:57:02 +00:00
struct evt_core_fdinfo* next_fdinfo = NULL;
while (next_fdinfo == NULL) {
2019-03-20 09:28:52 +00:00
int* fd = g_queue_pop_head(app_ctx->read_waiting);
if (fd == NULL) break;
next_fdinfo = evt_core_get_from_fd (ctx, *fd);
if (next_fdinfo == NULL) {
fprintf(stderr, "Unable to find fdinfo for fd=%d\n", *fd);
exit(EXIT_FAILURE);
} else if (strcmp(next_fdinfo->cat->name, "tcp-read") == 0 || strcmp(next_fdinfo->cat->name, "udp-read") == 0) {
2019-03-07 15:57:02 +00:00
next_fdinfo->cat->cb(ctx, next_fdinfo);
} else {
fprintf(stderr, "A fd from category %s can't be stored in read_waiting\n", next_fdinfo->cat->name);
exit(EXIT_FAILURE);
}
}
}