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 char addrbuf[256], *msg;
45 sk_getaddr(addr, addrbuf, lenof(addrbuf));
48 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
50 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
52 logevent(raw->frontend, msg);
56 static void raw_check_close(Raw raw)
59 * Called after we send EOF on either the socket or the console.
60 * Its job is to wind up the session once we have sent EOF on both.
62 if (raw->sent_console_eof && raw->sent_socket_eof) {
66 notify_remote_exit(raw->frontend);
71 static int raw_closing(Plug plug, const char *error_msg, int error_code,
77 /* A socket error has occurred. */
81 raw->closed_on_socket_error = TRUE;
82 notify_remote_exit(raw->frontend);
84 logevent(raw->frontend, error_msg);
85 connection_fatal(raw->frontend, "%s", error_msg);
87 /* Otherwise, the remote side closed the connection normally. */
88 if (!raw->sent_console_eof && from_backend_eof(raw->frontend)) {
90 * The front end wants us to close the outgoing side of the
91 * connection as soon as we see EOF from the far end.
93 if (!raw->sent_socket_eof) {
96 raw->sent_socket_eof= TRUE;
99 raw->sent_console_eof = TRUE;
100 raw_check_close(raw);
105 static int raw_receive(Plug plug, int urgent, char *data, int len)
107 Raw raw = (Raw) plug;
108 c_write(raw, data, len);
112 static void raw_sent(Plug plug, int bufsize)
114 Raw raw = (Raw) plug;
115 raw->bufsize = bufsize;
119 * Called to set up the raw connection.
121 * Returns an error message, or NULL on success.
123 * Also places the canonical host name into `realhost'. It must be
124 * freed by the caller.
126 static const char *raw_init(void *frontend_handle, void **backend_handle,
128 const char *host, int port, char **realhost,
129 int nodelay, int keepalive)
131 static const struct plug_function_table fn_table = {
143 raw = snew(struct raw_backend_data);
146 raw->closed_on_socket_error = FALSE;
147 *backend_handle = raw;
148 raw->sent_console_eof = raw->sent_socket_eof = FALSE;
151 raw->frontend = frontend_handle;
153 addressfamily = conf_get_int(conf, CONF_addressfamily);
157 addr = name_lookup(host, port, realhost, conf, addressfamily,
158 raw->frontend, "main connection");
159 if ((err = sk_addr_error(addr)) != NULL) {
165 port = 23; /* default telnet port */
170 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
172 if ((err = sk_socket_error(raw->s)) != NULL)
175 loghost = conf_get_str(conf, CONF_loghost);
180 *realhost = dupstr(loghost);
182 colon = host_strrchr(*realhost, ':');
190 static void raw_free(void *handle)
192 Raw raw = (Raw) handle;
200 * Stub routine (we don't have any need to reconfigure this backend).
202 static void raw_reconfig(void *handle, Conf *conf)
207 * Called to send data down the raw connection.
209 static int raw_send(void *handle, const char *buf, int len)
211 Raw raw = (Raw) handle;
216 raw->bufsize = sk_write(raw->s, buf, len);
222 * Called to query the current socket sendability status.
224 static int raw_sendbuffer(void *handle)
226 Raw raw = (Raw) handle;
231 * Called to set the size of the window
233 static void raw_size(void *handle, int width, int height)
240 * Send raw special codes. We only handle outgoing EOF here.
242 static void raw_special(void *handle, Telnet_Special code)
244 Raw raw = (Raw) handle;
245 if (code == TS_EOF && raw->s) {
246 sk_write_eof(raw->s);
247 raw->sent_socket_eof= TRUE;
248 raw_check_close(raw);
255 * Return a list of the special codes that make sense in this
258 static const struct telnet_special *raw_get_specials(void *handle)
263 static int raw_connected(void *handle)
265 Raw raw = (Raw) handle;
266 return raw->s != NULL;
269 static int raw_sendok(void *handle)
274 static void raw_unthrottle(void *handle, int backlog)
276 Raw raw = (Raw) handle;
277 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
280 static int raw_ldisc(void *handle, int option)
282 if (option == LD_EDIT || option == LD_ECHO)
287 static void raw_provide_ldisc(void *handle, void *ldisc)
289 /* This is a stub. */
292 static void raw_provide_logctx(void *handle, void *logctx)
294 /* This is a stub. */
297 static int raw_exitcode(void *handle)
299 Raw raw = (Raw) handle;
301 return -1; /* still connected */
302 else if (raw->closed_on_socket_error)
303 return INT_MAX; /* a socket error counts as an unclean exit */
305 /* Exit codes are a meaningless concept in the Raw protocol */
310 * cfg_info for Raw does nothing at all.
312 static int raw_cfg_info(void *handle)
317 Backend raw_backend = {
334 NULL /* test_for_upstream */,