sshchan.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Abstraction of the various ways to handle the local end of an SSH
  3. * connection-layer channel.
  4. */
  5. #ifndef PUTTY_SSHCHAN_H
  6. #define PUTTY_SSHCHAN_H
  7. struct ChannelVtable {
  8. void (*free)(Channel *);
  9. /* Called for channel types that were created at the same time as
  10. * we sent an outgoing CHANNEL_OPEN, when the confirmation comes
  11. * back from the server indicating that the channel has been
  12. * opened, or the failure message indicating that it hasn't,
  13. * respectively. In the latter case, this must _not_ free the
  14. * Channel structure - the client will call the free method
  15. * separately. But it might do logging or other local cleanup. */
  16. void (*open_confirmation)(Channel *);
  17. void (*open_failed)(Channel *, const char *error_text);
  18. int (*send)(Channel *, int is_stderr, const void *buf, int len);
  19. void (*send_eof)(Channel *);
  20. void (*set_input_wanted)(Channel *, int wanted);
  21. char *(*log_close_msg)(Channel *);
  22. int (*want_close)(Channel *, int sent_local_eof, int rcvd_remote_eof);
  23. /* A method for every channel request we know of. All of these
  24. * return TRUE for success or FALSE for failure. */
  25. int (*rcvd_exit_status)(Channel *, int status);
  26. int (*rcvd_exit_signal)(
  27. Channel *chan, ptrlen signame, int core_dumped, ptrlen msg);
  28. int (*rcvd_exit_signal_numeric)(
  29. Channel *chan, int signum, int core_dumped, ptrlen msg);
  30. /* A method for signalling success/failure responses to channel
  31. * requests initiated from the SshChannel vtable with want_reply
  32. * true. */
  33. void (*request_response)(Channel *, int success);
  34. };
  35. struct Channel {
  36. const struct ChannelVtable *vt;
  37. unsigned initial_fixed_window_size;
  38. };
  39. #define chan_free(ch) ((ch)->vt->free(ch))
  40. #define chan_open_confirmation(ch) ((ch)->vt->open_confirmation(ch))
  41. #define chan_open_failed(ch, err) ((ch)->vt->open_failed(ch, err))
  42. #define chan_send(ch, err, buf, len) ((ch)->vt->send(ch, err, buf, len))
  43. #define chan_send_eof(ch) ((ch)->vt->send_eof(ch))
  44. #define chan_set_input_wanted(ch, wanted) \
  45. ((ch)->vt->set_input_wanted(ch, wanted))
  46. #define chan_log_close_msg(ch) ((ch)->vt->log_close_msg(ch))
  47. #define chan_want_close(ch, leof, reof) ((ch)->vt->want_close(ch, leof, reof))
  48. #define chan_rcvd_exit_status(ch, status) \
  49. ((ch)->vt->rcvd_exit_status(ch, status))
  50. #define chan_rcvd_exit_signal(ch, sig, core, msg) \
  51. ((ch)->vt->rcvd_exit_signal(ch, sig, core, msg))
  52. #define chan_rcvd_exit_signal_numeric(ch, sig, core, msg) \
  53. ((ch)->vt->rcvd_exit_signal_numeric(ch, sig, core, msg))
  54. #define chan_request_response(ch, success) \
  55. ((ch)->vt->request_response(ch, success))
  56. /*
  57. * Reusable methods you can put in vtables to give default handling of
  58. * some of those functions.
  59. */
  60. /* open_confirmation / open_failed for any channel it doesn't apply to */
  61. void chan_remotely_opened_confirmation(Channel *chan);
  62. void chan_remotely_opened_failure(Channel *chan, const char *errtext);
  63. /* want_close for any channel that wants the default behaviour of not
  64. * closing until both directions have had an EOF */
  65. int chan_default_want_close(Channel *, int, int);
  66. /* default implementations that refuse all the channel requests */
  67. int chan_no_exit_status(Channel *, int);
  68. int chan_no_exit_signal(Channel *, ptrlen, int, ptrlen);
  69. int chan_no_exit_signal_numeric(Channel *, int, int, ptrlen);
  70. /* default implementation that never expects to receive a response */
  71. void chan_no_request_response(Channel *, int);
  72. /*
  73. * Constructor for a trivial do-nothing implementation of
  74. * ChannelVtable. Used for 'zombie' channels, i.e. channels whose
  75. * proper local source of data has been shut down or otherwise stopped
  76. * existing, but the SSH side is still there and needs some kind of a
  77. * Channel implementation to talk to. In particular, the want_close
  78. * method for this channel always returns 'yes, please close this
  79. * channel asap', regardless of whether local and/or remote EOF have
  80. * been sent - indeed, even if _neither_ has.
  81. */
  82. Channel *zombiechan_new(void);
  83. /* ----------------------------------------------------------------------
  84. * This structure is owned by an SSH connection layer, and identifies
  85. * the connection layer's end of the channel, for the Channel
  86. * implementation to talk back to.
  87. */
  88. struct SshChannelVtable {
  89. int (*write)(SshChannel *c, const void *, int);
  90. void (*write_eof)(SshChannel *c);
  91. void (*initiate_close)(SshChannel *c, const char *err);
  92. void (*unthrottle)(SshChannel *c, int bufsize);
  93. Conf *(*get_conf)(SshChannel *c);
  94. void (*window_override_removed)(SshChannel *c);
  95. void (*x11_sharing_handover)(SshChannel *c,
  96. ssh_sharing_connstate *share_cs,
  97. share_channel *share_chan,
  98. const char *peer_addr, int peer_port,
  99. int endian, int protomajor, int protominor,
  100. const void *initial_data, int initial_len);
  101. /*
  102. * All the outgoing channel requests we support. Each one has a
  103. * want_reply flag, which will cause a callback to
  104. * chan_request_response when the result is available.
  105. *
  106. * The ones that return 'int' use it to indicate that the SSH
  107. * protocol in use doesn't support this request at all.
  108. *
  109. * (It's also intentional that not all of them have a want_reply
  110. * flag: the ones that don't are because SSH-1 has no method for
  111. * signalling success or failure of that request, or because we
  112. * wouldn't do anything usefully different with the reply in any
  113. * case.)
  114. */
  115. void (*request_x11_forwarding)(
  116. SshChannel *c, int want_reply, const char *authproto,
  117. const char *authdata, int screen_number, int oneshot);
  118. void (*request_agent_forwarding)(
  119. SshChannel *c, int want_reply);
  120. void (*request_pty)(
  121. SshChannel *c, int want_reply, Conf *conf, int w, int h);
  122. int (*send_env_var)(
  123. SshChannel *c, int want_reply, const char *var, const char *value);
  124. void (*start_shell)(
  125. SshChannel *c, int want_reply);
  126. void (*start_command)(
  127. SshChannel *c, int want_reply, const char *command);
  128. int (*start_subsystem)(
  129. SshChannel *c, int want_reply, const char *subsystem);
  130. int (*send_serial_break)(
  131. SshChannel *c, int want_reply, int length); /* length=0 for default */
  132. int (*send_signal)(
  133. SshChannel *c, int want_reply, const char *signame);
  134. void (*send_terminal_size_change)(
  135. SshChannel *c, int w, int h);
  136. void (*hint_channel_is_simple)(SshChannel *c);
  137. };
  138. struct SshChannel {
  139. const struct SshChannelVtable *vt;
  140. ConnectionLayer *cl;
  141. };
  142. #define sshfwd_write(c, buf, len) ((c)->vt->write(c, buf, len))
  143. #define sshfwd_write_eof(c) ((c)->vt->write_eof(c))
  144. #define sshfwd_initiate_close(c, err) ((c)->vt->initiate_close(c, err))
  145. #define sshfwd_unthrottle(c, bufsize) ((c)->vt->unthrottle(c, bufsize))
  146. #define sshfwd_get_conf(c) ((c)->vt->get_conf(c))
  147. #define sshfwd_window_override_removed(c) ((c)->vt->window_override_removed(c))
  148. #define sshfwd_x11_sharing_handover(c, cs, ch, pa, pp, e, pmaj, pmin, d, l) \
  149. ((c)->vt->x11_sharing_handover(c, cs, ch, pa, pp, e, pmaj, pmin, d, l))
  150. #define sshfwd_request_x11_forwarding(c, wr, ap, ad, scr, oneshot) \
  151. ((c)->vt->request_x11_forwarding(c, wr, ap, ad, scr, oneshot))
  152. #define sshfwd_request_agent_forwarding(c, wr) \
  153. ((c)->vt->request_agent_forwarding(c, wr))
  154. #define sshfwd_request_pty(c, wr, conf, w, h) \
  155. ((c)->vt->request_pty(c, wr, conf, w, h))
  156. #define sshfwd_send_env_var(c, wr, var, value) \
  157. ((c)->vt->send_env_var(c, wr, var, value))
  158. #define sshfwd_start_shell(c, wr) \
  159. ((c)->vt->start_shell(c, wr))
  160. #define sshfwd_start_command(c, wr, cmd) \
  161. ((c)->vt->start_command(c, wr, cmd))
  162. #define sshfwd_start_subsystem(c, wr, subsys) \
  163. ((c)->vt->start_subsystem(c, wr, subsys))
  164. #define sshfwd_send_serial_break(c, wr, length) \
  165. ((c)->vt->send_serial_break(c, wr, length))
  166. #define sshfwd_send_signal(c, wr, sig) \
  167. ((c)->vt->send_signal(c, wr, sig))
  168. #define sshfwd_send_terminal_size_change(c, w, h) \
  169. ((c)->vt->send_terminal_size_change(c, w, h))
  170. #define sshfwd_hint_channel_is_simple(c) \
  171. ((c)->vt->hint_channel_is_simple(c))
  172. /* ----------------------------------------------------------------------
  173. * The 'main' or primary channel of the SSH connection is special,
  174. * because it's the one that's connected directly to parts of the
  175. * frontend such as the terminal and the specials menu. So it exposes
  176. * a richer API.
  177. */
  178. mainchan *mainchan_new(
  179. PacketProtocolLayer *ppl, ConnectionLayer *cl, Conf *conf,
  180. int term_width, int term_height, int is_simple, SshChannel **sc_out);
  181. void mainchan_get_specials(
  182. mainchan *mc, add_special_fn_t add_special, void *ctx);
  183. void mainchan_special_cmd(mainchan *mc, SessionSpecialCode code, int arg);
  184. void mainchan_terminal_size(mainchan *mc, int width, int height);
  185. #endif /* PUTTY_SSHCHAN_H */