sshbpp.h 5.4 KB

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