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