guac_instruction* guac_instruction_expect(guac_socket* socket, int usec_timeout, const char* opcode) { guac_instruction* instruction; /* Wait for data until timeout */ if (guac_instruction_waiting(socket, usec_timeout) <= 0) return NULL; /* Read available instruction */ instruction = guac_instruction_read(socket, usec_timeout); if (instruction == NULL) return NULL; /* Validate instruction */ if (strcmp(instruction->opcode, opcode) != 0) { guac_error = GUAC_STATUS_BAD_STATE; guac_error_message = "Instruction read did not have expected opcode"; guac_instruction_free(instruction); return NULL; } /* Return instruction if valid */ return instruction; }
void* __guacd_client_input_thread(void* data) { guac_client* client = (guac_client*) data; guac_socket* socket = client->socket; /* Guacamole client input loop */ while (client->state == GUAC_CLIENT_RUNNING) { /* Read instruction */ guac_instruction* instruction = guac_instruction_read(socket, GUACD_USEC_TIMEOUT); /* Stop on error */ if (instruction == NULL) { if (guac_error == GUAC_STATUS_INPUT_TIMEOUT) guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_TIMEOUT, "Client is not responding."); else { guacd_client_log_guac_error(client, "Error reading instruction"); guac_client_stop(client); } return NULL; } /* Reset guac_error and guac_error_message (client handlers are not * guaranteed to set these) */ guac_error = GUAC_STATUS_SUCCESS; guac_error_message = NULL; /* Call handler, stop on error */ if (guac_client_handle_instruction(client, instruction) < 0) { /* Log error */ guacd_client_log_guac_error(client, "Client instruction handler error"); /* Log handler details */ guac_client_log(client, GUAC_LOG_INFO, "Failing instruction handler in client was \"%s\"", instruction->opcode); guac_instruction_free(instruction); guac_client_stop(client); return NULL; } /* Free allocated instruction */ guac_instruction_free(instruction); } return NULL; }
void test_instruction_read() { int rfd, wfd; int fd[2], childpid; char test_string[] = "4.test,6.a" UTF8_4 "b," "5.12345,10.a" UTF8_8 "c;" "5.test2,10.hellohello,15.worldworldworld;"; /* Create pipe */ CU_ASSERT_EQUAL_FATAL(pipe(fd), 0); /* File descriptors */ rfd = fd[0]; wfd = fd[1]; /* Fork */ if ((childpid = fork()) == -1) { /* ERROR */ perror("fork"); return; } /* Child (pipe writer) */ if (childpid != 0) { close(rfd); CU_ASSERT_EQUAL( write(wfd, test_string, sizeof(test_string)), sizeof(test_string) ); exit(0); } /* Parent (unit test) */ else { guac_socket* socket; guac_instruction* instruction; close(wfd); /* Open guac socket */ socket = guac_socket_open(rfd); CU_ASSERT_PTR_NOT_NULL_FATAL(socket); /* Read instruction */ instruction = guac_instruction_read(socket, 1000000); CU_ASSERT_PTR_NOT_NULL_FATAL(instruction); /* Validate contents */ CU_ASSERT_STRING_EQUAL(instruction->opcode, "test"); CU_ASSERT_EQUAL_FATAL(instruction->argc, 3); CU_ASSERT_STRING_EQUAL(instruction->argv[0], "a" UTF8_4 "b"); CU_ASSERT_STRING_EQUAL(instruction->argv[1], "12345"); CU_ASSERT_STRING_EQUAL(instruction->argv[2], "a" UTF8_8 "c"); /* Read another instruction */ guac_instruction_free(instruction); instruction = guac_instruction_read(socket, 1000000); /* Validate contents */ CU_ASSERT_STRING_EQUAL(instruction->opcode, "test2"); CU_ASSERT_EQUAL_FATAL(instruction->argc, 2); CU_ASSERT_STRING_EQUAL(instruction->argv[0], "hellohello"); CU_ASSERT_STRING_EQUAL(instruction->argv[1], "worldworldworld"); guac_instruction_free(instruction); guac_socket_free(socket); } }