From 4c6833d470eda4be02b4dfb32cdeba9d93c67f43 Mon Sep 17 00:00:00 2001 From: cbdev Date: Sun, 26 May 2019 14:30:22 +0200 Subject: Backend configuration, main config parsing --- builtins.c | 8 ++--- websocksy.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++------------- websocksy.h | 13 +++---- 3 files changed, 98 insertions(+), 37 deletions(-) diff --git a/builtins.c b/builtins.c index d5b081f..3461521 100644 --- a/builtins.c +++ b/builtins.c @@ -21,9 +21,9 @@ static char* default_peer_proto = NULL; */ uint64_t backend_defaultpeer_init(){ ws_peer_info startup_peer = { - .transport = peer_tcp_client, - .host = strdup("localhost"), - .port = strdup("5900") + .transport = peer_transport_detect, + .host = (default_peer.host) ? default_peer.host : strdup("tcp://localhost"), + .port = (default_peer.port) ? default_peer.port : strdup("5900") }; default_peer = startup_peer; @@ -35,9 +35,9 @@ uint64_t backend_defaultpeer_init(){ */ uint64_t backend_defaultpeer_configure(char* key, char* value){ if(!strcmp(key, "host")){ - //TODO extract peer protocol free(default_peer.host); default_peer.host = strdup(value); + default_peer.transport = peer_transport_detect; } else if(!strcmp(key, "port")){ free(default_peer.port); diff --git a/websocksy.c b/websocksy.c index aa0cf77..55a6d6a 100644 --- a/websocksy.c +++ b/websocksy.c @@ -15,12 +15,11 @@ #include "network.h" #include "websocket.h" +#define DEFAULT_HOST "::" +#define DEFAULT_PORT "8001" + /* TODO * - TLS - * - config parsing - * - backend config - * - framing config / per peer? - * - per-connection framing state * - framing function discovery / registry */ @@ -46,8 +45,8 @@ static struct { char* port; ws_backend backend; } config = { - .host = "::", - .port = "8001", + .host = DEFAULT_HOST, + .port = DEFAULT_PORT, /* Assign the built-in defaultpeer backend by default */ .backend.init = backend_defaultpeer_init, .backend.config = backend_defaultpeer_configure, @@ -93,10 +92,34 @@ void client_cleanup(){ socks = 0; } +static peer_transport client_detect_transport(char* host){ + if(!strncmp(host, "tcp://", 6)){ + memmove(host, host + 6, strlen(host) - 5); + return peer_tcp_client; + } + else if(!strncmp(host, "udp://", 6)){ + memmove(host, host + 6, strlen(host) - 5); + return peer_udp_client; + } + else if(!strncmp(host, "fifotx://", 9)){ + memmove(host, host + 9, strlen(host) - 8); + return peer_fifo_tx; + } + else if(!strncmp(host, "fiforx://", 9)){ + memmove(host, host + 9, strlen(host) - 8); + return peer_fifo_rx; + } + else if(!strncmp(host, "unix://", 8)){ + memmove(host, host + 8, strlen(host) - 7); + return peer_unix; + } + + fprintf(stderr, "Peer address %s does not include any known protocol identifier, guessing tcp_client\n", host); + return peer_tcp_client; +} + /* Establish peer connection for negotiated websocket */ int client_connect(websocket* ws){ - int rv = 1; - ws->peer = config.backend.query(ws->request_path, ws->protocols, ws->protocol, ws->headers, ws->header, ws); if(!ws->peer.host || !ws->peer.port){ //no peer provided @@ -108,6 +131,11 @@ int client_connect(websocket* ws){ ws->peer.framing = framing_auto; } + //if required scan the hostname for a protocol + if(ws->peer.transport == peer_transport_detect){ + ws->peer.transport = client_detect_transport(ws->peer.host); + } + //TODO connection establishment should be async in the future switch(ws->peer.transport){ case peer_tcp_client: @@ -119,44 +147,82 @@ int client_connect(websocket* ws){ case peer_tcp_server: //TODO implement tcp server mode fprintf(stderr, "TCP Server mode not yet implemented\n"); - rv = 1; - break; + return 1; case peer_udp_server: ws->peer_fd = network_socket(ws->peer.host, ws->peer.port, SOCK_DGRAM, 1); break; case peer_fifo_tx: case peer_fifo_rx: case peer_unix: - default: //TODO implement other peer modes fprintf(stderr, "Peer connection mode not yet implemented\n"); - rv = 1; - break; + return 1; + default: + fprintf(stderr, "Invalid peer transport selected\n"); + return 1; } - rv = (ws->peer_fd == -1) ? 1 : 0; - return rv; + return (ws->peer_fd == -1) ? 1 : 0; } -/* - * Signal handler, attached to SIGINT - */ +/* Signal handler, attached to SIGINT */ static void signal_handler(int signum){ shutdown_requested = 1; } -/* - * Usage info - */ +/* Usage info */ static int usage(char* fn){ fprintf(stderr, "\nwebsocksy v%s - Proxy between websockets and 'real' sockets\n", WEBSOCKSY_VERSION); fprintf(stderr, "Usage:\n"); - fprintf(stderr, "\t%s [-p ] [-l ] [-b ]\n", fn); + fprintf(stderr, "\t%s [-p ] [-l ] [-b ] [-c