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 */
22 int term_width
, term_height
;
26 static void rlogin_size(void *handle
, int width
, int height
);
28 static void c_write(Rlogin rlogin
, char *buf
, int len
)
30 int backlog
= from_backend(rlogin
->frontend
, 0, buf
, len
);
31 sk_set_frozen(rlogin
->s
, backlog
> RLOGIN_MAX_BACKLOG
);
34 static int rlogin_closing(Plug plug
, char *error_msg
, int error_code
,
37 Rlogin rlogin
= (Rlogin
) plug
;
43 /* A socket error has occurred. */
44 logevent(rlogin
->frontend
, error_msg
);
45 connection_fatal("%s", error_msg
);
46 } /* Otherwise, the remote side closed the connection normally. */
50 static int rlogin_receive(Plug plug
, int urgent
, char *data
, int len
)
52 Rlogin rlogin
= (Rlogin
) plug
;
59 rlogin_size(rlogin
, rlogin
->term_width
, rlogin
->term_height
);
61 * We should flush everything (aka Telnet SYNCH) if we see
62 * 0x02, and we should turn off and on _local_ flow control
63 * on 0x10 and 0x20 respectively. I'm not convinced it's
68 * Main rlogin protocol. This is really simple: the first
69 * byte is expected to be NULL and is ignored, and the rest
72 static int firstbyte
= 1;
74 if (data
[0] == '\0') {
81 c_write(rlogin
, data
, len
);
86 static void rlogin_sent(Plug plug
, int bufsize
)
88 Rlogin rlogin
= (Rlogin
) plug
;
89 rlogin
->bufsize
= bufsize
;
93 * Called to set up the rlogin connection.
95 * Returns an error message, or NULL on success.
97 * Also places the canonical host name into `realhost'. It must be
98 * freed by the caller.
100 static char *rlogin_init(void *frontend_handle
, void **backend_handle
,
102 char *host
, int port
, char **realhost
, int nodelay
)
104 static const struct plug_function_table fn_table
= {
113 rlogin
= smalloc(sizeof(*rlogin
));
114 rlogin
->fn
= &fn_table
;
116 rlogin
->frontend
= frontend_handle
;
117 rlogin
->term_width
= cfg
->width
;
118 rlogin
->term_height
= cfg
->height
;
119 *backend_handle
= rlogin
;
126 buf
= dupprintf("Looking up host \"%s\"", host
);
127 logevent(rlogin
->frontend
, buf
);
130 addr
= name_lookup(host
, port
, realhost
, cfg
);
131 if ((err
= sk_addr_error(addr
)) != NULL
)
135 port
= 513; /* default rlogin port */
141 char *buf
, addrbuf
[100];
142 sk_getaddr(addr
, addrbuf
, 100);
143 buf
= dupprintf("Connecting to %s port %d", addrbuf
, port
);
144 logevent(rlogin
->frontend
, buf
);
147 rlogin
->s
= new_connection(addr
, *realhost
, port
, 1, 0,
148 nodelay
, (Plug
) rlogin
, cfg
);
149 if ((err
= sk_socket_error(rlogin
->s
)) != NULL
)
155 * Send local username, remote username, terminal/speed
161 sk_write(rlogin
->s
, &z
, 1);
162 sk_write(rlogin
->s
, cfg
->localusername
,
163 strlen(cfg
->localusername
));
164 sk_write(rlogin
->s
, &z
, 1);
165 sk_write(rlogin
->s
, cfg
->username
,
166 strlen(cfg
->username
));
167 sk_write(rlogin
->s
, &z
, 1);
168 sk_write(rlogin
->s
, cfg
->termtype
,
169 strlen(cfg
->termtype
));
170 sk_write(rlogin
->s
, "/", 1);
171 for (p
= cfg
->termspeed
; isdigit(*p
); p
++) continue;
172 sk_write(rlogin
->s
, cfg
->termspeed
, p
- cfg
->termspeed
);
173 rlogin
->bufsize
= sk_write(rlogin
->s
, &z
, 1);
180 * Stub routine (we don't have any need to reconfigure this backend).
182 static void rlogin_reconfig(void *handle
, Config
*cfg
)
187 * Called to send data down the rlogin connection.
189 static int rlogin_send(void *handle
, char *buf
, int len
)
191 Rlogin rlogin
= (Rlogin
) handle
;
193 if (rlogin
->s
== NULL
)
196 rlogin
->bufsize
= sk_write(rlogin
->s
, buf
, len
);
198 return rlogin
->bufsize
;
202 * Called to query the current socket sendability status.
204 static int rlogin_sendbuffer(void *handle
)
206 Rlogin rlogin
= (Rlogin
) handle
;
207 return rlogin
->bufsize
;
211 * Called to set the size of the window
213 static void rlogin_size(void *handle
, int width
, int height
)
215 Rlogin rlogin
= (Rlogin
) handle
;
216 char b
[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
218 rlogin
->term_width
= width
;
219 rlogin
->term_height
= height
;
221 if (rlogin
->s
== NULL
)
224 b
[6] = rlogin
->term_width
>> 8;
225 b
[7] = rlogin
->term_width
& 0xFF;
226 b
[4] = rlogin
->term_height
>> 8;
227 b
[5] = rlogin
->term_height
& 0xFF;
228 rlogin
->bufsize
= sk_write(rlogin
->s
, b
, 12);
233 * Send rlogin special codes.
235 static void rlogin_special(void *handle
, Telnet_Special code
)
241 static Socket
rlogin_socket(void *handle
)
243 Rlogin rlogin
= (Rlogin
) handle
;
247 static int rlogin_sendok(void *handle
)
249 /* Rlogin rlogin = (Rlogin) handle; */
253 static void rlogin_unthrottle(void *handle
, int backlog
)
255 Rlogin rlogin
= (Rlogin
) handle
;
256 sk_set_frozen(rlogin
->s
, backlog
> RLOGIN_MAX_BACKLOG
);
259 static int rlogin_ldisc(void *handle
, int option
)
261 /* Rlogin rlogin = (Rlogin) handle; */
265 static void rlogin_provide_ldisc(void *handle
, void *ldisc
)
267 /* This is a stub. */
270 static void rlogin_provide_logctx(void *handle
, void *logctx
)
272 /* This is a stub. */
275 static int rlogin_exitcode(void *handle
)
277 /* Rlogin rlogin = (Rlogin) handle; */
278 /* If we ever implement RSH, we'll probably need to do this properly */
282 Backend rlogin_backend
= {
293 rlogin_provide_ldisc
,
294 rlogin_provide_logctx
,