From d6a1444377408f9261e039199cb29c8c21a0545a Mon Sep 17 00:00:00 2001 From: Nicolai Dagestad Date: Tue, 6 Apr 2021 11:21:49 +0200 Subject: [PATCH] Implement reverse connections --- include/vnc.h | 1 + src/main.c | 32 ++++++++++++++++++++++++++------ src/vnc.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/include/vnc.h b/include/vnc.h index 16623e1..3d27601 100644 --- a/include/vnc.h +++ b/include/vnc.h @@ -37,6 +37,7 @@ struct vnc_client* vnc_client_create(void); void vnc_client_destroy(struct vnc_client* self); int vnc_client_connect(struct vnc_client* self, const char* address, int port); +int vnc_client_reverse_connect(struct vnc_client* self, char** address, int port); int vnc_client_set_pixel_format(struct vnc_client* self, enum wl_shm_format format); diff --git a/src/main.c b/src/main.c index 859692d..3c63899 100644 --- a/src/main.c +++ b/src/main.c @@ -524,6 +524,7 @@ Usage: wlvncc
[port]\n\ -h,--help Get help.\n\ -n,--hide-cursor Hide the client-side cursor.\n\ -q,--quality Quality level (0 - 9).\n\ + -q,--reverse-connection Listen on the port for the server to connect back to us.\n\ \n\ "); return r; @@ -537,7 +538,8 @@ int main(int argc, char* argv[]) const char* encodings = NULL; int quality = -1; int compression = -1; - static const char* shortopts = "a:q:c:e:hn"; + bool reverse_connection = false; + static const char* shortopts = "a:q:c:e:hnr"; static const struct option longopts[] = { { "app-id", required_argument, NULL, 'a' }, @@ -546,6 +548,7 @@ int main(int argc, char* argv[]) { "help", no_argument, NULL, 'h' }, { "quality", required_argument, NULL, 'q' }, { "hide-cursor", no_argument, NULL, 'n' }, + { "reverse-connection", no_argument, NULL, 'r'}, { NULL, 0, NULL, 0 } }; @@ -570,6 +573,9 @@ int main(int argc, char* argv[]) case 'n': cursor_type = POINTER_CURSOR_NONE; break; + case 'r': + reverse_connection = true; + break; case 'h': return usage(0); default: @@ -579,13 +585,21 @@ int main(int argc, char* argv[]) int n_args = argc - optind; - if (n_args < 1) + if (n_args < 1 && !reverse_connection) return usage(1); - const char* address = argv[optind]; + char* address = NULL; int port = 5900; - if (n_args >= 2) - port = atoi(argv[optind + 1]); + + if (!reverse_connection){ + address = argv[optind]; + if (n_args >= 2) + port = atoi(argv[optind + 1]); + } + else if (n_args >= 1){ + port = atoi(argv[optind]); + } + struct aml* aml = aml_new(); if (!aml) @@ -654,7 +668,13 @@ int main(int argc, char* argv[]) if (compression >= 0) vnc_client_set_compression_level(vnc, compression); - if (vnc_client_connect(vnc, address, port) < 0) { + if (reverse_connection) { + if (vnc_client_reverse_connect(vnc, &address, port) < 0){ + fprintf(stderr, "Failed to reverse connect\n"); + goto vnc_setup_failure; + } + } + else if (vnc_client_connect(vnc, address, port) < 0) { fprintf(stderr, "Failed to connect to server\n"); goto vnc_setup_failure; } diff --git a/src/vnc.c b/src/vnc.c index 5a0cb4d..7d5f4f3 100644 --- a/src/vnc.c +++ b/src/vnc.c @@ -139,6 +139,39 @@ int vnc_client_connect(struct vnc_client* self, const char* address, int port) return 0; } +int vnc_client_reverse_connect(struct vnc_client* self, char** address, int port){ + rfbClient* client = self->client; + + if (!ReverseConnectToRFBServer(client, *address, port)) + return -1; + + if (!InitialiseRFBConnection(client)) + return -1; + + client->width = client->si.framebufferWidth; + client->height = client->si.framebufferHeight; + + if (!client->MallocFrameBuffer(client)) + return -1; + + if (!SetFormatAndEncodings(client)) + return -1; + + if (client->updateRect.x < 0) { + client->updateRect.x = client->updateRect.y = 0; + client->updateRect.w = client->width; + client->updateRect.h = client->height; + } + + if (!SendFramebufferUpdateRequest(client, + client->updateRect.x, client->updateRect.y, + client->updateRect.w, client->updateRect.h, + FALSE)) + return -1; + + return 0; +} + int vnc_client_set_pixel_format(struct vnc_client* self, enum wl_shm_format format) {