18 #define RLOGIN_MAX_BACKLOG 4096
\r
20 typedef struct rlogin_tag {
\r
21 const struct plug_function_table *fn;
\r
22 /* the above field _must_ be first in the structure */
\r
28 int term_width, term_height;
\r
32 static void rlogin_size(void *handle, int width, int height);
\r
34 static void c_write(Rlogin rlogin, char *buf, int len)
\r
36 int backlog = from_backend(rlogin->frontend, 0, buf, len);
\r
37 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
\r
40 static void rlogin_log(Plug plug, int type, SockAddr addr, int port,
\r
41 const char *error_msg, int error_code)
\r
43 Rlogin rlogin = (Rlogin) plug;
\r
44 char addrbuf[256], *msg;
\r
46 sk_getaddr(addr, addrbuf, lenof(addrbuf));
\r
49 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
\r
51 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
\r
53 logevent(rlogin->frontend, msg);
\r
56 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
\r
59 Rlogin rlogin = (Rlogin) plug;
\r
61 sk_close(rlogin->s);
\r
63 notify_remote_exit(rlogin->frontend);
\r
66 /* A socket error has occurred. */
\r
67 logevent(rlogin->frontend, error_msg);
\r
68 connection_fatal(rlogin->frontend, "%s", error_msg);
\r
69 } /* Otherwise, the remote side closed the connection normally. */
\r
73 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
\r
75 Rlogin rlogin = (Rlogin) plug;
\r
82 rlogin->cansize = 1;
\r
83 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
\r
86 * We should flush everything (aka Telnet SYNCH) if we see
\r
87 * 0x02, and we should turn off and on _local_ flow control
\r
88 * on 0x10 and 0x20 respectively. I'm not convinced it's
\r
93 * Main rlogin protocol. This is really simple: the first
\r
94 * byte is expected to be NULL and is ignored, and the rest
\r
97 if (rlogin->firstbyte) {
\r
98 if (data[0] == '\0') {
\r
102 rlogin->firstbyte = 0;
\r
105 c_write(rlogin, data, len);
\r
110 static void rlogin_sent(Plug plug, int bufsize)
\r
112 Rlogin rlogin = (Rlogin) plug;
\r
113 rlogin->bufsize = bufsize;
\r
117 * Called to set up the rlogin connection.
\r
119 * Returns an error message, or NULL on success.
\r
121 * Also places the canonical host name into `realhost'. It must be
\r
122 * freed by the caller.
\r
124 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
\r
126 char *host, int port, char **realhost,
\r
127 int nodelay, int keepalive)
\r
129 static const struct plug_function_table fn_table = {
\r
139 rlogin = snew(struct rlogin_tag);
\r
140 rlogin->fn = &fn_table;
\r
142 rlogin->frontend = frontend_handle;
\r
143 rlogin->term_width = cfg->width;
\r
144 rlogin->term_height = cfg->height;
\r
145 rlogin->firstbyte = 1;
\r
146 rlogin->cansize = 0;
\r
147 *backend_handle = rlogin;
\r
150 * Try to find host.
\r
154 buf = dupprintf("Looking up host \"%s\"%s", host,
\r
155 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
\r
156 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
\r
158 logevent(rlogin->frontend, buf);
\r
161 addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily);
\r
162 if ((err = sk_addr_error(addr)) != NULL) {
\r
163 sk_addr_free(addr);
\r
168 port = 513; /* default rlogin port */
\r
173 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
\r
174 nodelay, keepalive, (Plug) rlogin, cfg);
\r
175 if ((err = sk_socket_error(rlogin->s)) != NULL)
\r
179 * Send local username, remote username, terminal/speed
\r
185 sk_write(rlogin->s, &z, 1);
\r
186 sk_write(rlogin->s, cfg->localusername,
\r
187 strlen(cfg->localusername));
\r
188 sk_write(rlogin->s, &z, 1);
\r
189 sk_write(rlogin->s, cfg->username,
\r
190 strlen(cfg->username));
\r
191 sk_write(rlogin->s, &z, 1);
\r
192 sk_write(rlogin->s, cfg->termtype,
\r
193 strlen(cfg->termtype));
\r
194 sk_write(rlogin->s, "/", 1);
\r
195 for (p = cfg->termspeed; isdigit((unsigned char)*p); p++) continue;
\r
196 sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed);
\r
197 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
\r
203 static void rlogin_free(void *handle)
\r
205 Rlogin rlogin = (Rlogin) handle;
\r
208 sk_close(rlogin->s);
\r
213 * Stub routine (we don't have any need to reconfigure this backend).
\r
215 static void rlogin_reconfig(void *handle, Config *cfg)
\r
220 * Called to send data down the rlogin connection.
\r
222 static int rlogin_send(void *handle, char *buf, int len)
\r
224 Rlogin rlogin = (Rlogin) handle;
\r
226 if (rlogin->s == NULL)
\r
229 rlogin->bufsize = sk_write(rlogin->s, buf, len);
\r
231 return rlogin->bufsize;
\r
235 * Called to query the current socket sendability status.
\r
237 static int rlogin_sendbuffer(void *handle)
\r
239 Rlogin rlogin = (Rlogin) handle;
\r
240 return rlogin->bufsize;
\r
244 * Called to set the size of the window
\r
246 static void rlogin_size(void *handle, int width, int height)
\r
248 Rlogin rlogin = (Rlogin) handle;
\r
249 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
\r
251 rlogin->term_width = width;
\r
252 rlogin->term_height = height;
\r
254 if (rlogin->s == NULL || !rlogin->cansize)
\r
257 b[6] = rlogin->term_width >> 8;
\r
258 b[7] = rlogin->term_width & 0xFF;
\r
259 b[4] = rlogin->term_height >> 8;
\r
260 b[5] = rlogin->term_height & 0xFF;
\r
261 rlogin->bufsize = sk_write(rlogin->s, b, 12);
\r
266 * Send rlogin special codes.
\r
268 static void rlogin_special(void *handle, Telnet_Special code)
\r
275 * Return a list of the special codes that make sense in this
\r
278 static const struct telnet_special *rlogin_get_specials(void *handle)
\r
283 static int rlogin_connected(void *handle)
\r
285 Rlogin rlogin = (Rlogin) handle;
\r
286 return rlogin->s != NULL;
\r
289 static int rlogin_sendok(void *handle)
\r
291 /* Rlogin rlogin = (Rlogin) handle; */
\r
295 static void rlogin_unthrottle(void *handle, int backlog)
\r
297 Rlogin rlogin = (Rlogin) handle;
\r
298 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
\r
301 static int rlogin_ldisc(void *handle, int option)
\r
303 /* Rlogin rlogin = (Rlogin) handle; */
\r
307 static void rlogin_provide_ldisc(void *handle, void *ldisc)
\r
309 /* This is a stub. */
\r
312 static void rlogin_provide_logctx(void *handle, void *logctx)
\r
314 /* This is a stub. */
\r
317 static int rlogin_exitcode(void *handle)
\r
319 Rlogin rlogin = (Rlogin) handle;
\r
320 if (rlogin->s != NULL)
\r
321 return -1; /* still connected */
\r
323 /* If we ever implement RSH, we'll probably need to do this properly */
\r
328 * cfg_info for rlogin does nothing at all.
\r
330 static int rlogin_cfg_info(void *handle)
\r
335 Backend rlogin_backend = {
\r
343 rlogin_get_specials,
\r
348 rlogin_provide_ldisc,
\r
349 rlogin_provide_logctx,
\r