msgio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "msgio.h"
  35. #include "msg.h"
  36. #define MSGIO_LOCAL_BUF_SIZE (1 << 10)
  37. ssize_t
  38. msgio_send(PRFileDesc *sock, const char *msg, size_t msg_len, size_t *start_pos)
  39. {
  40. ssize_t sent_bytes;
  41. if ((sent_bytes = PR_Send(sock, msg + *start_pos,
  42. msg_len - *start_pos, 0, PR_INTERVAL_NO_TIMEOUT)) != -1) {
  43. *start_pos += sent_bytes;
  44. }
  45. return (sent_bytes);
  46. }
  47. ssize_t
  48. msgio_send_blocking(PRFileDesc *sock, const char *msg, size_t msg_len)
  49. {
  50. PRPollDesc pfd;
  51. size_t already_sent_bytes;
  52. PRInt32 res;
  53. ssize_t ret;
  54. already_sent_bytes = 0;
  55. ret = 0;
  56. while (ret != -1 && already_sent_bytes < msg_len) {
  57. pfd.fd = sock;
  58. pfd.in_flags = PR_POLL_WRITE;
  59. pfd.out_flags = 0;
  60. if ((res = PR_Poll(&pfd, 1, PR_INTERVAL_NO_TIMEOUT)) > 0) {
  61. if (pfd.out_flags & PR_POLL_WRITE) {
  62. if ((msgio_send(sock, msg, msg_len, &already_sent_bytes) == -1) &&
  63. PR_GetError() != PR_WOULD_BLOCK_ERROR) {
  64. ret = -1;
  65. } else {
  66. ret = already_sent_bytes;
  67. }
  68. } else if (pfd.out_flags & (PR_POLL_ERR | PR_POLL_NVAL | PR_POLL_HUP)) {
  69. PR_SetError(PR_IO_ERROR, 0);
  70. ret = -1;
  71. }
  72. } else {
  73. ret = -1;
  74. }
  75. }
  76. return (ret);
  77. }
  78. /*
  79. * -1 = send returned 0,
  80. * -2 = unhandled error.
  81. * 0 = success but whole buffer is still not sent
  82. * 1 = all data was sent
  83. */
  84. int
  85. msgio_write(PRFileDesc *sock, const struct dynar *msg, size_t *already_sent_bytes)
  86. {
  87. PRInt32 sent;
  88. PRInt32 to_send;
  89. to_send = dynar_size(msg) - *already_sent_bytes;
  90. if (to_send > MSGIO_LOCAL_BUF_SIZE) {
  91. to_send = MSGIO_LOCAL_BUF_SIZE;
  92. }
  93. sent = PR_Send(sock, dynar_data(msg) + *already_sent_bytes, to_send, 0,
  94. PR_INTERVAL_NO_TIMEOUT);
  95. if (sent > 0) {
  96. *already_sent_bytes += sent;
  97. if (*already_sent_bytes == dynar_size(msg)) {
  98. /*
  99. * All data sent
  100. */
  101. return (1);
  102. }
  103. }
  104. if (sent == 0) {
  105. return (-1);
  106. }
  107. if (sent < 0 && PR_GetError() != PR_WOULD_BLOCK_ERROR) {
  108. return (-2);
  109. }
  110. return (0);
  111. }
  112. /*
  113. * 1 Full message received
  114. * 0 Partial read (no error)
  115. * -1 End of connection
  116. * -2 Unhandled error
  117. * -3 Fatal error. Unable to store message header
  118. * -4 Unable to store message
  119. * -5 Invalid msg type
  120. * -6 Msg too long
  121. */
  122. int
  123. msgio_read(PRFileDesc *sock, struct dynar *msg, size_t *already_received_bytes, int *skipping_msg)
  124. {
  125. char local_read_buffer[MSGIO_LOCAL_BUF_SIZE];
  126. PRInt32 readed;
  127. PRInt32 to_read;
  128. int ret;
  129. ret = 0;
  130. if (*already_received_bytes < msg_get_header_length()) {
  131. /*
  132. * Complete reading of header
  133. */
  134. to_read = msg_get_header_length() - *already_received_bytes;
  135. } else {
  136. /*
  137. * Read rest of message (or at least as much as possible)
  138. */
  139. to_read = (msg_get_header_length() + msg_get_len(msg)) - *already_received_bytes;
  140. }
  141. if (to_read > MSGIO_LOCAL_BUF_SIZE) {
  142. to_read = MSGIO_LOCAL_BUF_SIZE;
  143. }
  144. readed = PR_Recv(sock, local_read_buffer, to_read, 0, PR_INTERVAL_NO_TIMEOUT);
  145. if (readed > 0) {
  146. *already_received_bytes += readed;
  147. if (!*skipping_msg) {
  148. if (dynar_cat(msg, local_read_buffer, readed) == -1) {
  149. *skipping_msg = 1;
  150. ret = -4;
  151. }
  152. }
  153. if (*skipping_msg && *already_received_bytes < msg_get_header_length()) {
  154. /*
  155. * Fatal error. We were unable to store even message header
  156. */
  157. return (-3);
  158. }
  159. if (!*skipping_msg && *already_received_bytes == msg_get_header_length()) {
  160. /*
  161. * Full header received. Check type, maximum size, ...
  162. */
  163. if (!msg_is_valid_msg_type(msg)) {
  164. *skipping_msg = 1;
  165. ret = -5;
  166. } else if ((msg_get_header_length() + msg_get_len(msg)) >
  167. dynar_max_size(msg)) {
  168. *skipping_msg = 1;
  169. ret = -6;
  170. }
  171. }
  172. if (*already_received_bytes >= msg_get_header_length() &&
  173. *already_received_bytes == (msg_get_header_length() + msg_get_len(msg))) {
  174. /*
  175. * Full message skipped or received
  176. */
  177. ret = 1;
  178. }
  179. }
  180. if (readed == 0) {
  181. return (-1);
  182. }
  183. if (readed < 0 && PR_GetError() != PR_WOULD_BLOCK_ERROR) {
  184. return (-2);
  185. }
  186. return (ret);
  187. }