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);
55 static void raw_check_close(Raw raw)
58 * Called after we send EOF on either the socket or the console.
59 * Its job is to wind up the session once we have sent EOF on both.
61 if (raw->sent_console_eof && raw->sent_socket_eof) {
65 notify_remote_exit(raw->frontend);
70 static int raw_closing(Plug plug, const char *error_msg, int error_code,
76 /* A socket error has occurred. */
80 raw->closed_on_socket_error = TRUE;
81 notify_remote_exit(raw->frontend);
83 logevent(raw->frontend, error_msg);
84 connection_fatal(raw->frontend, "%s", error_msg);
86 /* Otherwise, the remote side closed the connection normally. */
87 if (!raw->sent_console_eof && from_backend_eof(raw->frontend)) {
89 * The front end wants us to close the outgoing side of the
90 * connection as soon as we see EOF from the far end.
92 if (!raw->sent_socket_eof) {
95 raw->sent_socket_eof= TRUE;
98 raw->sent_console_eof = TRUE;
104 static int raw_receive(Plug plug, int urgent, char *data, int len)
106 Raw raw = (Raw) plug;
107 c_write(raw, data, len);
111 static void raw_sent(Plug plug, int bufsize)
113 Raw raw = (Raw) plug;
114 raw->bufsize = bufsize;
118 * Called to set up the raw connection.
120 * Returns an error message, or NULL on success.
122 * Also places the canonical host name into `realhost'. It must be
123 * freed by the caller.
125 static const char *raw_init(void *frontend_handle, void **backend_handle,
127 char *host, int port, char **realhost, int nodelay,
130 static const struct plug_function_table fn_table = {
142 raw = snew(struct raw_backend_data);
145 raw->closed_on_socket_error = FALSE;
146 *backend_handle = raw;
147 raw->sent_console_eof = raw->sent_socket_eof = FALSE;
149 raw->frontend = frontend_handle;
151 addressfamily = conf_get_int(conf, CONF_addressfamily);
157 buf = dupprintf("Looking up host \"%s\"%s", host,
158 (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
159 (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
161 logevent(raw->frontend, buf);
164 addr = name_lookup(host, port, realhost, conf, addressfamily);
165 if ((err = sk_addr_error(addr)) != NULL) {
171 port = 23; /* default telnet port */
176 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
178 if ((err = sk_socket_error(raw->s)) != NULL)
181 loghost = conf_get_str(conf, CONF_loghost);
186 *realhost = dupstr(loghost);
187 colon = strrchr(*realhost, ':');
190 * FIXME: if we ever update this aspect of ssh.c for
191 * IPv6 literal management, this should change in line
201 static void raw_free(void *handle)
203 Raw raw = (Raw) handle;
211 * Stub routine (we don't have any need to reconfigure this backend).
213 static void raw_reconfig(void *handle, Conf *conf)
218 * Called to send data down the raw connection.
220 static int raw_send(void *handle, char *buf, int len)
222 Raw raw = (Raw) handle;
227 raw->bufsize = sk_write(raw->s, buf, len);
233 * Called to query the current socket sendability status.
235 static int raw_sendbuffer(void *handle)
237 Raw raw = (Raw) handle;
242 * Called to set the size of the window
244 static void raw_size(void *handle, int width, int height)
251 * Send raw special codes. We only handle outgoing EOF here.
253 static void raw_special(void *handle, Telnet_Special code)
255 Raw raw = (Raw) handle;
256 if (code == TS_EOF && raw->s) {
257 sk_write_eof(raw->s);
258 raw->sent_socket_eof= TRUE;
259 raw_check_close(raw);
266 * Return a list of the special codes that make sense in this
269 static const struct telnet_special *raw_get_specials(void *handle)
274 static int raw_connected(void *handle)
276 Raw raw = (Raw) handle;
277 return raw->s != NULL;
280 static int raw_sendok(void *handle)
285 static void raw_unthrottle(void *handle, int backlog)
287 Raw raw = (Raw) handle;
288 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
291 static int raw_ldisc(void *handle, int option)
293 if (option == LD_EDIT || option == LD_ECHO)
298 static void raw_provide_ldisc(void *handle, void *ldisc)
300 /* This is a stub. */
303 static void raw_provide_logctx(void *handle, void *logctx)
305 /* This is a stub. */
308 static int raw_exitcode(void *handle)
310 Raw raw = (Raw) handle;
312 return -1; /* still connected */
313 else if (raw->closed_on_socket_error)
314 return INT_MAX; /* a socket error counts as an unclean exit */
316 /* Exit codes are a meaningless concept in the Raw protocol */
321 * cfg_info for Raw does nothing at all.
323 static int raw_cfg_info(void *handle)
328 Backend raw_backend = {