Justin Bradford's proxy support patch. Currently supports only HTTP
[u/mdw/putty] / rlogin.c
CommitLineData
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 17static Socket s = NULL;
5471d09a 18static int rlogin_bufsize;
c91409da 19
20static void rlogin_size(void);
21
32874aea 22static 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 28static 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. */
37 connection_fatal(error_msg);
38 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 39 return 0;
40}
41
32874aea 42static int rlogin_receive(Plug plug, int urgent, char *data, int len)
43{
c91409da 44 if (urgent == 2) {
32874aea 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 */
8a5fb9e3 57 } else {
32874aea 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 }
2b0c045b 71 if (len > 0)
72 c_write(data, len);
c91409da 73 }
c91409da 74 return 1;
75}
76
3ad9d396 77static void rlogin_sent(Plug plug, int bufsize)
78{
79 rlogin_bufsize = bufsize;
80}
81
c91409da 82/*
83 * Called to set up the rlogin connection.
84 *
85 * Returns an error message, or NULL on success.
86 *
6e1ebb76 87 * Also places the canonical host name into `realhost'. It must be
88 * freed by the caller.
c91409da 89 */
2184a5d9 90static char *rlogin_init(char *host, int port, char **realhost, int nodelay)
32874aea 91{
7e78000d 92 static struct plug_function_table fn_table = {
93 rlogin_closing,
3ad9d396 94 rlogin_receive,
95 rlogin_sent
7e78000d 96 }, *fn_table_ptr = &fn_table;
97
c91409da 98 SockAddr addr;
99 char *err;
100
101 /*
102 * Try to find host.
103 */
3ad9d396 104 {
105 char buf[200];
106 sprintf(buf, "Looking up host \"%.170s\"", host);
107 logevent(buf);
108 }
c91409da 109 addr = sk_namelookup(host, realhost);
32874aea 110 if ((err = sk_addr_error(addr)))
c91409da 111 return err;
112
113 if (port < 0)
114 port = 513; /* default rlogin port */
115
116 /*
117 * Open socket.
118 */
3ad9d396 119 {
120 char buf[200], addrbuf[100];
121 sk_getaddr(addr, addrbuf, 100);
122 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
123 logevent(buf);
124 }
8eebd221 125 s = new_connection(addr, *realhost, port, 1, 0, nodelay, &fn_table_ptr);
32874aea 126 if ((err = sk_socket_error(s)))
c91409da 127 return err;
128
129 sk_addr_free(addr);
130
131 /*
132 * Send local username, remote username, terminal/speed
133 */
134
135 {
32874aea 136 char z = 0;
137 char *p;
138 sk_write(s, &z, 1);
139 sk_write(s, cfg.localusername, strlen(cfg.localusername));
140 sk_write(s, &z, 1);
141 sk_write(s, cfg.username, strlen(cfg.username));
142 sk_write(s, &z, 1);
143 sk_write(s, cfg.termtype, strlen(cfg.termtype));
144 sk_write(s, "/", 1);
145 for (p = cfg.termspeed; isdigit(*p); p++);
146 sk_write(s, cfg.termspeed, p - cfg.termspeed);
5471d09a 147 rlogin_bufsize = sk_write(s, &z, 1);
c91409da 148 }
149
c91409da 150 return NULL;
151}
152
153/*
154 * Called to send data down the rlogin connection.
155 */
5471d09a 156static int rlogin_send(char *buf, int len)
32874aea 157{
c91409da 158 if (s == NULL)
b5a460cd 159 return 0;
c91409da 160
5471d09a 161 rlogin_bufsize = sk_write(s, buf, len);
162
163 return rlogin_bufsize;
164}
165
166/*
167 * Called to query the current socket sendability status.
168 */
169static int rlogin_sendbuffer(void)
170{
171 return rlogin_bufsize;
c91409da 172}
173
174/*
175 * Called to set the size of the window
176 */
32874aea 177static void rlogin_size(void)
178{
e418595a 179 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
c91409da 180
cf293dd4 181 if (s == NULL)
182 return;
183
32874aea 184 b[6] = cols >> 8;
185 b[7] = cols & 0xFF;
186 b[4] = rows >> 8;
187 b[5] = rows & 0xFF;
5471d09a 188 rlogin_bufsize = sk_write(s, b, 12);
c91409da 189 return;
190}
191
192/*
193 * Send rlogin special codes.
194 */
32874aea 195static void rlogin_special(Telnet_Special code)
196{
c91409da 197 /* Do nothing! */
198 return;
199}
200
32874aea 201static Socket rlogin_socket(void)
202{
203 return s;
204}
c91409da 205
32874aea 206static int rlogin_sendok(void)
207{
208 return 1;
209}
c91409da 210
5471d09a 211static void rlogin_unthrottle(int backlog)
212{
213 sk_set_frozen(s, backlog > RLOGIN_MAX_BACKLOG);
214}
215
32874aea 216static int rlogin_ldisc(int option)
217{
0965bee0 218 return 0;
219}
220
d8d6c7e5 221static int rlogin_exitcode(void)
222{
223 /* If we ever implement RSH, we'll probably need to do this properly */
224 return 0;
225}
226
c91409da 227Backend rlogin_backend = {
228 rlogin_init,
229 rlogin_send,
5471d09a 230 rlogin_sendbuffer,
c91409da 231 rlogin_size,
232 rlogin_special,
233 rlogin_socket,
d8d6c7e5 234 rlogin_exitcode,
c91409da 235 rlogin_sendok,
0965bee0 236 rlogin_ldisc,
5471d09a 237 rlogin_unthrottle,
c91409da 238 1
239};