channel.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. #ifndef DROPBEAR_CHANNEL_H_
  25. #define DROPBEAR_CHANNEL_H_
  26. #include "includes.h"
  27. #include "buffer.h"
  28. #include "circbuffer.h"
  29. #include "netio.h"
  30. #define SSH_OPEN_ADMINISTRATIVELY_PROHIBITED 1
  31. #define SSH_OPEN_CONNECT_FAILED 2
  32. #define SSH_OPEN_UNKNOWN_CHANNEL_TYPE 3
  33. #define SSH_OPEN_RESOURCE_SHORTAGE 4
  34. /* Not a real type */
  35. #define SSH_OPEN_IN_PROGRESS 99
  36. #define CHAN_EXTEND_SIZE 3 /* how many extra slots to add when we need more */
  37. struct ChanType;
  38. struct Channel {
  39. unsigned int index; /* the local channel index */
  40. unsigned int remotechan;
  41. unsigned int recvwindow, transwindow;
  42. unsigned int recvdonelen;
  43. unsigned int recvmaxpacket, transmaxpacket;
  44. void* typedata; /* a pointer to type specific data */
  45. int writefd; /* read from wire, written to insecure side */
  46. int readfd; /* read from insecure side, written to wire */
  47. int errfd; /* used like writefd or readfd, depending if it's client or server.
  48. Doesn't exactly belong here, but is cleaner here */
  49. int bidir_fd; /* a boolean indicating that writefd/readfd are the same
  50. file descriptor (bidirectional), such as a network socket or PTY.
  51. That is handled differently when closing FDs */
  52. circbuffer *writebuf; /* data from the wire, for local consumption. Can be
  53. initially NULL */
  54. circbuffer *extrabuf; /* extended-data for the program - used like writebuf
  55. but for stderr */
  56. /* whether close/eof messages have been exchanged */
  57. int sent_close, recv_close;
  58. int recv_eof, sent_eof;
  59. /* once flushing is set, readfd will close once no more data is available
  60. (not waiting for EOF) */
  61. int flushing;
  62. struct dropbear_progress_connection *conn_pending;
  63. int initconn; /* used for TCP forwarding, whether the channel has been
  64. fully initialised */
  65. int await_open; /* flag indicating whether we've sent an open request
  66. for this channel (and are awaiting a confirmation
  67. or failure). */
  68. /* Used by client chansession to handle ~ escaping, NULL ignored otherwise */
  69. void (*read_mangler)(const struct Channel*, const unsigned char* bytes, int *len);
  70. const struct ChanType* type;
  71. enum dropbear_prio prio;
  72. };
  73. struct ChanType {
  74. const char *name;
  75. /* Sets up the channel */
  76. int (*inithandler)(struct Channel*);
  77. /* Called to check whether a channel should close, separately from the FD being EOF.
  78. Used for noticing process exiting */
  79. int (*check_close)(struct Channel*);
  80. /* Handler for ssh_msg_channel_request */
  81. void (*reqhandler)(struct Channel*);
  82. /* Called prior to sending ssh_msg_channel_close, used for sending exit status */
  83. void (*closehandler)(const struct Channel*);
  84. /* Frees resources, called just prior to channel being removed */
  85. void (*cleanup)(const struct Channel*);
  86. };
  87. /* Callback for connect_remote. errstring may be NULL if result == DROPBEAR_SUCCESS */
  88. void channel_connect_done(int result, int sock, void* user_data, const char* errstring);
  89. void chaninitialise(const struct ChanType *chantypes[]);
  90. void chancleanup(void);
  91. void setchannelfds(fd_set *readfds, fd_set *writefds, int allow_reads);
  92. void channelio(const fd_set *readfd, const fd_set *writefd);
  93. struct Channel* getchannel(void);
  94. /* Returns an arbitrary channel that is in a ready state - not
  95. being initialised and no EOF in either direction. NULL if none. */
  96. struct Channel* get_any_ready_channel(void);
  97. void recv_msg_channel_open(void);
  98. void recv_msg_channel_request(void);
  99. void send_msg_channel_failure(const struct Channel *channel);
  100. void send_msg_channel_success(const struct Channel *channel);
  101. void recv_msg_channel_data(void);
  102. void recv_msg_channel_extended_data(void);
  103. void recv_msg_channel_window_adjust(void);
  104. void recv_msg_channel_close(void);
  105. void recv_msg_channel_eof(void);
  106. void common_recv_msg_channel_data(struct Channel *channel, int fd,
  107. circbuffer * buf);
  108. #if DROPBEAR_CLIENT
  109. extern const struct ChanType clichansess;
  110. #endif
  111. #if DROPBEAR_LISTENERS || DROPBEAR_CLIENT
  112. int send_msg_channel_open_init(int fd, const struct ChanType *type);
  113. void recv_msg_channel_open_confirmation(void);
  114. void recv_msg_channel_open_failure(void);
  115. #endif
  116. void start_send_channel_request(const struct Channel *channel, const char *type);
  117. void send_msg_request_success(void);
  118. void send_msg_request_failure(void);
  119. #endif /* DROPBEAR_CHANNEL_H_ */