diff --git a/guacamole/proxy/client.c b/guacamole/proxy/client.c index 5025a9ae9..343e2a83b 100644 --- a/guacamole/proxy/client.c +++ b/guacamole/proxy/client.c @@ -53,13 +53,13 @@ void guac_free_png_buffer(png_byte** png_buffer, int h) { } -guac_client* guac_get_client(int client_fd, void (*client_init)(guac_client* client)) { +guac_client* guac_get_client(int client_fd, void (*client_init)(guac_client* client, const char* hostname, int port), const char* hostname, int port) { guac_client* client = malloc(sizeof(guac_client)); client->io = guac_open(client_fd); - client_init(client); + client_init(client, hostname, port); return client; diff --git a/guacamole/proxy/client.h b/guacamole/proxy/client.h index 3a709071d..7a0602005 100644 --- a/guacamole/proxy/client.h +++ b/guacamole/proxy/client.h @@ -36,7 +36,7 @@ typedef struct guac_client { } guac_client; -guac_client* guac_get_client(int client_fd, void (*client_init)(guac_client* client)); +guac_client* guac_get_client(int client_fd, void (*client_init)(guac_client* client, const char* hostname, int port), const char* hostname, int port); void guac_start_client(guac_client* client); void guac_free_client(guac_client* client); png_byte** guac_alloc_png_buffer(int w, int h, int bpp); diff --git a/guacamole/proxy/daemon.c b/guacamole/proxy/daemon.c index 56c2cf1c4..55d5e8f5a 100644 --- a/guacamole/proxy/daemon.c +++ b/guacamole/proxy/daemon.c @@ -18,6 +18,7 @@ */ #include +#include #include #include #include @@ -39,14 +40,25 @@ int main(int argc, char* argv[]) { int connected_socket_fd; pid_t client_pid ; + int listen_port; + const char* connect_host; + int connect_port; + + if (argc < 4) { + fprintf(stderr, "USAGE: %s LISTENPORT CONNECTHOST CONNECTPORT\n", argv[0]); + return 1; + } + + listen_port = atoi(argv[1]); + connect_host = argv[2]; + connect_port = atoi(argv[3]); - fprintf(stderr, "Guacamole starting...\n"); /* Get binding address */ memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */ server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; - server_addr.sin_port = htons(1234); + server_addr.sin_port = htons(listen_port); /* Get socket */ socket_fd = socket(AF_INET, SOCK_STREAM, 0); @@ -62,11 +74,11 @@ int main(int argc, char* argv[]) { return 2; } + fprintf(stderr, "[guacamole] listening on port %i, forwarding to %s:%i\n", listen_port, connect_host, connect_port); + /* Daemon loop */ for (;;) { - fprintf(stderr, "Listening...\n"); - /* Listen for connections */ if (listen(socket_fd, 5) < 0) { perror("Error listening on socket"); @@ -82,15 +94,20 @@ int main(int argc, char* argv[]) { } /* Fork client */ - client_pid = fork(); + client_pid = 0; /*fork();*/ if (client_pid < 0) { perror("Could not fork child"); + return 4; } /* In child ... */ else if (client_pid == 0) { - guac_client* client = guac_get_client(connected_socket_fd, vnc_guac_client_init); + guac_client* client; + + fprintf(stderr, "[guacamole] spawning client\n"); + + client = guac_get_client(connected_socket_fd, vnc_guac_client_init, connect_host, connect_port); guac_start_client(client); guac_free_client(client); @@ -100,13 +117,10 @@ int main(int argc, char* argv[]) { return 3; } - fprintf(stderr, "Child exiting.\n"); + fprintf(stderr, "[guacamole] client finished\n"); return 0; } - else - fprintf(stderr, "Child forked.\n"); - } /* Close socket */ diff --git a/guacamole/proxy/vnc_client.c b/guacamole/proxy/vnc_client.c index f1424446f..5bcc073ca 100644 --- a/guacamole/proxy/vnc_client.c +++ b/guacamole/proxy/vnc_client.c @@ -30,9 +30,9 @@ char __guac_password[] = "potato"; -char* __GUAC_VNC_TAG_IO = "GUACIO"; -char* __GUAC_VNC_TAG_PNG_BUFFER = "PNG_BUFFER"; -char* __GUAC_VNC_TAG_PNG_BUFFER_ALPHA = "PNG_BUFFER_ALPHA"; +static char* __GUAC_VNC_TAG_IO = "GUACIO"; +static char* __GUAC_VNC_TAG_PNG_BUFFER = "PNG_BUFFER"; +static char* __GUAC_VNC_TAG_PNG_BUFFER_ALPHA = "PNG_BUFFER_ALPHA"; typedef struct vnc_guac_client_data { @@ -169,8 +169,6 @@ char* guac_vnc_get_password(rfbClient* client) { char* password = malloc(64); strncpy(password, __guac_password, 63); - fprintf(stderr, "Sending password: %s\n", password); - return password; } @@ -214,8 +212,6 @@ void vnc_guac_client_key_handler(guac_client* client, int keysym, int pressed) { } - - void vnc_guac_client_free_handler(guac_client* client) { rfbClient* rfb_client = ((vnc_guac_client_data*) client->data)->rfb_client; @@ -232,9 +228,10 @@ void vnc_guac_client_free_handler(guac_client* client) { } -void vnc_guac_client_init(guac_client* client) { +void vnc_guac_client_init(guac_client* client, const char* hostname, int port) { + + char* hostname_copy; - char* hostname; rfbClient* rfb_client; png_byte** png_buffer; @@ -257,11 +254,11 @@ void vnc_guac_client_init(guac_client* client) { rfb_client->GetPassword = guac_vnc_get_password; /* Connect */ - hostname = malloc(64); - strcpy(hostname, "localhost"); + hostname_copy = malloc(1024); + strncpy(hostname_copy, hostname, 1024); - rfb_client->serverHost = hostname; - rfb_client->serverPort = 5902; + rfb_client->serverHost = hostname_copy; + rfb_client->serverPort = port; rfbInitClient(rfb_client, NULL, NULL); @@ -274,13 +271,6 @@ void vnc_guac_client_init(guac_client* client) { rfbClientSetClientData(rfb_client, __GUAC_VNC_TAG_PNG_BUFFER, png_buffer); rfbClientSetClientData(rfb_client, __GUAC_VNC_TAG_PNG_BUFFER_ALPHA, png_buffer_alpha); - /* Send name */ - guac_send_name(client->io, rfb_client->desktopName); - - /* Send size */ - guac_send_size(client->io, rfb_client->width, rfb_client->height); - guac_flush(client->io); - /* Set client data */ vnc_guac_client_data = malloc(sizeof(vnc_guac_client_data)); vnc_guac_client_data->rfb_client = rfb_client; @@ -293,7 +283,12 @@ void vnc_guac_client_init(guac_client* client) { client->mouse_handler = vnc_guac_client_mouse_handler; client->key_handler = vnc_guac_client_key_handler; - fprintf(stderr, "VNC Client loaded.\n"); + /* Send name */ + guac_send_name(client->io, rfb_client->desktopName); + + /* Send size */ + guac_send_size(client->io, rfb_client->width, rfb_client->height); + guac_flush(client->io); } diff --git a/guacamole/proxy/vnc_client.h b/guacamole/proxy/vnc_client.h index af187b08a..e50908a68 100644 --- a/guacamole/proxy/vnc_client.h +++ b/guacamole/proxy/vnc_client.h @@ -22,7 +22,7 @@ #include "client.h" -void vnc_guac_client_init(guac_client* client); +void vnc_guac_client_init(guac_client* client, const char* hostname, int port); #endif