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