sshbpp.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /* ic_in_raw is filled in by the BPP (probably by calling
  24. * ssh_bpp_common_setup). The BPP's owner triggers it when data is
  25. * added to in_raw, and also when the BPP is newly created. */
  26. IdempotentCallback ic_in_raw;
  27. /* ic_out_pq is entirely internal to the BPP itself; it's used as
  28. * the callback on out_pq. */
  29. IdempotentCallback ic_out_pq;
  30. int remote_bugs;
  31. /* Set this if remote connection closure should not generate an
  32. * error message (either because it's not to be treated as an
  33. * error at all, or because some other error message has already
  34. * been emitted). */
  35. int expect_close;
  36. };
  37. #define ssh_bpp_handle_input(bpp) ((bpp)->vt->handle_input(bpp))
  38. #define ssh_bpp_handle_output(bpp) ((bpp)->vt->handle_output(bpp))
  39. #define ssh_bpp_new_pktout(bpp, type) ((bpp)->vt->new_pktout(type))
  40. #define ssh_bpp_queue_disconnect(bpp, msg, cat) \
  41. ((bpp)->vt->queue_disconnect(bpp, msg, cat))
  42. /* ssh_bpp_free is more than just a macro wrapper on the vtable; it
  43. * does centralised parts of the freeing too. */
  44. void ssh_bpp_free(BinaryPacketProtocol *bpp);
  45. BinaryPacketProtocol *ssh1_bpp_new(LogContext *logctx);
  46. void ssh1_bpp_new_cipher(BinaryPacketProtocol *bpp,
  47. const struct ssh1_cipheralg *cipher,
  48. const void *session_key);
  49. /* Helper routine which does common BPP initialisation, e.g. setting
  50. * up in_pq and out_pq, and initialising input_consumer. */
  51. void ssh_bpp_common_setup(BinaryPacketProtocol *);
  52. /* Common helper functions between the SSH-2 full and bare BPPs */
  53. void ssh2_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  54. const char *msg, int category);
  55. int ssh2_bpp_check_unimplemented(BinaryPacketProtocol *bpp, PktIn *pktin);
  56. /* Convenience macro for BPPs to send formatted strings to the Event
  57. * Log. Assumes a function parameter called 'bpp' is in scope, and
  58. * takes a double pair of parens because it passes a whole argument
  59. * list to dupprintf. */
  60. #define bpp_logevent(params) ( \
  61. logevent_and_free((bpp)->logctx, dupprintf params))
  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. LogContext *logctx, 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, int delayed_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, int delayed_compression);
  98. /*
  99. * A query method specific to the interface between ssh2transport and
  100. * ssh2bpp. If true, it indicates that we're potentially in the
  101. * race-condition-prone part of delayed compression setup and so
  102. * asynchronous outgoing transport-layer packets are currently not
  103. * being sent, which means in particular that it would be a bad idea
  104. * to start a rekey because then we'd stop responding to anything
  105. * _other_ than transport-layer packets and deadlock the protocol.
  106. */
  107. int ssh2_bpp_rekey_inadvisable(BinaryPacketProtocol *bpp);
  108. BinaryPacketProtocol *ssh2_bare_bpp_new(LogContext *logctx);
  109. /*
  110. * The initial code to handle the SSH version exchange is also
  111. * structured as an implementation of BinaryPacketProtocol, because
  112. * that makes it easy to switch from that to the next BPP once it
  113. * tells us which one we're using.
  114. */
  115. struct ssh_version_receiver {
  116. void (*got_ssh_version)(struct ssh_version_receiver *rcv,
  117. int major_version);
  118. };
  119. BinaryPacketProtocol *ssh_verstring_new(
  120. Conf *conf, LogContext *logctx, int bare_connection_mode,
  121. const char *protoversion, struct ssh_version_receiver *rcv);
  122. const char *ssh_verstring_get_remote(BinaryPacketProtocol *);
  123. const char *ssh_verstring_get_local(BinaryPacketProtocol *);
  124. int ssh_verstring_get_bugs(BinaryPacketProtocol *);
  125. #endif /* PUTTY_SSHBPP_H */