`realhost', passed back from all the backend init functions, was
[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 static Socket s = NULL;
16
17 static void rlogin_size(void);
18
19 static int sb_opt, sb_len;
20 static char *sb_buf = NULL;
21 static int sb_size = 0;
22 #define SB_DELTA 1024
23
24 static void c_write(char *buf, int len)
25 {
26 from_backend(0, buf, len);
27 }
28
29 static int rlogin_closing(Plug plug, char *error_msg, int error_code,
30 int calling_back)
31 {
32 sk_close(s);
33 s = NULL;
34 if (error_msg) {
35 /* A socket error has occurred. */
36 connection_fatal(error_msg);
37 } /* Otherwise, the remote side closed the connection normally. */
38 return 0;
39 }
40
41 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
42 {
43 if (urgent == 2) {
44 char c;
45
46 c = *data++;
47 len--;
48 if (c == '\x80')
49 rlogin_size();
50 /*
51 * We should flush everything (aka Telnet SYNCH) if we see
52 * 0x02, and we should turn off and on _local_ flow control
53 * on 0x10 and 0x20 respectively. I'm not convinced it's
54 * worth it...
55 */
56 } else {
57 /*
58 * Main rlogin protocol. This is really simple: the first
59 * byte is expected to be NULL and is ignored, and the rest
60 * is printed.
61 */
62 static int firstbyte = 1;
63 if (firstbyte) {
64 if (data[0] == '\0') {
65 data++;
66 len--;
67 }
68 firstbyte = 0;
69 }
70 c_write(data, len);
71 }
72 return 1;
73 }
74
75 /*
76 * Called to set up the rlogin connection.
77 *
78 * Returns an error message, or NULL on success.
79 *
80 * Also places the canonical host name into `realhost'. It must be
81 * freed by the caller.
82 */
83 static char *rlogin_init(char *host, int port, char **realhost)
84 {
85 static struct plug_function_table fn_table = {
86 rlogin_closing,
87 rlogin_receive
88 }, *fn_table_ptr = &fn_table;
89
90 SockAddr addr;
91 char *err;
92
93 /*
94 * Try to find host.
95 */
96 addr = sk_namelookup(host, realhost);
97 if ((err = sk_addr_error(addr)))
98 return err;
99
100 if (port < 0)
101 port = 513; /* default rlogin port */
102
103 /*
104 * Open socket.
105 */
106 s = sk_new(addr, port, 1, 0, &fn_table_ptr);
107 if ((err = sk_socket_error(s)))
108 return err;
109
110 sk_addr_free(addr);
111
112 /*
113 * Send local username, remote username, terminal/speed
114 */
115
116 {
117 char z = 0;
118 char *p;
119 sk_write(s, &z, 1);
120 sk_write(s, cfg.localusername, strlen(cfg.localusername));
121 sk_write(s, &z, 1);
122 sk_write(s, cfg.username, strlen(cfg.username));
123 sk_write(s, &z, 1);
124 sk_write(s, cfg.termtype, strlen(cfg.termtype));
125 sk_write(s, "/", 1);
126 for (p = cfg.termspeed; isdigit(*p); p++);
127 sk_write(s, cfg.termspeed, p - cfg.termspeed);
128 sk_write(s, &z, 1);
129 }
130
131 return NULL;
132 }
133
134 /*
135 * Called to send data down the rlogin connection.
136 */
137 static void rlogin_send(char *buf, int len)
138 {
139
140 if (s == NULL)
141 return;
142
143 sk_write(s, buf, len);
144 }
145
146 /*
147 * Called to set the size of the window
148 */
149 static void rlogin_size(void)
150 {
151 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
152
153 b[6] = cols >> 8;
154 b[7] = cols & 0xFF;
155 b[4] = rows >> 8;
156 b[5] = rows & 0xFF;
157 sk_write(s, b, 12);
158 return;
159 }
160
161 /*
162 * Send rlogin special codes.
163 */
164 static void rlogin_special(Telnet_Special code)
165 {
166 /* Do nothing! */
167 return;
168 }
169
170 static Socket rlogin_socket(void)
171 {
172 return s;
173 }
174
175 static int rlogin_sendok(void)
176 {
177 return 1;
178 }
179
180 static int rlogin_ldisc(int option)
181 {
182 return 0;
183 }
184
185 Backend rlogin_backend = {
186 rlogin_init,
187 rlogin_send,
188 rlogin_size,
189 rlogin_special,
190 rlogin_socket,
191 rlogin_sendok,
192 rlogin_ldisc,
193 1
194 };