sshbpp.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Abstraction of the binary packet protocols used in SSH.
  3. */
  4. #ifndef PUTTY_SSHBPP_H
  5. #define PUTTY_SSHBPP_H
  6. struct BinaryPacketProtocolVtable {
  7. void (*free)(BinaryPacketProtocol *);
  8. void (*handle_input)(BinaryPacketProtocol *);
  9. void (*handle_output)(BinaryPacketProtocol *);
  10. PktOut *(*new_pktout)(int type);
  11. void (*queue_disconnect)(BinaryPacketProtocol *,
  12. const char *msg, int category);
  13. };
  14. struct BinaryPacketProtocol {
  15. const struct BinaryPacketProtocolVtable *vt;
  16. bufchain *in_raw, *out_raw;
  17. int input_eof; /* set this if in_raw will never be added to again */
  18. PktInQueue in_pq;
  19. PktOutQueue out_pq;
  20. PacketLogSettings *pls;
  21. LogContext *logctx;
  22. Ssh *ssh;
  23. Frontend *frontend;
  24. /* ic_in_raw is filled in by the BPP (probably by calling
  25. * ssh_bpp_common_setup). The BPP's owner triggers it when data is
  26. * added to in_raw, and also when the BPP is newly created. */
  27. IdempotentCallback ic_in_raw;
  28. /* ic_out_pq is entirely internal to the BPP itself; it's used as
  29. * the callback on out_pq. */
  30. IdempotentCallback ic_out_pq;
  31. int remote_bugs;
  32. /* Set this if remote connection closure should not generate an
  33. * error message (either because it's not to be treated as an
  34. * error at all, or because some other error message has already
  35. * been emitted). */
  36. int expect_close;
  37. };
  38. #define ssh_bpp_handle_input(bpp) ((bpp)->vt->handle_input(bpp))
  39. #define ssh_bpp_handle_output(bpp) ((bpp)->vt->handle_output(bpp))
  40. #define ssh_bpp_new_pktout(bpp, type) ((bpp)->vt->new_pktout(type))
  41. #define ssh_bpp_queue_disconnect(bpp, msg, cat) \
  42. ((bpp)->vt->queue_disconnect(bpp, msg, cat))
  43. /* ssh_bpp_free is more than just a macro wrapper on the vtable; it
  44. * does centralised parts of the freeing too. */
  45. void ssh_bpp_free(BinaryPacketProtocol *bpp);
  46. BinaryPacketProtocol *ssh1_bpp_new(Frontend *frontend);
  47. void ssh1_bpp_new_cipher(BinaryPacketProtocol *bpp,
  48. const struct ssh1_cipheralg *cipher,
  49. const void *session_key);
  50. /* requested_compression() notifies the SSH-1 BPP that we've just sent
  51. * a request to enable compression, which means that on receiving the
  52. * next SSH1_SMSG_SUCCESS or SSH1_SMSG_FAILURE message, it should set
  53. * up zlib compression if it was SUCCESS. */
  54. void ssh1_bpp_requested_compression(BinaryPacketProtocol *bpp);
  55. /* Helper routine which does common BPP initialisation, e.g. setting
  56. * up in_pq and out_pq, and initialising input_consumer. */
  57. void ssh_bpp_common_setup(BinaryPacketProtocol *);
  58. /* Common helper functions between the SSH-2 full and bare BPPs */
  59. void ssh2_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  60. const char *msg, int category);
  61. int ssh2_bpp_check_unimplemented(BinaryPacketProtocol *bpp, PktIn *pktin);
  62. /*
  63. * Structure that tracks how much data is sent and received, for
  64. * purposes of triggering an SSH-2 rekey when either one gets over a
  65. * configured limit. In each direction, the flag 'running' indicates
  66. * that we haven't hit the limit yet, and 'remaining' tracks how much
  67. * longer until we do. The macro DTS_CONSUME subtracts a given amount
  68. * from the counter in a particular direction, and evaluates to a
  69. * boolean indicating whether the limit has been hit.
  70. *
  71. * The limit is sticky: once 'running' has flipped to false,
  72. * 'remaining' is no longer decremented, so it shouldn't dangerously
  73. * wrap round.
  74. */
  75. struct DataTransferStats {
  76. struct {
  77. int running;
  78. unsigned long remaining;
  79. } in, out;
  80. };
  81. #define DTS_CONSUME(stats, direction, size) \
  82. ((stats)->direction.running && \
  83. (stats)->direction.remaining <= (size) ? \
  84. ((stats)->direction.running = FALSE, TRUE) : \
  85. ((stats)->direction.remaining -= (size), FALSE))
  86. BinaryPacketProtocol *ssh2_bpp_new(
  87. Frontend *frontend, struct DataTransferStats *stats);
  88. void ssh2_bpp_new_outgoing_crypto(
  89. BinaryPacketProtocol *bpp,
  90. const struct ssh2_cipheralg *cipher, const void *ckey, const void *iv,
  91. const struct ssh2_macalg *mac, int etm_mode, const void *mac_key,
  92. const struct ssh_compression_alg *compression);
  93. void ssh2_bpp_new_incoming_crypto(
  94. BinaryPacketProtocol *bpp,
  95. const struct ssh2_cipheralg *cipher, const void *ckey, const void *iv,
  96. const struct ssh2_macalg *mac, int etm_mode, const void *mac_key,
  97. const struct ssh_compression_alg *compression);
  98. BinaryPacketProtocol *ssh2_bare_bpp_new(Frontend *frontend);
  99. /*
  100. * The initial code to handle the SSH version exchange is also
  101. * structured as an implementation of BinaryPacketProtocol, because
  102. * that makes it easy to switch from that to the next BPP once it
  103. * tells us which one we're using.
  104. */
  105. struct ssh_version_receiver {
  106. void (*got_ssh_version)(struct ssh_version_receiver *rcv,
  107. int major_version);
  108. };
  109. BinaryPacketProtocol *ssh_verstring_new(
  110. Conf *conf, Frontend *frontend, int bare_connection_mode,
  111. const char *protoversion, struct ssh_version_receiver *rcv);
  112. const char *ssh_verstring_get_remote(BinaryPacketProtocol *);
  113. const char *ssh_verstring_get_local(BinaryPacketProtocol *);
  114. int ssh_verstring_get_bugs(BinaryPacketProtocol *);
  115. #endif /* PUTTY_SSHBPP_H */