guacamole-spice-protocol/src/libguac/socket-nest.c

138 lines
3.8 KiB
C
Raw Normal View History

/*
* Copyright (C) 2013 Glyptodon LLC
2012-10-19 21:06:39 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2012-10-19 21:06:39 +00:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2012-10-19 21:06:39 +00:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2014-01-01 22:44:28 +00:00
#include "config.h"
2012-10-19 21:06:39 +00:00
2014-01-01 22:44:28 +00:00
#include "error.h"
#include "protocol.h"
#include "socket.h"
#include "unicode.h"
#include <fcntl.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
2012-10-19 21:06:39 +00:00
#include <stdlib.h>
#include <string.h>
2014-01-01 22:44:28 +00:00
#include <sys/time.h>
#include <time.h>
2012-10-19 21:06:39 +00:00
#include <unistd.h>
#ifdef __MINGW32__
#include <winsock2.h>
#else
#include <sys/select.h>
#endif
#define GUAC_SOCKET_NEST_BUFFER_SIZE 8192
2012-12-02 03:39:22 +00:00
typedef struct __guac_socket_nest_data {
2012-10-19 21:06:39 +00:00
guac_socket* parent;
char buffer[GUAC_SOCKET_NEST_BUFFER_SIZE];
int index;
2012-12-02 03:39:22 +00:00
} __guac_socket_nest_data;
2012-10-19 21:06:39 +00:00
ssize_t __guac_socket_nest_write_handler(guac_socket* socket,
const void* buf, size_t count) {
2012-10-19 21:06:39 +00:00
2012-12-02 03:39:22 +00:00
__guac_socket_nest_data* data = (__guac_socket_nest_data*) socket->data;
2012-10-19 21:06:39 +00:00
unsigned char* source = (unsigned char*) buf;
/* Current location in destination buffer during copy */
char* current = data->buffer;
/* Number of bytes remaining in source buffer */
int remaining = count;
/* If we can't actually store that many bytes, reduce number of bytes
* expected to be written */
if (remaining > GUAC_SOCKET_NEST_BUFFER_SIZE)
remaining = GUAC_SOCKET_NEST_BUFFER_SIZE;
/* Current offset within destination buffer */
int offset;
/* Number of characters before start of next character */
int skip = 0;
/* Copy UTF-8 characters into buffer */
for (offset = 0; offset < GUAC_SOCKET_NEST_BUFFER_SIZE; offset++) {
/* Get next byte */
unsigned char c = *source;
remaining--;
/* If skipping, then skip */
if (skip > 0) skip--;
/* Otherwise, determine next skip value, and increment length */
else {
/* Determine skip value (size in bytes of rest of character) */
skip = guac_utf8_charsize(c) - 1;
/* If not enough bytes to complete character, break */
if (skip > remaining)
break;
}
/* Store byte */
*current = c;
/* Advance to next character */
source++;
current++;
}
/* Append null-terminator */
*current = 0;
/* Send nest instruction containing read UTF-8 segment */
guac_protocol_send_nest(data->parent, data->index, data->buffer);
/* Return number of bytes actually written */
return offset;
}
guac_socket* guac_socket_nest(guac_socket* parent, int index) {
/* Allocate socket and associated data */
guac_socket* socket = guac_socket_alloc();
2012-12-02 03:39:22 +00:00
__guac_socket_nest_data* data = malloc(sizeof(__guac_socket_nest_data));
2012-10-19 21:06:39 +00:00
/* Store file descriptor as socket data */
data->parent = parent;
socket->data = data;
/* Set write handler */
socket->write_handler = __guac_socket_nest_write_handler;
return socket;
}