Compiling but not tested project

This commit is contained in:
Quentin Dufour 2019-03-25 17:20:47 +01:00
parent 9d4aae315e
commit 4f1c1b3c1b
5 changed files with 143 additions and 126 deletions

View file

@ -21,20 +21,19 @@ void init_socks5_client(struct donar_client_ctx* app_ctx, int pos) {
int on_socks5_success(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
struct evt_core_fdinfo fdinfo_n = {0};
struct evt_core_cat cat_n = {0};
char url[1024];
struct socks5_ctx* s5ctx = fdinfo->other;
fdinfo_n.cat = &cat_n;
int ret = 0;
fdinfo_n.url = url;
fdinfo_n.fd = dup(fdinfo->fd);
fdinfo_n.cat->name = "tcp-write";
ret = asprintf(&fdinfo_n.url, "tcp:write:127.0.0.1:%d", s5ctx->port);
if (ret < 0) goto failed;
sprintf(fdinfo_n.url, "tcp:write:127.0.0.1:%d", s5ctx->port);
evt_core_add_fd (ctx, &fdinfo_n);
fdinfo_n.fd = dup(fdinfo->fd);
fdinfo_n.cat->name = "tcp-read";
ret = asprintf(&fdinfo_n.url, "tcp:read:127.0.0.1:%d", s5ctx->port);
if (ret < 0) goto failed;
sprintf(fdinfo_n.url, "tcp:read:127.0.0.1:%d", s5ctx->port);
evt_core_add_fd (ctx, &fdinfo_n);
evt_core_rm_fd (ctx, fdinfo->fd);

View file

@ -1,5 +1,4 @@
#pragma once
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <gmodule.h>

View file

@ -8,6 +8,11 @@
#include "net_tools.h"
#include "socks5.h"
struct measlat_ctx {
int count, size, interval;
char *host, *port, *transport;
};
struct measure_conf {
uint64_t max_measure;
uint64_t payload_size;
@ -79,6 +84,12 @@ int on_udp(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
return 0;
}
void free_timer_conf(void* v) {
struct measure_conf* mc = v;
free(mc->payload);
free(mc);
}
int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
ssize_t s;
uint64_t ticks;
@ -116,109 +127,6 @@ int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
return 1;
}
void free_timer_conf(void* v) {
struct measure_conf* mc = v;
free(mc->payload);
free(mc);
}
void register_categories(struct evt_core_ctx* evts) {
struct evt_core_cat tcp_read = {
.app_ctx = NULL,
.free_app_ctx = NULL,
.cb = on_udp,
.err_cb = on_udp_err,
.name = "tcp-read",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
struct evt_core_cat udp_read = {
.app_ctx = NULL,
.free_app_ctx = NULL,
.cb = on_udp,
.err_cb = on_udp_err,
.name = "udp-read",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
struct evt_core_cat timer = {
.app_ctx = NULL,
.free_app_ctx = NULL,
.cb = on_timer,
.err_cb = NULL,
.name = "timer",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
evt_core_init(evts);
evt_core_add_cat (evts, &udp_read);
evt_core_add_cat (evts, &tcp_read);
evt_core_add_cat(evts, &timer);
printf("--- Categories registered\n");
}
int register_tor_socket(struct evt_core_ctx* evts, char* host, char* port, int count, int size) {
int socks5_sock = -1, err = 0;
for (int i = 120; i > 0; i--) {
sleep(2);
printf("Remaining try %d/120 to connect to %s:%s\n", i, host, port);
if (socks5_sock >= 0) {
close(socks5_sock);
socks5_sock = -1;
}
socks5_sock = create_tcp_client ("127.0.0.1", "9050");
err = socks5_handshake_syn (socks5_sock);
if (err < 0) continue;
err = socks5_handshake_ack (socks5_sock);
if (err < 0) continue;
err = socks5_connect_dns (socks5_sock, host, atoi(port));
if (err < 0) continue;
err = socks5_reply (socks5_sock);
if (err < 0) {
fprintf(stderr, "Socks5 error %s\n", socks5_rep(-err));
continue;
}
else break;
}
if (socks5_sock < 0 || err < 0) {
exit(EXIT_FAILURE);
}
char url[1024];
struct evt_core_cat cat = {0};
struct evt_core_fdinfo fdinfo = {0};
fdinfo.cat = &cat;
fdinfo.url = url;
fdinfo.fd = socks5_sock;
fdinfo.cat->name = "tcp-read";
fdinfo.other = create_measure_conf (count, size);
fdinfo.free_other = free_mesure_conf;
sprintf(fdinfo.url, "tcp:read:%s:%s", host, port);
evt_core_add_fd (evts, &fdinfo);
printf("--- Tor socket registered\n");
return fdinfo.fd;
}
int register_udp_socket(struct evt_core_ctx* evts, char* host, char* port, int count, int size) {
int udp_sock = create_udp_client (host, port);
char url[1024];
struct evt_core_cat cat = {0};
struct evt_core_fdinfo fdinfo = {0};
fdinfo.cat = &cat;
fdinfo.url = url;
fdinfo.fd = udp_sock;
fdinfo.cat->name = "udp-read";
fdinfo.other = create_measure_conf (count, size);
fdinfo.free_other = free_mesure_conf;
sprintf(fdinfo.url, "udp:read:%s:%s", host, port);
evt_core_add_fd (evts, &fdinfo);
printf("--- UDP socket registered\n");
return fdinfo.fd;
}
void register_timer(struct evt_core_ctx* evts, int udp, int interval, int count, int size) {
struct timespec now;
@ -256,34 +164,144 @@ void register_timer(struct evt_core_ctx* evts, int udp, int interval, int count,
printf("--- Timer registered\n");
}
int on_socks5_success_measlat(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
char url[1024];
struct evt_core_cat cat = {0};
struct evt_core_fdinfo fdinfo_n = {0};
struct socks5_ctx* s5ctx = fdinfo->other;
fdinfo_n.cat = &cat;
fdinfo_n.url = url;
struct evt_core_cat* ucat = evt_core_get_from_cat (ctx, "tcp-read");
if (ucat == NULL) {
fprintf(stderr, "Category udp-read not found\n");
exit(EXIT_FAILURE);
}
struct measlat_ctx* mctx = ucat->app_ctx;
fdinfo_n.fd = dup(fdinfo->fd);
fdinfo_n.cat->name = "tcp-read";
fdinfo_n.other = create_measure_conf (mctx->count, mctx->size);
fdinfo_n.free_other = free_mesure_conf;
sprintf(fdinfo_n.url, "tcp:read:%s:%d", s5ctx->addr, s5ctx->port);
evt_core_add_fd (ctx, &fdinfo_n);
printf("--- Tor socket registered\n");
register_timer(ctx, fdinfo->fd, mctx->interval, mctx->count, mctx->size);
return 1;
}
void spawn_tor_socket(struct evt_core_ctx* evts) {
struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "tcp-read");
if (ucat == NULL) {
fprintf(stderr, "Category udp-read not found\n");
exit(EXIT_FAILURE);
}
struct measlat_ctx* mctx = ucat->app_ctx;
socks5_create_dns_client (evts, "127.0.0.1", "9050", mctx->host, atoi(mctx->port));
}
int on_socks5_failed_measlat(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
evt_core_rm_fd (ctx, fdinfo->fd);
sleep(1);
spawn_tor_socket(ctx);
return 1;
}
void register_categories(struct evt_core_ctx* evts, struct measlat_ctx* mctx) {
struct evt_core_cat template = {0};
template.app_ctx = mctx;
evt_core_init(evts);
template.cb = on_timer;
template.name = "timer";
template.flags = EPOLLIN | EPOLLET;
evt_core_add_cat(evts, &template);
template.cb = on_udp; // intended but not elegant
template.err_cb = on_udp_err; // intended but not elegant
template.name = "tcp-read";
template.flags = EPOLLIN | EPOLLET;
evt_core_add_cat(evts, &template);
template.cb = on_udp;
template.err_cb = on_udp_err;
template.name = "udp-read";
template.flags = EPOLLIN | EPOLLET;
evt_core_add_cat(evts, &template);
template.cb = on_socks5_success_measlat;
template.err_cb = on_socks5_failed_measlat;
template.name = "socks5-success";
template.flags = EPOLLET;
template.cb = on_socks5_failed_measlat;
template.err_cb = on_socks5_failed_measlat;
template.name = "socks5-failed";
template.flags = EPOLLET;
socks5_init(evts);
printf("--- Categories registered\n");
}
void spawn_udp_socket(struct evt_core_ctx* evts) {
struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "udp-read");
if (ucat == NULL) {
fprintf(stderr, "Category udp-read not found\n");
exit(EXIT_FAILURE);
}
struct measlat_ctx* mctx = ucat->app_ctx;
int udp_sock = create_udp_client (mctx->host, mctx->port);
char url[1024];
struct evt_core_cat cat = {0};
struct evt_core_fdinfo fdinfo = {0};
fdinfo.cat = &cat;
fdinfo.url = url;
fdinfo.fd = udp_sock;
fdinfo.cat->name = "udp-read";
fdinfo.other = create_measure_conf (mctx->count, mctx->size);
fdinfo.free_other = free_mesure_conf;
sprintf(fdinfo.url, "udp:read:%s:%s", mctx->host, mctx->port);
evt_core_add_fd (evts, &fdinfo);
printf("--- UDP socket registered\n");
register_timer(evts, fdinfo.fd, mctx->interval, mctx->count, mctx->size);
}
int main(int argc, char** argv) {
setvbuf(stdout, NULL, _IONBF, 0);
printf("~ measlat ~\n");
int opt, udp_fd, count = 0, size = 0, interval = 0;
char *host = NULL, *port = NULL, *transport = NULL;
int opt;
struct measlat_ctx mctx = {0};
struct evt_core_ctx evts = {0};
// 1. Parse parameters
while ((opt = getopt(argc, argv, "h:p:c:s:i:t:")) != -1) {
switch(opt) {
case 'h': // host
host = optarg;
mctx.host = optarg;
break;
case 'p': // port
port = optarg;
mctx.port = optarg;
break;
case 't': // transport
transport = optarg;
mctx.transport = optarg;
break;
case 'c': // count
count = atoi(optarg);
mctx.count = atoi(optarg);
break;
case 's': // size - payload in bytes
size = atoi(optarg);
mctx.size = atoi(optarg);
break;
case 'i': // interval - every ms
interval = atoi(optarg);
mctx.interval = atoi(optarg);
break;
default:
goto usage;
@ -292,17 +310,16 @@ int main(int argc, char** argv) {
// 2. Check and fix parameters
size_t header_size = sizeof(struct packet_header);
if (interval <= 0) interval = 1000;
if (count <= 0) count = 1;
if (size < header_size) size = header_size;
if (transport == NULL) transport = "udp";
if (host == NULL || port == NULL) goto usage;
if (mctx.interval <= 0) mctx.interval = 1000;
if (mctx.count <= 0) mctx.count = 1;
if (mctx.size < header_size) mctx.size = header_size;
if (mctx.transport == NULL) mctx.transport = "udp";
if (mctx.host == NULL || mctx.port == NULL) goto usage;
// 3. Bind events
register_categories(&evts);
if (strcmp(transport, "udp") == 0) udp_fd = register_udp_socket(&evts, host, port, count, size);
else if (strcmp(transport, "tor") == 0) udp_fd = register_tor_socket(&evts, host, port, count, size);
register_timer(&evts, udp_fd, interval, count, size);
register_categories(&evts, &mctx);
if (strcmp(mctx.transport, "udp") == 0) spawn_udp_socket(&evts);
else if (strcmp(mctx.transport, "tor") == 0) spawn_tor_socket(&evts);
// 4. Run main loop
evt_core_loop(&evts);

View file

@ -43,6 +43,7 @@ void socks5_create_dns_client(struct evt_core_ctx* ctx, char* proxy_host, char*
// 3. Fill socks5_ctx structures
s5ctx = fdinfo.other;
s5ctx->port = port;
s5ctx->addr = strdup(addr);
// 3.1 Client handshake to send
s5ctx->ch.ver = VER_SOCKS5;

View file

@ -109,6 +109,7 @@ struct server_reply {
struct socks5_ctx {
uint16_t port;
char* addr;
struct client_handshake ch;
struct server_handshake sh;