2019-05-06 10:02:25 +00:00
|
|
|
#include "proxy.h"
|
|
|
|
#include "algo_utils.h"
|
2019-08-09 16:09:15 +00:00
|
|
|
#include "packet.h"
|
2019-05-06 10:02:25 +00:00
|
|
|
|
|
|
|
struct dup2_ctx {
|
|
|
|
uint16_t recv_id;
|
|
|
|
uint16_t emit_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
void algo_dup2_init(struct evt_core_ctx* ctx, struct algo_ctx* app_ctx, struct algo_params* ap) {
|
|
|
|
app_ctx->misc = malloc(sizeof(struct dup2_ctx));
|
|
|
|
if (app_ctx->misc == NULL) {
|
|
|
|
perror("malloc failed in algo dup2 init");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
memset(app_ctx->misc, 0, sizeof(struct dup2_ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
int algo_dup2_on_stream(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
|
|
|
|
char url[256];
|
|
|
|
struct evt_core_fdinfo *to_fdinfo = NULL;
|
|
|
|
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
|
2019-08-12 13:28:22 +00:00
|
|
|
union abstract_packet *ap = (union abstract_packet*) &bp->ip;
|
2019-05-06 10:02:25 +00:00
|
|
|
struct dup2_ctx* dup2c = app_ctx->misc;
|
2019-08-12 13:28:22 +00:00
|
|
|
int32_t id = -1, port = -1;
|
2019-08-13 08:58:20 +00:00
|
|
|
if (ctx->verbose > 1) {
|
|
|
|
fprintf(stderr, " [algo_dup2] Received a buffer\n");
|
|
|
|
dump_buffer_packet(bp);
|
|
|
|
}
|
2019-08-12 13:28:22 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
switch (ap->fmt.headers.cmd) {
|
|
|
|
case CMD_UDP_METADATA_THUNDER:
|
|
|
|
id = ap->fmt.content.udp_metadata_thunder.id;
|
|
|
|
break;
|
|
|
|
case CMD_UDP_ENCAPSULATED:
|
|
|
|
port = ap->fmt.content.udp_encapsulated.port;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-08-09 16:09:15 +00:00
|
|
|
}
|
2019-08-12 13:28:22 +00:00
|
|
|
} while ((ap = ap_next(ap)) != NULL);
|
2019-08-13 08:47:50 +00:00
|
|
|
if (ctx->verbose > 1) fprintf(stderr, " [algo_dup2] Extracted port=%d and id=%d\n", port, id);
|
2019-08-09 16:09:15 +00:00
|
|
|
|
2019-08-12 13:28:22 +00:00
|
|
|
if (port == -1 || id == -1) {
|
2019-08-13 08:47:50 +00:00
|
|
|
fprintf(stderr, "Missing data port=%d and id=%d...\n", port, id);
|
2019-08-12 13:28:22 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2019-08-09 16:09:15 +00:00
|
|
|
|
2019-08-12 13:28:22 +00:00
|
|
|
// Check that received identifier has not been delivered
|
|
|
|
if (ring_ge(dup2c->recv_id, id)) {
|
2019-08-13 08:47:50 +00:00
|
|
|
if (ctx->verbose > 1) fprintf(stderr, " [algo_dup2] Packet already delivered, dropping\n");
|
2019-08-13 08:53:11 +00:00
|
|
|
mv_buffer_rtof(&app_ctx->br, fdinfo);
|
2019-08-12 13:28:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2019-08-09 16:09:15 +00:00
|
|
|
|
2019-08-12 13:28:22 +00:00
|
|
|
// Update delivered identifier
|
|
|
|
dup2c->recv_id = id;
|
2019-05-06 10:02:25 +00:00
|
|
|
|
2019-08-12 13:28:22 +00:00
|
|
|
// 1. Find destination
|
|
|
|
sprintf(url, "udp:write:127.0.0.1:%d", port);
|
|
|
|
to_fdinfo = evt_core_get_from_url (ctx, url);
|
|
|
|
if (to_fdinfo == NULL) {
|
|
|
|
fprintf(stderr, "No fd for URL %s in tcp-read. Dropping packet :( \n", url);
|
2019-08-13 08:53:11 +00:00
|
|
|
mv_buffer_rtof (&app_ctx->br, fdinfo);
|
2019-08-12 13:28:22 +00:00
|
|
|
return 1;
|
2019-05-06 10:02:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 13:28:22 +00:00
|
|
|
// 2. Move buffer
|
2019-08-13 08:47:50 +00:00
|
|
|
if (ctx->verbose > 1) fprintf(stderr, " [algo_dup2] Scheduling packet for write\n");
|
2019-08-13 08:53:11 +00:00
|
|
|
mv_buffer_rtow (&app_ctx->br, fdinfo, to_fdinfo);
|
2019-08-12 13:28:22 +00:00
|
|
|
main_on_udp_write(ctx, to_fdinfo);
|
|
|
|
|
|
|
|
return 0;
|
2019-05-06 10:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int algo_dup2_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
|
|
|
|
struct evt_core_fdinfo *to_fdinfo = NULL;
|
|
|
|
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
|
|
|
|
|
|
|
|
struct dup2_ctx* dup2c = app_ctx->misc;
|
2019-08-13 08:53:11 +00:00
|
|
|
dup2c->emit_id = dup2c->emit_id + 1;
|
2019-08-12 13:28:22 +00:00
|
|
|
union abstract_packet metadata = {
|
|
|
|
.fmt.headers.cmd = CMD_UDP_METADATA_THUNDER,
|
|
|
|
.fmt.headers.size = sizeof(metadata.fmt.headers) + sizeof(metadata.fmt.content.udp_metadata_thunder),
|
|
|
|
.fmt.headers.flags = 0,
|
|
|
|
.fmt.content.udp_metadata_thunder.id = dup2c->emit_id
|
|
|
|
};
|
|
|
|
buffer_append_ap (bp, &metadata);
|
2019-08-13 08:58:20 +00:00
|
|
|
if (ctx->verbose > 1) {
|
|
|
|
dump_buffer_packet(bp);
|
|
|
|
fprintf(stderr, " [algo_dup2] Added metadata\n");
|
|
|
|
}
|
2019-08-13 08:47:50 +00:00
|
|
|
|
2019-05-06 13:01:42 +00:00
|
|
|
struct evt_core_cat* cat = evt_core_get_from_cat (ctx, "tcp-write");
|
2019-05-09 13:33:27 +00:00
|
|
|
for (int i = 0; i < app_ctx->ap.links; i++) {
|
2019-05-06 10:02:25 +00:00
|
|
|
// 1. A whole packet has been read, we will find someone to write it
|
2019-05-06 15:17:45 +00:00
|
|
|
to_fdinfo = cat->socklist->len > i ? g_array_index(cat->socklist, struct evt_core_fdinfo*, i) : NULL;
|
2019-05-06 10:02:25 +00:00
|
|
|
if (to_fdinfo == NULL) {
|
2019-05-06 13:03:53 +00:00
|
|
|
fprintf(stderr, "No fd for cat %s in udp-read.\n", cat->name);
|
2019-05-06 12:52:57 +00:00
|
|
|
continue;
|
2019-05-06 10:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. We move the buffer and notify the target
|
2019-05-27 15:32:00 +00:00
|
|
|
dup_buffer_tow (&app_ctx->br, bp, to_fdinfo);
|
2019-05-06 10:02:25 +00:00
|
|
|
main_on_tcp_write(ctx, to_fdinfo);
|
|
|
|
}
|
2019-08-13 08:47:50 +00:00
|
|
|
if (ctx->verbose > 1) fprintf(stderr, " [algo_dup2] Packets sent\n");
|
2019-05-06 10:02:25 +00:00
|
|
|
|
2019-05-06 12:52:57 +00:00
|
|
|
// 3. Release the buffer
|
2019-05-27 15:32:00 +00:00
|
|
|
mv_buffer_rtof (&app_ctx->br, fdinfo);
|
2019-05-06 12:52:57 +00:00
|
|
|
|
2019-05-06 10:02:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int algo_dup2_on_err(struct evt_core_ctx *ctx, struct evt_core_fdinfo *fdinfo) {
|
|
|
|
// We do nothing
|
|
|
|
return 1;
|
|
|
|
}
|