channel.h 5.1 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. #define SSH_OPEN_ADMINISTRATIVELY_PROHIBITED 1
  30. #define SSH_OPEN_CONNECT_FAILED 2
  31. #define SSH_OPEN_UNKNOWN_CHANNEL_TYPE 3
  32. #define SSH_OPEN_RESOURCE_SHORTAGE 4
  33. /* Not a real type */
  34. #define SSH_OPEN_IN_PROGRESS 99
  35. #define CHAN_EXTEND_SIZE 3 /* how many extra slots to add when we need more */
  36. struct ChanType;
  37. enum dropbear_channel_prio {
  38. DROPBEAR_CHANNEL_PRIO_INTERACTIVE, /* pty shell, x11 */
  39. DROPBEAR_CHANNEL_PRIO_UNKNOWABLE, /* tcp - can't know what's being forwarded */
  40. DROPBEAR_CHANNEL_PRIO_BULK, /* the rest - probably scp or something */
  41. DROPBEAR_CHANNEL_PRIO_EARLY, /* channel is still being set up */
  42. };
  43. struct Channel {
  44. unsigned int index; /* the local channel index */
  45. unsigned int remotechan;
  46. unsigned int recvwindow, transwindow;
  47. unsigned int recvdonelen;
  48. unsigned int recvmaxpacket, transmaxpacket;
  49. void* typedata; /* a pointer to type specific data */
  50. int writefd; /* read from wire, written to insecure side */
  51. int readfd; /* read from insecure side, written to wire */
  52. int errfd; /* used like writefd or readfd, depending if it's client or server.
  53. Doesn't exactly belong here, but is cleaner here */
  54. circbuffer *writebuf; /* data from the wire, for local consumption. Can be
  55. initially NULL */
  56. circbuffer *extrabuf; /* extended-data for the program - used like writebuf
  57. but for stderr */
  58. /* whether close/eof messages have been exchanged */
  59. int sent_close, recv_close;
  60. int recv_eof, sent_eof;
  61. /* Set after running the ChanType-specific close hander
  62. * to ensure we don't run it twice (nor type->checkclose()). */
  63. int close_handler_done;
  64. struct dropbear_progress_connection *conn_pending;
  65. int initconn; /* used for TCP forwarding, whether the channel has been
  66. fully initialised */
  67. int await_open; /* flag indicating whether we've sent an open request
  68. for this channel (and are awaiting a confirmation
  69. or failure). */
  70. int flushing;
  71. /* Used by client chansession to handle ~ escaping, NULL ignored otherwise */
  72. void (*read_mangler)(struct Channel*, unsigned char* bytes, int *len);
  73. const struct ChanType* type;
  74. enum dropbear_channel_prio prio;
  75. };
  76. struct ChanType {
  77. int sepfds; /* Whether this channel has separate pipes for in/out or not */
  78. char *name;
  79. int (*inithandler)(struct Channel*);
  80. int (*check_close)(struct Channel*);
  81. void (*reqhandler)(struct Channel*);
  82. void (*closehandler)(struct Channel*);
  83. };
  84. /* Callback for connect_remote */
  85. void channel_connect_done(int result, int sock, void* user_data, const char* errstring);
  86. void chaninitialise(const struct ChanType *chantypes[]);
  87. void chancleanup(void);
  88. void setchannelfds(fd_set *readfds, fd_set *writefds, int allow_reads);
  89. void channelio(fd_set *readfd, fd_set *writefd);
  90. struct Channel* getchannel(void);
  91. /* Returns an arbitrary channel that is in a ready state - not
  92. being initialised and no EOF in either direction. NULL if none. */
  93. struct Channel* get_any_ready_channel(void);
  94. void recv_msg_channel_open(void);
  95. void recv_msg_channel_request(void);
  96. void send_msg_channel_failure(struct Channel *channel);
  97. void send_msg_channel_success(struct Channel *channel);
  98. void recv_msg_channel_data(void);
  99. void recv_msg_channel_extended_data(void);
  100. void recv_msg_channel_window_adjust(void);
  101. void recv_msg_channel_close(void);
  102. void recv_msg_channel_eof(void);
  103. void common_recv_msg_channel_data(struct Channel *channel, int fd,
  104. circbuffer * buf);
  105. #ifdef DROPBEAR_CLIENT
  106. extern const struct ChanType clichansess;
  107. #endif
  108. #if defined(USING_LISTENERS) || defined(DROPBEAR_CLIENT)
  109. int send_msg_channel_open_init(int fd, const struct ChanType *type);
  110. void recv_msg_channel_open_confirmation(void);
  111. void recv_msg_channel_open_failure(void);
  112. #endif
  113. void start_send_channel_request(struct Channel *channel, char *type);
  114. void send_msg_request_success(void);
  115. void send_msg_request_failure(void);
  116. #endif /* DROPBEAR_CHANNEL_H_ */