ktpd_session.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <sys/socket.h>
  11. #include <sys/un.h>
  12. #include <faux/str.h>
  13. #include <faux/async.h>
  14. #include <faux/msg.h>
  15. #include <klish/ktp.h>
  16. #include <klish/ktp_session.h>
  17. #include "private.h"
  18. static bool_t check_ktp_header(faux_hdr_t *hdr)
  19. {
  20. assert(hdr);
  21. if (!hdr)
  22. return BOOL_FALSE;
  23. if (faux_hdr_magic(hdr) != KTP_MAGIC)
  24. return BOOL_FALSE;
  25. if (faux_hdr_major(hdr) != KTP_MAJOR)
  26. return BOOL_FALSE;
  27. if (faux_hdr_minor(hdr) != KTP_MINOR)
  28. return BOOL_FALSE;
  29. if (faux_hdr_len(hdr) < (int)sizeof(*hdr))
  30. return BOOL_FALSE;
  31. return BOOL_TRUE;
  32. }
  33. static bool_t ktpd_session_dispatch(ktpd_session_t *session, faux_msg_t *msg)
  34. {
  35. assert(session);
  36. if (!session)
  37. return BOOL_FALSE;
  38. assert(msg);
  39. if (!msg)
  40. return BOOL_FALSE;
  41. printf("Dispatch %d\n", faux_msg_get_len(msg));
  42. return BOOL_TRUE;
  43. }
  44. /** @brief Low-level function to receive KTP message.
  45. *
  46. * Firstly function gets the header of message. Then it checks and parses
  47. * header and find out the length of whole message. Then it receives the rest
  48. * of message.
  49. */
  50. static bool_t ktpd_session_read_cb(faux_async_t *async,
  51. void *data, size_t len, void *user_data)
  52. {
  53. ktpd_session_t *session = (ktpd_session_t *)user_data;
  54. faux_msg_t *completed_msg = NULL;
  55. assert(async);
  56. assert(data);
  57. assert(session);
  58. // Receive header
  59. if (!session->hdr) {
  60. size_t whole_len = 0;
  61. size_t msg_wo_hdr = 0;
  62. session->hdr = (faux_hdr_t *)data;
  63. // Check for broken header
  64. if (!check_ktp_header(session->hdr)) {
  65. faux_free(session->hdr);
  66. session->hdr = NULL;
  67. return BOOL_FALSE;
  68. }
  69. whole_len = faux_hdr_len(session->hdr);
  70. // msg_wo_hdr >= 0 because check_ktp_header() validates whole_len
  71. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  72. // Plan to receive message body
  73. if (msg_wo_hdr > 0) {
  74. faux_async_set_read_limits(async,
  75. msg_wo_hdr, msg_wo_hdr);
  76. return BOOL_TRUE;
  77. }
  78. // Here message is completed (msg body has zero length)
  79. completed_msg = faux_msg_deserialize_parts(session->hdr, NULL, 0);
  80. // Receive message body
  81. } else {
  82. completed_msg = faux_msg_deserialize_parts(session->hdr, data, len);
  83. faux_free(data);
  84. }
  85. // Plan to receive msg header
  86. faux_async_set_read_limits(session->async,
  87. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  88. faux_free(session->hdr);
  89. session->hdr = NULL; // Ready to recv new header
  90. // Here message is completed
  91. ktpd_session_dispatch(session, completed_msg);
  92. faux_msg_free(completed_msg);
  93. return BOOL_TRUE;
  94. }
  95. static bool_t ktpd_session_stall_cb(faux_async_t *async,
  96. size_t len, void *user_data)
  97. {
  98. ktpd_session_t *session = (ktpd_session_t *)user_data;
  99. assert(async);
  100. assert(session);
  101. if (!session->stall_cb)
  102. return BOOL_TRUE;
  103. session->stall_cb(session, session->stall_udata);
  104. async = async; // Happy compiler
  105. len = len; // Happy compiler
  106. return BOOL_TRUE;
  107. }
  108. ktpd_session_t *ktpd_session_new(int sock)
  109. {
  110. ktpd_session_t *session = NULL;
  111. if (sock < 0)
  112. return NULL;
  113. session = faux_zmalloc(sizeof(*session));
  114. assert(session);
  115. if (!session)
  116. return NULL;
  117. // Init
  118. session->state = KTPD_SESSION_STATE_NOT_AUTHORIZED;
  119. session->async = faux_async_new(sock);
  120. assert(session->async);
  121. // Receive message header first
  122. faux_async_set_read_limits(session->async,
  123. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  124. faux_async_set_read_cb(session->async, ktpd_session_read_cb, session);
  125. session->hdr = NULL;
  126. return session;
  127. }
  128. void ktpd_session_free(ktpd_session_t *session)
  129. {
  130. if (!session)
  131. return;
  132. faux_free(session->hdr);
  133. close(ktpd_session_fd(session));
  134. faux_async_free(session->async);
  135. faux_free(session);
  136. }
  137. bool_t ktpd_session_connected(ktpd_session_t *session)
  138. {
  139. assert(session);
  140. if (!session)
  141. return BOOL_FALSE;
  142. if (KTPD_SESSION_STATE_DISCONNECTED == session->state)
  143. return BOOL_FALSE;
  144. return BOOL_TRUE;
  145. }
  146. int ktpd_session_fd(const ktpd_session_t *session)
  147. {
  148. assert(session);
  149. if (!session)
  150. return BOOL_FALSE;
  151. return faux_async_fd(session->async);
  152. }
  153. bool_t ktpd_session_async_in(ktpd_session_t *session)
  154. {
  155. assert(session);
  156. if (!session)
  157. return BOOL_FALSE;
  158. if (!ktpd_session_connected(session))
  159. return BOOL_FALSE;
  160. if (faux_async_in(session->async) < 0)
  161. return BOOL_FALSE;
  162. return BOOL_TRUE;
  163. }
  164. bool_t ktpd_session_async_out(ktpd_session_t *session)
  165. {
  166. assert(session);
  167. if (!session)
  168. return BOOL_FALSE;
  169. if (!ktpd_session_connected(session))
  170. return BOOL_FALSE;
  171. if (faux_async_out(session->async) < 0)
  172. return BOOL_FALSE;
  173. return BOOL_TRUE;
  174. }
  175. void ktpd_session_set_stall_cb(ktpd_session_t *session,
  176. faux_session_stall_cb_fn stall_cb, void *user_data)
  177. {
  178. assert(session);
  179. if (!session)
  180. return;
  181. session->stall_cb = stall_cb;
  182. session->stall_udata = user_data;
  183. faux_async_set_stall_cb(session->async, ktpd_session_stall_cb, session);
  184. }
  185. #if 0
  186. static void ktpd_session_bad_socket(ktpd_session_t *session)
  187. {
  188. assert(session);
  189. if (!session)
  190. return;
  191. session->state = KTPD_SESSION_STATE_DISCONNECTED;
  192. }
  193. #endif