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 */
24 int term_width
, term_height
;
28 static void rlogin_size(void *handle
, int width
, int height
);
30 static void c_write(Rlogin rlogin
, char *buf
, int len
)
32 int backlog
= from_backend(rlogin
->frontend
, 0, buf
, len
);
33 sk_set_frozen(rlogin
->s
, backlog
> RLOGIN_MAX_BACKLOG
);
36 static void rlogin_log(Plug plug
, int type
, SockAddr addr
, int port
,
37 const char *error_msg
, int error_code
)
39 Rlogin rlogin
= (Rlogin
) plug
;
40 char addrbuf
[256], *msg
;
42 sk_getaddr(addr
, addrbuf
, lenof(addrbuf
));
45 msg
= dupprintf("Connecting to %s port %d", addrbuf
, port
);
47 msg
= dupprintf("Failed to connect to %s: %s", addrbuf
, error_msg
);
49 logevent(rlogin
->frontend
, msg
);
52 static int rlogin_closing(Plug plug
, const char *error_msg
, int error_code
,
55 Rlogin rlogin
= (Rlogin
) plug
;
59 notify_remote_exit(rlogin
->frontend
);
62 /* A socket error has occurred. */
63 logevent(rlogin
->frontend
, error_msg
);
64 connection_fatal(rlogin
->frontend
, "%s", error_msg
);
65 } /* Otherwise, the remote side closed the connection normally. */
69 static int rlogin_receive(Plug plug
, int urgent
, char *data
, int len
)
71 Rlogin rlogin
= (Rlogin
) plug
;
79 rlogin_size(rlogin
, rlogin
->term_width
, rlogin
->term_height
);
82 * We should flush everything (aka Telnet SYNCH) if we see
83 * 0x02, and we should turn off and on _local_ flow control
84 * on 0x10 and 0x20 respectively. I'm not convinced it's
89 * Main rlogin protocol. This is really simple: the first
90 * byte is expected to be NULL and is ignored, and the rest
93 if (rlogin
->firstbyte
) {
94 if (data
[0] == '\0') {
98 rlogin
->firstbyte
= 0;
101 c_write(rlogin
, data
, len
);
106 static void rlogin_sent(Plug plug
, int bufsize
)
108 Rlogin rlogin
= (Rlogin
) plug
;
109 rlogin
->bufsize
= bufsize
;
113 * Called to set up the rlogin connection.
115 * Returns an error message, or NULL on success.
117 * Also places the canonical host name into `realhost'. It must be
118 * freed by the caller.
120 static const char *rlogin_init(void *frontend_handle
, void **backend_handle
,
122 char *host
, int port
, char **realhost
,
123 int nodelay
, int keepalive
)
125 static const struct plug_function_table fn_table
= {
135 rlogin
= snew(struct rlogin_tag
);
136 rlogin
->fn
= &fn_table
;
138 rlogin
->frontend
= frontend_handle
;
139 rlogin
->term_width
= cfg
->width
;
140 rlogin
->term_height
= cfg
->height
;
141 rlogin
->firstbyte
= 1;
143 *backend_handle
= rlogin
;
150 buf
= dupprintf("Looking up host \"%s\"%s", host
,
151 (cfg
->addressfamily
== ADDRTYPE_IPV4 ?
" (IPv4)" :
152 (cfg
->addressfamily
== ADDRTYPE_IPV6 ?
" (IPv6)" :
154 logevent(rlogin
->frontend
, buf
);
157 addr
= name_lookup(host
, port
, realhost
, cfg
, cfg
->addressfamily
);
158 if ((err
= sk_addr_error(addr
)) != NULL
) {
164 port
= 513; /* default rlogin port */
169 rlogin
->s
= new_connection(addr
, *realhost
, port
, 1, 0,
170 nodelay
, keepalive
, (Plug
) rlogin
, cfg
);
171 if ((err
= sk_socket_error(rlogin
->s
)) != NULL
)
175 * Send local username, remote username, terminal/speed
181 sk_write(rlogin
->s
, &z
, 1);
182 sk_write(rlogin
->s
, cfg
->localusername
,
183 strlen(cfg
->localusername
));
184 sk_write(rlogin
->s
, &z
, 1);
185 sk_write(rlogin
->s
, cfg
->username
,
186 strlen(cfg
->username
));
187 sk_write(rlogin
->s
, &z
, 1);
188 sk_write(rlogin
->s
, cfg
->termtype
,
189 strlen(cfg
->termtype
));
190 sk_write(rlogin
->s
, "/", 1);
191 for (p
= cfg
->termspeed
; isdigit((unsigned char)*p
); p
++) continue;
192 sk_write(rlogin
->s
, cfg
->termspeed
, p
- cfg
->termspeed
);
193 rlogin
->bufsize
= sk_write(rlogin
->s
, &z
, 1);
199 static void rlogin_free(void *handle
)
201 Rlogin rlogin
= (Rlogin
) handle
;
209 * Stub routine (we don't have any need to reconfigure this backend).
211 static void rlogin_reconfig(void *handle
, Config
*cfg
)
216 * Called to send data down the rlogin connection.
218 static int rlogin_send(void *handle
, char *buf
, int len
)
220 Rlogin rlogin
= (Rlogin
) handle
;
222 if (rlogin
->s
== NULL
)
225 rlogin
->bufsize
= sk_write(rlogin
->s
, buf
, len
);
227 return rlogin
->bufsize
;
231 * Called to query the current socket sendability status.
233 static int rlogin_sendbuffer(void *handle
)
235 Rlogin rlogin
= (Rlogin
) handle
;
236 return rlogin
->bufsize
;
240 * Called to set the size of the window
242 static void rlogin_size(void *handle
, int width
, int height
)
244 Rlogin rlogin
= (Rlogin
) handle
;
245 char b
[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
247 rlogin
->term_width
= width
;
248 rlogin
->term_height
= height
;
250 if (rlogin
->s
== NULL
|| !rlogin
->cansize
)
253 b
[6] = rlogin
->term_width
>> 8;
254 b
[7] = rlogin
->term_width
& 0xFF;
255 b
[4] = rlogin
->term_height
>> 8;
256 b
[5] = rlogin
->term_height
& 0xFF;
257 rlogin
->bufsize
= sk_write(rlogin
->s
, b
, 12);
262 * Send rlogin special codes.
264 static void rlogin_special(void *handle
, Telnet_Special code
)
271 * Return a list of the special codes that make sense in this
274 static const struct telnet_special
*rlogin_get_specials(void *handle
)
279 static Socket
rlogin_socket(void *handle
)
281 Rlogin rlogin
= (Rlogin
) handle
;
285 static int rlogin_sendok(void *handle
)
287 /* Rlogin rlogin = (Rlogin) handle; */
291 static void rlogin_unthrottle(void *handle
, int backlog
)
293 Rlogin rlogin
= (Rlogin
) handle
;
294 sk_set_frozen(rlogin
->s
, backlog
> RLOGIN_MAX_BACKLOG
);
297 static int rlogin_ldisc(void *handle
, int option
)
299 /* Rlogin rlogin = (Rlogin) handle; */
303 static void rlogin_provide_ldisc(void *handle
, void *ldisc
)
305 /* This is a stub. */
308 static void rlogin_provide_logctx(void *handle
, void *logctx
)
310 /* This is a stub. */
313 static int rlogin_exitcode(void *handle
)
315 Rlogin rlogin
= (Rlogin
) handle
;
316 if (rlogin
->s
!= NULL
)
317 return -1; /* still connected */
319 /* If we ever implement RSH, we'll probably need to do this properly */
324 * cfg_info for rlogin does nothing at all.
326 static int rlogin_cfg_info(void *handle
)
331 Backend rlogin_backend
= {
344 rlogin_provide_ldisc
,
345 rlogin_provide_logctx
,