ktpd_session.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_msg_t *msg)
  19. {
  20. assert(msg);
  21. if (!msg)
  22. return BOOL_FALSE;
  23. if (faux_msg_get_magic(msg) != KTP_MAGIC)
  24. return BOOL_FALSE;
  25. if (faux_msg_get_major(msg) != KTP_MAJOR)
  26. return BOOL_FALSE;
  27. if (faux_msg_get_minor(msg) != KTP_MINOR)
  28. return BOOL_FALSE;
  29. if (faux_msg_get_len(msg) < (int)sizeof(faux_hdr_t))
  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. printf("cb %ld, phdr %ld, hdr %ld\n", len, sizeof(faux_phdr_t), sizeof(faux_hdr_t));
  59. // Receive header
  60. if (!session->partial_msg) {
  61. size_t whole_len = 0;
  62. size_t msg_wo_hdr = 0;
  63. session->partial_msg = (faux_msg_t *)data;
  64. // Check for broken header
  65. if (!check_ktp_header(session->partial_msg)) {
  66. // Use faux_free() instead faux_msg_free() because
  67. // it's not completed message.
  68. faux_free(session->partial_msg);
  69. session->partial_msg = NULL;
  70. return BOOL_FALSE;
  71. }
  72. whole_len = faux_msg_get_len(session->partial_msg);
  73. // msg_wo_hdr >= 0 because check_ktp_header() validates whole_len
  74. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  75. // Plan to receive message body
  76. if (msg_wo_hdr > 0) {
  77. faux_async_set_read_limits(async,
  78. msg_wo_hdr, msg_wo_hdr);
  79. return BOOL_TRUE;
  80. }
  81. // Here message is completed (msg body has zero length)
  82. completed_msg = session->partial_msg;
  83. // Receive message body
  84. } else {
  85. completed_msg = realloc(session->partial_msg,
  86. sizeof(faux_hdr_t) + len);
  87. memcpy((char *)completed_msg + sizeof(faux_hdr_t), data, len);
  88. }
  89. // Here message is completed
  90. ktpd_session_dispatch(session, session->partial_msg);
  91. faux_msg_free(session->partial_msg);
  92. session->partial_msg = NULL; // Ready to recv new header
  93. // Plan to receive msg header
  94. faux_async_set_read_limits(session->async,
  95. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  96. return BOOL_TRUE;
  97. }
  98. static bool_t ktpd_session_stall_cb(faux_async_t *async,
  99. size_t len, void *user_data)
  100. {
  101. ktpd_session_t *session = (ktpd_session_t *)user_data;
  102. assert(async);
  103. assert(session);
  104. if (!session->stall_cb)
  105. return BOOL_TRUE;
  106. session->stall_cb(session, session->stall_udata);
  107. async = async; // Happy compiler
  108. len = len; // Happy compiler
  109. return BOOL_TRUE;
  110. }
  111. ktpd_session_t *ktpd_session_new(int sock)
  112. {
  113. ktpd_session_t *session = NULL;
  114. if (sock < 0)
  115. return NULL;
  116. session = faux_zmalloc(sizeof(*session));
  117. assert(session);
  118. if (!session)
  119. return NULL;
  120. // Init
  121. session->state = KTPD_SESSION_STATE_NOT_AUTHORIZED;
  122. session->async = faux_async_new(sock);
  123. assert(session->async);
  124. // Receive message header first
  125. faux_async_set_read_limits(session->async,
  126. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  127. faux_async_set_read_cb(session->async, ktpd_session_read_cb, session);
  128. session->partial_msg = NULL;
  129. return session;
  130. }
  131. void ktpd_session_free(ktpd_session_t *session)
  132. {
  133. if (!session)
  134. return;
  135. faux_free(session->partial_msg); // It can be partial faux_msg_t
  136. close(ktpd_session_fd(session));
  137. faux_async_free(session->async);
  138. faux_free(session);
  139. }
  140. bool_t ktpd_session_connected(ktpd_session_t *session)
  141. {
  142. assert(session);
  143. if (!session)
  144. return BOOL_FALSE;
  145. if (KTPD_SESSION_STATE_DISCONNECTED == session->state)
  146. return BOOL_FALSE;
  147. return BOOL_TRUE;
  148. }
  149. int ktpd_session_fd(const ktpd_session_t *session)
  150. {
  151. assert(session);
  152. if (!session)
  153. return BOOL_FALSE;
  154. return faux_async_fd(session->async);
  155. }
  156. bool_t ktpd_session_async_in(ktpd_session_t *session)
  157. {
  158. assert(session);
  159. if (!session)
  160. return BOOL_FALSE;
  161. if (!ktpd_session_connected(session))
  162. return BOOL_FALSE;
  163. if (faux_async_in(session->async) < 0)
  164. return BOOL_FALSE;
  165. return BOOL_TRUE;
  166. }
  167. bool_t ktpd_session_async_out(ktpd_session_t *session)
  168. {
  169. assert(session);
  170. if (!session)
  171. return BOOL_FALSE;
  172. if (!ktpd_session_connected(session))
  173. return BOOL_FALSE;
  174. if (faux_async_out(session->async) < 0)
  175. return BOOL_FALSE;
  176. return BOOL_TRUE;
  177. }
  178. void ktpd_session_set_stall_cb(ktpd_session_t *session,
  179. faux_session_stall_cb_f stall_cb, void *user_data)
  180. {
  181. assert(session);
  182. if (!session)
  183. return;
  184. session->stall_cb = stall_cb;
  185. session->stall_udata = user_data;
  186. faux_async_set_stall_cb(session->async, ktpd_session_stall_cb, session);
  187. }
  188. #if 0
  189. static void ktpd_session_bad_socket(ktpd_session_t *session)
  190. {
  191. assert(session);
  192. if (!session)
  193. return;
  194. session->state = KTPD_SESSION_STATE_DISCONNECTED;
  195. }
  196. #endif