WIP multiple port support

This commit is contained in:
Quentin Dufour 2019-02-15 18:09:51 +01:00
parent fb6a9dfab0
commit a03d90ca84
6 changed files with 47 additions and 32 deletions

View file

@ -3,33 +3,36 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <gmodule.h>
#include "algo_skel.h" #include "algo_skel.h"
#include "donar_client.h" #include "donar_client.h"
#include "donar_server.h" #include "donar_server.h"
void free_port (void* ptr) {
free(ptr);
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
printf("~ Donar ~\n"); printf("~ Donar ~\n");
int opt, is_server, is_client, errored; int opt, is_server, is_client, errored;
char *host, *port, *onion_file, *algo; char *port, *onion_file, *algo;
host = NULL;
port = NULL;
onion_file = NULL; onion_file = NULL;
algo = NULL; algo = NULL;
is_server = 0; is_server = 0;
is_client = 0; is_client = 0;
errored = 0; errored = 0;
GPtrArray* ports = g_ptr_array_new_with_free_func (free_port);
while ((opt = getopt(argc, argv, "csh:p:o:a:")) != -1) { while ((opt = getopt(argc, argv, "csh:p:o:a:")) != -1) {
switch(opt) { switch(opt) {
case 's': case 's':
is_server = 1; is_server = 1;
break; break;
case 'h':
host = strdup(optarg);
break;
case 'p': case 'p':
port = strdup(optarg); port = strdup(optarg);
if (port == NULL) goto terminate;
g_ptr_array_add (ports, port);
break; break;
case 'o': case 'o':
onion_file = strdup(optarg); onion_file = strdup(optarg);
@ -53,24 +56,23 @@ int main(int argc, char** argv) {
if (is_server) { if (is_server) {
struct donar_server_ctx ctx; struct donar_server_ctx ctx;
if (host == NULL || port == NULL) goto in_error; if (ports->len < 1) goto in_error;
donar_server(&ctx, &as, host, port); donar_server(&ctx, &as, ports);
} else if (is_client) { } else if (is_client) {
struct donar_client_ctx ctx; struct donar_client_ctx ctx;
if (port == NULL || onion_file == NULL) goto in_error; if (ports->len < 1 || onion_file == NULL) goto in_error;
donar_client(&ctx, &as, onion_file, port); donar_client(&ctx, &as, onion_file, ports);
} }
goto terminate; goto terminate;
in_error: in_error:
errored = 1; errored = 1;
fprintf(stderr, "Usage as client : %s -c -a <algo> -p <udp port> -o <onion service file>\n", argv[0]); fprintf(stderr, "Usage as client : %s -c -a <algo> -p <udp port 1> -p <udp port 2> -o <onion service file>\n", argv[0]);
fprintf(stderr, "Usage as server : %s -s -a <algo> -h <udp host> -p <udp port>\n\n", argv[0]); fprintf(stderr, "Usage as server : %s -s -a <algo> -p <udp port 1> -p <udp port 2>\n\n", argv[0]);
fprintf(stderr, "Passed parameters: client=%d, server=%d, algo=%s, host=%s, port=%s, onion_file=%s\n", fprintf(stderr, "Passed parameters: client=%d, server=%d, algo=%s, registered_ports=%d, onion_file=%s\n",
is_client, is_server, algo, host, port, onion_file); is_client, is_server, algo, ports->len, onion_file);
terminate: terminate:
if (host != NULL) free(host);
if (onion_file != NULL) free(onion_file); if (onion_file != NULL) free(onion_file);
if (port != NULL) free(port); if (port != NULL) free(port);
if (algo != NULL) free(algo); if (algo != NULL) free(algo);

View file

@ -88,9 +88,9 @@ on_socks5_err:
init_tcp_client (app_ctx, pos); init_tcp_client (app_ctx, pos);
} }
void init_udp_server(struct donar_client_ctx* ctx, char* port) { void init_udp_socket(char* port, struct donar_client_ctx* ctx) {
int sock1, sock2; int sock1, sock2;
sock1 = create_udp_server(port); sock1 = create_udp_client ("127.0.0.1", port);
if (sock1 < 0) goto socket_failed; if (sock1 < 0) goto socket_failed;
sock2 = dup(sock1); sock2 = dup(sock1);
if (sock2 < 0) goto socket_failed; if (sock2 < 0) goto socket_failed;
@ -99,11 +99,11 @@ void init_udp_server(struct donar_client_ctx* ctx, char* port) {
return; return;
socket_failed: socket_failed:
fprintf(stderr, "Socket initialization for the UDP server failed\n"); fprintf(stderr, "UDP socket init failed\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
void donar_client(struct donar_client_ctx* ctx, struct algo_skel* algo, char* onion_file, char* port) { void donar_client(struct donar_client_ctx* ctx, struct algo_skel* algo, char* onion_file, GPtrArray* ports) {
evt_core_init (&(ctx->evts)); evt_core_init (&(ctx->evts));
struct evt_core_cat init_socks5 = { struct evt_core_cat init_socks5 = {
.app_ctx = ctx, .app_ctx = ctx,
@ -129,8 +129,8 @@ void donar_client(struct donar_client_ctx* ctx, struct algo_skel* algo, char* on
} }
printf("--- TCP Clients Connected\n"); printf("--- TCP Clients Connected\n");
init_udp_server (ctx, port); g_ptr_array_foreach (ports, (void(*)(void*, void*))init_udp_socket, ctx);
printf("--- UDP Server is listening\n"); printf("--- UDP Sockets are configured\n");
evt_core_loop(&(ctx->evts)); evt_core_loop(&(ctx->evts));

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <gmodule.h>
#include "algo_skel.h" #include "algo_skel.h"
#include "tor_os.h" #include "tor_os.h"
#include "socks5.h" #include "socks5.h"
@ -17,4 +18,4 @@ struct donar_client_ctx {
} client_sock[CLIENT_PORT_SIZE]; } client_sock[CLIENT_PORT_SIZE];
}; };
void donar_client(struct donar_client_ctx* ctx, struct algo_skel* as, char* onion_file, char* port); void donar_client(struct donar_client_ctx* ctx, struct algo_skel* as, char* onion_file, GPtrArray* ports);

View file

@ -40,8 +40,22 @@ socket_create_err:
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
void serv_init_udp_socket(char* port, struct donar_server_ctx* ctx) {
int sock1, sock2;
sock1 = create_udp_client ("127.0.0.1", port);
if (sock1 < 0) goto socket_failed;
sock2 = dup(sock1);
if (sock2 < 0) goto socket_failed;
evt_core_add_fd (&(ctx->evts), "udp-read", sock1);
evt_core_add_fd (&(ctx->evts), "udp-write", sock2);
return;
void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, char* udp_host, char* udp_port) { socket_failed:
fprintf(stderr, "UDP socket init failed\n");
exit(EXIT_FAILURE);
}
void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, GPtrArray* ports) {
evt_core_init (&(ctx->evts)); evt_core_init (&(ctx->evts));
evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_co)); evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_co));
evt_core_add_cat (&(ctx->evts), &(algo->on_udp_read)); evt_core_add_cat (&(ctx->evts), &(algo->on_udp_read));
@ -50,14 +64,6 @@ void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, char* ud
evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_write)); evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_write));
printf("--- Categories created\n"); printf("--- Categories created\n");
int sock = create_udp_client (udp_host, udp_port);
if (sock < 0) {
fprintf(stderr, "Unable to create a UDP client socket\n");
exit(EXIT_FAILURE);
}
evt_core_add_fd (&(ctx->evts), "udp-read", sock);
evt_core_add_fd (&(ctx->evts), "udp-write", dup(sock));
printf("--- UDP client is connected\n");
for (uint16_t i = 0; i < PORT_SIZE ; i++) { for (uint16_t i = 0; i < PORT_SIZE ; i++) {
ctx->ports[i] = 7500 + i; ctx->ports[i] = 7500 + i;
@ -67,6 +73,9 @@ void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, char* ud
init_tcp_servers(ctx); init_tcp_servers(ctx);
printf("--- TCP servers are listening\n"); printf("--- TCP servers are listening\n");
g_ptr_array_foreach (ports, (void(*)(void*, void*))serv_init_udp_socket, ctx);
printf("--- UDP Sockets are configured\n");
evt_core_loop (&(ctx->evts)); evt_core_loop (&(ctx->evts));
destroy_resources (&(ctx->tos), &(ctx->tctl)); destroy_resources (&(ctx->tos), &(ctx->tctl));

View file

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <errno.h> #include <errno.h>
#include <gmodule.h>
#include "socks5.h" #include "socks5.h"
#include "tor_os.h" #include "tor_os.h"
#include "tor_ctl.h" #include "tor_ctl.h"
@ -18,4 +19,4 @@ struct donar_server_ctx {
uint16_t ports[PORT_SIZE]; uint16_t ports[PORT_SIZE];
}; };
void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, char* udp_host, char* udp_port); void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, GPtrArray* ports);

View file

@ -31,6 +31,8 @@ union abstract_packet {
char raw; char raw;
struct { struct {
uint16_t size; uint16_t size;
uint16_t port;
uint8_t id;
char payload; char payload;
} str; } str;
}; };