qdevice-net-socket.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "msg.h"
  35. #include "msgio.h"
  36. #include "qnet-config.h"
  37. #include "qdevice-log.h"
  38. #include "qdevice-net-msg-received.h"
  39. #include "qdevice-net-nss.h"
  40. #include "qdevice-net-send.h"
  41. #include "qdevice-net-socket.h"
  42. /*
  43. * -1 means end of connection (EOF) or some other unhandled error. 0 = success
  44. */
  45. int
  46. qdevice_net_socket_read(struct qdevice_net_instance *instance)
  47. {
  48. int res;
  49. int ret_val;
  50. int orig_skipping_msg;
  51. orig_skipping_msg = instance->skipping_msg;
  52. res = msgio_read(instance->socket, &instance->receive_buffer,
  53. &instance->msg_already_received_bytes, &instance->skipping_msg);
  54. if (!orig_skipping_msg && instance->skipping_msg) {
  55. qdevice_log(LOG_DEBUG, "msgio_read set skipping_msg");
  56. }
  57. ret_val = 0;
  58. switch (res) {
  59. case 0:
  60. /*
  61. * Partial read
  62. */
  63. break;
  64. case -1:
  65. qdevice_log(LOG_DEBUG, "Server closed connection");
  66. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_SERVER_CLOSED_CONNECTION;
  67. ret_val = -1;
  68. break;
  69. case -2:
  70. qdevice_log(LOG_ERR, "Unhandled error when reading from server. "
  71. "Disconnecting from server");
  72. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_READ_MESSAGE;
  73. ret_val = -1;
  74. break;
  75. case -3:
  76. qdevice_log(LOG_ERR, "Can't store message header from server. "
  77. "Disconnecting from server");
  78. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_READ_MESSAGE;
  79. ret_val = -1;
  80. break;
  81. case -4:
  82. qdevice_log(LOG_ERR, "Can't store message from server. "
  83. "Disconnecting from server");
  84. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_READ_MESSAGE;
  85. ret_val = -1;
  86. break;
  87. case -5:
  88. qdevice_log(LOG_WARNING, "Server sent unsupported msg type %u. "
  89. "Disconnecting from server", msg_get_type(&instance->receive_buffer));
  90. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNSUPPORTED_MSG;
  91. ret_val = -1;
  92. break;
  93. case -6:
  94. qdevice_log(LOG_WARNING,
  95. "Server wants to send too long message %u bytes. Disconnecting from server",
  96. msg_get_len(&instance->receive_buffer));
  97. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_READ_MESSAGE;
  98. ret_val = -1;
  99. break;
  100. case 1:
  101. /*
  102. * Full message received / skipped
  103. */
  104. if (!instance->skipping_msg) {
  105. if (qdevice_net_msg_received(instance) == -1) {
  106. ret_val = -1;
  107. }
  108. } else {
  109. qdevice_log(LOG_CRIT, "net_socket_read in skipping msg state");
  110. exit(1);
  111. }
  112. instance->skipping_msg = 0;
  113. instance->msg_already_received_bytes = 0;
  114. dynar_clean(&instance->receive_buffer);
  115. break;
  116. default:
  117. qdevice_log(LOG_CRIT, "qdevice_net_socket_read unhandled error %d", res);
  118. exit(1);
  119. break;
  120. }
  121. return (ret_val);
  122. }
  123. static int
  124. qdevice_net_socket_write_finished(struct qdevice_net_instance *instance)
  125. {
  126. PRFileDesc *new_pr_fd;
  127. if (instance->state == QDEVICE_NET_INSTANCE_STATE_WAITING_STARTTLS_BEING_SENT) {
  128. /*
  129. * StartTLS sent to server. Begin with TLS handshake
  130. */
  131. if ((new_pr_fd = nss_sock_start_ssl_as_client(instance->socket,
  132. QDEVICE_NET_NSS_SERVER_CN,
  133. qdevice_net_nss_bad_cert_hook,
  134. qdevice_net_nss_get_client_auth_data,
  135. instance, 0, NULL)) == NULL) {
  136. qdevice_log_nss(LOG_ERR, "Can't start TLS");
  137. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_START_TLS;
  138. return (-1);
  139. }
  140. /*
  141. * And send init msg
  142. */
  143. if (qdevice_net_send_init(instance) != 0) {
  144. instance->disconnect_reason =
  145. QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  146. return (-1);
  147. }
  148. instance->socket = new_pr_fd;
  149. instance->using_tls = 1;
  150. }
  151. return (0);
  152. }
  153. int
  154. qdevice_net_socket_write(struct qdevice_net_instance *instance)
  155. {
  156. int res;
  157. struct send_buffer_list_entry *send_buffer;
  158. enum msg_type sent_msg_type;
  159. send_buffer = send_buffer_list_get_active(&instance->send_buffer_list);
  160. if (send_buffer == NULL) {
  161. qdevice_log(LOG_CRIT, "send_buffer_list_get_active returned NULL");
  162. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SEND_MESSAGE;
  163. return (-1);
  164. }
  165. res = msgio_write(instance->socket, &send_buffer->buffer,
  166. &send_buffer->msg_already_sent_bytes);
  167. if (res == 1) {
  168. sent_msg_type = msg_get_type(&send_buffer->buffer);
  169. send_buffer_list_delete(&instance->send_buffer_list, send_buffer);
  170. if (sent_msg_type != MSG_TYPE_ECHO_REQUEST) {
  171. if (qdevice_net_socket_write_finished(instance) == -1) {
  172. return (-1);
  173. }
  174. }
  175. }
  176. if (res == -1) {
  177. qdevice_log_nss(LOG_CRIT, "PR_Send returned 0");
  178. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_SERVER_CLOSED_CONNECTION;
  179. return (-1);
  180. }
  181. if (res == -2) {
  182. qdevice_log_nss(LOG_ERR, "Unhandled error when sending message to server");
  183. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SEND_MESSAGE;
  184. return (-1);
  185. }
  186. return (0);
  187. }