4
0

msgio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /*
  38. * -1 = send returned 0,
  39. * -2 = unhandled error.
  40. * 0 = success but whole buffer is still not sent
  41. * 1 = all data was sent
  42. */
  43. int
  44. msgio_write(PRFileDesc *sock, const struct dynar *msg, size_t *already_sent_bytes)
  45. {
  46. PRInt32 sent;
  47. PRInt32 to_send_i32;
  48. size_t to_send;
  49. to_send = dynar_size(msg) - *already_sent_bytes;
  50. if (to_send > MSGIO_LOCAL_BUF_SIZE) {
  51. to_send_i32 = MSGIO_LOCAL_BUF_SIZE;
  52. } else {
  53. to_send_i32 = (PRInt32)to_send;
  54. }
  55. sent = PR_Send(sock, dynar_data(msg) + *already_sent_bytes, to_send_i32, 0,
  56. PR_INTERVAL_NO_TIMEOUT);
  57. if (sent > 0) {
  58. *already_sent_bytes += (size_t)sent;
  59. if (*already_sent_bytes == dynar_size(msg)) {
  60. /*
  61. * All data sent
  62. */
  63. return (1);
  64. }
  65. }
  66. if (sent == 0) {
  67. return (-1);
  68. }
  69. if (sent < 0 && PR_GetError() != PR_WOULD_BLOCK_ERROR) {
  70. return (-2);
  71. }
  72. return (0);
  73. }
  74. /*
  75. * 1 Full message received
  76. * 0 Partial read (no error)
  77. * -1 End of connection
  78. * -2 Unhandled error
  79. * -3 Fatal error. Unable to store message header
  80. * -4 Unable to store message
  81. * -5 Invalid msg type
  82. * -6 Msg too long
  83. */
  84. int
  85. msgio_read(PRFileDesc *sock, struct dynar *msg, size_t *already_received_bytes, int *skipping_msg)
  86. {
  87. char local_read_buffer[MSGIO_LOCAL_BUF_SIZE];
  88. PRInt32 readed;
  89. size_t to_read;
  90. PRInt32 to_read_i32;
  91. int ret;
  92. ret = 0;
  93. if (*already_received_bytes < msg_get_header_length()) {
  94. /*
  95. * Complete reading of header
  96. */
  97. to_read = msg_get_header_length() - *already_received_bytes;
  98. } else {
  99. /*
  100. * Read rest of message (or at least as much as possible)
  101. */
  102. to_read = (msg_get_header_length() + msg_get_len(msg)) - *already_received_bytes;
  103. }
  104. if (to_read > MSGIO_LOCAL_BUF_SIZE) {
  105. to_read_i32 = MSGIO_LOCAL_BUF_SIZE;
  106. } else {
  107. to_read_i32 = (PRInt32)to_read;
  108. }
  109. readed = PR_Recv(sock, local_read_buffer, to_read_i32, 0, PR_INTERVAL_NO_TIMEOUT);
  110. if (readed > 0) {
  111. *already_received_bytes += (size_t)readed;
  112. if (!*skipping_msg) {
  113. if (dynar_cat(msg, local_read_buffer, readed) == -1) {
  114. *skipping_msg = 1;
  115. ret = -4;
  116. }
  117. }
  118. if (*skipping_msg && *already_received_bytes < msg_get_header_length()) {
  119. /*
  120. * Fatal error. We were unable to store even message header
  121. */
  122. return (-3);
  123. }
  124. if (!*skipping_msg && *already_received_bytes == msg_get_header_length()) {
  125. /*
  126. * Full header received. Check type, maximum size, ...
  127. */
  128. if (!msg_is_valid_msg_type(msg)) {
  129. *skipping_msg = 1;
  130. ret = -5;
  131. } else if ((msg_get_header_length() + msg_get_len(msg)) >
  132. dynar_max_size(msg)) {
  133. *skipping_msg = 1;
  134. ret = -6;
  135. }
  136. }
  137. if (*already_received_bytes >= msg_get_header_length() &&
  138. *already_received_bytes == (msg_get_header_length() + msg_get_len(msg))) {
  139. /*
  140. * Full message skipped or received
  141. */
  142. ret = 1;
  143. }
  144. }
  145. if (readed == 0) {
  146. return (-1);
  147. }
  148. if (readed < 0 && PR_GetError() != PR_WOULD_BLOCK_ERROR) {
  149. return (-2);
  150. }
  151. return (ret);
  152. }