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\"%s", host
,
130 (cfg
->addressfamily
== ADDRTYPE_IPV4 ?
" (IPv4)" :
131 (cfg
->addressfamily
== ADDRTYPE_IPV6 ?
" (IPv6)" :
133 logevent(rlogin
->frontend
, buf
);
136 addr
= name_lookup(host
, port
, realhost
, cfg
, cfg
->addressfamily
);
137 if ((err
= sk_addr_error(addr
)) != NULL
) {
143 port
= 513; /* default rlogin port */
149 char *buf
, addrbuf
[100];
150 sk_getaddr(addr
, addrbuf
, 100);
151 buf
= dupprintf("Connecting to %s port %d", addrbuf
, port
);
152 logevent(rlogin
->frontend
, buf
);
155 rlogin
->s
= new_connection(addr
, *realhost
, port
, 1, 0,
156 nodelay
, keepalive
, (Plug
) rlogin
, cfg
);
157 if ((err
= sk_socket_error(rlogin
->s
)) != NULL
)
161 * Send local username, remote username, terminal/speed
167 sk_write(rlogin
->s
, &z
, 1);
168 sk_write(rlogin
->s
, cfg
->localusername
,
169 strlen(cfg
->localusername
));
170 sk_write(rlogin
->s
, &z
, 1);
171 sk_write(rlogin
->s
, cfg
->username
,
172 strlen(cfg
->username
));
173 sk_write(rlogin
->s
, &z
, 1);
174 sk_write(rlogin
->s
, cfg
->termtype
,
175 strlen(cfg
->termtype
));
176 sk_write(rlogin
->s
, "/", 1);
177 for (p
= cfg
->termspeed
; isdigit((unsigned char)*p
); p
++) continue;
178 sk_write(rlogin
->s
, cfg
->termspeed
, p
- cfg
->termspeed
);
179 rlogin
->bufsize
= sk_write(rlogin
->s
, &z
, 1);
185 static void rlogin_free(void *handle
)
187 Rlogin rlogin
= (Rlogin
) handle
;
195 * Stub routine (we don't have any need to reconfigure this backend).
197 static void rlogin_reconfig(void *handle
, Config
*cfg
)
202 * Called to send data down the rlogin connection.
204 static int rlogin_send(void *handle
, char *buf
, int len
)
206 Rlogin rlogin
= (Rlogin
) handle
;
208 if (rlogin
->s
== NULL
)
211 rlogin
->bufsize
= sk_write(rlogin
->s
, buf
, len
);
213 return rlogin
->bufsize
;
217 * Called to query the current socket sendability status.
219 static int rlogin_sendbuffer(void *handle
)
221 Rlogin rlogin
= (Rlogin
) handle
;
222 return rlogin
->bufsize
;
226 * Called to set the size of the window
228 static void rlogin_size(void *handle
, int width
, int height
)
230 Rlogin rlogin
= (Rlogin
) handle
;
231 char b
[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
233 rlogin
->term_width
= width
;
234 rlogin
->term_height
= height
;
236 if (rlogin
->s
== NULL
)
239 b
[6] = rlogin
->term_width
>> 8;
240 b
[7] = rlogin
->term_width
& 0xFF;
241 b
[4] = rlogin
->term_height
>> 8;
242 b
[5] = rlogin
->term_height
& 0xFF;
243 rlogin
->bufsize
= sk_write(rlogin
->s
, b
, 12);
248 * Send rlogin special codes.
250 static void rlogin_special(void *handle
, Telnet_Special code
)
257 * Return a list of the special codes that make sense in this
260 static const struct telnet_special
*rlogin_get_specials(void *handle
)
265 static Socket
rlogin_socket(void *handle
)
267 Rlogin rlogin
= (Rlogin
) handle
;
271 static int rlogin_sendok(void *handle
)
273 /* Rlogin rlogin = (Rlogin) handle; */
277 static void rlogin_unthrottle(void *handle
, int backlog
)
279 Rlogin rlogin
= (Rlogin
) handle
;
280 sk_set_frozen(rlogin
->s
, backlog
> RLOGIN_MAX_BACKLOG
);
283 static int rlogin_ldisc(void *handle
, int option
)
285 /* Rlogin rlogin = (Rlogin) handle; */
289 static void rlogin_provide_ldisc(void *handle
, void *ldisc
)
291 /* This is a stub. */
294 static void rlogin_provide_logctx(void *handle
, void *logctx
)
296 /* This is a stub. */
299 static int rlogin_exitcode(void *handle
)
301 Rlogin rlogin
= (Rlogin
) handle
;
302 if (rlogin
->s
!= NULL
)
303 return -1; /* still connected */
305 /* If we ever implement RSH, we'll probably need to do this properly */
310 * cfg_info for rlogin does nothing at all.
312 static int rlogin_cfg_info(void *handle
)
317 Backend rlogin_backend
= {
330 rlogin_provide_ldisc
,
331 rlogin_provide_logctx
,