channels.h revision 192595
11573Srgrimes/* $OpenBSD: channels.h,v 1.98 2009/02/12 03:00:56 djm Exp $ */ 289739Sphantom 31573Srgrimes/* 41573Srgrimes * Author: Tatu Ylonen <ylo@cs.hut.fi> 51573Srgrimes * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 61573Srgrimes * All rights reserved 71573Srgrimes * 81573Srgrimes * As far as I am concerned, the code I have written for this software 91573Srgrimes * can be used freely for any purpose. Any derived versions of this 101573Srgrimes * software must be clearly marked as such, and if the derived work is 111573Srgrimes * incompatible with the protocol description in the RFC file, it must be 121573Srgrimes * called by a name other than "ssh" or "Secure Shell". 131573Srgrimes */ 141573Srgrimes/* 151573Srgrimes * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved. 161573Srgrimes * 171573Srgrimes * Redistribution and use in source and binary forms, with or without 181573Srgrimes * modification, are permitted provided that the following conditions 191573Srgrimes * are met: 201573Srgrimes * 1. Redistributions of source code must retain the above copyright 211573Srgrimes * notice, this list of conditions and the following disclaimer. 221573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 231573Srgrimes * notice, this list of conditions and the following disclaimer in the 241573Srgrimes * documentation and/or other materials provided with the distribution. 251573Srgrimes * 261573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 271573Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 281573Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 291573Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 301573Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 311573Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 321573Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 331573Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 341573Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 351573Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 361573Srgrimes */ 3792986Sobrien 3892986Sobrien#ifndef CHANNEL_H 391573Srgrimes#define CHANNEL_H 4019988Sache 4119988Sache/* Definitions for channel types. */ 42101259Sache#define SSH_CHANNEL_X11_LISTENER 1 /* Listening for inet X11 conn. */ 431573Srgrimes#define SSH_CHANNEL_PORT_LISTENER 2 /* Listening on a port. */ 441573Srgrimes#define SSH_CHANNEL_OPENING 3 /* waiting for confirmation */ 45116845Sphantom#define SSH_CHANNEL_OPEN 4 /* normal open two-way channel */ 461573Srgrimes#define SSH_CHANNEL_CLOSED 5 /* waiting for close confirmation */ 471573Srgrimes#define SSH_CHANNEL_AUTH_SOCKET 6 /* authentication socket */ 4819988Sache#define SSH_CHANNEL_X11_OPEN 7 /* reading first X11 packet */ 4911695Sache#define SSH_CHANNEL_INPUT_DRAINING 8 /* sending remaining data to conn */ 5072165Sphantom#define SSH_CHANNEL_OUTPUT_DRAINING 9 /* sending remaining data to app */ 5172165Sphantom#define SSH_CHANNEL_LARVAL 10 /* larval session */ 5272165Sphantom#define SSH_CHANNEL_RPORT_LISTENER 11 /* Listening to a R-style port */ 5322330Sache#define SSH_CHANNEL_CONNECTING 12 54101498Sache#define SSH_CHANNEL_DYNAMIC 13 5589739Sphantom#define SSH_CHANNEL_ZOMBIE 14 /* Almost dead. */ 561573Srgrimes#define SSH_CHANNEL_MAX_TYPE 15 5711695Sache 5811695Sachestruct Channel; 5911695Sachetypedef struct Channel Channel; 60228921Sjilles 6111695Sachetypedef void channel_callback_fn(int, void *); 6211695Sachetypedef int channel_infilter_fn(struct Channel *, char *, int); 6311695Sachetypedef void channel_filter_cleanup_fn(int, void *); 6411695Sachetypedef u_char *channel_outfilter_fn(struct Channel *, u_char **, u_int *); 6511695Sache 6611695Sache/* Channel success/failure callbacks */ 6735523Sachetypedef void channel_confirm_cb(int, struct Channel *, void *); 6811695Sachetypedef void channel_confirm_abandon_cb(struct Channel *, void *); 6911695Sachestruct channel_confirm { 7011695Sache TAILQ_ENTRY(channel_confirm) entry; 7111695Sache channel_confirm_cb *cb; 7211695Sache channel_confirm_abandon_cb *abandon_cb; 7322330Sache void *ctx; 7411695Sache}; 7511695SacheTAILQ_HEAD(channel_confirms, channel_confirm); 7611695Sache 7711695Sache/* Context for non-blocking connects */ 7811695Sachestruct channel_connect { 7911695Sache char *host; 8035523Sache int port; 8111695Sache struct addrinfo *ai, *aitop; 8211695Sache}; 8311695Sache 84116846Sphantomstruct Channel { 85116846Sphantom int type; /* channel type/state */ 86116846Sphantom int self; /* my own channel identifier */ 87116846Sphantom int remote_id; /* channel identifier for remote peer */ 88116846Sphantom u_int istate; /* input from channel (state of receive half) */ 8911695Sache u_int ostate; /* output to channel (state of transmit half) */ 9011695Sache int flags; /* close sent/rcvd */ 9122330Sache int rfd; /* read fd */ 9222330Sache int wfd; /* write fd */ 9311695Sache int efd; /* extended fd */ 9422330Sache int sock; /* sock fd */ 951573Srgrimes int ctl_fd; /* control fd (client sharing) */ 9692905Sobrien int isatty; /* rfd is a tty */ 9792905Sobrien int wfd_isatty; /* wfd is a tty */ 98227753Stheraven int client_tty; /* (client) TTY has been requested */ 991573Srgrimes int force_drain; /* force close on iEOF */ 1001573Srgrimes int delayed; /* fdset hack */ 1011573Srgrimes Buffer input; /* data read from socket, to be sent over 1021573Srgrimes * encrypted connection */ 1031573Srgrimes Buffer output; /* data received over encrypted connection for 1041573Srgrimes * send on socket */ 105101366Sache Buffer extended; 106125274Sache char *path; 1071573Srgrimes /* path for unix domain sockets, or host name for forwards */ 108101259Sache int listening_port; /* port being listened for forwards */ 109101259Sache int host_port; /* remote port to connect for forwards */ 1101573Srgrimes char *remote_name; /* remote hostname */ 111101259Sache 1121573Srgrimes u_int remote_window; 113101366Sache u_int remote_maxpacket; 11419964Sache u_int local_window; 1151573Srgrimes u_int local_window_max; 1161573Srgrimes u_int local_consumed; 1171573Srgrimes u_int local_maxpacket; 1181573Srgrimes int extended_usage; 1191573Srgrimes int single_connection; 12019971Sache 1211573Srgrimes char *ctype; /* type */ 1221573Srgrimes 1231573Srgrimes /* callback */ 1241573Srgrimes channel_callback_fn *open_confirm; 1251573Srgrimes void *open_confirm_ctx; 1261573Srgrimes channel_callback_fn *detach_user; 12719964Sache int detach_close; 1281573Srgrimes struct channel_confirms status_confirms; 129125274Sache 130125274Sache /* filter */ 131101366Sache channel_infilter_fn *input_filter; 132101366Sache channel_outfilter_fn *output_filter; 133101366Sache void *filter_ctx; 134101366Sache channel_filter_cleanup_fn *filter_cleanup; 1351573Srgrimes 136125274Sache /* keep boundaries */ 137125274Sache int datagram; 138125274Sache 139125274Sache /* non-blocking connect */ 140125274Sache struct channel_connect connect_ctx; 141125274Sache}; 142125274Sache 1431573Srgrimes#define CHAN_EXTENDED_IGNORE 0 144101366Sache#define CHAN_EXTENDED_READ 1 145101366Sache#define CHAN_EXTENDED_WRITE 2 146101366Sache 147101366Sache/* default window/packet sizes for tcp/x11-fwd-channel */ 148101366Sache#define CHAN_SES_PACKET_DEFAULT (32*1024) 149101366Sache#define CHAN_SES_WINDOW_DEFAULT (64*CHAN_SES_PACKET_DEFAULT) 150101366Sache#define CHAN_TCP_PACKET_DEFAULT (32*1024) 15122330Sache#define CHAN_TCP_WINDOW_DEFAULT (64*CHAN_TCP_PACKET_DEFAULT) 152101366Sache#define CHAN_X11_PACKET_DEFAULT (16*1024) 153101366Sache#define CHAN_X11_WINDOW_DEFAULT (4*CHAN_X11_PACKET_DEFAULT) 154101366Sache 155101366Sache/* possible input states */ 156101193Sache#define CHAN_INPUT_OPEN 0 157101366Sache#define CHAN_INPUT_WAIT_DRAIN 1 1581573Srgrimes#define CHAN_INPUT_WAIT_OCLOSE 2 159101259Sache#define CHAN_INPUT_CLOSED 3 160101259Sache 161101259Sache/* possible output states */ 162101259Sache#define CHAN_OUTPUT_OPEN 0 1631573Srgrimes#define CHAN_OUTPUT_WAIT_DRAIN 1 164101259Sache#define CHAN_OUTPUT_WAIT_IEOF 2 1651573Srgrimes#define CHAN_OUTPUT_CLOSED 3 166101193Sache 167101223Sache#define CHAN_CLOSE_SENT 0x01 168101366Sache#define CHAN_CLOSE_RCVD 0x02 169101366Sache#define CHAN_EOF_SENT 0x04 170101366Sache#define CHAN_EOF_RCVD 0x08 171101366Sache 172114443Snectar#define CHAN_RBUF 16*1024 173101259Sache 17422330Sache/* check whether 'efd' is still in use */ 175123801Sache#define CHANNEL_EFD_INPUT_ACTIVE(c) \ 176123801Sache (compat20 && c->extended_usage == CHAN_EXTENDED_READ && \ 1771573Srgrimes (c->efd != -1 || \ 178123801Sache buffer_len(&c->extended) > 0)) 179123801Sache#define CHANNEL_EFD_OUTPUT_ACTIVE(c) \ 1801573Srgrimes (compat20 && c->extended_usage == CHAN_EXTENDED_WRITE && \ 18153050Sache c->efd != -1 && (!(c->flags & (CHAN_EOF_RCVD|CHAN_CLOSE_RCVD)) || \ 1821573Srgrimes buffer_len(&c->extended) > 0)) 183101259Sache 18453050Sache/* channel management */ 18553050Sache 1861573SrgrimesChannel *channel_by_id(int); 1871573SrgrimesChannel *channel_lookup(int); 1881573SrgrimesChannel *channel_new(char *, int, int, int, int, u_int, u_int, int, char *, int); 18965420Simpvoid channel_set_fds(int, int, int, int, int, int, int, u_int); 19019971Sachevoid channel_free(Channel *); 1911573Srgrimesvoid channel_free_all(void); 19219971Sachevoid channel_stop_listening(void); 19319971Sache 19419964Sachevoid channel_send_open(int); 195101366Sachevoid channel_request_start(int, char *, int); 19619964Sachevoid channel_register_cleanup(int, channel_callback_fn *, int); 19719964Sachevoid channel_register_open_confirm(int, channel_callback_fn *, void *); 198101259Sachevoid channel_register_filter(int, channel_infilter_fn *, 199101498Sache channel_outfilter_fn *, channel_filter_cleanup_fn *, void *); 200101498Sachevoid channel_register_status_confirm(int, channel_confirm_cb *, 201101498Sache channel_confirm_abandon_cb *, void *); 202101498Sachevoid channel_cancel_cleanup(int); 20319964Sacheint channel_close_fd(int *); 204101259Sachevoid channel_send_window_changes(void); 20519964Sache 20619964Sache/* protocol handler */ 20719971Sache 20819964Sachevoid channel_input_close(int, u_int32_t, void *); 2091573Srgrimesvoid channel_input_close_confirmation(int, u_int32_t, void *); 2101573Srgrimesvoid channel_input_data(int, u_int32_t, void *); 2111573Srgrimesvoid channel_input_extended_data(int, u_int32_t, void *); 21211695Sachevoid channel_input_ieof(int, u_int32_t, void *); 21311695Sachevoid channel_input_oclose(int, u_int32_t, void *); 21419964Sachevoid channel_input_open_confirmation(int, u_int32_t, void *); 21511695Sachevoid channel_input_open_failure(int, u_int32_t, void *); 21611695Sachevoid channel_input_port_open(int, u_int32_t, void *); 21711695Sachevoid channel_input_window_adjust(int, u_int32_t, void *); 21811695Sachevoid channel_input_status_confirm(int, u_int32_t, void *); 21911695Sache 22035523Sache/* file descriptor handling (read/write) */ 221101259Sache 222101259Sachevoid channel_prepare_select(fd_set **, fd_set **, int *, u_int*, int); 223101259Sachevoid channel_after_select(fd_set *, fd_set *); 22435523Sachevoid channel_output_poll(void); 22511695Sache 22611695Sacheint channel_not_very_much_buffered_data(void); 22711695Sachevoid channel_close_all(void); 22811695Sacheint channel_still_open(void); 22911695Sachechar *channel_open_message(void); 23011695Sacheint channel_find_open(void); 2311573Srgrimes 2321573Srgrimes/* tcp forwarding */ 2331573Srgrimesvoid channel_set_af(int af); 23419971Sachevoid channel_permit_all_opens(void); 23519971Sachevoid channel_add_permitted_opens(char *, int); 236101498Sacheint channel_add_adm_permitted_opens(char *, int); 237116847Sphantomvoid channel_clear_permitted_opens(void); 23819964Sachevoid channel_clear_adm_permitted_opens(void); 239101259Sachevoid channel_print_adm_permitted_opens(void); 240101259Sacheint channel_input_port_forward_request(int, int); 241101259SacheChannel *channel_connect_to(const char *, u_short, char *, char *); 242101259SacheChannel *channel_connect_by_listen_address(u_short, char *, char *); 243101259Sacheint channel_request_remote_forwarding(const char *, u_short, 244101259Sache const char *, u_short); 245101259Sacheint channel_setup_local_fwd_listener(const char *, u_short, 246116847Sphantom const char *, u_short, int); 247116847Sphantomvoid channel_request_rforward_cancel(const char *host, u_short port); 248116847Sphantomint channel_setup_remote_fwd_listener(const char *, u_short, int *, int); 249116847Sphantomint channel_cancel_rport_listener(const char *, u_short); 250116847Sphantom 25119964Sache/* x11 forwarding */ 252101259Sache 253101259Sacheint x11_connect_display(void); 254117270Sacheint x11_create_display_inet(int, int, int, u_int *, int **); 255101292Sachevoid x11_input_open(int, u_int32_t, void *); 256101259Sachevoid x11_request_forwarding_with_spoofing(int, const char *, const char *, 257101292Sache const char *); 258101292Sachevoid deny_input_open(int, u_int32_t, void *); 259101259Sache 260101292Sache/* agent forwarding */ 261101292Sache 262101259Sachevoid auth_request_forwarding(void); 263101292Sache 264101292Sache/* channel close */ 265101259Sache 266101292Sacheint chan_is_dead(Channel *, int); 267101292Sachevoid chan_mark_dead(Channel *); 268101259Sache 269101292Sache/* channel events */ 270101292Sache 271101259Sachevoid chan_rcvd_oclose(Channel *); 272101259Sachevoid chan_rcvd_eow(Channel *); /* SSH2-only */ 273101259Sachevoid chan_read_failed(Channel *); 2746485Sachevoid chan_ibuf_empty(Channel *); 275101292Sache 276101292Sachevoid chan_rcvd_ieof(Channel *); 277101292Sachevoid chan_write_failed(Channel *); 278101292Sachevoid chan_obuf_empty(Channel *); 279101498Sache 280101292Sache#endif 281227753Stheraven