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