1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate /* $OpenBSD: channels.h,v 1.70 2002/06/24 14:33:27 markus Exp $ */ 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gate #ifndef _CHANNELS_H 8*0Sstevel@tonic-gate #define _CHANNELS_H 9*0Sstevel@tonic-gate 10*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gate #ifdef __cplusplus 13*0Sstevel@tonic-gate extern "C" { 14*0Sstevel@tonic-gate #endif 15*0Sstevel@tonic-gate 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gate /* 18*0Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi> 19*0Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 20*0Sstevel@tonic-gate * All rights reserved 21*0Sstevel@tonic-gate * 22*0Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software 23*0Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this 24*0Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is 25*0Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be 26*0Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell". 27*0Sstevel@tonic-gate */ 28*0Sstevel@tonic-gate /* 29*0Sstevel@tonic-gate * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved. 30*0Sstevel@tonic-gate * 31*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 32*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 33*0Sstevel@tonic-gate * are met: 34*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 35*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 36*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 37*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 38*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 39*0Sstevel@tonic-gate * 40*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 41*0Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 42*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 43*0Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 44*0Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46*0Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47*0Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48*0Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 49*0Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50*0Sstevel@tonic-gate */ 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate #include "buffer.h" 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate /* Definitions for channel types. */ 55*0Sstevel@tonic-gate #define SSH_CHANNEL_X11_LISTENER 1 /* Listening for inet X11 conn. */ 56*0Sstevel@tonic-gate #define SSH_CHANNEL_PORT_LISTENER 2 /* Listening on a port. */ 57*0Sstevel@tonic-gate #define SSH_CHANNEL_OPENING 3 /* waiting for confirmation */ 58*0Sstevel@tonic-gate #define SSH_CHANNEL_OPEN 4 /* normal open two-way channel */ 59*0Sstevel@tonic-gate #define SSH_CHANNEL_CLOSED 5 /* waiting for close confirmation */ 60*0Sstevel@tonic-gate #define SSH_CHANNEL_AUTH_SOCKET 6 /* authentication socket */ 61*0Sstevel@tonic-gate #define SSH_CHANNEL_X11_OPEN 7 /* reading first X11 packet */ 62*0Sstevel@tonic-gate #define SSH_CHANNEL_INPUT_DRAINING 8 /* sending remaining data to conn */ 63*0Sstevel@tonic-gate #define SSH_CHANNEL_OUTPUT_DRAINING 9 /* sending remaining data to app */ 64*0Sstevel@tonic-gate #define SSH_CHANNEL_LARVAL 10 /* larval session */ 65*0Sstevel@tonic-gate #define SSH_CHANNEL_RPORT_LISTENER 11 /* Listening to a R-style port */ 66*0Sstevel@tonic-gate #define SSH_CHANNEL_CONNECTING 12 67*0Sstevel@tonic-gate #define SSH_CHANNEL_DYNAMIC 13 68*0Sstevel@tonic-gate #define SSH_CHANNEL_ZOMBIE 14 /* Almost dead. */ 69*0Sstevel@tonic-gate #define SSH_CHANNEL_MAX_TYPE 15 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate #define SSH_CHANNEL_PATH_LEN 256 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate struct Channel; 74*0Sstevel@tonic-gate typedef struct Channel Channel; 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate typedef void channel_callback_fn(int, void *); 77*0Sstevel@tonic-gate typedef int channel_filter_fn(struct Channel *, char *, int); 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate struct Channel { 80*0Sstevel@tonic-gate int type; /* channel type/state */ 81*0Sstevel@tonic-gate int self; /* my own channel identifier */ 82*0Sstevel@tonic-gate int remote_id; /* channel identifier for remote peer */ 83*0Sstevel@tonic-gate u_int istate; /* input from channel (state of receive half) */ 84*0Sstevel@tonic-gate u_int ostate; /* output to channel (state of transmit half) */ 85*0Sstevel@tonic-gate int wait_for_exit; /* no close till after exit-status is sent */ 86*0Sstevel@tonic-gate int flags; /* close sent/rcvd */ 87*0Sstevel@tonic-gate int rfd; /* read fd */ 88*0Sstevel@tonic-gate int wfd; /* write fd */ 89*0Sstevel@tonic-gate int efd; /* extended fd */ 90*0Sstevel@tonic-gate int sock; /* sock fd */ 91*0Sstevel@tonic-gate int isatty; /* rfd is a tty */ 92*0Sstevel@tonic-gate int wfd_isatty; /* wfd is a tty */ 93*0Sstevel@tonic-gate int force_drain; /* force close on iEOF */ 94*0Sstevel@tonic-gate int delayed; /* fdset hack */ 95*0Sstevel@tonic-gate Buffer input; /* data read from socket, to be sent over 96*0Sstevel@tonic-gate * encrypted connection */ 97*0Sstevel@tonic-gate Buffer output; /* data received over encrypted connection for 98*0Sstevel@tonic-gate * send on socket */ 99*0Sstevel@tonic-gate Buffer extended; 100*0Sstevel@tonic-gate char path[SSH_CHANNEL_PATH_LEN]; 101*0Sstevel@tonic-gate /* path for unix domain sockets, or host name for forwards */ 102*0Sstevel@tonic-gate int listening_port; /* port being listened for forwards */ 103*0Sstevel@tonic-gate int host_port; /* remote port to connect for forwards */ 104*0Sstevel@tonic-gate char *remote_name; /* remote hostname */ 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate u_int remote_window; 107*0Sstevel@tonic-gate u_int remote_maxpacket; 108*0Sstevel@tonic-gate u_int local_window; 109*0Sstevel@tonic-gate u_int local_window_max; 110*0Sstevel@tonic-gate u_int local_consumed; 111*0Sstevel@tonic-gate u_int local_maxpacket; 112*0Sstevel@tonic-gate int extended_usage; 113*0Sstevel@tonic-gate int single_connection; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate char *ctype; /* type */ 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate /* callback */ 118*0Sstevel@tonic-gate channel_callback_fn *confirm; 119*0Sstevel@tonic-gate channel_callback_fn *detach_user; 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate /* filter */ 122*0Sstevel@tonic-gate channel_filter_fn *input_filter; 123*0Sstevel@tonic-gate }; 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate #define CHAN_EXTENDED_IGNORE 0 126*0Sstevel@tonic-gate #define CHAN_EXTENDED_READ 1 127*0Sstevel@tonic-gate #define CHAN_EXTENDED_WRITE 2 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate /* default window/packet sizes for tcp/x11-fwd-channel */ 130*0Sstevel@tonic-gate #define CHAN_SES_PACKET_DEFAULT (32*1024) 131*0Sstevel@tonic-gate #define CHAN_SES_WINDOW_DEFAULT (4*CHAN_SES_PACKET_DEFAULT) 132*0Sstevel@tonic-gate #define CHAN_TCP_PACKET_DEFAULT (32*1024) 133*0Sstevel@tonic-gate #define CHAN_TCP_WINDOW_DEFAULT (4*CHAN_TCP_PACKET_DEFAULT) 134*0Sstevel@tonic-gate #define CHAN_X11_PACKET_DEFAULT (16*1024) 135*0Sstevel@tonic-gate #define CHAN_X11_WINDOW_DEFAULT (4*CHAN_X11_PACKET_DEFAULT) 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate /* possible input states */ 138*0Sstevel@tonic-gate #define CHAN_INPUT_OPEN 0 139*0Sstevel@tonic-gate #define CHAN_INPUT_WAIT_DRAIN 1 140*0Sstevel@tonic-gate #define CHAN_INPUT_WAIT_OCLOSE 2 141*0Sstevel@tonic-gate #define CHAN_INPUT_CLOSED 3 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate /* possible output states */ 144*0Sstevel@tonic-gate #define CHAN_OUTPUT_OPEN 0 145*0Sstevel@tonic-gate #define CHAN_OUTPUT_WAIT_DRAIN 1 146*0Sstevel@tonic-gate #define CHAN_OUTPUT_WAIT_IEOF 2 147*0Sstevel@tonic-gate #define CHAN_OUTPUT_CLOSED 3 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate #define CHAN_CLOSE_SENT 0x01 150*0Sstevel@tonic-gate #define CHAN_CLOSE_RCVD 0x02 151*0Sstevel@tonic-gate #define CHAN_EOF_SENT 0x04 152*0Sstevel@tonic-gate #define CHAN_EOF_RCVD 0x08 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate /* check whether 'efd' is still in use */ 155*0Sstevel@tonic-gate #define CHANNEL_EFD_INPUT_ACTIVE(c) \ 156*0Sstevel@tonic-gate (compat20 && c->extended_usage == CHAN_EXTENDED_READ && \ 157*0Sstevel@tonic-gate (c->efd != -1 || \ 158*0Sstevel@tonic-gate buffer_len(&c->extended) > 0)) 159*0Sstevel@tonic-gate #define CHANNEL_EFD_OUTPUT_ACTIVE(c) \ 160*0Sstevel@tonic-gate (compat20 && c->extended_usage == CHAN_EXTENDED_WRITE && \ 161*0Sstevel@tonic-gate ((c->efd != -1 && !(c->flags & (CHAN_EOF_RCVD|CHAN_CLOSE_RCVD))) || \ 162*0Sstevel@tonic-gate buffer_len(&c->extended) > 0)) 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate /* channel management */ 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate Channel *channel_lookup(int); 167*0Sstevel@tonic-gate Channel *channel_new(char *, int, int, int, int, u_int, u_int, int, char *, int); 168*0Sstevel@tonic-gate void channel_set_fds(int, int, int, int, int, int, u_int); 169*0Sstevel@tonic-gate void channel_set_wait_for_exit(int, int); 170*0Sstevel@tonic-gate void channel_free(Channel *); 171*0Sstevel@tonic-gate void channel_free_all(void); 172*0Sstevel@tonic-gate void channel_stop_listening(void); 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate void channel_send_open(int); 175*0Sstevel@tonic-gate void channel_request_start(int, char *, int); 176*0Sstevel@tonic-gate void channel_register_cleanup(int, channel_callback_fn *); 177*0Sstevel@tonic-gate void channel_register_confirm(int, channel_callback_fn *); 178*0Sstevel@tonic-gate void channel_register_filter(int, channel_filter_fn *); 179*0Sstevel@tonic-gate void channel_cancel_cleanup(int); 180*0Sstevel@tonic-gate int channel_close_fd(int *); 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate /* protocol handler */ 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate void channel_input_close(int, u_int32_t, void *); 185*0Sstevel@tonic-gate void channel_input_close_confirmation(int, u_int32_t, void *); 186*0Sstevel@tonic-gate void channel_input_data(int, u_int32_t, void *); 187*0Sstevel@tonic-gate void channel_input_extended_data(int, u_int32_t, void *); 188*0Sstevel@tonic-gate void channel_input_ieof(int, u_int32_t, void *); 189*0Sstevel@tonic-gate void channel_input_oclose(int, u_int32_t, void *); 190*0Sstevel@tonic-gate void channel_input_open_confirmation(int, u_int32_t, void *); 191*0Sstevel@tonic-gate void channel_input_open_failure(int, u_int32_t, void *); 192*0Sstevel@tonic-gate void channel_input_port_open(int, u_int32_t, void *); 193*0Sstevel@tonic-gate void channel_input_window_adjust(int, u_int32_t, void *); 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate /* file descriptor handling (read/write) */ 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate void channel_prepare_select(fd_set **, fd_set **, int *, int*, int); 198*0Sstevel@tonic-gate void channel_after_select(fd_set *, fd_set *); 199*0Sstevel@tonic-gate void channel_output_poll(void); 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate int channel_not_very_much_buffered_data(void); 202*0Sstevel@tonic-gate void channel_close_all(void); 203*0Sstevel@tonic-gate int channel_still_open(void); 204*0Sstevel@tonic-gate char *channel_open_message(void); 205*0Sstevel@tonic-gate int channel_find_open(void); 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate /* tcp forwarding */ 208*0Sstevel@tonic-gate void channel_set_af(int af); 209*0Sstevel@tonic-gate void channel_permit_all_opens(void); 210*0Sstevel@tonic-gate void channel_add_permitted_opens(char *, int); 211*0Sstevel@tonic-gate void channel_clear_permitted_opens(void); 212*0Sstevel@tonic-gate void channel_input_port_forward_request(int, int); 213*0Sstevel@tonic-gate int channel_connect_to(const char *, u_short); 214*0Sstevel@tonic-gate int channel_connect_by_listen_address(u_short); 215*0Sstevel@tonic-gate void channel_request_remote_forwarding(u_short, const char *, u_short); 216*0Sstevel@tonic-gate int channel_setup_local_fwd_listener(u_short, const char *, u_short, int); 217*0Sstevel@tonic-gate int channel_setup_remote_fwd_listener(const char *, u_short, int); 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gate /* x11 forwarding */ 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate int x11_connect_display(void); 222*0Sstevel@tonic-gate int x11_create_display_inet(int, int, int, u_int *); 223*0Sstevel@tonic-gate void x11_input_open(int, u_int32_t, void *); 224*0Sstevel@tonic-gate void x11_request_forwarding_with_spoofing(int, const char *, const char *); 225*0Sstevel@tonic-gate void deny_input_open(int, u_int32_t, void *); 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate /* agent forwarding */ 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate void auth_request_forwarding(void); 230*0Sstevel@tonic-gate void auth_input_open_request(int, u_int32_t, void *); 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate /* channel close */ 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate int chan_is_dead(Channel *, int); 235*0Sstevel@tonic-gate void chan_mark_dead(Channel *); 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate /* channel events */ 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate void chan_rcvd_oclose(Channel *); 240*0Sstevel@tonic-gate void chan_read_failed(Channel *); 241*0Sstevel@tonic-gate void chan_ibuf_empty(Channel *); 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate void chan_rcvd_ieof(Channel *); 244*0Sstevel@tonic-gate void chan_write_failed(Channel *); 245*0Sstevel@tonic-gate void chan_obuf_empty(Channel *); 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate #ifdef __cplusplus 248*0Sstevel@tonic-gate } 249*0Sstevel@tonic-gate #endif 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate #endif /* _CHANNELS_H */ 252