ktp_session.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef _klish_ktp_session_h
  2. #define _klish_ktp_session_h
  3. #include <faux/faux.h>
  4. #include <faux/list.h>
  5. #include <faux/eloop.h>
  6. #include <faux/error.h>
  7. #include <klish/ksession.h>
  8. #include <klish/ktp.h>
  9. #define USOCK_PATH_MAX sizeof(((struct sockaddr_un *)0)->sun_path)
  10. #define KLISH_DEFAULT_UNIX_SOCKET_PATH "/tmp/klish-unix-socket"
  11. typedef struct ktpd_session_s ktpd_session_t;
  12. typedef struct ktp_session_s ktp_session_t;
  13. C_DECL_BEGIN
  14. // Common KTP functions
  15. int ktp_connect_unix(const char *sun_path);
  16. void ktp_disconnect(int fd);
  17. int ktp_accept(int listen_sock);
  18. bool_t ktp_check_header(faux_hdr_t *hdr);
  19. faux_msg_t *ktp_msg_preform(ktp_cmd_e cmd, uint32_t status);
  20. bool_t ktp_send_error(faux_async_t *async, ktp_cmd_e cmd, const char *error);
  21. bool_t ktp_peer_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  22. void *associated_data, void *user_data);
  23. bool_t ktp_stall_cb(faux_async_t *async, size_t len, void *user_data);
  24. // Help structure
  25. typedef struct help_s {
  26. char *prefix;
  27. char *line;
  28. } help_t;
  29. int help_compare(const void *first, const void *second);
  30. int help_kcompare(const void *key, const void *list_item);
  31. help_t *help_new(char *prefix, char *line);
  32. void help_free(void *ptr);
  33. // Client KTP session
  34. typedef enum {
  35. KTP_SESSION_STATE_ERROR = 'e', // Some unknown error
  36. KTP_SESSION_STATE_DISCONNECTED = 'd',
  37. KTP_SESSION_STATE_UNAUTHORIZED = 'a',
  38. KTP_SESSION_STATE_IDLE = 'i',
  39. KTP_SESSION_STATE_WAIT_FOR_COMPLETION = 'v',
  40. KTP_SESSION_STATE_WAIT_FOR_HELP = 'h',
  41. KTP_SESSION_STATE_WAIT_FOR_CMD = 'c',
  42. } ktp_session_state_e;
  43. typedef enum {
  44. KTP_SESSION_CB_STDOUT,
  45. KTP_SESSION_CB_STDERR,
  46. KTP_SESSION_CB_AUTH_ACK,
  47. KTP_SESSION_CB_CMD_ACK_INCOMPLETED,
  48. KTP_SESSION_CB_CMD_ACK,
  49. KTP_SESSION_CB_COMPLETION_ACK,
  50. KTP_SESSION_CB_HELP_ACK,
  51. KTP_SESSION_CB_EXIT,
  52. KTP_SESSION_CB_MAX,
  53. } ktp_session_cb_e;
  54. typedef bool_t (*ktp_session_stdout_cb_fn)(ktp_session_t *ktp,
  55. const char *line, size_t len, void *udata);
  56. typedef bool_t (*ktp_session_event_cb_fn)(ktp_session_t *ktp,
  57. const faux_msg_t *msg, void *udata);
  58. ktp_session_t *ktp_session_new(int sock, faux_eloop_t *eloop);
  59. void ktp_session_free(ktp_session_t *session);
  60. faux_eloop_t *ktp_session_eloop(const ktp_session_t *ktp);
  61. bool_t ktp_session_done(const ktp_session_t *ktp);
  62. bool_t ktp_session_set_done(ktp_session_t *ktp, bool_t done);
  63. faux_error_t *ktp_session_error(const ktp_session_t *ktp);
  64. bool_t ktp_session_set_cb(ktp_session_t *ktp, ktp_session_cb_e cb_id,
  65. void *fn, void *udata);
  66. bool_t ktp_session_connected(ktp_session_t *session);
  67. int ktp_session_fd(const ktp_session_t *session);
  68. faux_async_t *ktp_session_async(const ktp_session_t *ktp);
  69. bool_t ktp_session_stop_on_answer(const ktp_session_t *ktp);
  70. bool_t ktp_session_set_stop_on_answer(ktp_session_t *ktp, bool_t stop_on_answer);
  71. ktp_session_state_e ktp_session_state(const ktp_session_t *ktp);
  72. bool_t ktp_session_cmd(ktp_session_t *ktp, const char *line,
  73. faux_error_t *error, bool_t dry_run);
  74. bool_t ktp_session_auth(ktp_session_t *ktp, faux_error_t *error);
  75. bool_t ktp_session_completion(ktp_session_t *ktp, const char *line,
  76. bool_t dry_run);
  77. bool_t ktp_session_help(ktp_session_t *ktp, const char *line);
  78. bool_t ktp_session_stdin(ktp_session_t *ktp, const char *line, size_t line_len);
  79. bool_t ktp_session_stdin_close(ktp_session_t *ktp);
  80. bool_t ktp_session_stdout_close(ktp_session_t *ktp);
  81. bool_t ktp_session_stderr_close(ktp_session_t *ktp);
  82. bool_t ktp_session_retcode(ktp_session_t *ktp, int *retcode);
  83. ktp_status_e ktp_session_cmd_features(const ktp_session_t *ktp);
  84. bool_t ktp_session_stdout_need_newline(ktp_session_t *ktp);
  85. bool_t ktp_session_stderr_need_newline(ktp_session_t *ktp);
  86. int ktp_session_last_stream(ktp_session_t *ktp);
  87. // Server KTP session
  88. typedef bool_t (*ktpd_session_stall_cb_fn)(ktpd_session_t *session,
  89. void *user_data);
  90. ktpd_session_t *ktpd_session_new(int sock, kscheme_t *scheme,
  91. const char *start_entry, faux_eloop_t *eloop);
  92. void ktpd_session_free(ktpd_session_t *session);
  93. bool_t ktpd_session_connected(ktpd_session_t *session);
  94. int ktpd_session_fd(const ktpd_session_t *session);
  95. bool_t ktpd_session_async_in(ktpd_session_t *session);
  96. bool_t ktpd_session_async_out(ktpd_session_t *session);
  97. C_DECL_END
  98. #endif // _klish_ktp_session_h