Run entire source base through GNU indent to tidy up the varying
[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'.
81 */
82 static char *rlogin_init(char *host, int port, char **realhost)
83 {
84 static struct plug_function_table fn_table = {
85 rlogin_closing,
86 rlogin_receive
87 }, *fn_table_ptr = &fn_table;
88
89 SockAddr addr;
90 char *err;
91
92 /*
93 * Try to find host.
94 */
95 addr = sk_namelookup(host, realhost);
96 if ((err = sk_addr_error(addr)))
97 return err;
98
99 if (port < 0)
100 port = 513; /* default rlogin port */
101
102 /*
103 * Open socket.
104 */
105 s = sk_new(addr, port, 1, 0, &fn_table_ptr);
106 if ((err = sk_socket_error(s)))
107 return err;
108
109 sk_addr_free(addr);
110
111 /*
112 * Send local username, remote username, terminal/speed
113 */
114
115 {
116 char z = 0;
117 char *p;
118 sk_write(s, &z, 1);
119 sk_write(s, cfg.localusername, strlen(cfg.localusername));
120 sk_write(s, &z, 1);
121 sk_write(s, cfg.username, strlen(cfg.username));
122 sk_write(s, &z, 1);
123 sk_write(s, cfg.termtype, strlen(cfg.termtype));
124 sk_write(s, "/", 1);
125 for (p = cfg.termspeed; isdigit(*p); p++);
126 sk_write(s, cfg.termspeed, p - cfg.termspeed);
127 sk_write(s, &z, 1);
128 }
129
130 return NULL;
131 }
132
133 /*
134 * Called to send data down the rlogin connection.
135 */
136 static void rlogin_send(char *buf, int len)
137 {
138
139 if (s == NULL)
140 return;
141
142 sk_write(s, buf, len);
143 }
144
145 /*
146 * Called to set the size of the window
147 */
148 static void rlogin_size(void)
149 {
150 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
151
152 b[6] = cols >> 8;
153 b[7] = cols & 0xFF;
154 b[4] = rows >> 8;
155 b[5] = rows & 0xFF;
156 sk_write(s, b, 12);
157 return;
158 }
159
160 /*
161 * Send rlogin special codes.
162 */
163 static void rlogin_special(Telnet_Special code)
164 {
165 /* Do nothing! */
166 return;
167 }
168
169 static Socket rlogin_socket(void)
170 {
171 return s;
172 }
173
174 static int rlogin_sendok(void)
175 {
176 return 1;
177 }
178
179 static int rlogin_ldisc(int option)
180 {
181 return 0;
182 }
183
184 Backend rlogin_backend = {
185 rlogin_init,
186 rlogin_send,
187 rlogin_size,
188 rlogin_special,
189 rlogin_socket,
190 rlogin_sendok,
191 rlogin_ldisc,
192 1
193 };