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