channels.h revision 162856
1264269Ssbruno/* $OpenBSD: channels.h,v 1.88 2006/08/03 03:34:42 deraadt Exp $ */
2264269Ssbruno
3264269Ssbruno/*
4264269Ssbruno * Author: Tatu Ylonen <ylo@cs.hut.fi>
5264269Ssbruno * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6264269Ssbruno *                    All rights reserved
7264269Ssbruno *
8264269Ssbruno * As far as I am concerned, the code I have written for this software
9264269Ssbruno * can be used freely for any purpose.  Any derived versions of this
10264269Ssbruno * software must be clearly marked as such, and if the derived work is
11264269Ssbruno * incompatible with the protocol description in the RFC file, it must be
12264269Ssbruno * called by a name other than "ssh" or "Secure Shell".
13264269Ssbruno */
14264269Ssbruno/*
15264269Ssbruno * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
16264269Ssbruno *
17264269Ssbruno * Redistribution and use in source and binary forms, with or without
18264269Ssbruno * modification, are permitted provided that the following conditions
19264269Ssbruno * are met:
20264269Ssbruno * 1. Redistributions of source code must retain the above copyright
21264269Ssbruno *    notice, this list of conditions and the following disclaimer.
22264269Ssbruno * 2. Redistributions in binary form must reproduce the above copyright
23264269Ssbruno *    notice, this list of conditions and the following disclaimer in the
24264269Ssbruno *    documentation and/or other materials provided with the distribution.
25264269Ssbruno *
26264269Ssbruno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27264269Ssbruno * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28264269Ssbruno * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29264269Ssbruno * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30264269Ssbruno * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31264269Ssbruno * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32264269Ssbruno * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33264269Ssbruno * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34264269Ssbruno * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35264269Ssbruno * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36264269Ssbruno */
37264269Ssbruno
38264269Ssbruno#ifndef CHANNEL_H
39264269Ssbruno#define CHANNEL_H
40264269Ssbruno
41264269Ssbruno/* Definitions for channel types. */
42264269Ssbruno#define SSH_CHANNEL_X11_LISTENER	1	/* Listening for inet X11 conn. */
43264269Ssbruno#define SSH_CHANNEL_PORT_LISTENER	2	/* Listening on a port. */
44264269Ssbruno#define SSH_CHANNEL_OPENING		3	/* waiting for confirmation */
45264269Ssbruno#define SSH_CHANNEL_OPEN		4	/* normal open two-way channel */
46264269Ssbruno#define SSH_CHANNEL_CLOSED		5	/* waiting for close confirmation */
47264269Ssbruno#define SSH_CHANNEL_AUTH_SOCKET		6	/* authentication socket */
48264269Ssbruno#define SSH_CHANNEL_X11_OPEN		7	/* reading first X11 packet */
49264269Ssbruno#define SSH_CHANNEL_INPUT_DRAINING	8	/* sending remaining data to conn */
50264269Ssbruno#define SSH_CHANNEL_OUTPUT_DRAINING	9	/* sending remaining data to app */
51264269Ssbruno#define SSH_CHANNEL_LARVAL		10	/* larval session */
52264269Ssbruno#define SSH_CHANNEL_RPORT_LISTENER	11	/* Listening to a R-style port  */
53264269Ssbruno#define SSH_CHANNEL_CONNECTING		12
54264269Ssbruno#define SSH_CHANNEL_DYNAMIC		13
55264269Ssbruno#define SSH_CHANNEL_ZOMBIE		14	/* Almost dead. */
56264269Ssbruno#define SSH_CHANNEL_MAX_TYPE		15
57264269Ssbruno
58264269Ssbruno#define SSH_CHANNEL_PATH_LEN		256
59264269Ssbruno
60264269Ssbrunostruct Channel;
61264269Ssbrunotypedef struct Channel Channel;
62264269Ssbruno
63264269Ssbrunotypedef void channel_callback_fn(int, void *);
64264269Ssbrunotypedef int channel_infilter_fn(struct Channel *, char *, int);
65264269Ssbrunotypedef u_char *channel_outfilter_fn(struct Channel *, u_char **, u_int *);
66264269Ssbruno
67264269Ssbrunostruct Channel {
68264269Ssbruno	int     type;		/* channel type/state */
69264269Ssbruno	int     self;		/* my own channel identifier */
70264269Ssbruno	int     remote_id;	/* channel identifier for remote peer */
71264269Ssbruno	u_int   istate;		/* input from channel (state of receive half) */
72264269Ssbruno	u_int   ostate;		/* output to channel  (state of transmit half) */
73264269Ssbruno	int     flags;		/* close sent/rcvd */
74264269Ssbruno	int     rfd;		/* read fd */
75264269Ssbruno	int     wfd;		/* write fd */
76264269Ssbruno	int     efd;		/* extended fd */
77264269Ssbruno	int     sock;		/* sock fd */
78264269Ssbruno	int     ctl_fd;		/* control fd (client sharing) */
79264269Ssbruno	int     isatty;		/* rfd is a tty */
80264269Ssbruno	int     wfd_isatty;	/* wfd is a tty */
81264269Ssbruno	int	client_tty;	/* (client) TTY has been requested */
82264269Ssbruno	int     force_drain;	/* force close on iEOF */
83264269Ssbruno	int     delayed;		/* fdset hack */
84264269Ssbruno	Buffer  input;		/* data read from socket, to be sent over
85264269Ssbruno				 * encrypted connection */
86264269Ssbruno	Buffer  output;		/* data received over encrypted connection for
87264269Ssbruno				 * send on socket */
88264269Ssbruno	Buffer  extended;
89264269Ssbruno	char    path[SSH_CHANNEL_PATH_LEN];
90264269Ssbruno		/* path for unix domain sockets, or host name for forwards */
91264269Ssbruno	int     listening_port;	/* port being listened for forwards */
92264269Ssbruno	int     host_port;	/* remote port to connect for forwards */
93264269Ssbruno	char   *remote_name;	/* remote hostname */
94264269Ssbruno
95264269Ssbruno	u_int	remote_window;
96264269Ssbruno	u_int	remote_maxpacket;
97264269Ssbruno	u_int	local_window;
98264269Ssbruno	u_int	local_window_max;
99264269Ssbruno	u_int	local_consumed;
100264269Ssbruno	u_int	local_maxpacket;
101264269Ssbruno	int     extended_usage;
102264269Ssbruno	int	single_connection;
103264269Ssbruno
104264269Ssbruno	char   *ctype;		/* type */
105264269Ssbruno
106264269Ssbruno	/* callback */
107264269Ssbruno	channel_callback_fn	*confirm;
108264269Ssbruno	void			*confirm_ctx;
109264269Ssbruno	channel_callback_fn	*detach_user;
110264269Ssbruno	int			detach_close;
111264269Ssbruno
112264269Ssbruno	/* filter */
113264269Ssbruno	channel_infilter_fn	*input_filter;
114264269Ssbruno	channel_outfilter_fn	*output_filter;
115264269Ssbruno
116264269Ssbruno	int     datagram;	/* keep boundaries */
117264269Ssbruno};
118264269Ssbruno
119264269Ssbruno#define CHAN_EXTENDED_IGNORE		0
120264269Ssbruno#define CHAN_EXTENDED_READ		1
121264269Ssbruno#define CHAN_EXTENDED_WRITE		2
122264269Ssbruno
123264269Ssbruno/* default window/packet sizes for tcp/x11-fwd-channel */
124264269Ssbruno#define CHAN_SES_PACKET_DEFAULT	(32*1024)
125264269Ssbruno#define CHAN_SES_WINDOW_DEFAULT	(4*CHAN_SES_PACKET_DEFAULT)
126264269Ssbruno#define CHAN_TCP_PACKET_DEFAULT	(32*1024)
127264269Ssbruno#define CHAN_TCP_WINDOW_DEFAULT	(4*CHAN_TCP_PACKET_DEFAULT)
128264269Ssbruno#define CHAN_X11_PACKET_DEFAULT	(16*1024)
129264269Ssbruno#define CHAN_X11_WINDOW_DEFAULT	(4*CHAN_X11_PACKET_DEFAULT)
130264269Ssbruno
131264269Ssbruno/* possible input states */
132264269Ssbruno#define CHAN_INPUT_OPEN			0
133264269Ssbruno#define CHAN_INPUT_WAIT_DRAIN		1
134264269Ssbruno#define CHAN_INPUT_WAIT_OCLOSE		2
135264269Ssbruno#define CHAN_INPUT_CLOSED		3
136264269Ssbruno
137264269Ssbruno/* possible output states */
138264269Ssbruno#define CHAN_OUTPUT_OPEN		0
139264269Ssbruno#define CHAN_OUTPUT_WAIT_DRAIN		1
140264269Ssbruno#define CHAN_OUTPUT_WAIT_IEOF		2
141264269Ssbruno#define CHAN_OUTPUT_CLOSED		3
142264269Ssbruno
143264269Ssbruno#define CHAN_CLOSE_SENT			0x01
144264269Ssbruno#define CHAN_CLOSE_RCVD			0x02
145264269Ssbruno#define CHAN_EOF_SENT			0x04
146264269Ssbruno#define CHAN_EOF_RCVD			0x08
147264269Ssbruno
148264269Ssbruno#define CHAN_RBUF	16*1024
149264269Ssbruno
150264269Ssbruno/* check whether 'efd' is still in use */
151264269Ssbruno#define CHANNEL_EFD_INPUT_ACTIVE(c) \
152264269Ssbruno	(compat20 && c->extended_usage == CHAN_EXTENDED_READ && \
153264269Ssbruno	(c->efd != -1 || \
154264269Ssbruno	buffer_len(&c->extended) > 0))
155264269Ssbruno#define CHANNEL_EFD_OUTPUT_ACTIVE(c) \
156264269Ssbruno	(compat20 && c->extended_usage == CHAN_EXTENDED_WRITE && \
157264269Ssbruno	c->efd != -1 && (!(c->flags & (CHAN_EOF_RCVD|CHAN_CLOSE_RCVD)) || \
158264269Ssbruno	buffer_len(&c->extended) > 0))
159264269Ssbruno
160264269Ssbruno/* channel management */
161264269Ssbruno
162264269SsbrunoChannel	*channel_by_id(int);
163264269SsbrunoChannel	*channel_lookup(int);
164264269SsbrunoChannel *channel_new(char *, int, int, int, int, u_int, u_int, int, char *, int);
165264269Ssbrunovoid	 channel_set_fds(int, int, int, int, int, int, u_int);
166264269Ssbrunovoid	 channel_free(Channel *);
167264269Ssbrunovoid	 channel_free_all(void);
168264269Ssbrunovoid	 channel_stop_listening(void);
169264269Ssbruno
170264269Ssbrunovoid	 channel_send_open(int);
171264269Ssbrunovoid	 channel_request_start(int, char *, int);
172264269Ssbrunovoid	 channel_register_cleanup(int, channel_callback_fn *, int);
173264269Ssbrunovoid	 channel_register_confirm(int, channel_callback_fn *, void *);
174264269Ssbrunovoid	 channel_register_filter(int, channel_infilter_fn *, channel_outfilter_fn *);
175264269Ssbrunovoid	 channel_cancel_cleanup(int);
176264269Ssbrunoint	 channel_close_fd(int *);
177264269Ssbrunovoid	 channel_send_window_changes(void);
178264269Ssbruno
179264269Ssbruno/* protocol handler */
180264269Ssbruno
181264269Ssbrunovoid	 channel_input_close(int, u_int32_t, void *);
182264269Ssbrunovoid	 channel_input_close_confirmation(int, u_int32_t, void *);
183264269Ssbrunovoid	 channel_input_data(int, u_int32_t, void *);
184264269Ssbrunovoid	 channel_input_extended_data(int, u_int32_t, void *);
185264269Ssbrunovoid	 channel_input_ieof(int, u_int32_t, void *);
186264269Ssbrunovoid	 channel_input_oclose(int, u_int32_t, void *);
187264269Ssbrunovoid	 channel_input_open_confirmation(int, u_int32_t, void *);
188264269Ssbrunovoid	 channel_input_open_failure(int, u_int32_t, void *);
189264269Ssbrunovoid	 channel_input_port_open(int, u_int32_t, void *);
190264269Ssbrunovoid	 channel_input_window_adjust(int, u_int32_t, void *);
191264269Ssbruno
192264269Ssbruno/* file descriptor handling (read/write) */
193264269Ssbruno
194264269Ssbrunovoid	 channel_prepare_select(fd_set **, fd_set **, int *, u_int*, int);
195264269Ssbrunovoid     channel_after_select(fd_set *, fd_set *);
196264269Ssbrunovoid     channel_output_poll(void);
197264269Ssbruno
198264269Ssbrunoint      channel_not_very_much_buffered_data(void);
199264269Ssbrunovoid     channel_close_all(void);
200264269Ssbrunoint      channel_still_open(void);
201264269Ssbrunochar	*channel_open_message(void);
202264269Ssbrunoint	 channel_find_open(void);
203264269Ssbruno
204264269Ssbruno/* tcp forwarding */
205264269Ssbrunovoid	 channel_set_af(int af);
206264269Ssbrunovoid     channel_permit_all_opens(void);
207264269Ssbrunovoid	 channel_add_permitted_opens(char *, int);
208264269Ssbrunoint	 channel_add_adm_permitted_opens(char *, int);
209264269Ssbrunovoid	 channel_clear_permitted_opens(void);
210264269Ssbrunovoid	 channel_clear_adm_permitted_opens(void);
211264269Ssbrunoint      channel_input_port_forward_request(int, int);
212264269Ssbrunoint	 channel_connect_to(const char *, u_short);
213264269Ssbrunoint	 channel_connect_by_listen_address(u_short);
214264269Ssbrunoint	 channel_request_remote_forwarding(const char *, u_short,
215264269Ssbruno	     const char *, u_short);
216264269Ssbrunoint	 channel_setup_local_fwd_listener(const char *, u_short,
217264269Ssbruno	     const char *, u_short, int);
218264269Ssbrunovoid	 channel_request_rforward_cancel(const char *host, u_short port);
219264269Ssbrunoint	 channel_setup_remote_fwd_listener(const char *, u_short, int);
220264269Ssbrunoint	 channel_cancel_rport_listener(const char *, u_short);
221264269Ssbruno
222264269Ssbruno/* x11 forwarding */
223264269Ssbruno
224264269Ssbrunoint	 x11_connect_display(void);
225264269Ssbrunoint	 x11_create_display_inet(int, int, int, u_int *, int **);
226264269Ssbrunovoid     x11_input_open(int, u_int32_t, void *);
227264269Ssbrunovoid	 x11_request_forwarding_with_spoofing(int, const char *, const char *,
228264269Ssbruno	    const char *);
229264269Ssbrunovoid	 deny_input_open(int, u_int32_t, void *);
230264269Ssbruno
231264269Ssbruno/* agent forwarding */
232264269Ssbruno
233264269Ssbrunovoid	 auth_request_forwarding(void);
234264269Ssbruno
235264269Ssbruno/* channel close */
236264269Ssbruno
237264269Ssbrunoint	 chan_is_dead(Channel *, int);
238264269Ssbrunovoid	 chan_mark_dead(Channel *);
239264269Ssbruno
240264269Ssbruno/* channel events */
241264269Ssbruno
242264269Ssbrunovoid	 chan_rcvd_oclose(Channel *);
243264269Ssbrunovoid	 chan_read_failed(Channel *);
244264269Ssbrunovoid	 chan_ibuf_empty(Channel *);
245264269Ssbruno
246264269Ssbrunovoid	 chan_rcvd_ieof(Channel *);
247264269Ssbrunovoid	 chan_write_failed(Channel *);
248264269Ssbrunovoid	 chan_obuf_empty(Channel *);
249264269Ssbruno
250264269Ssbruno#endif
251264269Ssbruno