Working meas lat via tor

This commit is contained in:
Quentin Dufour 2019-03-11 15:17:15 +01:00
parent 76f11a2def
commit 5a3a367704

View file

@ -6,6 +6,7 @@
#include <errno.h> #include <errno.h>
#include "evt_core.h" #include "evt_core.h"
#include "net_tools.h" #include "net_tools.h"
#include "socks5.h"
struct measure_conf { struct measure_conf {
uint64_t max_measure; uint64_t max_measure;
@ -101,12 +102,13 @@ int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
perror("clock_gettime error"); perror("clock_gettime error");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
struct evt_core_fdinfo* udpinfo = evt_core_get_first_from_cat (ctx, "udp-read"); struct evt_core_fdinfo* tgtinfo = evt_core_get_first_from_cat (ctx, "udp-read");
if (udpinfo == NULL) { if (tgtinfo == NULL) tgtinfo = evt_core_get_first_from_cat (ctx, "tcp-read");
if (tgtinfo == NULL) {
printf("No connection yet\n"); printf("No connection yet\n");
return 1; return 1;
} }
s = send(udpinfo->fd, mc->payload, mc->payload_size, 0); s = send(tgtinfo->fd, mc->payload, mc->payload_size, 0);
if (s < 0) { if (s < 0) {
perror("Send error"); perror("Send error");
//exit(EXIT_FAILURE); //exit(EXIT_FAILURE);
@ -121,6 +123,15 @@ void free_timer_conf(void* v) {
} }
void register_categories(struct evt_core_ctx* evts) { 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 = { struct evt_core_cat udp_read = {
.app_ctx = NULL, .app_ctx = NULL,
.free_app_ctx = NULL, .free_app_ctx = NULL,
@ -142,11 +153,55 @@ void register_categories(struct evt_core_ctx* evts) {
evt_core_init(evts); evt_core_init(evts);
evt_core_add_cat (evts, &udp_read); evt_core_add_cat (evts, &udp_read);
evt_core_add_cat (evts, &tcp_read);
evt_core_add_cat(evts, &timer); evt_core_add_cat(evts, &timer);
printf("--- Categories registered\n"); 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 = 30; i > 0; i--) {
sleep(2);
printf("Try %d/30 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 register_udp_socket(struct evt_core_ctx* evts, char* host, char* port, int count, int size) {
int udp_sock = create_udp_client (host, port); int udp_sock = create_udp_client (host, port);
char url[1024]; char url[1024];
@ -206,25 +261,28 @@ int main(int argc, char** argv) {
printf("~ measlat ~\n"); printf("~ measlat ~\n");
int opt, udp_fd, count = 0, size = 0, interval = 0; int opt, udp_fd, count = 0, size = 0, interval = 0;
char *host = NULL, *port = NULL; char *host = NULL, *port = NULL, *transport = NULL;
struct evt_core_ctx evts = {0}; struct evt_core_ctx evts = {0};
// 1. Parse parameters // 1. Parse parameters
while ((opt = getopt(argc, argv, "h:p:c:s:i:")) != -1) { while ((opt = getopt(argc, argv, "h:p:c:s:i:t:")) != -1) {
switch(opt) { switch(opt) {
case 'h': case 'h': // host
host = optarg; host = optarg;
break; break;
case 'p': case 'p': // port
port = optarg; port = optarg;
break; break;
case 'c': case 't': // transport
transport = optarg;
break;
case 'c': // count
count = atoi(optarg); count = atoi(optarg);
break; break;
case 's': case 's': // size - payload in bytes
size = atoi(optarg); size = atoi(optarg);
break; break;
case 'i': case 'i': // interval - every ms
interval = atoi(optarg); interval = atoi(optarg);
break; break;
default: default:
@ -237,11 +295,13 @@ int main(int argc, char** argv) {
if (interval <= 0) interval = 1000; if (interval <= 0) interval = 1000;
if (count <= 0) count = 1; if (count <= 0) count = 1;
if (size < header_size) size = header_size; if (size < header_size) size = header_size;
if (transport == NULL) transport = "udp";
if (host == NULL || port == NULL) goto usage; if (host == NULL || port == NULL) goto usage;
// 3. Bind events // 3. Bind events
register_categories(&evts); register_categories(&evts);
udp_fd = register_udp_socket(&evts, host, port, count, size); 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_timer(&evts, udp_fd, interval, count, size);
// 4. Run main loop // 4. Run main loop
@ -249,6 +309,6 @@ int main(int argc, char** argv) {
return 0; return 0;
usage: usage:
fprintf(stderr, "Usage: %s -h <host> -p <port> [-c <count>] [-i <ms>] [-s <bytes>]\n", argv[0]); fprintf(stderr, "Usage: %s -h <host> -p <port> [-t <udp|tor>] [-c <count>] [-i <ms>] [-s <bytes>]\n", argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }