Add a new back-end function to return the exit code of the remote
[u/mdw/putty] / rlogin.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5
6 #include "putty.h"
7
8 #ifndef FALSE
9 #define FALSE 0
10 #endif
11 #ifndef TRUE
12 #define TRUE 1
13 #endif
14
15 #define RLOGIN_MAX_BACKLOG 4096
16
17 static Socket s = NULL;
18 static int rlogin_bufsize;
19
20 static void rlogin_size(void);
21
22 static void c_write(char *buf, int len)
23 {
24 int backlog = from_backend(0, buf, len);
25 sk_set_frozen(s, backlog > RLOGIN_MAX_BACKLOG);
26 }
27
28 static int rlogin_closing(Plug plug, char *error_msg, int error_code,
29 int calling_back)
30 {
31 if (s) {
32 sk_close(s);
33 s = NULL;
34 }
35 if (error_msg) {
36 /* A socket error has occurred. */
37 connection_fatal(error_msg);
38 } /* Otherwise, the remote side closed the connection normally. */
39 return 0;
40 }
41
42 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
43 {
44 if (urgent == 2) {
45 char c;
46
47 c = *data++;
48 len--;
49 if (c == '\x80')
50 rlogin_size();
51 /*
52 * We should flush everything (aka Telnet SYNCH) if we see
53 * 0x02, and we should turn off and on _local_ flow control
54 * on 0x10 and 0x20 respectively. I'm not convinced it's
55 * worth it...
56 */
57 } else {
58 /*
59 * Main rlogin protocol. This is really simple: the first
60 * byte is expected to be NULL and is ignored, and the rest
61 * is printed.
62 */
63 static int firstbyte = 1;
64 if (firstbyte) {
65 if (data[0] == '\0') {
66 data++;
67 len--;
68 }
69 firstbyte = 0;
70 }
71 c_write(data, len);
72 }
73 return 1;
74 }
75
76 static void rlogin_sent(Plug plug, int bufsize)
77 {
78 rlogin_bufsize = bufsize;
79 }
80
81 /*
82 * Called to set up the rlogin connection.
83 *
84 * Returns an error message, or NULL on success.
85 *
86 * Also places the canonical host name into `realhost'. It must be
87 * freed by the caller.
88 */
89 static char *rlogin_init(char *host, int port, char **realhost, int nodelay)
90 {
91 static struct plug_function_table fn_table = {
92 rlogin_closing,
93 rlogin_receive,
94 rlogin_sent
95 }, *fn_table_ptr = &fn_table;
96
97 SockAddr addr;
98 char *err;
99
100 /*
101 * Try to find host.
102 */
103 {
104 char buf[200];
105 sprintf(buf, "Looking up host \"%.170s\"", host);
106 logevent(buf);
107 }
108 addr = sk_namelookup(host, realhost);
109 if ((err = sk_addr_error(addr)))
110 return err;
111
112 if (port < 0)
113 port = 513; /* default rlogin port */
114
115 /*
116 * Open socket.
117 */
118 {
119 char buf[200], addrbuf[100];
120 sk_getaddr(addr, addrbuf, 100);
121 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
122 logevent(buf);
123 }
124 s = sk_new(addr, port, 1, 0, nodelay, &fn_table_ptr);
125 if ((err = sk_socket_error(s)))
126 return err;
127
128 sk_addr_free(addr);
129
130 /*
131 * Send local username, remote username, terminal/speed
132 */
133
134 {
135 char z = 0;
136 char *p;
137 sk_write(s, &z, 1);
138 sk_write(s, cfg.localusername, strlen(cfg.localusername));
139 sk_write(s, &z, 1);
140 sk_write(s, cfg.username, strlen(cfg.username));
141 sk_write(s, &z, 1);
142 sk_write(s, cfg.termtype, strlen(cfg.termtype));
143 sk_write(s, "/", 1);
144 for (p = cfg.termspeed; isdigit(*p); p++);
145 sk_write(s, cfg.termspeed, p - cfg.termspeed);
146 rlogin_bufsize = sk_write(s, &z, 1);
147 }
148
149 return NULL;
150 }
151
152 /*
153 * Called to send data down the rlogin connection.
154 */
155 static int rlogin_send(char *buf, int len)
156 {
157 if (s == NULL)
158 return 0;
159
160 rlogin_bufsize = sk_write(s, buf, len);
161
162 return rlogin_bufsize;
163 }
164
165 /*
166 * Called to query the current socket sendability status.
167 */
168 static int rlogin_sendbuffer(void)
169 {
170 return rlogin_bufsize;
171 }
172
173 /*
174 * Called to set the size of the window
175 */
176 static void rlogin_size(void)
177 {
178 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
179
180 if (s == NULL)
181 return;
182
183 b[6] = cols >> 8;
184 b[7] = cols & 0xFF;
185 b[4] = rows >> 8;
186 b[5] = rows & 0xFF;
187 rlogin_bufsize = sk_write(s, b, 12);
188 return;
189 }
190
191 /*
192 * Send rlogin special codes.
193 */
194 static void rlogin_special(Telnet_Special code)
195 {
196 /* Do nothing! */
197 return;
198 }
199
200 static Socket rlogin_socket(void)
201 {
202 return s;
203 }
204
205 static int rlogin_sendok(void)
206 {
207 return 1;
208 }
209
210 static void rlogin_unthrottle(int backlog)
211 {
212 sk_set_frozen(s, backlog > RLOGIN_MAX_BACKLOG);
213 }
214
215 static int rlogin_ldisc(int option)
216 {
217 return 0;
218 }
219
220 static int rlogin_exitcode(void)
221 {
222 /* If we ever implement RSH, we'll probably need to do this properly */
223 return 0;
224 }
225
226 Backend rlogin_backend = {
227 rlogin_init,
228 rlogin_send,
229 rlogin_sendbuffer,
230 rlogin_size,
231 rlogin_special,
232 rlogin_socket,
233 rlogin_exitcode,
234 rlogin_sendok,
235 rlogin_ldisc,
236 rlogin_unthrottle,
237 1
238 };