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;
31 static void raw_size(void *handle, int width, int height);
33 static void c_write(Raw raw, char *buf, int len)
35 int backlog = from_backend(raw->frontend, 0, buf, len);
36 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
39 static void raw_log(Plug plug, int type, SockAddr addr, int port,
40 const char *error_msg, int error_code)
43 backend_socket_log(raw->frontend, type, addr, port,
44 error_msg, error_code);
47 static void raw_check_close(Raw raw)
50 * Called after we send EOF on either the socket or the console.
51 * Its job is to wind up the session once we have sent EOF on both.
53 if (raw->sent_console_eof && raw->sent_socket_eof) {
57 notify_remote_exit(raw->frontend);
62 static int raw_closing(Plug plug, const char *error_msg, int error_code,
68 /* A socket error has occurred. */
72 raw->closed_on_socket_error = TRUE;
73 notify_remote_exit(raw->frontend);
75 logevent(raw->frontend, error_msg);
76 connection_fatal(raw->frontend, "%s", error_msg);
78 /* Otherwise, the remote side closed the connection normally. */
79 if (!raw->sent_console_eof && from_backend_eof(raw->frontend)) {
81 * The front end wants us to close the outgoing side of the
82 * connection as soon as we see EOF from the far end.
84 if (!raw->sent_socket_eof) {
87 raw->sent_socket_eof= TRUE;
90 raw->sent_console_eof = TRUE;
96 static int raw_receive(Plug plug, int urgent, char *data, int len)
99 c_write(raw, data, len);
103 static void raw_sent(Plug plug, int bufsize)
105 Raw raw = (Raw) plug;
106 raw->bufsize = bufsize;
110 * Called to set up the raw connection.
112 * Returns an error message, or NULL on success.
114 * Also places the canonical host name into `realhost'. It must be
115 * freed by the caller.
117 static const char *raw_init(void *frontend_handle, void **backend_handle,
119 const char *host, int port, char **realhost,
120 int nodelay, int keepalive)
122 static const struct plug_function_table fn_table = {
134 raw = snew(struct raw_backend_data);
137 raw->closed_on_socket_error = FALSE;
138 *backend_handle = raw;
139 raw->sent_console_eof = raw->sent_socket_eof = FALSE;
142 raw->frontend = frontend_handle;
144 addressfamily = conf_get_int(conf, CONF_addressfamily);
148 addr = name_lookup(host, port, realhost, conf, addressfamily,
149 raw->frontend, "main connection");
150 if ((err = sk_addr_error(addr)) != NULL) {
156 port = 23; /* default telnet port */
161 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
163 if ((err = sk_socket_error(raw->s)) != NULL)
166 loghost = conf_get_str(conf, CONF_loghost);
171 *realhost = dupstr(loghost);
173 colon = host_strrchr(*realhost, ':');
181 static void raw_free(void *handle)
183 Raw raw = (Raw) handle;
191 * Stub routine (we don't have any need to reconfigure this backend).
193 static void raw_reconfig(void *handle, Conf *conf)
198 * Called to send data down the raw connection.
200 static int raw_send(void *handle, const char *buf, int len)
202 Raw raw = (Raw) handle;
207 raw->bufsize = sk_write(raw->s, buf, len);
213 * Called to query the current socket sendability status.
215 static int raw_sendbuffer(void *handle)
217 Raw raw = (Raw) handle;
222 * Called to set the size of the window
224 static void raw_size(void *handle, int width, int height)
231 * Send raw special codes. We only handle outgoing EOF here.
233 static void raw_special(void *handle, Telnet_Special code)
235 Raw raw = (Raw) handle;
236 if (code == TS_EOF && raw->s) {
237 sk_write_eof(raw->s);
238 raw->sent_socket_eof= TRUE;
239 raw_check_close(raw);
246 * Return a list of the special codes that make sense in this
249 static const struct telnet_special *raw_get_specials(void *handle)
254 static int raw_connected(void *handle)
256 Raw raw = (Raw) handle;
257 return raw->s != NULL;
260 static int raw_sendok(void *handle)
265 static void raw_unthrottle(void *handle, int backlog)
267 Raw raw = (Raw) handle;
268 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
271 static int raw_ldisc(void *handle, int option)
273 if (option == LD_EDIT || option == LD_ECHO)
278 static void raw_provide_ldisc(void *handle, void *ldisc)
280 /* This is a stub. */
283 static void raw_provide_logctx(void *handle, void *logctx)
285 /* This is a stub. */
288 static int raw_exitcode(void *handle)
290 Raw raw = (Raw) handle;
292 return -1; /* still connected */
293 else if (raw->closed_on_socket_error)
294 return INT_MAX; /* a socket error counts as an unclean exit */
296 /* Exit codes are a meaningless concept in the Raw protocol */
301 * cfg_info for Raw does nothing at all.
303 static int raw_cfg_info(void *handle)
308 Backend raw_backend = {
325 NULL /* test_for_upstream */,