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 int rlogin_closing(Plug plug, const char *error_msg, int error_code,
38 Rlogin rlogin = (Rlogin) plug;
42 notify_remote_exit(rlogin->frontend);
45 /* A socket error has occurred. */
46 logevent(rlogin->frontend, error_msg);
47 connection_fatal(rlogin->frontend, "%s", error_msg);
48 } /* Otherwise, the remote side closed the connection normally. */
52 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
54 Rlogin rlogin = (Rlogin) plug;
61 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
63 * We should flush everything (aka Telnet SYNCH) if we see
64 * 0x02, and we should turn off and on _local_ flow control
65 * on 0x10 and 0x20 respectively. I'm not convinced it's
70 * Main rlogin protocol. This is really simple: the first
71 * byte is expected to be NULL and is ignored, and the rest
74 if (rlogin->firstbyte) {
75 if (data[0] == '\0') {
79 rlogin->firstbyte = 0;
82 c_write(rlogin, data, len);
87 static void rlogin_sent(Plug plug, int bufsize)
89 Rlogin rlogin = (Rlogin) plug;
90 rlogin->bufsize = bufsize;
94 * Called to set up the rlogin connection.
96 * Returns an error message, or NULL on success.
98 * Also places the canonical host name into `realhost'. It must be
99 * freed by the caller.
101 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
103 char *host, int port, char **realhost,
104 int nodelay, int keepalive)
106 static const struct plug_function_table fn_table = {
115 rlogin = snew(struct rlogin_tag);
116 rlogin->fn = &fn_table;
118 rlogin->frontend = frontend_handle;
119 rlogin->term_width = cfg->width;
120 rlogin->term_height = cfg->height;
121 rlogin->firstbyte = 1;
122 *backend_handle = rlogin;
129 buf = dupprintf("Looking up host \"%s\"", host);
130 logevent(rlogin->frontend, buf);
133 addr = name_lookup(host, port, realhost, cfg);
134 if ((err = sk_addr_error(addr)) != NULL) {
140 port = 513; /* default rlogin port */
146 char *buf, addrbuf[100];
147 sk_getaddr(addr, addrbuf, 100);
148 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
149 logevent(rlogin->frontend, buf);
152 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
153 nodelay, keepalive, (Plug) rlogin, cfg);
154 if ((err = sk_socket_error(rlogin->s)) != NULL)
158 * Send local username, remote username, terminal/speed
164 sk_write(rlogin->s, &z, 1);
165 sk_write(rlogin->s, cfg->localusername,
166 strlen(cfg->localusername));
167 sk_write(rlogin->s, &z, 1);
168 sk_write(rlogin->s, cfg->username,
169 strlen(cfg->username));
170 sk_write(rlogin->s, &z, 1);
171 sk_write(rlogin->s, cfg->termtype,
172 strlen(cfg->termtype));
173 sk_write(rlogin->s, "/", 1);
174 for (p = cfg->termspeed; isdigit((unsigned char)*p); p++) continue;
175 sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed);
176 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
182 static void rlogin_free(void *handle)
184 Rlogin rlogin = (Rlogin) handle;
192 * Stub routine (we don't have any need to reconfigure this backend).
194 static void rlogin_reconfig(void *handle, Config *cfg)
199 * Called to send data down the rlogin connection.
201 static int rlogin_send(void *handle, char *buf, int len)
203 Rlogin rlogin = (Rlogin) handle;
205 if (rlogin->s == NULL)
208 rlogin->bufsize = sk_write(rlogin->s, buf, len);
210 return rlogin->bufsize;
214 * Called to query the current socket sendability status.
216 static int rlogin_sendbuffer(void *handle)
218 Rlogin rlogin = (Rlogin) handle;
219 return rlogin->bufsize;
223 * Called to set the size of the window
225 static void rlogin_size(void *handle, int width, int height)
227 Rlogin rlogin = (Rlogin) handle;
228 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
230 rlogin->term_width = width;
231 rlogin->term_height = height;
233 if (rlogin->s == NULL)
236 b[6] = rlogin->term_width >> 8;
237 b[7] = rlogin->term_width & 0xFF;
238 b[4] = rlogin->term_height >> 8;
239 b[5] = rlogin->term_height & 0xFF;
240 rlogin->bufsize = sk_write(rlogin->s, b, 12);
245 * Send rlogin special codes.
247 static void rlogin_special(void *handle, Telnet_Special code)
254 * Return a list of the special codes that make sense in this
257 static const struct telnet_special *rlogin_get_specials(void *handle)
262 static Socket rlogin_socket(void *handle)
264 Rlogin rlogin = (Rlogin) handle;
268 static int rlogin_sendok(void *handle)
270 /* Rlogin rlogin = (Rlogin) handle; */
274 static void rlogin_unthrottle(void *handle, int backlog)
276 Rlogin rlogin = (Rlogin) handle;
277 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
280 static int rlogin_ldisc(void *handle, int option)
282 /* Rlogin rlogin = (Rlogin) handle; */
286 static void rlogin_provide_ldisc(void *handle, void *ldisc)
288 /* This is a stub. */
291 static void rlogin_provide_logctx(void *handle, void *logctx)
293 /* This is a stub. */
296 static int rlogin_exitcode(void *handle)
298 Rlogin rlogin = (Rlogin) handle;
299 if (rlogin->s != NULL)
300 return -1; /* still connected */
302 /* If we ever implement RSH, we'll probably need to do this properly */
306 Backend rlogin_backend = {
319 rlogin_provide_ldisc,
320 rlogin_provide_logctx,