OSDN Git Service

Enhance all settings encryption.
[ffftp/ffftp.git] / putty / RAW.C
1 /*\r
2  * "Raw" backend.\r
3  */\r
4 \r
5 #include <stdio.h>\r
6 #include <stdlib.h>\r
7 #include <limits.h>\r
8 \r
9 #include "putty.h"\r
10 \r
11 #ifndef FALSE\r
12 #define FALSE 0\r
13 #endif\r
14 #ifndef TRUE\r
15 #define TRUE 1\r
16 #endif\r
17 \r
18 #define RAW_MAX_BACKLOG 4096\r
19 \r
20 typedef struct raw_backend_data {\r
21     const struct plug_function_table *fn;\r
22     /* the above field _must_ be first in the structure */\r
23 \r
24     Socket s;\r
25     int closed_on_socket_error;\r
26     int bufsize;\r
27     void *frontend;\r
28     int sent_console_eof, sent_socket_eof;\r
29 } *Raw;\r
30 \r
31 static void raw_size(void *handle, int width, int height);\r
32 \r
33 static void c_write(Raw raw, char *buf, int len)\r
34 {\r
35     int backlog = from_backend(raw->frontend, 0, buf, len);\r
36     sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);\r
37 }\r
38 \r
39 static void raw_log(Plug plug, int type, SockAddr addr, int port,\r
40                     const char *error_msg, int error_code)\r
41 {\r
42     Raw raw = (Raw) plug;\r
43     char addrbuf[256], *msg;\r
44 \r
45     sk_getaddr(addr, addrbuf, lenof(addrbuf));\r
46 \r
47     if (type == 0)\r
48         msg = dupprintf("Connecting to %s port %d", addrbuf, port);\r
49     else\r
50         msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);\r
51 \r
52     logevent(raw->frontend, msg);\r
53     sfree(msg);\r
54 }\r
55 \r
56 static void raw_check_close(Raw raw)\r
57 {\r
58     /*\r
59      * Called after we send EOF on either the socket or the console.\r
60      * Its job is to wind up the session once we have sent EOF on both.\r
61      */\r
62     if (raw->sent_console_eof && raw->sent_socket_eof) {\r
63         if (raw->s) {\r
64             sk_close(raw->s);\r
65             raw->s = NULL;\r
66             notify_remote_exit(raw->frontend);\r
67         }\r
68     }\r
69 }\r
70 \r
71 static int raw_closing(Plug plug, const char *error_msg, int error_code,\r
72                        int calling_back)\r
73 {\r
74     Raw raw = (Raw) plug;\r
75 \r
76     if (error_msg) {\r
77         /* A socket error has occurred. */\r
78         if (raw->s) {\r
79             sk_close(raw->s);\r
80             raw->s = NULL;\r
81             raw->closed_on_socket_error = TRUE;\r
82             notify_remote_exit(raw->frontend);\r
83         }\r
84         logevent(raw->frontend, error_msg);\r
85         connection_fatal(raw->frontend, "%s", error_msg);\r
86     } else {\r
87         /* Otherwise, the remote side closed the connection normally. */\r
88         if (!raw->sent_console_eof && from_backend_eof(raw->frontend)) {\r
89             /*\r
90              * The front end wants us to close the outgoing side of the\r
91              * connection as soon as we see EOF from the far end.\r
92              */\r
93             if (!raw->sent_socket_eof) {\r
94                 if (raw->s)\r
95                     sk_write_eof(raw->s);\r
96                 raw->sent_socket_eof= TRUE;\r
97             }\r
98         }\r
99         raw->sent_console_eof = TRUE;\r
100         raw_check_close(raw);\r
101     }\r
102     return 0;\r
103 }\r
104 \r
105 static int raw_receive(Plug plug, int urgent, char *data, int len)\r
106 {\r
107     Raw raw = (Raw) plug;\r
108     c_write(raw, data, len);\r
109     return 1;\r
110 }\r
111 \r
112 static void raw_sent(Plug plug, int bufsize)\r
113 {\r
114     Raw raw = (Raw) plug;\r
115     raw->bufsize = bufsize;\r
116 }\r
117 \r
118 /*\r
119  * Called to set up the raw connection.\r
120  * \r
121  * Returns an error message, or NULL on success.\r
122  *\r
123  * Also places the canonical host name into `realhost'. It must be\r
124  * freed by the caller.\r
125  */\r
126 static const char *raw_init(void *frontend_handle, void **backend_handle,\r
127                             Conf *conf,\r
128                             char *host, int port, char **realhost, int nodelay,\r
129                             int keepalive)\r
130 {\r
131     static const struct plug_function_table fn_table = {\r
132         raw_log,\r
133         raw_closing,\r
134         raw_receive,\r
135         raw_sent\r
136     };\r
137     SockAddr addr;\r
138     const char *err;\r
139     Raw raw;\r
140     int addressfamily;\r
141     char *loghost;\r
142 \r
143     raw = snew(struct raw_backend_data);\r
144     raw->fn = &fn_table;\r
145     raw->s = NULL;\r
146     raw->closed_on_socket_error = FALSE;\r
147     *backend_handle = raw;\r
148     raw->sent_console_eof = raw->sent_socket_eof = FALSE;\r
149 \r
150     raw->frontend = frontend_handle;\r
151 \r
152     addressfamily = conf_get_int(conf, CONF_addressfamily);\r
153     /*\r
154      * Try to find host.\r
155      */\r
156     {\r
157         char *buf;\r
158         buf = dupprintf("Looking up host \"%s\"%s", host,\r
159                         (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :\r
160                          (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :\r
161                           "")));\r
162         logevent(raw->frontend, buf);\r
163         sfree(buf);\r
164     }\r
165     addr = name_lookup(host, port, realhost, conf, addressfamily);\r
166     if ((err = sk_addr_error(addr)) != NULL) {\r
167         sk_addr_free(addr);\r
168         return err;\r
169     }\r
170 \r
171     if (port < 0)\r
172         port = 23;                     /* default telnet port */\r
173 \r
174     /*\r
175      * Open socket.\r
176      */\r
177     raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,\r
178                             (Plug) raw, conf);\r
179     if ((err = sk_socket_error(raw->s)) != NULL)\r
180         return err;\r
181 \r
182     loghost = conf_get_str(conf, CONF_loghost);\r
183     if (*loghost) {\r
184         char *colon;\r
185 \r
186         sfree(*realhost);\r
187         *realhost = dupstr(loghost);\r
188         colon = strrchr(*realhost, ':');\r
189         if (colon) {\r
190             /*\r
191              * FIXME: if we ever update this aspect of ssh.c for\r
192              * IPv6 literal management, this should change in line\r
193              * with it.\r
194              */\r
195             *colon++ = '\0';\r
196         }\r
197     }\r
198 \r
199     return NULL;\r
200 }\r
201 \r
202 static void raw_free(void *handle)\r
203 {\r
204     Raw raw = (Raw) handle;\r
205 \r
206     if (raw->s)\r
207         sk_close(raw->s);\r
208     sfree(raw);\r
209 }\r
210 \r
211 /*\r
212  * Stub routine (we don't have any need to reconfigure this backend).\r
213  */\r
214 static void raw_reconfig(void *handle, Conf *conf)\r
215 {\r
216 }\r
217 \r
218 /*\r
219  * Called to send data down the raw connection.\r
220  */\r
221 static int raw_send(void *handle, char *buf, int len)\r
222 {\r
223     Raw raw = (Raw) handle;\r
224 \r
225     if (raw->s == NULL)\r
226         return 0;\r
227 \r
228     raw->bufsize = sk_write(raw->s, buf, len);\r
229 \r
230     return raw->bufsize;\r
231 }\r
232 \r
233 /*\r
234  * Called to query the current socket sendability status.\r
235  */\r
236 static int raw_sendbuffer(void *handle)\r
237 {\r
238     Raw raw = (Raw) handle;\r
239     return raw->bufsize;\r
240 }\r
241 \r
242 /*\r
243  * Called to set the size of the window\r
244  */\r
245 static void raw_size(void *handle, int width, int height)\r
246 {\r
247     /* Do nothing! */\r
248     return;\r
249 }\r
250 \r
251 /*\r
252  * Send raw special codes. We only handle outgoing EOF here.\r
253  */\r
254 static void raw_special(void *handle, Telnet_Special code)\r
255 {\r
256     Raw raw = (Raw) handle;\r
257     if (code == TS_EOF && raw->s) {\r
258         sk_write_eof(raw->s);\r
259         raw->sent_socket_eof= TRUE;\r
260         raw_check_close(raw);\r
261     }\r
262 \r
263     return;\r
264 }\r
265 \r
266 /*\r
267  * Return a list of the special codes that make sense in this\r
268  * protocol.\r
269  */\r
270 static const struct telnet_special *raw_get_specials(void *handle)\r
271 {\r
272     return NULL;\r
273 }\r
274 \r
275 static int raw_connected(void *handle)\r
276 {\r
277     Raw raw = (Raw) handle;\r
278     return raw->s != NULL;\r
279 }\r
280 \r
281 static int raw_sendok(void *handle)\r
282 {\r
283     return 1;\r
284 }\r
285 \r
286 static void raw_unthrottle(void *handle, int backlog)\r
287 {\r
288     Raw raw = (Raw) handle;\r
289     sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);\r
290 }\r
291 \r
292 static int raw_ldisc(void *handle, int option)\r
293 {\r
294     if (option == LD_EDIT || option == LD_ECHO)\r
295         return 1;\r
296     return 0;\r
297 }\r
298 \r
299 static void raw_provide_ldisc(void *handle, void *ldisc)\r
300 {\r
301     /* This is a stub. */\r
302 }\r
303 \r
304 static void raw_provide_logctx(void *handle, void *logctx)\r
305 {\r
306     /* This is a stub. */\r
307 }\r
308 \r
309 static int raw_exitcode(void *handle)\r
310 {\r
311     Raw raw = (Raw) handle;\r
312     if (raw->s != NULL)\r
313         return -1;                     /* still connected */\r
314     else if (raw->closed_on_socket_error)\r
315         return INT_MAX;     /* a socket error counts as an unclean exit */\r
316     else\r
317         /* Exit codes are a meaningless concept in the Raw protocol */\r
318         return 0;\r
319 }\r
320 \r
321 /*\r
322  * cfg_info for Raw does nothing at all.\r
323  */\r
324 static int raw_cfg_info(void *handle)\r
325 {\r
326     return 0;\r
327 }\r
328 \r
329 Backend raw_backend = {\r
330     raw_init,\r
331     raw_free,\r
332     raw_reconfig,\r
333     raw_send,\r
334     raw_sendbuffer,\r
335     raw_size,\r
336     raw_special,\r
337     raw_get_specials,\r
338     raw_connected,\r
339     raw_exitcode,\r
340     raw_sendok,\r
341     raw_ldisc,\r
342     raw_provide_ldisc,\r
343     raw_provide_logctx,\r
344     raw_unthrottle,\r
345     raw_cfg_info,\r
346     "raw",\r
347     PROT_RAW,\r
348     0\r
349 };\r