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