18 #define RAW_MAX_BACKLOG 4096
\r
20 typedef struct raw_backend_data {
\r
21 const struct plug_function_table *fn;
\r
22 /* the above field _must_ be first in the structure */
\r
25 int closed_on_socket_error;
\r
28 int sent_console_eof, sent_socket_eof;
\r
31 static void raw_size(void *handle, int width, int height);
\r
33 static void c_write(Raw raw, char *buf, int len)
\r
35 int backlog = from_backend(raw->frontend, 0, buf, len);
\r
36 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
\r
39 static void raw_log(Plug plug, int type, SockAddr addr, int port,
\r
40 const char *error_msg, int error_code)
\r
42 Raw raw = (Raw) plug;
\r
43 char addrbuf[256], *msg;
\r
45 sk_getaddr(addr, addrbuf, lenof(addrbuf));
\r
48 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
\r
50 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
\r
52 logevent(raw->frontend, msg);
\r
56 static void raw_check_close(Raw raw)
\r
59 * Called after we send EOF on either the socket or the console.
\r
60 * Its job is to wind up the session once we have sent EOF on both.
\r
62 if (raw->sent_console_eof && raw->sent_socket_eof) {
\r
66 notify_remote_exit(raw->frontend);
\r
71 static int raw_closing(Plug plug, const char *error_msg, int error_code,
\r
74 Raw raw = (Raw) plug;
\r
77 /* A socket error has occurred. */
\r
81 raw->closed_on_socket_error = TRUE;
\r
82 notify_remote_exit(raw->frontend);
\r
84 logevent(raw->frontend, error_msg);
\r
85 connection_fatal(raw->frontend, "%s", error_msg);
\r
87 /* Otherwise, the remote side closed the connection normally. */
\r
88 if (!raw->sent_console_eof && from_backend_eof(raw->frontend)) {
\r
90 * The front end wants us to close the outgoing side of the
\r
91 * connection as soon as we see EOF from the far end.
\r
93 if (!raw->sent_socket_eof) {
\r
95 sk_write_eof(raw->s);
\r
96 raw->sent_socket_eof= TRUE;
\r
99 raw->sent_console_eof = TRUE;
\r
100 raw_check_close(raw);
\r
105 static int raw_receive(Plug plug, int urgent, char *data, int len)
\r
107 Raw raw = (Raw) plug;
\r
108 c_write(raw, data, len);
\r
112 static void raw_sent(Plug plug, int bufsize)
\r
114 Raw raw = (Raw) plug;
\r
115 raw->bufsize = bufsize;
\r
119 * Called to set up the raw connection.
\r
121 * Returns an error message, or NULL on success.
\r
123 * Also places the canonical host name into `realhost'. It must be
\r
124 * freed by the caller.
\r
126 static const char *raw_init(void *frontend_handle, void **backend_handle,
\r
128 char *host, int port, char **realhost, int nodelay,
\r
131 static const struct plug_function_table fn_table = {
\r
143 raw = snew(struct raw_backend_data);
\r
144 raw->fn = &fn_table;
\r
146 raw->closed_on_socket_error = FALSE;
\r
147 *backend_handle = raw;
\r
148 raw->sent_console_eof = raw->sent_socket_eof = FALSE;
\r
151 raw->frontend = frontend_handle;
\r
153 addressfamily = conf_get_int(conf, CONF_addressfamily);
\r
155 * Try to find host.
\r
159 buf = dupprintf("Looking up host \"%s\"%s", host,
\r
160 (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
\r
161 (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
\r
163 logevent(raw->frontend, buf);
\r
166 addr = name_lookup(host, port, realhost, conf, addressfamily);
\r
167 if ((err = sk_addr_error(addr)) != NULL) {
\r
168 sk_addr_free(addr);
\r
173 port = 23; /* default telnet port */
\r
178 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
\r
180 if ((err = sk_socket_error(raw->s)) != NULL)
\r
183 loghost = conf_get_str(conf, CONF_loghost);
\r
188 *realhost = dupstr(loghost);
\r
190 colon = host_strrchr(*realhost, ':');
\r
198 static void raw_free(void *handle)
\r
200 Raw raw = (Raw) handle;
\r
208 * Stub routine (we don't have any need to reconfigure this backend).
\r
210 static void raw_reconfig(void *handle, Conf *conf)
\r
215 * Called to send data down the raw connection.
\r
217 static int raw_send(void *handle, char *buf, int len)
\r
219 Raw raw = (Raw) handle;
\r
221 if (raw->s == NULL)
\r
224 raw->bufsize = sk_write(raw->s, buf, len);
\r
226 return raw->bufsize;
\r
230 * Called to query the current socket sendability status.
\r
232 static int raw_sendbuffer(void *handle)
\r
234 Raw raw = (Raw) handle;
\r
235 return raw->bufsize;
\r
239 * Called to set the size of the window
\r
241 static void raw_size(void *handle, int width, int height)
\r
248 * Send raw special codes. We only handle outgoing EOF here.
\r
250 static void raw_special(void *handle, Telnet_Special code)
\r
252 Raw raw = (Raw) handle;
\r
253 if (code == TS_EOF && raw->s) {
\r
254 sk_write_eof(raw->s);
\r
255 raw->sent_socket_eof= TRUE;
\r
256 raw_check_close(raw);
\r
263 * Return a list of the special codes that make sense in this
\r
266 static const struct telnet_special *raw_get_specials(void *handle)
\r
271 static int raw_connected(void *handle)
\r
273 Raw raw = (Raw) handle;
\r
274 return raw->s != NULL;
\r
277 static int raw_sendok(void *handle)
\r
282 static void raw_unthrottle(void *handle, int backlog)
\r
284 Raw raw = (Raw) handle;
\r
285 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
\r
288 static int raw_ldisc(void *handle, int option)
\r
290 if (option == LD_EDIT || option == LD_ECHO)
\r
295 static void raw_provide_ldisc(void *handle, void *ldisc)
\r
297 /* This is a stub. */
\r
300 static void raw_provide_logctx(void *handle, void *logctx)
\r
302 /* This is a stub. */
\r
305 static int raw_exitcode(void *handle)
\r
307 Raw raw = (Raw) handle;
\r
308 if (raw->s != NULL)
\r
309 return -1; /* still connected */
\r
310 else if (raw->closed_on_socket_error)
\r
311 return INT_MAX; /* a socket error counts as an unclean exit */
\r
313 /* Exit codes are a meaningless concept in the Raw protocol */
\r
318 * cfg_info for Raw does nothing at all.
\r
320 static int raw_cfg_info(void *handle)
\r
325 Backend raw_backend = {
\r
339 raw_provide_logctx,
\r