tor_multipath_voip/src/algo_naive.c

261 lines
8.5 KiB
C
Raw Normal View History

2019-03-07 15:57:02 +00:00
#include "algo_utils.h"
2019-02-11 21:40:00 +00:00
#include "algo_skel.h"
2019-02-20 16:46:58 +00:00
int on_tcp_read(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo);
int on_tcp_write(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo);
int on_udp_read(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo);
int on_udp_write (struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo);
2019-02-18 20:55:53 +00:00
2019-02-20 16:46:58 +00:00
int on_tcp_co(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2019-02-18 20:55:53 +00:00
int conn_sock1, conn_sock2;
2019-02-18 13:35:09 +00:00
struct sockaddr_in addr;
2019-02-11 21:40:00 +00:00
socklen_t in_len;
2019-02-18 20:55:53 +00:00
char url[1024], port[6];
2019-02-18 13:35:09 +00:00
struct evt_core_cat local_cat = {0};
2019-02-18 20:55:53 +00:00
struct evt_core_fdinfo to_fdinfo = {0};
to_fdinfo.cat = &local_cat;
to_fdinfo.url = url;
2019-02-11 21:40:00 +00:00
2019-02-12 16:38:11 +00:00
in_len = sizeof(addr);
2019-02-18 20:55:53 +00:00
conn_sock1 = accept(fdinfo->fd, (struct sockaddr*)&addr, &in_len);
2019-02-11 21:40:00 +00:00
2019-03-25 16:33:08 +00:00
if (conn_sock1 == -1 && errno == EAGAIN) return 1;
2019-02-12 16:38:11 +00:00
if (conn_sock1 == -1) goto co_error;
conn_sock2 = dup(conn_sock1);
if (conn_sock2 == -1) goto co_error;
2019-02-12 18:29:00 +00:00
//printf("fd=%d accepts, creating fds=%d,%d\n", fd, conn_sock1, conn_sock2);
2019-02-18 13:35:09 +00:00
url_get_port(port, fdinfo->url);
2019-02-18 13:35:09 +00:00
2019-02-18 20:55:53 +00:00
to_fdinfo.fd = conn_sock1;
to_fdinfo.cat->name = "tcp-read";
sprintf(to_fdinfo.url, "tcp:read:127.0.0.1:%s", port);
evt_core_add_fd (ctx, &to_fdinfo);
2019-02-18 13:35:09 +00:00
2019-02-18 20:55:53 +00:00
to_fdinfo.fd = conn_sock2;
to_fdinfo.cat->name = "tcp-write";
sprintf(to_fdinfo.url, "tcp:write:127.0.0.1:%s", port);
evt_core_add_fd (ctx, &to_fdinfo);
2019-02-11 21:40:00 +00:00
2019-03-25 16:33:08 +00:00
return 0;
2019-02-11 21:40:00 +00:00
co_error:
perror("Failed to handle new connection");
exit(EXIT_FAILURE);
}
2019-02-20 16:46:58 +00:00
int on_tcp_read(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2019-02-18 16:51:13 +00:00
struct buffer_packet* bp;
struct evt_core_fdinfo *to_fdinfo = NULL;
2019-03-07 15:57:02 +00:00
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
2019-02-18 16:51:13 +00:00
int read_res = FDS_READY;
char url[255];
2019-02-18 20:55:53 +00:00
// 1. Get current read buffer OR a new read buffer OR subscribe to be notified later
2019-02-20 16:46:58 +00:00
if ((bp = get_read_buffer(app_ctx, fdinfo)) == NULL) return 1;
2019-02-18 20:55:53 +00:00
// 2. Try to read a whole packet in the buffer
2019-02-20 16:46:58 +00:00
while (bp->mode == BP_READING) {
2019-02-18 20:55:53 +00:00
read_res = read_packet_from_tcp (fdinfo->fd, bp);
2019-02-18 16:51:13 +00:00
if (read_res == FDS_ERR) goto co_error;
2019-02-20 16:46:58 +00:00
if (read_res == FDS_AGAIN) return 1;
2019-02-18 16:51:13 +00:00
}
2019-02-18 20:55:53 +00:00
// 3. A whole packet has been read, we will find someone to write it
2019-02-18 16:51:13 +00:00
sprintf(url, "udp:write:127.0.0.1:%d", bp->ip.ap.str.port);
to_fdinfo = evt_core_get_from_url (ctx, url);
2019-02-19 18:31:26 +00:00
if (to_fdinfo == NULL) {
fprintf(stderr, "No fd for URL %s in tcp-read. Dropping packet :( \n", url);
2019-03-20 14:13:16 +00:00
mv_buffer_wtof (app_ctx, fdinfo);
2019-02-20 16:46:58 +00:00
return 1;
2019-02-19 18:31:26 +00:00
}
2019-02-20 08:34:10 +00:00
//printf("Pass packet from %s to %s\n", fdinfo->url, url);
2019-02-18 16:51:13 +00:00
2019-02-18 20:55:53 +00:00
// 4. We move the buffer and notify the target
2019-03-20 14:13:16 +00:00
mv_buffer_rtow (app_ctx, fdinfo, to_fdinfo);
2019-02-18 20:55:53 +00:00
on_udp_write(ctx, to_fdinfo);
2019-02-18 16:51:13 +00:00
2019-02-20 16:46:58 +00:00
return 0;
2019-02-18 16:51:13 +00:00
co_error:
perror("Failed to TCP read");
exit(EXIT_FAILURE);
}
2019-02-20 16:46:58 +00:00
int on_tcp_write(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2019-02-18 16:51:13 +00:00
struct buffer_packet* bp;
2019-03-07 15:57:02 +00:00
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
2019-02-18 16:51:13 +00:00
int write_res = FDS_READY;
if (!app_ctx->is_rdy && strcmp(fdinfo->url, "tcp:write:127.0.0.1:7500") == 0) {
app_ctx->is_rdy = 1;
printf("=== Requested circuit is up ===\n");
}
2019-02-18 20:55:53 +00:00
// 1. Get current write buffer OR a buffer from the waiting queue OR leave
2019-02-20 16:46:58 +00:00
if ((bp = get_write_buffer(app_ctx, fdinfo)) == NULL) return 1;
2019-02-18 16:51:13 +00:00
2019-02-18 20:55:53 +00:00
// 2. Write data from the buffer to the socket
2019-02-20 16:46:58 +00:00
while (bp->mode == BP_WRITING) {
2019-02-18 20:55:53 +00:00
write_res = write_packet_to_tcp(fdinfo->fd, bp);
2019-02-18 16:51:13 +00:00
if (write_res == FDS_ERR) goto co_error;
2019-02-20 16:46:58 +00:00
if (write_res == FDS_AGAIN) return 1;
2019-02-18 16:51:13 +00:00
}
2019-02-18 20:55:53 +00:00
// 3. A whole packet has been written
// Release the buffer and notify
2019-03-20 14:13:16 +00:00
mv_buffer_wtof(app_ctx, fdinfo);
2019-02-18 20:55:53 +00:00
notify_read(ctx, app_ctx);
2019-02-20 16:46:58 +00:00
return 0;
2019-02-18 16:51:13 +00:00
co_error:
perror("Failed to TCP write");
exit(EXIT_FAILURE);
}
2019-02-20 16:46:58 +00:00
int on_udp_read(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2019-02-18 20:55:53 +00:00
struct buffer_packet* bp;
struct evt_core_fdinfo *to_fdinfo;
2019-03-07 15:57:02 +00:00
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
2019-02-14 10:16:38 +00:00
int read_res = FDS_READY;
2019-02-18 20:55:53 +00:00
char url[255];
2019-02-14 10:16:38 +00:00
2019-02-18 20:55:53 +00:00
// 1. Get current read buffer OR a new read buffer OR subscribe to be notified later
2019-02-20 16:46:58 +00:00
if ((bp = get_read_buffer(app_ctx, fdinfo)) == NULL) return 1;
2019-02-14 10:16:38 +00:00
2019-02-18 20:55:53 +00:00
// 2. Read packet from socket
2019-02-19 09:48:44 +00:00
bp->ip.ap.str.port = url_get_port_int (fdinfo->url);
2019-02-19 13:49:44 +00:00
read_res = read_packet_from_udp (fdinfo->fd, bp, fdinfo->other);
2019-02-18 20:55:53 +00:00
if (read_res == FDS_ERR) goto co_error;
2019-02-20 16:46:58 +00:00
if (read_res == FDS_AGAIN) return 1;
2019-02-14 10:16:38 +00:00
2019-02-18 20:55:53 +00:00
// 3. A whole packet has been read, we will find someone to write it
2019-02-19 14:28:12 +00:00
sprintf(url, "tcp:write:127.0.0.1:7500");
2019-02-18 20:55:53 +00:00
to_fdinfo = evt_core_get_from_url (ctx, url);
2019-02-19 18:31:26 +00:00
if (to_fdinfo == NULL) {
fprintf(stderr, "No fd for URL %s in udp-read. Dropping packet :( \n", url);
2019-03-20 14:13:16 +00:00
mv_buffer_wtof (app_ctx, fdinfo);
2019-02-20 16:46:58 +00:00
return 1;
2019-02-19 18:31:26 +00:00
}
2019-02-20 08:34:10 +00:00
//printf("Pass packet from %s to %s\n", fdinfo->url, url);
2019-02-18 20:55:53 +00:00
// 4. We move the buffer and notify the target
2019-03-20 14:13:16 +00:00
mv_buffer_rtow (app_ctx, fdinfo, to_fdinfo);
2019-02-18 20:55:53 +00:00
on_tcp_write(ctx, to_fdinfo);
2019-02-11 21:40:00 +00:00
2019-02-20 16:46:58 +00:00
return 0;
2019-02-19 09:44:22 +00:00
2019-02-11 21:40:00 +00:00
co_error:
2019-02-18 20:55:53 +00:00
perror("Failed to UDP read");
2019-02-11 21:40:00 +00:00
exit(EXIT_FAILURE);
}
2019-02-20 16:46:58 +00:00
int on_udp_write (struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2019-02-18 20:55:53 +00:00
struct buffer_packet* bp;
2019-03-07 15:57:02 +00:00
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
int write_res = FDS_READY;
2019-02-18 20:55:53 +00:00
// 1. Get current write buffer OR a buffer from the waiting queue OR leave
2019-02-20 16:46:58 +00:00
if ((bp = get_write_buffer(app_ctx, fdinfo)) == NULL) return 1;
2019-02-18 20:55:53 +00:00
// 2. Write buffer
2019-02-19 13:49:44 +00:00
write_res = write_packet_to_udp(fdinfo->fd, bp, fdinfo->other);
2019-02-18 20:55:53 +00:00
if (write_res == FDS_ERR) goto co_error;
2019-02-20 16:46:58 +00:00
if (write_res == FDS_AGAIN) return 1;
2019-02-11 22:40:37 +00:00
2019-02-18 20:55:53 +00:00
// 3. A whole packet has been written
// Release the buffer and notify
2019-03-20 14:13:16 +00:00
mv_buffer_wtof(app_ctx, fdinfo);
2019-02-18 20:55:53 +00:00
notify_read(ctx, app_ctx);
2019-02-11 22:40:37 +00:00
2019-02-20 16:46:58 +00:00
return 0;
2019-02-11 22:40:37 +00:00
co_error:
2019-02-18 20:55:53 +00:00
perror("Failed to UDP write");
2019-02-11 22:40:37 +00:00
exit(EXIT_FAILURE);
2019-02-11 21:40:00 +00:00
}
2019-02-20 16:46:58 +00:00
int on_err(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2019-03-07 15:57:02 +00:00
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
2019-02-18 20:55:53 +00:00
struct buffer_packet* bp;
// 1. If has a "used" buffer, remove it
bp = g_hash_table_lookup (app_ctx->used_buffer, &(fdinfo->fd));
if (bp != NULL) {
2019-04-04 08:19:55 +00:00
fprintf(stderr, "begin removing entry in app_ctx->used_buffer for %s\n", fdinfo->url);
2019-02-18 20:55:53 +00:00
g_hash_table_remove (app_ctx->used_buffer, &(fdinfo->fd));
2019-04-04 08:19:55 +00:00
fprintf(stderr, "end removing entry in app_ctx->used_buffer\n");
2019-02-18 20:55:53 +00:00
memset(bp, 0, sizeof(struct buffer_packet));
g_queue_push_tail(app_ctx->free_buffer, bp);
}
// 2. If appears in the write waiting queue, remove it
GQueue* writew = g_hash_table_lookup (app_ctx->write_waiting, &(fdinfo->fd));
while (writew != NULL && (bp = g_queue_pop_head (writew)) != NULL) {
memset(bp, 0, sizeof(struct buffer_packet));
g_queue_push_tail(app_ctx->free_buffer, bp);
}
2019-04-04 09:32:11 +00:00
if (writew) g_hash_table_remove (app_ctx->write_waiting, &(fdinfo->fd));
2019-02-18 20:55:53 +00:00
// 3. If appears in the read waiting queue, remove it
g_queue_remove_all (app_ctx->read_waiting, &(fdinfo->fd));
2019-02-20 16:46:58 +00:00
2019-03-05 15:57:14 +00:00
return 0;
2019-02-18 20:55:53 +00:00
}
void algo_naive(struct evt_core_ctx* evt, struct algo_skel* as, struct algo_params* ap) {
2019-03-07 15:57:02 +00:00
struct algo_ctx* ctx = malloc(sizeof(struct algo_ctx));
2019-02-12 21:38:42 +00:00
if (ctx == NULL) goto init_err;
2019-03-07 15:57:02 +00:00
memset(ctx, 0, sizeof(struct algo_ctx));
2019-02-18 20:55:53 +00:00
ctx->free_buffer = g_queue_new ();
ctx->read_waiting = g_queue_new ();
2019-03-20 14:13:16 +00:00
ctx->application_waiting = g_hash_table_new (NULL, NULL);
2019-02-18 20:55:53 +00:00
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);
ctx->ap = *ap;
ctx->is_rdy = 0;
2019-02-18 20:55:53 +00:00
for (int i = 0; i < sizeof(ctx->bps) / sizeof(ctx->bps[0]); i++) {
g_queue_push_tail(ctx->free_buffer, &(ctx->bps[i]));
}
2019-02-12 21:38:42 +00:00
2019-02-11 21:40:00 +00:00
as->on_tcp_co.name = "tcp-listen";
2019-02-12 10:17:37 +00:00
as->on_tcp_co.flags = EPOLLIN;
2019-02-12 21:38:42 +00:00
as->on_tcp_co.free_app_ctx = free_nothing;
2019-02-11 21:40:00 +00:00
as->on_tcp_co.cb = on_tcp_co;
2019-02-12 10:17:37 +00:00
as->on_tcp_read.name = "tcp-read";
2019-02-12 18:29:00 +00:00
as->on_tcp_read.flags = EPOLLIN | EPOLLET | EPOLLRDHUP;
2019-02-12 21:38:42 +00:00
as->on_tcp_read.app_ctx = ctx;
2019-02-12 10:17:37 +00:00
as->on_tcp_read.free_app_ctx = free_naive;
2019-02-18 16:51:13 +00:00
as->on_tcp_read.cb = on_tcp_read;
2019-02-18 20:55:53 +00:00
as->on_tcp_read.err_cb = on_err;
2019-02-12 21:38:42 +00:00
ctx->ref_count++;
2019-02-12 10:17:37 +00:00
as->on_udp_read.name = "udp-read";
as->on_udp_read.flags = EPOLLIN | EPOLLET;
2019-02-12 21:38:42 +00:00
as->on_udp_read.app_ctx = ctx;
2019-02-12 10:17:37 +00:00
as->on_udp_read.free_app_ctx = free_naive;
2019-02-18 16:51:13 +00:00
as->on_udp_read.cb = on_udp_read;
2019-02-18 20:55:53 +00:00
as->on_udp_read.err_cb = on_err;
2019-02-12 21:38:42 +00:00
ctx->ref_count++;
2019-02-12 10:17:37 +00:00
as->on_tcp_write.name = "tcp-write";
2019-02-12 18:29:00 +00:00
as->on_tcp_write.flags = EPOLLOUT | EPOLLET | EPOLLRDHUP;
2019-02-12 21:38:42 +00:00
as->on_tcp_write.app_ctx = ctx;
as->on_tcp_write.free_app_ctx = free_naive;
2019-02-18 16:51:13 +00:00
as->on_tcp_write.cb = on_tcp_write;
2019-02-18 20:55:53 +00:00
as->on_tcp_write.err_cb = on_err;
2019-02-12 21:38:42 +00:00
ctx->ref_count++;
2019-02-12 10:17:37 +00:00
as->on_udp_write.name = "udp-write";
as->on_udp_write.flags = EPOLLOUT | EPOLLET;
2019-02-12 21:38:42 +00:00
as->on_udp_write.app_ctx = ctx;
as->on_udp_write.free_app_ctx = free_naive;
2019-02-18 16:51:13 +00:00
as->on_udp_write.cb = on_udp_write;
2019-02-18 20:55:53 +00:00
as->on_udp_write.err_cb = on_err;
2019-02-12 21:38:42 +00:00
ctx->ref_count++;
2019-02-12 10:17:37 +00:00
2019-02-12 16:47:14 +00:00
return;
init_err:
fprintf(stderr, "Failed to init algo naive\n");
exit(EXIT_FAILURE);
2019-02-11 21:40:00 +00:00
}