Add tor echo logging feature
This commit is contained in:
parent
8b04b391b0
commit
c919367e35
1 changed files with 27 additions and 9 deletions
|
@ -6,6 +6,12 @@
|
|||
#include "evt_core.h"
|
||||
#include "net_tools.h"
|
||||
#include "url.h"
|
||||
#include "measure.h"
|
||||
|
||||
struct torecho_ctx {
|
||||
uint8_t is_measlat;
|
||||
struct measure_conf mc;
|
||||
};
|
||||
|
||||
void te_create_onion_services(struct tor_os_str* tos, struct tor_ctl* tctl, uint16_t* ports, int ports_count, enum TOR_ONION_FLAGS tof) {
|
||||
tor_os_create (tos, "onion_services.pub", "onion_services.txt", ports_count);
|
||||
|
@ -55,21 +61,23 @@ co_error:
|
|||
}
|
||||
|
||||
int te_on_tcp(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
|
||||
char buffer[1500];
|
||||
ssize_t nread, nwritten;
|
||||
struct sockaddr_in addr;
|
||||
socklen_t addrlen = sizeof(addr);
|
||||
struct torecho_ctx *tctx = fdinfo->cat->app_ctx;
|
||||
|
||||
nread = recv(fdinfo->fd, buffer, sizeof(buffer), 0);
|
||||
nread = recv(fdinfo->fd, tctx->mc.payload, tctx->mc.payload_size, 0);
|
||||
if (nread == -1 && errno == EAGAIN) return 1; // Read done
|
||||
if (nread == 0) { fprintf(stderr, "WARN! Read 0 bytes.\n"); return 1; }
|
||||
if (nread < 0 || nread > sizeof(buffer)) {
|
||||
fprintf(stderr, "Message is either truncated or an error occured. nread=%ld\n", nread);
|
||||
if (nread < 0 || nread > tctx->mc.payload_size) {
|
||||
fprintf(stderr, "Message is either truncated or an error occured. nread=%ld, expected=%ld\n", nread, tctx->mc.payload_size);
|
||||
perror("read errno");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
nwritten = send(fdinfo->fd, buffer, nread, 0);
|
||||
if (tctx->is_measlat) measure_parse (nread, &tctx->mc);
|
||||
|
||||
nwritten = send(fdinfo->fd, tctx->mc.payload, nread, 0);
|
||||
// @FIXME don't support EAGAIN on write. Could be intended, you don't think so?
|
||||
if (nwritten != nread) {
|
||||
fprintf(stderr, "Didn't write the same number of bytes as read - not supported. nread=%ld, nwritten=%ld\n", nread, nwritten);
|
||||
|
@ -91,13 +99,21 @@ int main(int argc, char** argv) {
|
|||
struct tor_ctl tctl;
|
||||
enum TOR_ONION_FLAGS tof = TOR_ONION_FLAG_NONE;
|
||||
char url[1024];
|
||||
struct torecho_ctx tctx = {0};
|
||||
tctx.mc.inhibit_exit = 1;
|
||||
tctx.mc.payload_size = 1500;
|
||||
|
||||
|
||||
while ((opt = getopt(argc, argv, "n")) != -1) {
|
||||
while ((opt = getopt(argc, argv, "ns:m")) != -1) {
|
||||
switch(opt) {
|
||||
case 'n':
|
||||
tof |= TOR_ONION_FLAG_NON_ANONYMOUS;
|
||||
break;
|
||||
case 'm':
|
||||
tctx.is_measlat = 1;
|
||||
break;
|
||||
case 's':
|
||||
tctx.mc.payload_size = atoi(optarg);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -105,7 +121,7 @@ int main(int argc, char** argv) {
|
|||
|
||||
// 1. Register categories
|
||||
struct evt_core_cat tcp_co = {
|
||||
.app_ctx = NULL,
|
||||
.app_ctx = &tctx,
|
||||
.free_app_ctx = NULL,
|
||||
.cb = te_on_tcp_co,
|
||||
.err_cb = NULL,
|
||||
|
@ -114,7 +130,7 @@ int main(int argc, char** argv) {
|
|||
.socklist = NULL
|
||||
};
|
||||
struct evt_core_cat tcp_all = {
|
||||
.app_ctx = NULL,
|
||||
.app_ctx = &tctx,
|
||||
.free_app_ctx = NULL,
|
||||
.cb = te_on_tcp,
|
||||
.err_cb = NULL,
|
||||
|
@ -122,6 +138,8 @@ int main(int argc, char** argv) {
|
|||
.flags = EPOLLIN | EPOLLOUT | EPOLLET | EPOLLRDHUP,
|
||||
.socklist = NULL
|
||||
};
|
||||
measure_prepare (&tctx.mc);
|
||||
|
||||
evt_core_init(&evts, 0);
|
||||
evt_core_add_cat(&evts, &tcp_co);
|
||||
evt_core_add_cat(&evts, &tcp_all);
|
||||
|
|
Loading…
Reference in a new issue