1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <stdlib.h> #include <string.h> #include <assert.h> #include "protocol.h" #include "log.h" #include "socket.h" int proto_init_ctx(protocol_ctx **ctx, proto_init_cb init_fn) { assert(ctx && init_fn); if (!*ctx) *ctx = (protocol_ctx *) malloc(sizeof(**ctx)); assert(*ctx); memset(*ctx, 0, sizeof(**ctx)); if (init_fn(*ctx)) return 1; return 0; } int proto_setup(protocol_ctx *ctx, const char *listen_addr, const char *listen_port, const char *jail_host, const char *jail_port) { assert(ctx); if (fwd_setup_server(&ctx->src, listen_addr, listen_port)) return 1; if (fwd_setup_client_silent(&ctx->dst, jail_host, jail_port)) return 1; if (!ctx->cbs.on_listen) return 1; if (ctx->cbs.on_listen(ctx, listen_addr, listen_port)) return 1; return 0; } int proto_validate_ctx(const protocol_ctx *ctx) { assert(ctx); assert(ctx->cbs.on_listen && ctx->cbs.on_shutdown); return 0; }