17 #define RAW_MAX_BACKLOG 4096
19 typedef struct raw_backend_data {
20 const struct plug_function_table *fn;
21 /* the above field _must_ be first in the structure */
26 int sent_console_eof, sent_socket_eof;
29 static void raw_size(void *handle, int width, int height);
31 static void c_write(Raw raw, char *buf, int len)
33 int backlog = from_backend(raw->frontend, 0, buf, len);
34 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
37 static void raw_log(Plug plug, int type, SockAddr addr, int port,
38 const char *error_msg, int error_code)
41 char addrbuf[256], *msg;
43 sk_getaddr(addr, addrbuf, lenof(addrbuf));
46 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
48 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
50 logevent(raw->frontend, msg);
53 static void raw_check_close(Raw raw)
56 * Called after we send EOF on either the socket or the console.
57 * Its job is to wind up the session once we have sent EOF on both.
59 if (raw->sent_console_eof && raw->sent_socket_eof) {
63 notify_remote_exit(raw->frontend);
68 static int raw_closing(Plug plug, const char *error_msg, int error_code,
74 /* A socket error has occurred. */
78 notify_remote_exit(raw->frontend);
80 logevent(raw->frontend, error_msg);
81 connection_fatal(raw->frontend, "%s", error_msg);
83 /* Otherwise, the remote side closed the connection normally. */
84 if (!raw->sent_console_eof && from_backend_eof(raw->frontend)) {
86 * The front end wants us to close the outgoing side of the
87 * connection as soon as we see EOF from the far end.
89 if (!raw->sent_socket_eof) {
92 raw->sent_socket_eof= TRUE;
95 raw->sent_console_eof = TRUE;
101 static int raw_receive(Plug plug, int urgent, char *data, int len)
103 Raw raw = (Raw) plug;
104 c_write(raw, data, len);
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 char *host, int port, char **realhost, int nodelay,
127 static const struct plug_function_table fn_table = {
139 raw = snew(struct raw_backend_data);
142 *backend_handle = raw;
143 raw->sent_console_eof = raw->sent_socket_eof = FALSE;
145 raw->frontend = frontend_handle;
147 addressfamily = conf_get_int(conf, CONF_addressfamily);
153 buf = dupprintf("Looking up host \"%s\"%s", host,
154 (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
155 (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
157 logevent(raw->frontend, buf);
160 addr = name_lookup(host, port, realhost, conf, addressfamily);
161 if ((err = sk_addr_error(addr)) != NULL) {
167 port = 23; /* default telnet port */
172 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
174 if ((err = sk_socket_error(raw->s)) != NULL)
177 loghost = conf_get_str(conf, CONF_loghost);
182 *realhost = dupstr(loghost);
183 colon = strrchr(*realhost, ':');
186 * FIXME: if we ever update this aspect of ssh.c for
187 * IPv6 literal management, this should change in line
197 static void raw_free(void *handle)
199 Raw raw = (Raw) handle;
207 * Stub routine (we don't have any need to reconfigure this backend).
209 static void raw_reconfig(void *handle, Conf *conf)
214 * Called to send data down the raw connection.
216 static int raw_send(void *handle, char *buf, int len)
218 Raw raw = (Raw) handle;
223 raw->bufsize = sk_write(raw->s, buf, len);
229 * Called to query the current socket sendability status.
231 static int raw_sendbuffer(void *handle)
233 Raw raw = (Raw) handle;
238 * Called to set the size of the window
240 static void raw_size(void *handle, int width, int height)
247 * Send raw special codes. We only handle outgoing EOF here.
249 static void raw_special(void *handle, Telnet_Special code)
251 Raw raw = (Raw) handle;
252 if (code == TS_EOF && raw->s) {
253 sk_write_eof(raw->s);
254 raw->sent_socket_eof= TRUE;
255 raw_check_close(raw);
262 * Return a list of the special codes that make sense in this
265 static const struct telnet_special *raw_get_specials(void *handle)
270 static int raw_connected(void *handle)
272 Raw raw = (Raw) handle;
273 return raw->s != NULL;
276 static int raw_sendok(void *handle)
281 static void raw_unthrottle(void *handle, int backlog)
283 Raw raw = (Raw) handle;
284 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
287 static int raw_ldisc(void *handle, int option)
289 if (option == LD_EDIT || option == LD_ECHO)
294 static void raw_provide_ldisc(void *handle, void *ldisc)
296 /* This is a stub. */
299 static void raw_provide_logctx(void *handle, void *logctx)
301 /* This is a stub. */
304 static int raw_exitcode(void *handle)
306 Raw raw = (Raw) handle;
308 return -1; /* still connected */
310 /* Exit codes are a meaningless concept in the Raw protocol */
315 * cfg_info for Raw does nothing at all.
317 static int raw_cfg_info(void *handle)
322 Backend raw_backend = {