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