qnetd-client-net.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <sys/types.h>
  35. #include "msgio.h"
  36. #include "msg.h"
  37. #include "nss-sock.h"
  38. #include "qnetd-log.h"
  39. #include "qnetd-client-net.h"
  40. #include "qnetd-client-send.h"
  41. #include "qnetd-client-msg-received.h"
  42. static int
  43. qnetd_client_net_write_finished(struct qnetd_instance *instance, struct qnetd_client *client)
  44. {
  45. /*
  46. * Callback is currently unused
  47. */
  48. return (0);
  49. }
  50. int
  51. qnetd_client_net_write(struct qnetd_instance *instance, struct qnetd_client *client)
  52. {
  53. int res;
  54. struct send_buffer_list_entry *send_buffer;
  55. send_buffer = send_buffer_list_get_active(&client->send_buffer_list);
  56. if (send_buffer == NULL) {
  57. qnetd_log_nss(LOG_CRIT, "send_buffer_list_get_active returned NULL");
  58. return (-1);
  59. }
  60. res = msgio_write(client->socket, &send_buffer->buffer,
  61. &send_buffer->msg_already_sent_bytes);
  62. if (res == 1) {
  63. send_buffer_list_delete(&client->send_buffer_list, send_buffer);
  64. if (qnetd_client_net_write_finished(instance, client) == -1) {
  65. return (-1);
  66. }
  67. }
  68. if (res == -1) {
  69. qnetd_log_nss(LOG_CRIT, "PR_Send returned 0");
  70. return (-1);
  71. }
  72. if (res == -2) {
  73. qnetd_log_nss(LOG_ERR, "Unhandled error when sending message to client");
  74. return (-1);
  75. }
  76. return (0);
  77. }
  78. /*
  79. * -1 means end of connection (EOF) or some other unhandled error. 0 = success
  80. */
  81. int
  82. qnetd_client_net_read(struct qnetd_instance *instance, struct qnetd_client *client)
  83. {
  84. int res;
  85. int ret_val;
  86. int orig_skipping_msg;
  87. orig_skipping_msg = client->skipping_msg;
  88. res = msgio_read(client->socket, &client->receive_buffer,
  89. &client->msg_already_received_bytes, &client->skipping_msg);
  90. if (!orig_skipping_msg && client->skipping_msg) {
  91. qnetd_log(LOG_DEBUG, "msgio_read set skipping_msg");
  92. }
  93. ret_val = 0;
  94. switch (res) {
  95. case 0:
  96. /*
  97. * Partial read
  98. */
  99. break;
  100. case -1:
  101. qnetd_log(LOG_DEBUG, "Client closed connection");
  102. ret_val = -1;
  103. break;
  104. case -2:
  105. qnetd_log_nss(LOG_ERR, "Unhandled error when reading from client. "
  106. "Disconnecting client");
  107. ret_val = -1;
  108. break;
  109. case -3:
  110. qnetd_log(LOG_ERR, "Can't store message header from client. Disconnecting client");
  111. ret_val = -1;
  112. break;
  113. case -4:
  114. qnetd_log(LOG_ERR, "Can't store message from client. Skipping message");
  115. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG;
  116. break;
  117. case -5:
  118. qnetd_log(LOG_WARNING, "Client sent unsupported msg type %u. Skipping message",
  119. msg_get_type(&client->receive_buffer));
  120. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE;
  121. break;
  122. case -6:
  123. qnetd_log(LOG_WARNING,
  124. "Client wants to send too long message %u bytes. Skipping message",
  125. msg_get_len(&client->receive_buffer));
  126. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_MESSAGE_TOO_LONG;
  127. break;
  128. case 1:
  129. /*
  130. * Full message received / skipped
  131. */
  132. if (!client->skipping_msg) {
  133. if (qnetd_client_msg_received(instance, client) == -1) {
  134. ret_val = -1;
  135. }
  136. } else {
  137. if (qnetd_client_send_err(client, 0, 0, client->skipping_msg_reason) != 0) {
  138. ret_val = -1;
  139. }
  140. }
  141. client->skipping_msg = 0;
  142. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_NO_ERROR;
  143. client->msg_already_received_bytes = 0;
  144. dynar_clean(&client->receive_buffer);
  145. break;
  146. default:
  147. qnetd_log(LOG_ERR, "Unhandled msgio_read error %d\n", res);
  148. exit(1);
  149. break;
  150. }
  151. return (ret_val);
  152. }
  153. int
  154. qnetd_client_net_accept(struct qnetd_instance *instance)
  155. {
  156. PRNetAddr client_addr;
  157. PRFileDesc *client_socket;
  158. struct qnetd_client *client;
  159. if ((client_socket = PR_Accept(instance->server.socket, &client_addr,
  160. PR_INTERVAL_NO_TIMEOUT)) == NULL) {
  161. qnetd_log_nss(LOG_ERR, "Can't accept connection");
  162. return (-1);
  163. }
  164. if (nss_sock_set_non_blocking(client_socket) != 0) {
  165. qnetd_log_nss(LOG_ERR, "Can't set client socket to non blocking mode");
  166. PR_Close(client_socket);
  167. return (-1);
  168. }
  169. if (instance->max_clients != 0 &&
  170. qnetd_client_list_no_clients(&instance->clients) >= instance->max_clients) {
  171. qnetd_log(LOG_ERR, "Maximum clients reached. Not accepting connection");
  172. PR_Close(client_socket);
  173. return (-1);
  174. }
  175. client = qnetd_client_list_add(&instance->clients, client_socket, &client_addr,
  176. instance->max_client_receive_size, instance->max_client_send_buffers,
  177. instance->max_client_send_size, &instance->main_timer_list);
  178. if (client == NULL) {
  179. qnetd_log(LOG_ERR, "Can't add client to list");
  180. PR_Close(client_socket);
  181. return (-2);
  182. }
  183. return (0);
  184. }