PageRenderTime 1ms CodeModel.GetById 2ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libguac/libguac/src/socket.c

https://github.com/flexiant/extility-guacamole-server
C | 320 lines | 179 code | 87 blank | 54 comment | 31 complexity | d4dc8b705b60937102dfab4746cd653a MD5 | raw file
  1
  2/* ***** BEGIN LICENSE BLOCK *****
  3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4 *
  5 * The contents of this file are subject to the Mozilla Public License Version
  6 * 1.1 (the "License"); you may not use this file except in compliance with
  7 * the License. You may obtain a copy of the License at
  8 * http://www.mozilla.org/MPL/
  9 *
 10 * Software distributed under the License is distributed on an "AS IS" basis,
 11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 12 * for the specific language governing rights and limitations under the
 13 * License.
 14 *
 15 * The Original Code is libguac.
 16 *
 17 * The Initial Developer of the Original Code is
 18 * Michael Jumper.
 19 * Portions created by the Initial Developer are Copyright (C) 2010
 20 * the Initial Developer. All Rights Reserved.
 21 *
 22 * Contributor(s):
 23 * David PHAM-VAN <d.pham-van@ulteo.com> Ulteo SAS - http://www.ulteo.com
 24 *
 25 * Alternatively, the contents of this file may be used under the terms of
 26 * either the GNU General Public License Version 2 or later (the "GPL"), or
 27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 28 * in which case the provisions of the GPL or the LGPL are applicable instead
 29 * of those above. If you wish to allow use of your version of this file only
 30 * under the terms of either the GPL or the LGPL, and not to allow others to
 31 * use your version of this file under the terms of the MPL, indicate your
 32 * decision by deleting the provisions above and replace them with the notice
 33 * and other provisions required by the GPL or the LGPL. If you do not delete
 34 * the provisions above, a recipient may use your version of this file under
 35 * the terms of any one of the MPL, the GPL or the LGPL.
 36 *
 37 * ***** END LICENSE BLOCK ***** */
 38
 39#include <stdlib.h>
 40#include <string.h>
 41#include <unistd.h>
 42#include <fcntl.h>
 43#include <stdio.h>
 44#include <stdint.h>
 45#include <inttypes.h>
 46
 47#ifdef __MINGW32__
 48#include <winsock2.h>
 49#else
 50#include <sys/select.h>
 51#endif
 52
 53#include <time.h>
 54#include <sys/time.h>
 55
 56#include "socket.h"
 57#include "error.h"
 58
 59char __guac_socket_BASE64_CHARACTERS[64] = {
 60    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
 61    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
 62    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
 63    'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', 
 64};
 65
 66guac_socket* guac_socket_open(int fd) {
 67
 68    guac_socket* socket = malloc(sizeof(guac_socket));
 69
 70    /* If no memory available, return with error */
 71    if (socket == NULL) {
 72        guac_error = GUAC_STATUS_NO_MEMORY;
 73        guac_error_message = "Could not allocate memory for socket";
 74        return NULL;
 75    }
 76
 77    socket->__ready = 0;
 78    socket->__written = 0;
 79    socket->fd = fd;
 80
 81    /* Allocate instruction buffer */
 82    socket->__instructionbuf_size = 1024;
 83    socket->__instructionbuf = malloc(socket->__instructionbuf_size);
 84
 85    /* If no memory available, return with error */
 86    if (socket->__instructionbuf == NULL) {
 87        guac_error = GUAC_STATUS_NO_MEMORY;
 88        guac_error_message = "Could not allocate memory for instruction buffer";
 89        free(socket);
 90        return NULL;
 91    }
 92
 93    /* Init members */
 94    socket->__instructionbuf_used_length = 0;
 95    socket->__instructionbuf_parse_start = 0;
 96    socket->__instructionbuf_elementc = 0;
 97
 98    return socket;
 99
100}
101
102void guac_socket_close(guac_socket* socket) {
103    guac_socket_flush(socket);
104    free(socket->__instructionbuf);
105    free(socket);
106}
107
108/* Write bytes, limit rate */
109ssize_t __guac_socket_write(guac_socket* socket, const char* buf, int count) {
110
111    int retval;
112
113#ifdef __MINGW32__
114    /* MINGW32 WINSOCK only works with send() */
115    retval = send(socket->fd, buf, count, 0);
116#else
117    /* Use write() for all other platforms */
118    retval = write(socket->fd, buf, count);
119#endif
120
121    /* Record errors in guac_error */
122    if (retval < 0) {
123        guac_error = GUAC_STATUS_SEE_ERRNO;
124        guac_error_message = "Error writing data to socket";
125    }
126
127    return retval;
128}
129
130ssize_t guac_socket_write_int(guac_socket* socket, int64_t i) {
131
132    char buffer[128];
133    snprintf(buffer, sizeof(buffer), "%"PRIi64, i);
134    return guac_socket_write_string(socket, buffer);
135
136}
137
138ssize_t guac_socket_write_string(guac_socket* socket, const char* str) {
139
140    char* __out_buf = socket->__out_buf;
141
142    int retval;
143
144    for (; *str != '\0'; str++) {
145
146        __out_buf[socket->__written++] = *str; 
147
148        /* Flush when necessary, return on error */
149        if (socket->__written > 8188 /* sizeof(__out_buf) - 4 */) {
150
151            retval = __guac_socket_write(socket, __out_buf, socket->__written);
152
153            if (retval < 0)
154                return retval;
155
156            socket->__written = 0;
157        }
158
159    }
160
161    return 0;
162
163}
164
165ssize_t __guac_socket_write_base64_triplet(guac_socket* socket, int a, int b, int c) {
166
167    char* __out_buf = socket->__out_buf;
168
169    int retval;
170
171    /* Byte 1 */
172    __out_buf[socket->__written++] = __guac_socket_BASE64_CHARACTERS[(a & 0xFC) >> 2]; /* [AAAAAA]AABBBB BBBBCC CCCCCC */
173
174    if (b >= 0) {
175        __out_buf[socket->__written++] = __guac_socket_BASE64_CHARACTERS[((a & 0x03) << 4) | ((b & 0xF0) >> 4)]; /* AAAAAA[AABBBB]BBBBCC CCCCCC */
176
177        if (c >= 0) {
178            __out_buf[socket->__written++] = __guac_socket_BASE64_CHARACTERS[((b & 0x0F) << 2) | ((c & 0xC0) >> 6)]; /* AAAAAA AABBBB[BBBBCC]CCCCCC */
179            __out_buf[socket->__written++] = __guac_socket_BASE64_CHARACTERS[c & 0x3F]; /* AAAAAA AABBBB BBBBCC[CCCCCC] */
180        }
181        else { 
182            __out_buf[socket->__written++] = __guac_socket_BASE64_CHARACTERS[((b & 0x0F) << 2)]; /* AAAAAA AABBBB[BBBB--]------ */
183            __out_buf[socket->__written++] = '='; /* AAAAAA AABBBB BBBB--[------] */
184        }
185    }
186    else {
187        __out_buf[socket->__written++] = __guac_socket_BASE64_CHARACTERS[((a & 0x03) << 4)]; /* AAAAAA[AA----]------ ------ */
188        __out_buf[socket->__written++] = '='; /* AAAAAA AA----[------]------ */
189        __out_buf[socket->__written++] = '='; /* AAAAAA AA---- ------[------] */
190    }
191
192    /* At this point, 4 bytes have been socket->__written */
193
194    /* Flush when necessary, return on error */
195    if (socket->__written > 8188 /* sizeof(__out_buf) - 4 */) {
196        retval = __guac_socket_write(socket, __out_buf, socket->__written);
197        if (retval < 0)
198            return retval;
199
200        socket->__written = 0;
201    }
202
203    if (b < 0)
204        return 1;
205
206    if (c < 0)
207        return 2;
208
209    return 3;
210
211}
212
213ssize_t __guac_socket_write_base64_byte(guac_socket* socket, int buf) {
214
215    int* __ready_buf = socket->__ready_buf;
216
217    int retval;
218
219    __ready_buf[socket->__ready++] = buf;
220
221    /* Flush triplet */
222    if (socket->__ready == 3) {
223        retval = __guac_socket_write_base64_triplet(socket, __ready_buf[0], __ready_buf[1], __ready_buf[2]);
224        if (retval < 0)
225            return retval;
226
227        socket->__ready = 0;
228    }
229
230    return 1;
231}
232
233ssize_t guac_socket_write_base64(guac_socket* socket, const void* buf, size_t count) {
234
235    int retval;
236
237    const unsigned char* char_buf = (const unsigned char*) buf;
238    const unsigned char* end = char_buf + count;
239
240    while (char_buf < end) {
241
242        retval = __guac_socket_write_base64_byte(socket, *(char_buf++));
243        if (retval < 0)
244            return retval;
245
246    }
247
248    return 0;
249
250}
251
252ssize_t guac_socket_flush(guac_socket* socket) {
253
254    int retval;
255
256    /* Flush remaining bytes in buffer */
257    if (socket->__written > 0) {
258        retval = __guac_socket_write(socket, socket->__out_buf, socket->__written);
259        if (retval < 0)
260            return retval;
261
262        socket->__written = 0;
263    }
264
265    return 0;
266
267}
268
269ssize_t guac_socket_flush_base64(guac_socket* socket) {
270
271    int retval;
272
273    /* Flush triplet to output buffer */
274    while (socket->__ready > 0) {
275        retval = __guac_socket_write_base64_byte(socket, -1);
276        if (retval < 0)
277            return retval;
278    }
279
280    return 0;
281
282}
283
284
285int guac_socket_select(guac_socket* socket, int usec_timeout) {
286
287    fd_set fds;
288    struct timeval timeout;
289    int retval;
290
291    /* No timeout if usec_timeout is negative */
292    if (usec_timeout < 0)
293        retval = select(socket->fd + 1, &fds, NULL, NULL, NULL); 
294
295    /* Handle timeout if specified */
296    else {
297        timeout.tv_sec = usec_timeout/1000000;
298        timeout.tv_usec = usec_timeout%1000000;
299
300        FD_ZERO(&fds);
301        FD_SET(socket->fd, &fds);
302
303        retval = select(socket->fd + 1, &fds, NULL, NULL, &timeout);
304    }
305
306    /* Properly set guac_error */
307    if (retval <  0) {
308        guac_error = GUAC_STATUS_SEE_ERRNO;
309        guac_error_message = "Error while waiting for data on socket";
310    }
311
312    if (retval == 0) {
313        guac_error = GUAC_STATUS_INPUT_TIMEOUT;
314        guac_error_message = "Timeout while waiting for data on socket";
315    }
316
317    return retval;
318
319}
320