guacamole-spice-protocol/libguac/include/socket.h

358 lines
12 KiB
C
Raw Normal View History

2010-12-08 21:14:04 +00:00
2011-02-16 02:04:36 +00:00
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is libguac.
*
* The Initial Developer of the Original Code is
* Michael Jumper.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
2010-12-08 21:14:04 +00:00
#ifndef _GUAC_SOCKET_H
#define _GUAC_SOCKET_H
2010-12-08 21:14:04 +00:00
2011-07-20 15:45:23 +00:00
#include <stdint.h>
2010-12-08 21:14:04 +00:00
#include <unistd.h>
/**
* Defines the guac_socket object and functionss for using and manipulating it.
2010-12-08 21:14:04 +00:00
*
* @file socket.h
2010-12-08 21:14:04 +00:00
*/
typedef struct guac_socket guac_socket;
/**
* Generic read handler for socket read operations, modeled after the standard
* POSIX read() function. When set within a guac_socket, a handler of this type
* will be called when data needs to be read into the socket.
*
* @param socket The guac_socket being read from.
* @param buf The arbitrary buffer we must populate with data.
* @param count The maximum number of bytes to read into the buffer.
* @return The number of bytes read, or -1 if an error occurs.
*/
typedef ssize_t guac_socket_read_handler(guac_socket* socket,
void* buf, size_t count);
/**
* Generic write handler for socket write operations, modeled after the standard
* POSIX write() function. When set within a guac_socket, a handler of this type
* will be called when data needs to be write into the socket.
*
* @param socket The guac_socket being written to.
* @param buf The arbitrary buffer containing data to be written.
* @param count The maximum number of bytes to write from the buffer.
* @return The number of bytes written, or -1 if an error occurs.
*/
typedef ssize_t guac_socket_write_handler(guac_socket* socket,
void* buf, size_t count);
2012-10-19 07:34:04 +00:00
/**
* Generic handler for socket select operations, similar to the POSIX select()
* function. When guac_socket_select() is called on a guac_socket, its
* guac_socket_select_handler will be invoked, if defined.
*
* @param socket The guac_socket being selected.
* @param usec_timeout The maximum number of microseconds to wait for data, or
* -1 to potentially wait forever.
* @return Positive on success, zero if the timeout elapsed and no data is
* available, negative on error.
*/
typedef int guac_socket_select_handler(guac_socket* socket, int usec_timeout);
/**
* Generic handler for the closing of a socket, modeled after the standard
* POSIX close() function. When set within a guac_socket, a handler of this type
* will be called when the socket is closed.
*
* @param socket The guac_socket being closed.
* @return Zero on success, or -1 if an error occurs.
*/
typedef int guac_socket_free_handler(guac_socket* socket);
2010-12-08 21:14:04 +00:00
/**
* The core I/O object of Guacamole. guac_socket provides buffered input and
* output as well as convenience methods for efficiently writing base64 data.
2010-12-08 21:14:04 +00:00
*/
struct guac_socket {
/**
* Arbitrary socket-specific data.
*/
void* data;
2010-12-08 21:14:04 +00:00
/**
* Handler which will be called when data needs to be read from the socket.
2010-12-08 21:14:04 +00:00
*/
guac_socket_read_handler* read_handler;
/**
* Handler which will be called whenever data is written to this socket.
* Note that because guac_socket automatically buffers written data, this
* handler might only get called when the socket is flushed.
*/
guac_socket_write_handler* write_handler;
2012-10-19 07:34:04 +00:00
/**
* Handler which will be called whenever guac_socket_select is invoked
* on this socket.
*/
guac_socket_select_handler* select_handler;
/**
* Handler which will be called when the socket is free'd (closed).
*/
guac_socket_free_handler* free_handler;
2010-12-08 21:14:04 +00:00
/**
* The number of bytes present in the base64 "ready" buffer.
*/
int __ready;
2010-12-08 21:14:04 +00:00
/**
* The base64 "ready" buffer. Once this buffer is filled, base64 data is
* flushed to the main write buffer.
*/
int __ready_buf[3];
2010-12-08 21:14:04 +00:00
/**
* The number of bytes currently in the main write buffer.
*/
int __written;
2011-03-12 03:35:44 +00:00
2010-12-08 21:14:04 +00:00
/**
* The main write buffer. Bytes written go here before being flushed
* to the open file descriptor.
*/
char __out_buf[8192];
2010-12-08 21:14:04 +00:00
/**
* The current location of parsing within the instruction buffer.
*/
int __instructionbuf_parse_start;
2010-12-08 21:14:04 +00:00
/**
* The current size of the instruction buffer.
*/
int __instructionbuf_size;
2010-12-08 21:14:04 +00:00
/**
* The number of bytes currently in the instruction buffer.
*/
int __instructionbuf_used_length;
2010-12-08 21:14:04 +00:00
/**
* The instruction buffer. This is essentially the input buffer,
* provided as a convenience to be used to buffer instructions until
* those instructions are complete and ready to be parsed.
*/
char* __instructionbuf;
2010-12-08 21:14:04 +00:00
/**
* The number of elements parsed so far.
*/
int __instructionbuf_elementc;
/**
* Array of pointers into the instruction buffer, where each pointer
* points to the start of the corresponding element.
*/
char* __instructionbuf_elementv[64];
};
/**
* Allocates a new, completely blank guac_socket. This guac_socket will do
* absolutely nothing when used unless its handlers are defined.
*
* @returns A newly-allocated guac_socket, or NULL if the guac_socket could
* not be allocated.
*/
guac_socket* guac_socket_alloc();
/**
* Frees the given guac_socket and all associated resources.
*
* @param socket The guac_socket to free.
*/
void guac_socket_free(guac_socket* socket);
2010-12-08 21:14:04 +00:00
/**
* Allocates and initializes a new guac_socket object with the given open
2010-12-08 21:14:04 +00:00
* file descriptor.
*
* If an error occurs while allocating the guac_socket object, NULL is returned,
2011-11-21 09:32:04 +00:00
* and guac_error is set appropriately.
*
* @param fd An open file descriptor that this guac_socket object should manage.
* @return A newly allocated guac_socket object associated with the given
2011-11-21 09:32:04 +00:00
* file descriptor, or NULL if an error occurs while allocating
* the guac_socket object.
2010-12-08 21:14:04 +00:00
*/
guac_socket* guac_socket_open(int fd);
2010-12-08 21:14:04 +00:00
/**
* Allocates and initializes a new guac_socket which writes all data via
* nest instructions to the given existing, open guac_socket.
*
* If an error occurs while allocating the guac_socket object, NULL is returned,
* and guac_error is set appropriately.
*
* @param parent The guac_socket this new guac_socket should write nest
* instructions to.
* @param index The stream index to use for the written nest instructions.
* @return A newly allocated guac_socket object associated with the given
* guac_socket and stream index, or NULL if an error occurs while
* allocating the guac_socket object.
*/
guac_socket* guac_socket_nest(guac_socket* parent, int index);
2010-12-08 21:14:04 +00:00
/**
* Writes the given unsigned int to the given guac_socket object. The data
2010-12-08 21:14:04 +00:00
* written may be buffered until the buffer is flushed automatically or
* manually.
*
2011-11-21 09:32:04 +00:00
* If an error occurs while writing, a non-zero value is returned, and
* guac_error is set appropriately.
*
2011-11-26 00:34:43 +00:00
* @param socket The guac_socket object to write to.
2010-12-08 21:14:04 +00:00
* @param i The unsigned int to write.
* @return Zero on success, or non-zero if an error occurs while writing.
*/
2011-11-26 00:34:43 +00:00
ssize_t guac_socket_write_int(guac_socket* socket, int64_t i);
2010-12-08 21:14:04 +00:00
/**
* Writes the given string to the given guac_socket object. The data
2010-12-08 21:14:04 +00:00
* written may be buffered until the buffer is flushed automatically or
* manually. Note that if the string can contain characters used
* internally by the Guacamole protocol (commas, semicolons, or
* backslashes) it will need to be escaped.
*
2011-11-21 09:32:04 +00:00
* If an error occurs while writing, a non-zero value is returned, and
* guac_error is set appropriately.
*
2011-11-26 00:34:43 +00:00
* @param socket The guac_socket object to write to.
2010-12-08 21:14:04 +00:00
* @param str The string to write.
* @return Zero on success, or non-zero if an error occurs while writing.
*/
2011-11-26 00:34:43 +00:00
ssize_t guac_socket_write_string(guac_socket* socket, const char* str);
2010-12-08 21:14:04 +00:00
/**
* Writes the given binary data to the given guac_socket object as base64-
* encoded data. The data written may be buffered until the buffer is flushed
2010-12-08 21:14:04 +00:00
* automatically or manually. Beware that because base64 data is buffered
* on top of the write buffer already used, a call to guac_socket_flush_base64()
* must be made before non-base64 writes (or writes of an independent block of
2010-12-08 21:14:04 +00:00
* base64 data) can be made.
*
2011-11-21 09:32:04 +00:00
* If an error occurs while writing, a non-zero value is returned, and
* guac_error is set appropriately.
*
2011-11-26 00:34:43 +00:00
* @param socket The guac_socket object to write to.
2010-12-08 21:14:04 +00:00
* @param buf A buffer containing the data to write.
* @param count The number of bytes to write.
* @return Zero on success, or non-zero if an error occurs while writing.
*/
2011-11-26 00:34:43 +00:00
ssize_t guac_socket_write_base64(guac_socket* socket, const void* buf, size_t count);
2010-12-08 21:14:04 +00:00
/**
* Writes the given data to the specified socket. The data written may be
* buffered until the buffer is flushed automatically or manually.
*
* If an error occurs while writing, a non-zero value is returned, and
* guac_error is set appropriately.
*
* @param socket The guac_socket object to write to.
* @param buf A buffer containing the data to write.
* @param count The number of bytes to write.
* @return Zero on success, or non-zero if an error occurs while writing.
*/
ssize_t guac_socket_write(guac_socket* socket, const void* buf, size_t count);
/**
* Attempts to read data from the socket, filling up to the specified number
* of bytes in the given buffer.
*
* If an error occurs while writing, a non-zero value is returned, and
* guac_error is set appropriately.
*
* @param socket The guac_socket to read from.
* @param buf The buffer to read bytes into.
* @param count The maximum number of bytes to read.
* @return The number of bytes read, or non-zero if an error occurs while
* reading.
*/
ssize_t guac_socket_read(guac_socket* socket, void* buf, size_t count);
2010-12-08 21:14:04 +00:00
/**
* Flushes the base64 buffer, writing padding characters as necessary.
*
2011-11-21 09:32:04 +00:00
* If an error occurs while writing, a non-zero value is returned, and
* guac_error is set appropriately.
*
2011-11-26 00:34:43 +00:00
* @param socket The guac_socket object to flush
2010-12-08 21:14:04 +00:00
* @return Zero on success, or non-zero if an error occurs during flush.
*/
2011-11-26 00:34:43 +00:00
ssize_t guac_socket_flush_base64(guac_socket* socket);
2010-12-08 21:14:04 +00:00
/**
* Flushes the write buffer.
*
2011-11-21 09:32:04 +00:00
* If an error occurs while writing, a non-zero value is returned, and
* guac_error is set appropriately.
*
2011-11-26 00:34:43 +00:00
* @param socket The guac_socket object to flush
2010-12-08 21:14:04 +00:00
* @return Zero on success, or non-zero if an error occurs during flush.
*/
2011-11-26 00:34:43 +00:00
ssize_t guac_socket_flush(guac_socket* socket);
2010-12-08 21:14:04 +00:00
/**
* Waits for input to be available on the given guac_socket object until the
2010-12-08 21:14:04 +00:00
* specified timeout elapses.
*
2011-11-24 00:25:25 +00:00
* If an error occurs while waiting, a negative value is returned, and
* guac_error is set appropriately.
*
* If a timeout occurs while waiting, zero value is returned, and
* guac_error is set to GUAC_STATUS_INPUT_TIMEOUT.
*
2011-11-26 00:34:43 +00:00
* @param socket The guac_socket object to wait for.
2010-12-08 21:14:04 +00:00
* @param usec_timeout The maximum number of microseconds to wait for data, or
* -1 to potentially wait forever.
* @return Positive on success, zero if the timeout elapsed and no data is
* available, negative on error.
*/
2011-11-26 00:34:43 +00:00
int guac_socket_select(guac_socket* socket, int usec_timeout);
2010-12-08 21:14:04 +00:00
#endif