18 #define RAW_MAX_BACKLOG 4096
\r
20 typedef struct raw_backend_data {
\r
21 const struct plug_function_table *fn;
\r
22 /* the above field _must_ be first in the structure */
\r
25 int closed_on_socket_error;
\r
28 int sent_console_eof, sent_socket_eof, session_started;
\r
33 static void raw_size(void *handle, int width, int height);
\r
35 static void c_write(Raw raw, char *buf, int len)
\r
37 int backlog = from_backend(raw->frontend, 0, buf, len);
\r
38 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
\r
41 static void raw_log(Plug plug, int type, SockAddr addr, int port,
\r
42 const char *error_msg, int error_code)
\r
44 Raw raw = (Raw) plug;
\r
45 backend_socket_log(raw->frontend, type, addr, port,
\r
46 error_msg, error_code, raw->conf, raw->session_started);
\r
49 static void raw_check_close(Raw raw)
\r
52 * Called after we send EOF on either the socket or the console.
\r
53 * Its job is to wind up the session once we have sent EOF on both.
\r
55 if (raw->sent_console_eof && raw->sent_socket_eof) {
\r
59 notify_remote_exit(raw->frontend);
\r
64 static int raw_closing(Plug plug, const char *error_msg, int error_code,
\r
67 Raw raw = (Raw) plug;
\r
70 /* A socket error has occurred. */
\r
74 raw->closed_on_socket_error = TRUE;
\r
75 notify_remote_exit(raw->frontend);
\r
77 logevent(raw->frontend, error_msg);
\r
78 connection_fatal(raw->frontend, "%s", error_msg);
\r
80 /* Otherwise, the remote side closed the connection normally. */
\r
81 if (!raw->sent_console_eof && from_backend_eof(raw->frontend)) {
\r
83 * The front end wants us to close the outgoing side of the
\r
84 * connection as soon as we see EOF from the far end.
\r
86 if (!raw->sent_socket_eof) {
\r
88 sk_write_eof(raw->s);
\r
89 raw->sent_socket_eof= TRUE;
\r
92 raw->sent_console_eof = TRUE;
\r
93 raw_check_close(raw);
\r
98 static int raw_receive(Plug plug, int urgent, char *data, int len)
\r
100 Raw raw = (Raw) plug;
\r
101 c_write(raw, data, len);
\r
102 /* We count 'session start', for proxy logging purposes, as being
\r
103 * when data is received from the network and printed. */
\r
104 raw->session_started = TRUE;
\r
108 static void raw_sent(Plug plug, int bufsize)
\r
110 Raw raw = (Raw) plug;
\r
111 raw->bufsize = bufsize;
\r
115 * Called to set up the raw connection.
\r
117 * Returns an error message, or NULL on success.
\r
119 * Also places the canonical host name into `realhost'. It must be
\r
120 * freed by the caller.
\r
122 static const char *raw_init(void *frontend_handle, void **backend_handle,
\r
124 const char *host, int port, char **realhost,
\r
125 int nodelay, int keepalive)
\r
127 static const struct plug_function_table fn_table = {
\r
139 raw = snew(struct raw_backend_data);
\r
140 raw->fn = &fn_table;
\r
142 raw->closed_on_socket_error = FALSE;
\r
143 *backend_handle = raw;
\r
144 raw->sent_console_eof = raw->sent_socket_eof = FALSE;
\r
146 raw->session_started = FALSE;
\r
147 raw->conf = conf_copy(conf);
\r
149 raw->frontend = frontend_handle;
\r
151 addressfamily = conf_get_int(conf, CONF_addressfamily);
\r
153 * Try to find host.
\r
155 addr = name_lookup(host, port, realhost, conf, addressfamily,
\r
156 raw->frontend, "main connection");
\r
157 if ((err = sk_addr_error(addr)) != NULL) {
\r
158 sk_addr_free(addr);
\r
163 port = 23; /* default telnet port */
\r
168 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
\r
170 if ((err = sk_socket_error(raw->s)) != NULL)
\r
173 loghost = conf_get_str(conf, CONF_loghost);
\r
178 *realhost = dupstr(loghost);
\r
180 colon = host_strrchr(*realhost, ':');
\r
188 static void raw_free(void *handle)
\r
190 Raw raw = (Raw) handle;
\r
194 conf_free(raw->conf);
\r
199 * Stub routine (we don't have any need to reconfigure this backend).
\r
201 static void raw_reconfig(void *handle, Conf *conf)
\r
206 * Called to send data down the raw connection.
\r
208 static int raw_send(void *handle, const char *buf, int len)
\r
210 Raw raw = (Raw) handle;
\r
212 if (raw->s == NULL)
\r
215 raw->bufsize = sk_write(raw->s, buf, len);
\r
217 return raw->bufsize;
\r
221 * Called to query the current socket sendability status.
\r
223 static int raw_sendbuffer(void *handle)
\r
225 Raw raw = (Raw) handle;
\r
226 return raw->bufsize;
\r
230 * Called to set the size of the window
\r
232 static void raw_size(void *handle, int width, int height)
\r
239 * Send raw special codes. We only handle outgoing EOF here.
\r
241 static void raw_special(void *handle, Telnet_Special code)
\r
243 Raw raw = (Raw) handle;
\r
244 if (code == TS_EOF && raw->s) {
\r
245 sk_write_eof(raw->s);
\r
246 raw->sent_socket_eof= TRUE;
\r
247 raw_check_close(raw);
\r
254 * Return a list of the special codes that make sense in this
\r
257 static const struct telnet_special *raw_get_specials(void *handle)
\r
262 static int raw_connected(void *handle)
\r
264 Raw raw = (Raw) handle;
\r
265 return raw->s != NULL;
\r
268 static int raw_sendok(void *handle)
\r
273 static void raw_unthrottle(void *handle, int backlog)
\r
275 Raw raw = (Raw) handle;
\r
276 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
\r
279 static int raw_ldisc(void *handle, int option)
\r
281 if (option == LD_EDIT || option == LD_ECHO)
\r
286 static void raw_provide_ldisc(void *handle, void *ldisc)
\r
288 /* This is a stub. */
\r
291 static void raw_provide_logctx(void *handle, void *logctx)
\r
293 /* This is a stub. */
\r
296 static int raw_exitcode(void *handle)
\r
298 Raw raw = (Raw) handle;
\r
299 if (raw->s != NULL)
\r
300 return -1; /* still connected */
\r
301 else if (raw->closed_on_socket_error)
\r
302 return INT_MAX; /* a socket error counts as an unclean exit */
\r
304 /* Exit codes are a meaningless concept in the Raw protocol */
\r
309 * cfg_info for Raw does nothing at all.
\r
311 static int raw_cfg_info(void *handle)
\r
316 Backend raw_backend = {
\r
330 raw_provide_logctx,
\r
333 NULL /* test_for_upstream */,
\r