eaf1e20a |
1 | /* |
2 | * "Raw" backend. |
3 | */ |
4 | |
5e1a8e27 |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
5e1a8e27 |
7 | |
8 | #include "putty.h" |
9 | |
10 | #ifndef FALSE |
11 | #define FALSE 0 |
12 | #endif |
13 | #ifndef TRUE |
14 | #define TRUE 1 |
15 | #endif |
16 | |
5471d09a |
17 | #define RAW_MAX_BACKLOG 4096 |
18 | |
51470298 |
19 | typedef struct raw_backend_data { |
20 | const struct plug_function_table *fn; |
21 | /* the above field _must_ be first in the structure */ |
5e1a8e27 |
22 | |
51470298 |
23 | Socket s; |
24 | int bufsize; |
25 | void *frontend; |
26 | } *Raw; |
5e1a8e27 |
27 | |
51470298 |
28 | static void raw_size(void *handle, int width, int height); |
29 | |
30 | static void c_write(Raw raw, char *buf, int len) |
32874aea |
31 | { |
51470298 |
32 | int backlog = from_backend(raw->frontend, 0, buf, len); |
33 | sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG); |
5e1a8e27 |
34 | } |
35 | |
7555d6a5 |
36 | static void raw_log(Plug plug, int type, SockAddr addr, int port, |
37 | const char *error_msg, int error_code) |
38 | { |
39 | Raw raw = (Raw) plug; |
40 | char addrbuf[256], *msg; |
41 | |
42 | sk_getaddr(addr, addrbuf, lenof(addrbuf)); |
43 | |
44 | if (type == 0) |
45 | msg = dupprintf("Connecting to %s port %d", addrbuf, port); |
46 | else |
47 | msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); |
48 | |
49 | logevent(raw->frontend, msg); |
50 | } |
51 | |
cbe2d68f |
52 | static int raw_closing(Plug plug, const char *error_msg, int error_code, |
32874aea |
53 | int calling_back) |
54 | { |
51470298 |
55 | Raw raw = (Raw) plug; |
56 | |
57 | if (raw->s) { |
58 | sk_close(raw->s); |
59 | raw->s = NULL; |
39934deb |
60 | notify_remote_exit(raw->frontend); |
f3ab576e |
61 | } |
7e78000d |
62 | if (error_msg) { |
32874aea |
63 | /* A socket error has occurred. */ |
a8327734 |
64 | logevent(raw->frontend, error_msg); |
971bcc0a |
65 | connection_fatal(raw->frontend, "%s", error_msg); |
32874aea |
66 | } /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
67 | return 0; |
68 | } |
69 | |
32874aea |
70 | static int raw_receive(Plug plug, int urgent, char *data, int len) |
71 | { |
51470298 |
72 | Raw raw = (Raw) plug; |
73 | c_write(raw, data, len); |
8df7a775 |
74 | return 1; |
5e1a8e27 |
75 | } |
76 | |
3ad9d396 |
77 | static void raw_sent(Plug plug, int bufsize) |
78 | { |
51470298 |
79 | Raw raw = (Raw) plug; |
80 | raw->bufsize = bufsize; |
3ad9d396 |
81 | } |
82 | |
5e1a8e27 |
83 | /* |
8df7a775 |
84 | * Called to set up the raw connection. |
85 | * |
5e1a8e27 |
86 | * Returns an error message, or NULL on success. |
87 | * |
6e1ebb76 |
88 | * Also places the canonical host name into `realhost'. It must be |
89 | * freed by the caller. |
5e1a8e27 |
90 | */ |
cbe2d68f |
91 | static const char *raw_init(void *frontend_handle, void **backend_handle, |
4a693cfc |
92 | Conf *conf, |
79bf227b |
93 | char *host, int port, char **realhost, int nodelay, |
94 | int keepalive) |
32874aea |
95 | { |
51470298 |
96 | static const struct plug_function_table fn_table = { |
7555d6a5 |
97 | raw_log, |
7e78000d |
98 | raw_closing, |
3ad9d396 |
99 | raw_receive, |
100 | raw_sent |
51470298 |
101 | }; |
8df7a775 |
102 | SockAddr addr; |
cbe2d68f |
103 | const char *err; |
51470298 |
104 | Raw raw; |
4a693cfc |
105 | int addressfamily; |
106 | char *loghost; |
5e1a8e27 |
107 | |
3d88e64d |
108 | raw = snew(struct raw_backend_data); |
51470298 |
109 | raw->fn = &fn_table; |
110 | raw->s = NULL; |
111 | *backend_handle = raw; |
112 | |
113 | raw->frontend = frontend_handle; |
887035a5 |
114 | |
4a693cfc |
115 | addressfamily = conf_get_int(conf, CONF_addressfamily); |
5e1a8e27 |
116 | /* |
117 | * Try to find host. |
118 | */ |
3ad9d396 |
119 | { |
57356d63 |
120 | char *buf; |
05581745 |
121 | buf = dupprintf("Looking up host \"%s\"%s", host, |
4a693cfc |
122 | (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : |
123 | (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : |
05581745 |
124 | ""))); |
a8327734 |
125 | logevent(raw->frontend, buf); |
57356d63 |
126 | sfree(buf); |
3ad9d396 |
127 | } |
4a693cfc |
128 | addr = name_lookup(host, port, realhost, conf, addressfamily); |
f85e6f6e |
129 | if ((err = sk_addr_error(addr)) != NULL) { |
130 | sk_addr_free(addr); |
8df7a775 |
131 | return err; |
f85e6f6e |
132 | } |
5e1a8e27 |
133 | |
134 | if (port < 0) |
135 | port = 23; /* default telnet port */ |
136 | |
137 | /* |
138 | * Open socket. |
139 | */ |
79bf227b |
140 | raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive, |
4a693cfc |
141 | (Plug) raw, conf); |
6f1e7b78 |
142 | if ((err = sk_socket_error(raw->s)) != NULL) |
8df7a775 |
143 | return err; |
5e1a8e27 |
144 | |
4a693cfc |
145 | loghost = conf_get_str(conf, CONF_loghost); |
146 | if (*loghost) { |
881da168 |
147 | char *colon; |
148 | |
149 | sfree(*realhost); |
4a693cfc |
150 | *realhost = dupstr(loghost); |
881da168 |
151 | colon = strrchr(*realhost, ':'); |
152 | if (colon) { |
153 | /* |
154 | * FIXME: if we ever update this aspect of ssh.c for |
155 | * IPv6 literal management, this should change in line |
156 | * with it. |
157 | */ |
158 | *colon++ = '\0'; |
159 | } |
160 | } |
161 | |
5e1a8e27 |
162 | return NULL; |
163 | } |
164 | |
fabd1805 |
165 | static void raw_free(void *handle) |
166 | { |
167 | Raw raw = (Raw) handle; |
168 | |
169 | if (raw->s) |
170 | sk_close(raw->s); |
171 | sfree(raw); |
172 | } |
173 | |
5e1a8e27 |
174 | /* |
86916870 |
175 | * Stub routine (we don't have any need to reconfigure this backend). |
176 | */ |
4a693cfc |
177 | static void raw_reconfig(void *handle, Conf *conf) |
86916870 |
178 | { |
179 | } |
180 | |
181 | /* |
5e1a8e27 |
182 | * Called to send data down the raw connection. |
183 | */ |
51470298 |
184 | static int raw_send(void *handle, char *buf, int len) |
32874aea |
185 | { |
51470298 |
186 | Raw raw = (Raw) handle; |
187 | |
188 | if (raw->s == NULL) |
b5a460cd |
189 | return 0; |
5e1a8e27 |
190 | |
51470298 |
191 | raw->bufsize = sk_write(raw->s, buf, len); |
5471d09a |
192 | |
51470298 |
193 | return raw->bufsize; |
5471d09a |
194 | } |
195 | |
196 | /* |
197 | * Called to query the current socket sendability status. |
198 | */ |
51470298 |
199 | static int raw_sendbuffer(void *handle) |
5471d09a |
200 | { |
51470298 |
201 | Raw raw = (Raw) handle; |
202 | return raw->bufsize; |
5e1a8e27 |
203 | } |
204 | |
205 | /* |
206 | * Called to set the size of the window |
207 | */ |
51470298 |
208 | static void raw_size(void *handle, int width, int height) |
32874aea |
209 | { |
5e1a8e27 |
210 | /* Do nothing! */ |
211 | return; |
212 | } |
213 | |
214 | /* |
215 | * Send raw special codes. |
216 | */ |
51470298 |
217 | static void raw_special(void *handle, Telnet_Special code) |
32874aea |
218 | { |
5e1a8e27 |
219 | /* Do nothing! */ |
220 | return; |
221 | } |
222 | |
125105d1 |
223 | /* |
224 | * Return a list of the special codes that make sense in this |
225 | * protocol. |
226 | */ |
227 | static const struct telnet_special *raw_get_specials(void *handle) |
228 | { |
229 | return NULL; |
230 | } |
231 | |
6226c939 |
232 | static int raw_connected(void *handle) |
32874aea |
233 | { |
51470298 |
234 | Raw raw = (Raw) handle; |
6226c939 |
235 | return raw->s != NULL; |
32874aea |
236 | } |
8ccc75b0 |
237 | |
51470298 |
238 | static int raw_sendok(void *handle) |
32874aea |
239 | { |
240 | return 1; |
241 | } |
4017be6d |
242 | |
51470298 |
243 | static void raw_unthrottle(void *handle, int backlog) |
5471d09a |
244 | { |
51470298 |
245 | Raw raw = (Raw) handle; |
246 | sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG); |
5471d09a |
247 | } |
248 | |
51470298 |
249 | static int raw_ldisc(void *handle, int option) |
32874aea |
250 | { |
0965bee0 |
251 | if (option == LD_EDIT || option == LD_ECHO) |
32874aea |
252 | return 1; |
0965bee0 |
253 | return 0; |
254 | } |
255 | |
b9d7bcad |
256 | static void raw_provide_ldisc(void *handle, void *ldisc) |
257 | { |
258 | /* This is a stub. */ |
259 | } |
260 | |
a8327734 |
261 | static void raw_provide_logctx(void *handle, void *logctx) |
262 | { |
263 | /* This is a stub. */ |
264 | } |
265 | |
51470298 |
266 | static int raw_exitcode(void *handle) |
d8d6c7e5 |
267 | { |
0da1a790 |
268 | Raw raw = (Raw) handle; |
269 | if (raw->s != NULL) |
270 | return -1; /* still connected */ |
271 | else |
272 | /* Exit codes are a meaningless concept in the Raw protocol */ |
273 | return 0; |
d8d6c7e5 |
274 | } |
275 | |
f89c3294 |
276 | /* |
277 | * cfg_info for Raw does nothing at all. |
278 | */ |
279 | static int raw_cfg_info(void *handle) |
280 | { |
281 | return 0; |
282 | } |
283 | |
5e1a8e27 |
284 | Backend raw_backend = { |
285 | raw_init, |
fabd1805 |
286 | raw_free, |
86916870 |
287 | raw_reconfig, |
5e1a8e27 |
288 | raw_send, |
5471d09a |
289 | raw_sendbuffer, |
5e1a8e27 |
290 | raw_size, |
4017be6d |
291 | raw_special, |
125105d1 |
292 | raw_get_specials, |
6226c939 |
293 | raw_connected, |
d8d6c7e5 |
294 | raw_exitcode, |
97db3be4 |
295 | raw_sendok, |
0965bee0 |
296 | raw_ldisc, |
b9d7bcad |
297 | raw_provide_ldisc, |
a8327734 |
298 | raw_provide_logctx, |
5471d09a |
299 | raw_unthrottle, |
f89c3294 |
300 | raw_cfg_info, |
9e164d82 |
301 | "raw", |
302 | PROT_RAW, |
a4451dd1 |
303 | 0 |
5e1a8e27 |
304 | }; |