GUACAMOLE-269: Changes to initializing opcode array.

This commit is contained in:
Nick Couchman 2018-04-02 09:10:11 -04:00
parent c898f35959
commit ea946f2492
3 changed files with 42 additions and 33 deletions

View File

@ -192,7 +192,6 @@ void* ssh_client_thread(void* data) {
return NULL; return NULL;
} }
/* Initialize a ttymode array */
const int num_tty_opcodes = 1; const int num_tty_opcodes = 1;
char ssh_ttymodes[(GUAC_SSH_TTY_OPCODE_SIZE * num_tty_opcodes) + 1]; char ssh_ttymodes[(GUAC_SSH_TTY_OPCODE_SIZE * num_tty_opcodes) + 1];
@ -302,13 +301,15 @@ void* ssh_client_thread(void* data) {
} }
/* Set up the ttymode array prior to requesting the PTY */ /* Set up the ttymode array prior to requesting the PTY */
if (guac_ssh_ttymodes_init(ssh_ttymodes, sizeof(ssh_ttymodes), int ttymodeBytes = guac_ssh_ttymodes_init(ssh_ttymodes, sizeof(ssh_ttymodes),
num_tty_opcodes, (guac_ssh_ttymode){ GUAC_SSH_TTY_OP_VERASE, settings->backspace})) GUAC_SSH_TTY_OP_VERASE, settings->backspace, GUAC_SSH_TTY_OP_END);
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Error configuring TTY mode encoding."); if (ttymodeBytes < 1)
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Error storing TTY mode encoding \
opcodes and values in array.");
/* Request PTY */ /* Request PTY */
if (libssh2_channel_request_pty_ex(ssh_client->term_channel, "linux", sizeof("linux")-1, if (libssh2_channel_request_pty_ex(ssh_client->term_channel, "linux", sizeof("linux")-1,
ssh_ttymodes, sizeof(ssh_ttymodes), ssh_client->term->term_width, ssh_ttymodes, ttymodeBytes, ssh_client->term->term_width,
ssh_client->term->term_height, 0, 0)) { ssh_client->term->term_height, 0, 0)) {
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "Unable to allocate PTY."); guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "Unable to allocate PTY.");
return NULL; return NULL;

View File

@ -21,38 +21,49 @@
#include "ttymode.h" #include "ttymode.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
int guac_ssh_ttymodes_init(char opcode_array[], const int array_size, int guac_ssh_ttymodes_init(char opcode_array[], const int array_size,
const int num_opcodes, ...) { ...) {
/* Initialize the variable argument list. */ /* Initialize the variable argument list. */
va_list args; va_list args;
va_start(args, num_opcodes); va_start(args, array_size);
/* Check to make sure the array has enough space.
We need one extra byte at the end for the ending opcode. */
if ((num_opcodes * GUAC_SSH_TTY_OPCODE_SIZE) >= (array_size))
return 1;
/* Initialize array pointer and byte counter. */
char *current = opcode_array; char *current = opcode_array;
for (int i = 0; i < num_opcodes; i++) { int bytes = 0;
/* Get the next argument to this function */ /* Loop through variable argument list. */
guac_ssh_ttymode* ttymode = va_arg(args, guac_ssh_ttymode*); while (true) {
/* Check to make sure we don't overrun array. */
if (bytes >= array_size)
return -1;
/* Place opcode and value in array */ /* Next argument should be an opcode. */
*(current++) = ttymode->opcode; char opcode = (char)va_arg(args, int);
*(current++) = (ttymode->value >> 24) & 0xFF; *(current++) = opcode;
*(current++) = (ttymode->value >> 16) & 0xFF; bytes += sizeof(char);
*(current++) = (ttymode->value >> 8) & 0xFF;
*(current++) = ttymode->value & 0xFF; /* If it's the end opcode, we're done. */
if (opcode == GUAC_SSH_TTY_OP_END)
break;
/* Next argument should be 4-byte value. */
uint32_t value = va_arg(args, uint32_t);
*(current++) = (value >> 24) & 0xFF;
*(current++) = (value >> 16) & 0xFF;
*(current++) = (value >> 8) & 0xFF;
*(current++) = value & 0xFF;
bytes += sizeof(uint32_t);
} }
/* Put the end opcode in the last opcode space */ /* We're done processing arguments. */
*(current) = GUAC_SSH_TTY_OP_END; va_end(args);
return 0; return bytes;
} }

View File

@ -76,9 +76,9 @@ typedef struct guac_ssh_ttymode {
* in a single array, beginning with the opcode and followed * in a single array, beginning with the opcode and followed
* by a four byte value, repeating until the end opcode is * by a four byte value, repeating until the end opcode is
* encountered. This function takes the array, the array * encountered. This function takes the array, the array
* size, expected number of opcodes, and that number of * size, and a variable number of opcode and value pair
* guac_ssh_ttymode arguments and puts them in the array * arguments and puts them in the array expected by the
* exepcted by the SSH connection. * SSH connection.
* *
* @param opcode_array * @param opcode_array
* Pointer to the opcode array that will ultimately * Pointer to the opcode array that will ultimately
@ -87,18 +87,15 @@ typedef struct guac_ssh_ttymode {
* @param array_size * @param array_size
* Size, in bytes, of the array. * Size, in bytes, of the array.
* *
* @param num_opcodes
* Number of opcodes to expect.
*
* @params ... * @params ...
* A variable number of guac_ssh_ttymodes * A variable number of opcode and value pairs
* to place in the array. * to place in the array.
* *
* @return * @return
* Zero of the function is successful, non-zero * Number of bytes written to the array, or zero
* if a failure occurs. * if a failure occurs.
*/ */
int guac_ssh_ttymodes_init(char opcode_array[], const int array_size, int guac_ssh_ttymodes_init(char opcode_array[], const int array_size,
const int num_opcodes, ...); ...);
#endif #endif