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