From b6e31d7bdca4a159f9ec2276247673d8a1459295 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 6 Mar 2019 16:39:39 +0100 Subject: [PATCH] Rework code. Buffering was not a good idea --- Dockerfile | 2 +- README.md | 8 ++++++++ scripts/xp1 | 34 ++++++++++++++++++++++++++++++++++ scripts/xp1-client | 10 ++++++---- scripts/xp1-server | 7 ++++--- src/donar.c | 1 + src/donar_client.c | 16 +++++++++++----- src/evt_core.c | 2 +- src/socks5.c | 4 ++++ src/socks5.h | 13 +++++++++++++ 10 files changed, 83 insertions(+), 14 deletions(-) create mode 100755 scripts/xp1 diff --git a/Dockerfile b/Dockerfile index faf4e44..1778bd1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ RUN mkdir out && \ ninja FROM fedora:29 -RUN dnf install -y glib2 tor +RUN dnf install -y glib2 tor valgrind WORKDIR /home/donar RUN mkdir /home/donar/shared && mkdir /home/donar/res && chown -R 1000 /home/donar USER 1000 diff --git a/README.md b/README.md index 03cac15..8a1a74e 100644 --- a/README.md +++ b/README.md @@ -25,14 +25,22 @@ mkdir -p ./{xp1-shared,xp1-res} sudo chown -R 1000 ./{xp1-shared,xp1-res} sudo docker run -t -i \ + --privileged \ -v `pwd`/xp1-shared:/home/donar/shared \ registry.gitlab.inria.fr/qdufour/donar \ xp1-server sudo docker run -t -i \ + --privileged \ -v `pwd`/xp1-res:/home/donar/res \ -v `pwd`/xp1-shared:/home/donar/shared \ registry.gitlab.inria.fr/qdufour/donar \ xp1-client 1000 100 100 ``` + +## Run an XP instance + +``` +sudo ./scripts/xp1 1000 100 100 +``` diff --git a/scripts/xp1 b/scripts/xp1 new file mode 100755 index 0000000..78945d4 --- /dev/null +++ b/scripts/xp1 @@ -0,0 +1,34 @@ +#!/bin/bash + +# Create workdir +XP_FOLDER=`mktemp -d -p . xp1-XXXXXXXXXXXXXXXX` +mkdir -p $XP_FOLDER +cd $XP_FOLDER + +# Create folders +mkdir -p ./{shared,log,res} +chown -R 1000 ./{shared,log,res} + +# Run server +docker run \ + --privileged \ + --rm \ + -v `pwd`/shared:/home/donar/shared \ + -v `pwd`/log:/home/donar/log \ + registry.gitlab.inria.fr/qdufour/donar \ + xp1-server & + +sleep 30 + +# Run client +docker run \ + --privileged \ + --rm \ + -v `pwd`/res:/home/donar/res \ + -v `pwd`/shared:/home/donar/shared \ + -v `pwd`/log:/home/donar/log \ + registry.gitlab.inria.fr/qdufour/donar \ + xp1-client $1 $2 $3 + +# Kill server +kill %1 diff --git a/scripts/xp1-client b/scripts/xp1-client index 33c60b8..df0bcad 100755 --- a/scripts/xp1-client +++ b/scripts/xp1-client @@ -1,7 +1,9 @@ #!/bin/bash -tor -f /etc/torrc & +mkdir -p ./log +tor -f /etc/torrc > ./log/client-tor-stdout.log 2> ./log/client-tor-stderr.log & sleep 2 mkdir -p ./shared -donar -a naive -c -o ./shared/onion_services.pub -r 9000 & -measlat -h 127.13.3.7 -p 9000 # Used to wait for connections -measlat -h 127.13.3.7 -p 9000 -c $1 -i $2 -s $3 > `mktemp -p ./res res-XXXXXXXXXXXXXXXX.txt` +valgrind donar -a naive -c -o ./shared/onion_services.pub -r 9000 > ./log/client-donar-stdout.log 2> ./log/client-donar-stderr.log & +sleep 2 +measlat -h 127.13.3.7 -p 9000 > /dev/null 2>&1 # Used to wait for connections +measlat -h 127.13.3.7 -p 9000 -c $1 -i $2 -s $3 > `mktemp -p ./res res-XXXXXXXXXXXXXXXX.txt` 2> ./log/client-measlat-stderr.log diff --git a/scripts/xp1-server b/scripts/xp1-server index 1cfc4d9..a5a16d1 100755 --- a/scripts/xp1-server +++ b/scripts/xp1-server @@ -1,8 +1,9 @@ #!/bin/bash -tor -f /etc/torrc & +mkdir -p ./log +tor -f /etc/torrc > ./log/server-tor-stdout.log 2> ./log/server-tor-stderr.log & sleep 2 mkdir -p ./shared cd ./shared pwd -donar -a naive -s -e 9000 & -udpecho -p 9000 +valgrind donar -a naive -s -e 9000 > ../log/server-donar-stdout.log 2> ../log/server-donar-stderr.log & +udpecho -p 9000 > ../log/server-udpecho-stdout.log 2> ../log/server-udpecho-stderr.log diff --git a/src/donar.c b/src/donar.c index 825977d..e098bdc 100644 --- a/src/donar.c +++ b/src/donar.c @@ -13,6 +13,7 @@ void free_port (void* ptr) { } int main(int argc, char** argv) { + setvbuf(stdout, NULL, _IONBF, 0); printf("~ Donar ~\n"); int opt, is_server, is_client, errored; diff --git a/src/donar_client.c b/src/donar_client.c index 9b1df78..2a0bd21 100644 --- a/src/donar_client.c +++ b/src/donar_client.c @@ -67,13 +67,20 @@ int configure_tcp_clients(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdin case SOCKS5_STATE_ACK: //@FIXME: We suppose that we will be able to do the whole read at once which is wrong too err = socks5_reply (fdinfo->fd); - fprintf(stderr, "captured err: %d\n", err); - if (err < 0) goto on_socks5_err; + if (err == -SOCKS5_REP_GENERAL_FAILURE || err == -SOCKS5_REP_TTLEXP || err == -SOCKS5_REP_HOSTUNREACH) { + fprintf(stderr, "%s is not ready, received %s\n", fdinfo->url, socks5_rep(-err)); + goto on_socks5_err; + } else if (err < 0) { + fprintf(stderr, "An other error occured on %s\n", fdinfo->url); + exit(EXIT_FAILURE); + } app_ctx->client_sock[pos].state = SOCKS5_STATE_RDY; int sock1, sock2; sock1 = dup(fdinfo->fd); sock2 = dup(fdinfo->fd); - if (sock1 < 0 || sock2 < 0) goto on_socks5_err; + if (sock1 < 0 || sock2 < 0) { + exit(EXIT_FAILURE); + } void* fdcat = evt_core_rm_fd (ctx, fdinfo->fd); if (fdcat == NULL) { exit(EXIT_FAILURE); @@ -107,10 +114,9 @@ int configure_tcp_clients(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdin return 1; on_socks5_err: - perror("An error occured while connecting to an Onion Service"); + //perror("An error occured while connecting to an Onion Service"); app_ctx->client_sock[pos].state = SOCKS5_STATE_ERR; evt_core_rm_fd (ctx, fdinfo->fd); - perror("0"); sleep(2); init_tcp_client (app_ctx, pos); return 1; diff --git a/src/evt_core.c b/src/evt_core.c index 4ef18af..7e3ee92 100644 --- a/src/evt_core.c +++ b/src/evt_core.c @@ -178,7 +178,7 @@ void evt_core_loop(struct evt_core_ctx* ctx) { if (fdinfo != NULL) { if (fdinfo->cat->err_cb != NULL) { if (fdinfo->cat->err_cb(ctx, fdinfo) == 1) { - fprintf(stderr, "fd=%d on cat=%s is handled by app\n", err_fd, fdinfo->cat->name); + fprintf(stderr, "Error on fd=%d on cat=%s is handled by app, not clearing it\n", err_fd, fdinfo->cat->name); continue; } } diff --git a/src/socks5.c b/src/socks5.c index 2e450e1..a519965 100644 --- a/src/socks5.c +++ b/src/socks5.c @@ -104,3 +104,7 @@ int socks5_connect_dns(int sock, char* addr, uint16_t port) { } return 0; } + +char* socks5_rep (enum socks5_rep rep) { + return rep_msg[rep]; +} diff --git a/src/socks5.h b/src/socks5.h index c56acb6..742f663 100644 --- a/src/socks5.h +++ b/src/socks5.h @@ -36,6 +36,18 @@ union socks5_addr { uint8_t ipv6[16]; }; +enum socks5_rep { + SOCKS5_REP_SUCCESS, + SOCKS5_REP_GENERAL_FAILURE, + SOCKS5_REP_CONOTALLOWED, + SOCKS5_REP_NETUNREACH, + SOCKS5_REP_HOSTUNREACH, + SOCKS5_REP_COREFUSED, + SOCKS5_REP_TTLEXP, + SOCKS5_REP_CMDNOTSUP, + SOCKS5_REP_ADDRNOTSUP +}; + static char* rep_msg[] = { "Succeeded", "General SOCKS server failure", @@ -87,3 +99,4 @@ int socks5_handshake_syn(int sock); int socks5_handshake_ack(int sock); int socks5_connect_dns(int sock, char* addr, uint16_t port); int socks5_reply(int sock); +char* socks5_rep (enum socks5_rep rep);