ktpd_session.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/ksession.h>
  16. #include <klish/ktp.h>
  17. #include <klish/ktp_session.h>
  18. #include "private.h"
  19. static bool_t check_ktp_header(faux_hdr_t *hdr)
  20. {
  21. assert(hdr);
  22. if (!hdr)
  23. return BOOL_FALSE;
  24. if (faux_hdr_magic(hdr) != KTP_MAGIC)
  25. return BOOL_FALSE;
  26. if (faux_hdr_major(hdr) != KTP_MAJOR)
  27. return BOOL_FALSE;
  28. if (faux_hdr_minor(hdr) != KTP_MINOR)
  29. return BOOL_FALSE;
  30. if (faux_hdr_len(hdr) < (int)sizeof(*hdr))
  31. return BOOL_FALSE;
  32. return BOOL_TRUE;
  33. }
  34. static bool_t ktpd_session_process_cmd(ktpd_session_t *session, faux_msg_t *msg)
  35. {
  36. bool_t rc = BOOL_FALSE;
  37. assert(session);
  38. if (!session)
  39. goto err;
  40. assert(msg);
  41. if (!msg)
  42. goto err;
  43. goto err;
  44. rc = BOOL_TRUE;
  45. err:
  46. if (!rc) {
  47. char *buf = NULL;
  48. size_t buf_len = 0;
  49. faux_msg_t *emsg = faux_msg_new(KTP_MAGIC, KTP_MAJOR, KTP_MINOR);
  50. const char *error = "Can't process line";
  51. faux_msg_set_cmd(emsg, KTP_CMD_ACK);
  52. faux_msg_add_param(emsg, KTP_PARAM_ERROR, error, strlen(error));
  53. // faux_net_t *net = faux_net_new();
  54. // faux_net_set_fd(net, ktpd_session_fd(session));
  55. faux_msg_debug(emsg);
  56. // printf("Send len: %ld\n", faux_msg_send(emsg, net));
  57. // faux_net_free(net);
  58. // buf = buf;
  59. // buf_len = buf_len;
  60. faux_msg_serialize(emsg, &buf, &buf_len);
  61. faux_async_write(session->async, buf, buf_len);
  62. faux_free(buf);
  63. faux_msg_free(emsg);
  64. }
  65. return rc;
  66. }
  67. static bool_t ktpd_session_dispatch(ktpd_session_t *session, faux_msg_t *msg)
  68. {
  69. assert(session);
  70. if (!session)
  71. return BOOL_FALSE;
  72. assert(msg);
  73. if (!msg)
  74. return BOOL_FALSE;
  75. printf("Dispatch cmd %c\n", (char)faux_msg_get_cmd(msg));
  76. switch (faux_msg_get_cmd(msg)) {
  77. case KTP_CMD:
  78. ktpd_session_process_cmd(session, msg);
  79. break;
  80. /* case KTP_COMPLETION:
  81. break;
  82. case KTP_HELP:
  83. break;
  84. */ default:
  85. printf("Unsupported command\n");
  86. break;
  87. }
  88. return BOOL_TRUE;
  89. }
  90. /** @brief Low-level function to receive KTP message.
  91. *
  92. * Firstly function gets the header of message. Then it checks and parses
  93. * header and find out the length of whole message. Then it receives the rest
  94. * of message.
  95. */
  96. static bool_t ktpd_session_read_cb(faux_async_t *async,
  97. void *data, size_t len, void *user_data)
  98. {
  99. ktpd_session_t *session = (ktpd_session_t *)user_data;
  100. faux_msg_t *completed_msg = NULL;
  101. assert(async);
  102. assert(data);
  103. assert(session);
  104. // Receive header
  105. if (!session->hdr) {
  106. size_t whole_len = 0;
  107. size_t msg_wo_hdr = 0;
  108. session->hdr = (faux_hdr_t *)data;
  109. // Check for broken header
  110. if (!check_ktp_header(session->hdr)) {
  111. faux_free(session->hdr);
  112. session->hdr = NULL;
  113. return BOOL_FALSE;
  114. }
  115. whole_len = faux_hdr_len(session->hdr);
  116. // msg_wo_hdr >= 0 because check_ktp_header() validates whole_len
  117. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  118. // Plan to receive message body
  119. if (msg_wo_hdr > 0) {
  120. faux_async_set_read_limits(async,
  121. msg_wo_hdr, msg_wo_hdr);
  122. return BOOL_TRUE;
  123. }
  124. // Here message is completed (msg body has zero length)
  125. completed_msg = faux_msg_deserialize_parts(session->hdr, NULL, 0);
  126. // Receive message body
  127. } else {
  128. completed_msg = faux_msg_deserialize_parts(session->hdr, data, len);
  129. faux_free(data);
  130. }
  131. // Plan to receive msg header
  132. faux_async_set_read_limits(session->async,
  133. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  134. faux_free(session->hdr);
  135. session->hdr = NULL; // Ready to recv new header
  136. // Here message is completed
  137. ktpd_session_dispatch(session, completed_msg);
  138. faux_msg_free(completed_msg);
  139. return BOOL_TRUE;
  140. }
  141. static bool_t ktpd_session_stall_cb(faux_async_t *async,
  142. size_t len, void *user_data)
  143. {
  144. ktpd_session_t *session = (ktpd_session_t *)user_data;
  145. assert(async);
  146. assert(session);
  147. if (!session->stall_cb)
  148. return BOOL_TRUE;
  149. session->stall_cb(session, session->stall_udata);
  150. async = async; // Happy compiler
  151. len = len; // Happy compiler
  152. return BOOL_TRUE;
  153. }
  154. ktpd_session_t *ktpd_session_new(int sock, const kscheme_t *scheme,
  155. const char *start_entry)
  156. {
  157. ktpd_session_t *session = NULL;
  158. if (sock < 0)
  159. return NULL;
  160. session = faux_zmalloc(sizeof(*session));
  161. assert(session);
  162. if (!session)
  163. return NULL;
  164. // Init
  165. session->state = KTPD_SESSION_STATE_NOT_AUTHORIZED;
  166. session->ksession = ksession_new(scheme, start_entry);
  167. assert(session->ksession);
  168. session->async = faux_async_new(sock);
  169. assert(session->async);
  170. // Receive message header first
  171. faux_async_set_read_limits(session->async,
  172. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  173. faux_async_set_read_cb(session->async, ktpd_session_read_cb, session);
  174. session->hdr = NULL;
  175. return session;
  176. }
  177. void ktpd_session_free(ktpd_session_t *session)
  178. {
  179. if (!session)
  180. return;
  181. ksession_free(session->ksession);
  182. faux_free(session->hdr);
  183. close(ktpd_session_fd(session));
  184. faux_async_free(session->async);
  185. faux_free(session);
  186. }
  187. bool_t ktpd_session_connected(ktpd_session_t *session)
  188. {
  189. assert(session);
  190. if (!session)
  191. return BOOL_FALSE;
  192. if (KTPD_SESSION_STATE_DISCONNECTED == session->state)
  193. return BOOL_FALSE;
  194. return BOOL_TRUE;
  195. }
  196. int ktpd_session_fd(const ktpd_session_t *session)
  197. {
  198. assert(session);
  199. if (!session)
  200. return BOOL_FALSE;
  201. return faux_async_fd(session->async);
  202. }
  203. bool_t ktpd_session_async_in(ktpd_session_t *session)
  204. {
  205. assert(session);
  206. if (!session)
  207. return BOOL_FALSE;
  208. if (!ktpd_session_connected(session))
  209. return BOOL_FALSE;
  210. if (faux_async_in(session->async) < 0)
  211. return BOOL_FALSE;
  212. return BOOL_TRUE;
  213. }
  214. bool_t ktpd_session_async_out(ktpd_session_t *session)
  215. {
  216. assert(session);
  217. if (!session)
  218. return BOOL_FALSE;
  219. if (!ktpd_session_connected(session))
  220. return BOOL_FALSE;
  221. if (faux_async_out(session->async) < 0)
  222. return BOOL_FALSE;
  223. return BOOL_TRUE;
  224. }
  225. void ktpd_session_set_stall_cb(ktpd_session_t *session,
  226. faux_session_stall_cb_fn stall_cb, void *user_data)
  227. {
  228. assert(session);
  229. if (!session)
  230. return;
  231. session->stall_cb = stall_cb;
  232. session->stall_udata = user_data;
  233. faux_async_set_stall_cb(session->async, ktpd_session_stall_cb, session);
  234. }
  235. #if 0
  236. static void ktpd_session_bad_socket(ktpd_session_t *session)
  237. {
  238. assert(session);
  239. if (!session)
  240. return;
  241. session->state = KTPD_SESSION_STATE_DISCONNECTED;
  242. }
  243. #endif