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