14 #define RLOGIN_MAX_BACKLOG 4096
16 typedef struct rlogin_tag {
17 const struct plug_function_table *fn;
18 /* the above field _must_ be first in the structure */
23 int term_width, term_height;
27 static void rlogin_size(void *handle, int width, int height);
29 static void c_write(Rlogin rlogin, char *buf, int len)
31 int backlog = from_backend(rlogin->frontend, 0, buf, len);
32 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
35 static void rlogin_log(Plug plug, int type, SockAddr addr, int port,
36 const char *error_msg, int error_code)
38 Rlogin rlogin = (Rlogin) plug;
39 char addrbuf[256], *msg;
41 sk_getaddr(addr, addrbuf, lenof(addrbuf));
44 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
46 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
48 logevent(rlogin->frontend, msg);
51 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
54 Rlogin rlogin = (Rlogin) plug;
58 notify_remote_exit(rlogin->frontend);
61 /* A socket error has occurred. */
62 logevent(rlogin->frontend, error_msg);
63 connection_fatal(rlogin->frontend, "%s", error_msg);
64 } /* Otherwise, the remote side closed the connection normally. */
68 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
70 Rlogin rlogin = (Rlogin) plug;
77 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
79 * We should flush everything (aka Telnet SYNCH) if we see
80 * 0x02, and we should turn off and on _local_ flow control
81 * on 0x10 and 0x20 respectively. I'm not convinced it's
86 * Main rlogin protocol. This is really simple: the first
87 * byte is expected to be NULL and is ignored, and the rest
90 if (rlogin->firstbyte) {
91 if (data[0] == '\0') {
95 rlogin->firstbyte = 0;
98 c_write(rlogin, data, len);
103 static void rlogin_sent(Plug plug, int bufsize)
105 Rlogin rlogin = (Rlogin) plug;
106 rlogin->bufsize = bufsize;
110 * Called to set up the rlogin 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 *rlogin_init(void *frontend_handle, void **backend_handle,
119 char *host, int port, char **realhost,
120 int nodelay, int keepalive)
122 static const struct plug_function_table fn_table = {
132 rlogin = snew(struct rlogin_tag);
133 rlogin->fn = &fn_table;
135 rlogin->frontend = frontend_handle;
136 rlogin->term_width = cfg->width;
137 rlogin->term_height = cfg->height;
138 rlogin->firstbyte = 1;
139 *backend_handle = rlogin;
146 buf = dupprintf("Looking up host \"%s\"%s", host,
147 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
148 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
150 logevent(rlogin->frontend, buf);
153 addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily);
154 if ((err = sk_addr_error(addr)) != NULL) {
160 port = 513; /* default rlogin port */
165 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
166 nodelay, keepalive, (Plug) rlogin, cfg);
167 if ((err = sk_socket_error(rlogin->s)) != NULL)
171 * Send local username, remote username, terminal/speed
177 sk_write(rlogin->s, &z, 1);
178 sk_write(rlogin->s, cfg->localusername,
179 strlen(cfg->localusername));
180 sk_write(rlogin->s, &z, 1);
181 sk_write(rlogin->s, cfg->username,
182 strlen(cfg->username));
183 sk_write(rlogin->s, &z, 1);
184 sk_write(rlogin->s, cfg->termtype,
185 strlen(cfg->termtype));
186 sk_write(rlogin->s, "/", 1);
187 for (p = cfg->termspeed; isdigit((unsigned char)*p); p++) continue;
188 sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed);
189 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
195 static void rlogin_free(void *handle)
197 Rlogin rlogin = (Rlogin) handle;
205 * Stub routine (we don't have any need to reconfigure this backend).
207 static void rlogin_reconfig(void *handle, Config *cfg)
212 * Called to send data down the rlogin connection.
214 static int rlogin_send(void *handle, char *buf, int len)
216 Rlogin rlogin = (Rlogin) handle;
218 if (rlogin->s == NULL)
221 rlogin->bufsize = sk_write(rlogin->s, buf, len);
223 return rlogin->bufsize;
227 * Called to query the current socket sendability status.
229 static int rlogin_sendbuffer(void *handle)
231 Rlogin rlogin = (Rlogin) handle;
232 return rlogin->bufsize;
236 * Called to set the size of the window
238 static void rlogin_size(void *handle, int width, int height)
240 Rlogin rlogin = (Rlogin) handle;
241 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
243 rlogin->term_width = width;
244 rlogin->term_height = height;
246 if (rlogin->s == NULL)
249 b[6] = rlogin->term_width >> 8;
250 b[7] = rlogin->term_width & 0xFF;
251 b[4] = rlogin->term_height >> 8;
252 b[5] = rlogin->term_height & 0xFF;
253 rlogin->bufsize = sk_write(rlogin->s, b, 12);
258 * Send rlogin special codes.
260 static void rlogin_special(void *handle, Telnet_Special code)
267 * Return a list of the special codes that make sense in this
270 static const struct telnet_special *rlogin_get_specials(void *handle)
275 static Socket rlogin_socket(void *handle)
277 Rlogin rlogin = (Rlogin) handle;
281 static int rlogin_sendok(void *handle)
283 /* Rlogin rlogin = (Rlogin) handle; */
287 static void rlogin_unthrottle(void *handle, int backlog)
289 Rlogin rlogin = (Rlogin) handle;
290 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
293 static int rlogin_ldisc(void *handle, int option)
295 /* Rlogin rlogin = (Rlogin) handle; */
299 static void rlogin_provide_ldisc(void *handle, void *ldisc)
301 /* This is a stub. */
304 static void rlogin_provide_logctx(void *handle, void *logctx)
306 /* This is a stub. */
309 static int rlogin_exitcode(void *handle)
311 Rlogin rlogin = (Rlogin) handle;
312 if (rlogin->s != NULL)
313 return -1; /* still connected */
315 /* If we ever implement RSH, we'll probably need to do this properly */
320 * cfg_info for rlogin does nothing at all.
322 static int rlogin_cfg_info(void *handle)
327 Backend rlogin_backend = {
340 rlogin_provide_ldisc,
341 rlogin_provide_logctx,