msgio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2015 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 means send returned 0, -2 unhandled error. 0 = success but whole buffer is still not sent, 1 = all data was sent
  80. */
  81. int
  82. msgio_write(PRFileDesc *sock, const struct dynar *msg, size_t *already_sent_bytes)
  83. {
  84. PRInt32 sent;
  85. PRInt32 to_send;
  86. to_send = dynar_size(msg) - *already_sent_bytes;
  87. if (to_send > MSGIO_LOCAL_BUF_SIZE) {
  88. to_send = MSGIO_LOCAL_BUF_SIZE;
  89. }
  90. sent = PR_Send(sock, dynar_data(msg) + *already_sent_bytes, to_send, 0, PR_INTERVAL_NO_TIMEOUT);
  91. if (sent > 0) {
  92. *already_sent_bytes += sent;
  93. if (*already_sent_bytes == dynar_size(msg)) {
  94. /*
  95. * All data sent
  96. */
  97. return (1);
  98. }
  99. }
  100. if (sent == 0) {
  101. return (-1);
  102. }
  103. if (sent < 0 && PR_GetError() != PR_WOULD_BLOCK_ERROR) {
  104. return (-2);
  105. }
  106. return (0);
  107. }
  108. /*
  109. * -1 End of connection
  110. * -2 Unhandled error
  111. * -3 Fatal error. Unable to store message header
  112. * -4 Unable to store message
  113. * -5 Invalid msg type
  114. * -6 Msg too long
  115. */
  116. int
  117. msgio_read(PRFileDesc *sock, struct dynar *msg, size_t *already_received_bytes, int *skipping_msg)
  118. {
  119. char local_read_buffer[MSGIO_LOCAL_BUF_SIZE];
  120. PRInt32 readed;
  121. PRInt32 to_read;
  122. int ret;
  123. ret = 0;
  124. if (*already_received_bytes < msg_get_header_length()) {
  125. /*
  126. * Complete reading of header
  127. */
  128. to_read = msg_get_header_length() - *already_received_bytes;
  129. } else {
  130. /*
  131. * Read rest of message (or at least as much as possible)
  132. */
  133. to_read = (msg_get_header_length() + msg_get_len(msg)) - *already_received_bytes;
  134. }
  135. if (to_read > MSGIO_LOCAL_BUF_SIZE) {
  136. to_read = MSGIO_LOCAL_BUF_SIZE;
  137. }
  138. readed = PR_Recv(sock, local_read_buffer, to_read, 0, PR_INTERVAL_NO_TIMEOUT);
  139. if (readed > 0) {
  140. *already_received_bytes += readed;
  141. if (!*skipping_msg) {
  142. if (dynar_cat(msg, local_read_buffer, readed) == -1) {
  143. *skipping_msg = 1;
  144. ret = -4;
  145. }
  146. }
  147. if (*skipping_msg && *already_received_bytes < msg_get_header_length()) {
  148. /*
  149. * Fatal error. We were unable to store even message header
  150. */
  151. return (-3);
  152. }
  153. if (!*skipping_msg && *already_received_bytes == msg_get_header_length()) {
  154. /*
  155. * Full header received. Check type, maximum size, ...
  156. */
  157. if (!msg_is_valid_msg_type(msg)) {
  158. *skipping_msg = 1;
  159. ret = -5;
  160. } else if (msg_get_header_length() + msg_get_len(msg) > dynar_max_size(msg)) {
  161. *skipping_msg = 1;
  162. ret = -6;
  163. }
  164. }
  165. if (*already_received_bytes >= msg_get_header_length() &&
  166. *already_received_bytes == (msg_get_header_length() + msg_get_len(msg))) {
  167. /*
  168. * Full message skipped or received
  169. */
  170. ret = 1;
  171. }
  172. }
  173. if (readed == 0) {
  174. return (-1);
  175. }
  176. if (readed < 0 && PR_GetError() != PR_WOULD_BLOCK_ERROR) {
  177. return (-2);
  178. }
  179. return (ret);
  180. }