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