/usr/include/guacamole/socket.h is in libguac-dev 0.8.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | /* ***** 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 ***** */
#ifndef _GUAC_SOCKET_H
#define _GUAC_SOCKET_H
#include <pthread.h>
#include <stdint.h>
#include <unistd.h>
/**
* Defines the guac_socket object and functionss for using and manipulating it.
*
* @file socket.h
*/
/**
* The number of bytes to buffer within each socket before flushing.
*/
#define GUAC_SOCKET_OUTPUT_BUFFER_SIZE 8192
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,
const void* buf, size_t count);
/**
* 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);
/**
* The core I/O object of Guacamole. guac_socket provides buffered input and
* output as well as convenience methods for efficiently writing base64 data.
*/
struct guac_socket {
/**
* Arbitrary socket-specific data.
*/
void* data;
/**
* Handler which will be called when data needs to be read from the socket.
*/
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;
/**
* 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;
/**
* The number of bytes present in the base64 "ready" buffer.
*/
int __ready;
/**
* The base64 "ready" buffer. Once this buffer is filled, base64 data is
* flushed to the main write buffer.
*/
int __ready_buf[3];
/**
* The number of bytes currently in the main write buffer.
*/
int __written;
/**
* The main write buffer. Bytes written go here before being flushed
* to the open file descriptor.
*/
char __out_buf[GUAC_SOCKET_OUTPUT_BUFFER_SIZE];
/**
* The current location of parsing within the instruction buffer.
*/
int __instructionbuf_parse_start;
/**
* The current size of the instruction buffer.
*/
int __instructionbuf_size;
/**
* The number of bytes currently in the instruction buffer.
*/
int __instructionbuf_used_length;
/**
* 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;
/**
* 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];
/**
* Whether instructions should be guaranteed atomic across threads using
* locks. By default, thread safety is disabled on sockets.
*/
int __threadsafe_instructions;
/**
* Lock which is acquired when an instruction is being written, and
* released when the instruction is finished being written.
*/
pthread_mutex_t __instruction_write_lock;
/**
* Lock which is acquired when the buffer is being modified or flushed.
*/
pthread_mutex_t __buffer_lock;
};
/**
* 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);
/**
* Declares that the given socket must behave in a threadsafe way. Calling
* this function on a socket guarantees that the socket will send instructions
* atomically. Without automatic threadsafe sockets, multiple threads writing
* to the same socket must ensure that instructions will not potentially
* overlap.
*
* @param socket The guac_socket to declare as threadsafe.
*/
void guac_socket_require_threadsafe(guac_socket* socket);
/**
* Marks the beginning of a Guacamole protocol instruction. If threadsafety
* is enabled on the socket, other instructions will be blocked from sending
* until this instruction is complete.
*
* @param socket The guac_socket beginning an instruction.
*/
void guac_socket_instruction_begin(guac_socket* socket);
/**
* Marks the end of a Guacamole protocol instruction. If threadsafety
* is enabled on the socket, other instructions will be allowed to send.
*
* @param socket The guac_socket ending an instruction.
*/
void guac_socket_instruction_end(guac_socket* socket);
/**
* Marks the beginning of a socket's buffer modification. If threadsafety is
* enabled on the socket, other functions which modify the buffer will be
* blocked until this modification is complete.
*
* @param socket The guac_socket whose buffer is being updated.
*/
void guac_socket_update_buffer_begin(guac_socket* socket);
/**
* Marks the end of a socket's buffer modification. If threadsafety is enabled
* on the socket, other functions which modify the buffer will now be allowed
* to continue.
*
* @param socket The guac_socket whose buffer is done being updated.
*/
void guac_socket_update_buffer_end(guac_socket* socket);
/**
* Allocates and initializes a new guac_socket object with the given open
* file descriptor.
*
* If an error occurs while allocating the guac_socket object, NULL is returned,
* 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
* file descriptor, or NULL if an error occurs while allocating
* the guac_socket object.
*/
guac_socket* guac_socket_open(int fd);
/**
* 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);
/**
* Writes the given unsigned int to the given guac_socket object. 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 i The unsigned int to write.
* @return Zero on success, or non-zero if an error occurs while writing.
*/
ssize_t guac_socket_write_int(guac_socket* socket, int64_t i);
/**
* Writes the given string to the given guac_socket object. The data
* 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.
*
* 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 str The string to write.
* @return Zero on success, or non-zero if an error occurs while writing.
*/
ssize_t guac_socket_write_string(guac_socket* socket, const char* str);
/**
* 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
* 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
* base64 data) can be made.
*
* 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_base64(guac_socket* socket, const void* buf, size_t count);
/**
* Writes the given data to the specified socket. The data written is not
* buffered, and will be sent immediately.
*
* 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);
/**
* Flushes the base64 buffer, writing padding characters as necessary.
*
* 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 flush
* @return Zero on success, or non-zero if an error occurs during flush.
*/
ssize_t guac_socket_flush_base64(guac_socket* socket);
/**
* Flushes the write buffer.
*
* 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 flush
* @return Zero on success, or non-zero if an error occurs during flush.
*/
ssize_t guac_socket_flush(guac_socket* socket);
/**
* Waits for input to be available on the given guac_socket object until the
* specified timeout elapses.
*
* 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.
*
* @param socket The guac_socket object to wait for.
* @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.
*/
int guac_socket_select(guac_socket* socket, int usec_timeout);
#endif
|