18 #define RLOGIN_MAX_BACKLOG 4096
20 typedef struct rlogin_tag {
21 const struct plug_function_table *fn;
22 /* the above field _must_ be first in the structure */
28 int term_width, term_height;
32 static void rlogin_size(void *handle, int width, int height);
34 static void c_write(Rlogin rlogin, char *buf, int len)
36 int backlog = from_backend(rlogin->frontend, 0, buf, len);
37 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
40 static void rlogin_log(Plug plug, int type, SockAddr addr, int port,
41 const char *error_msg, int error_code)
43 Rlogin rlogin = (Rlogin) plug;
44 char addrbuf[256], *msg;
46 sk_getaddr(addr, addrbuf, lenof(addrbuf));
49 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
51 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
53 logevent(rlogin->frontend, msg);
56 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
59 Rlogin rlogin = (Rlogin) plug;
63 notify_remote_exit(rlogin->frontend);
66 /* A socket error has occurred. */
67 logevent(rlogin->frontend, error_msg);
68 connection_fatal(rlogin->frontend, "%s", error_msg);
69 } /* Otherwise, the remote side closed the connection normally. */
73 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
75 Rlogin rlogin = (Rlogin) plug;
83 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
86 * We should flush everything (aka Telnet SYNCH) if we see
87 * 0x02, and we should turn off and on _local_ flow control
88 * on 0x10 and 0x20 respectively. I'm not convinced it's
93 * Main rlogin protocol. This is really simple: the first
94 * byte is expected to be NULL and is ignored, and the rest
97 if (rlogin->firstbyte) {
98 if (data[0] == '\0') {
102 rlogin->firstbyte = 0;
105 c_write(rlogin, data, len);
110 static void rlogin_sent(Plug plug, int bufsize)
112 Rlogin rlogin = (Rlogin) plug;
113 rlogin->bufsize = bufsize;
117 * Called to set up the rlogin connection.
119 * Returns an error message, or NULL on success.
121 * Also places the canonical host name into `realhost'. It must be
122 * freed by the caller.
124 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
126 char *host, int port, char **realhost,
127 int nodelay, int keepalive)
129 static const struct plug_function_table fn_table = {
139 rlogin = snew(struct rlogin_tag);
140 rlogin->fn = &fn_table;
142 rlogin->frontend = frontend_handle;
143 rlogin->term_width = cfg->width;
144 rlogin->term_height = cfg->height;
145 rlogin->firstbyte = 1;
147 *backend_handle = rlogin;
154 buf = dupprintf("Looking up host \"%s\"%s", host,
155 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
156 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
158 logevent(rlogin->frontend, buf);
161 addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily);
162 if ((err = sk_addr_error(addr)) != NULL) {
168 port = 513; /* default rlogin port */
173 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
174 nodelay, keepalive, (Plug) rlogin, cfg);
175 if ((err = sk_socket_error(rlogin->s)) != NULL)
179 * Send local username, remote username, terminal/speed
185 char ruser[sizeof(cfg->username)];
186 (void) get_remote_username(cfg, ruser, sizeof(ruser));
187 sk_write(rlogin->s, &z, 1);
188 sk_write(rlogin->s, cfg->localusername,
189 strlen(cfg->localusername));
190 sk_write(rlogin->s, &z, 1);
191 sk_write(rlogin->s, ruser,
193 sk_write(rlogin->s, &z, 1);
194 sk_write(rlogin->s, cfg->termtype,
195 strlen(cfg->termtype));
196 sk_write(rlogin->s, "/", 1);
197 for (p = cfg->termspeed; isdigit((unsigned char)*p); p++) continue;
198 sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed);
199 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
206 *realhost = dupstr(cfg->loghost);
207 colon = strrchr(*realhost, ':');
210 * FIXME: if we ever update this aspect of ssh.c for
211 * IPv6 literal management, this should change in line
221 static void rlogin_free(void *handle)
223 Rlogin rlogin = (Rlogin) handle;
231 * Stub routine (we don't have any need to reconfigure this backend).
233 static void rlogin_reconfig(void *handle, Config *cfg)
238 * Called to send data down the rlogin connection.
240 static int rlogin_send(void *handle, char *buf, int len)
242 Rlogin rlogin = (Rlogin) handle;
244 if (rlogin->s == NULL)
247 rlogin->bufsize = sk_write(rlogin->s, buf, len);
249 return rlogin->bufsize;
253 * Called to query the current socket sendability status.
255 static int rlogin_sendbuffer(void *handle)
257 Rlogin rlogin = (Rlogin) handle;
258 return rlogin->bufsize;
262 * Called to set the size of the window
264 static void rlogin_size(void *handle, int width, int height)
266 Rlogin rlogin = (Rlogin) handle;
267 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
269 rlogin->term_width = width;
270 rlogin->term_height = height;
272 if (rlogin->s == NULL || !rlogin->cansize)
275 b[6] = rlogin->term_width >> 8;
276 b[7] = rlogin->term_width & 0xFF;
277 b[4] = rlogin->term_height >> 8;
278 b[5] = rlogin->term_height & 0xFF;
279 rlogin->bufsize = sk_write(rlogin->s, b, 12);
284 * Send rlogin special codes.
286 static void rlogin_special(void *handle, Telnet_Special code)
293 * Return a list of the special codes that make sense in this
296 static const struct telnet_special *rlogin_get_specials(void *handle)
301 static int rlogin_connected(void *handle)
303 Rlogin rlogin = (Rlogin) handle;
304 return rlogin->s != NULL;
307 static int rlogin_sendok(void *handle)
309 /* Rlogin rlogin = (Rlogin) handle; */
313 static void rlogin_unthrottle(void *handle, int backlog)
315 Rlogin rlogin = (Rlogin) handle;
316 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
319 static int rlogin_ldisc(void *handle, int option)
321 /* Rlogin rlogin = (Rlogin) handle; */
325 static void rlogin_provide_ldisc(void *handle, void *ldisc)
327 /* This is a stub. */
330 static void rlogin_provide_logctx(void *handle, void *logctx)
332 /* This is a stub. */
335 static int rlogin_exitcode(void *handle)
337 Rlogin rlogin = (Rlogin) handle;
338 if (rlogin->s != NULL)
339 return -1; /* still connected */
341 /* If we ever implement RSH, we'll probably need to do this properly */
346 * cfg_info for rlogin does nothing at all.
348 static int rlogin_cfg_info(void *handle)
353 Backend rlogin_backend = {
366 rlogin_provide_ldisc,
367 rlogin_provide_logctx,