qnetd-client-net.c 6.9 KB

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