CPatch: New memory management
[TortoiseGit.git] / src / TortoisePlink / RAW.C
blob1aa20f89b331dcbbe51f12e617d95231414843fe
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
11 #ifndef FALSE\r
12 #define FALSE 0\r
13 #endif\r
14 #ifndef TRUE\r
15 #define TRUE 1\r
16 #endif\r
18 #define RAW_MAX_BACKLOG 4096\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
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, session_started;\r
30     Conf *conf;\r
31 } *Raw;\r
33 static void raw_size(void *handle, int width, int height);\r
35 static void c_write(Raw raw, char *buf, int len)\r
36 {\r
37     int backlog = from_backend(raw->frontend, 0, buf, len);\r
38     sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);\r
39 }\r
41 static void raw_log(Plug plug, int type, SockAddr addr, int port,\r
42                     const char *error_msg, int error_code)\r
43 {\r
44     Raw raw = (Raw) plug;\r
45     backend_socket_log(raw->frontend, type, addr, port,\r
46                        error_msg, error_code, raw->conf, raw->session_started);\r
47 }\r
49 static void raw_check_close(Raw raw)\r
50 {\r
51     /*\r
52      * Called after we send EOF on either the socket or the console.\r
53      * Its job is to wind up the session once we have sent EOF on both.\r
54      */\r
55     if (raw->sent_console_eof && raw->sent_socket_eof) {\r
56         if (raw->s) {\r
57             sk_close(raw->s);\r
58             raw->s = NULL;\r
59             notify_remote_exit(raw->frontend);\r
60         }\r
61     }\r
62 }\r
64 static void raw_closing(Plug plug, const char *error_msg, int error_code,\r
65                         int calling_back)\r
66 {\r
67     Raw raw = (Raw) plug;\r
69     if (error_msg) {\r
70         /* A socket error has occurred. */\r
71         if (raw->s) {\r
72             sk_close(raw->s);\r
73             raw->s = NULL;\r
74             raw->closed_on_socket_error = TRUE;\r
75             notify_remote_exit(raw->frontend);\r
76         }\r
77         logevent(raw->frontend, error_msg);\r
78         connection_fatal(raw->frontend, "%s", error_msg);\r
79     } else {\r
80         /* Otherwise, the remote side closed the connection normally. */\r
81         if (!raw->sent_console_eof && from_backend_eof(raw->frontend)) {\r
82             /*\r
83              * The front end wants us to close the outgoing side of the\r
84              * connection as soon as we see EOF from the far end.\r
85              */\r
86             if (!raw->sent_socket_eof) {\r
87                 if (raw->s)\r
88                     sk_write_eof(raw->s);\r
89                 raw->sent_socket_eof= TRUE;\r
90             }\r
91         }\r
92         raw->sent_console_eof = TRUE;\r
93         raw_check_close(raw);\r
94     }\r
95 }\r
97 static void raw_receive(Plug plug, int urgent, char *data, int len)\r
98 {\r
99     Raw raw = (Raw) plug;\r
100     c_write(raw, data, len);\r
101     /* We count 'session start', for proxy logging purposes, as being\r
102      * when data is received from the network and printed. */\r
103     raw->session_started = TRUE;\r
106 static void raw_sent(Plug plug, int bufsize)\r
108     Raw raw = (Raw) plug;\r
109     raw->bufsize = bufsize;\r
112 /*\r
113  * Called to set up the raw connection.\r
114  * \r
115  * Returns an error message, or NULL on success.\r
116  *\r
117  * Also places the canonical host name into `realhost'. It must be\r
118  * freed by the caller.\r
119  */\r
120 static const char *raw_init(void *frontend_handle, void **backend_handle,\r
121                             Conf *conf,\r
122                             const char *host, int port, char **realhost,\r
123                             int nodelay, int keepalive)\r
125     static const struct plug_function_table fn_table = {\r
126         raw_log,\r
127         raw_closing,\r
128         raw_receive,\r
129         raw_sent\r
130     };\r
131     SockAddr addr;\r
132     const char *err;\r
133     Raw raw;\r
134     int addressfamily;\r
135     char *loghost;\r
137     raw = snew(struct raw_backend_data);\r
138     raw->fn = &fn_table;\r
139     raw->s = NULL;\r
140     raw->closed_on_socket_error = FALSE;\r
141     *backend_handle = raw;\r
142     raw->sent_console_eof = raw->sent_socket_eof = FALSE;\r
143     raw->bufsize = 0;\r
144     raw->session_started = FALSE;\r
145     raw->conf = conf_copy(conf);\r
147     raw->frontend = frontend_handle;\r
149     addressfamily = conf_get_int(conf, CONF_addressfamily);\r
150     /*\r
151      * Try to find host.\r
152      */\r
153     addr = name_lookup(host, port, realhost, conf, addressfamily,\r
154                        raw->frontend, "main connection");\r
155     if ((err = sk_addr_error(addr)) != NULL) {\r
156         sk_addr_free(addr);\r
157         return err;\r
158     }\r
160     if (port < 0)\r
161         port = 23;                     /* default telnet port */\r
163     /*\r
164      * Open socket.\r
165      */\r
166     raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,\r
167                             (Plug) raw, conf);\r
168     if ((err = sk_socket_error(raw->s)) != NULL)\r
169         return err;\r
171     loghost = conf_get_str(conf, CONF_loghost);\r
172     if (*loghost) {\r
173         char *colon;\r
175         sfree(*realhost);\r
176         *realhost = dupstr(loghost);\r
178         colon = host_strrchr(*realhost, ':');\r
179         if (colon)\r
180             *colon++ = '\0';\r
181     }\r
183     return NULL;\r
186 static void raw_free(void *handle)\r
188     Raw raw = (Raw) handle;\r
190     if (raw->s)\r
191         sk_close(raw->s);\r
192     conf_free(raw->conf);\r
193     sfree(raw);\r
196 /*\r
197  * Stub routine (we don't have any need to reconfigure this backend).\r
198  */\r
199 static void raw_reconfig(void *handle, Conf *conf)\r
203 /*\r
204  * Called to send data down the raw connection.\r
205  */\r
206 static int raw_send(void *handle, const char *buf, int len)\r
208     Raw raw = (Raw) handle;\r
210     if (raw->s == NULL)\r
211         return 0;\r
213     raw->bufsize = sk_write(raw->s, buf, len);\r
215     return raw->bufsize;\r
218 /*\r
219  * Called to query the current socket sendability status.\r
220  */\r
221 static int raw_sendbuffer(void *handle)\r
223     Raw raw = (Raw) handle;\r
224     return raw->bufsize;\r
227 /*\r
228  * Called to set the size of the window\r
229  */\r
230 static void raw_size(void *handle, int width, int height)\r
232     /* Do nothing! */\r
233     return;\r
236 /*\r
237  * Send raw special codes. We only handle outgoing EOF here.\r
238  */\r
239 static void raw_special(void *handle, Telnet_Special code)\r
241     Raw raw = (Raw) handle;\r
242     if (code == TS_EOF && raw->s) {\r
243         sk_write_eof(raw->s);\r
244         raw->sent_socket_eof= TRUE;\r
245         raw_check_close(raw);\r
246     }\r
248     return;\r
251 /*\r
252  * Return a list of the special codes that make sense in this\r
253  * protocol.\r
254  */\r
255 static const struct telnet_special *raw_get_specials(void *handle)\r
257     return NULL;\r
260 static int raw_connected(void *handle)\r
262     Raw raw = (Raw) handle;\r
263     return raw->s != NULL;\r
266 static int raw_sendok(void *handle)\r
268     return 1;\r
271 static void raw_unthrottle(void *handle, int backlog)\r
273     Raw raw = (Raw) handle;\r
274     sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);\r
277 static int raw_ldisc(void *handle, int option)\r
279     if (option == LD_EDIT || option == LD_ECHO)\r
280         return 1;\r
281     return 0;\r
284 static void raw_provide_ldisc(void *handle, void *ldisc)\r
286     /* This is a stub. */\r
289 static void raw_provide_logctx(void *handle, void *logctx)\r
291     /* This is a stub. */\r
294 static int raw_exitcode(void *handle)\r
296     Raw raw = (Raw) handle;\r
297     if (raw->s != NULL)\r
298         return -1;                     /* still connected */\r
299     else if (raw->closed_on_socket_error)\r
300         return INT_MAX;     /* a socket error counts as an unclean exit */\r
301     else\r
302         /* Exit codes are a meaningless concept in the Raw protocol */\r
303         return 0;\r
306 /*\r
307  * cfg_info for Raw does nothing at all.\r
308  */\r
309 static int raw_cfg_info(void *handle)\r
311     return 0;\r
314 Backend raw_backend = {\r
315     raw_init,\r
316     raw_free,\r
317     raw_reconfig,\r
318     raw_send,\r
319     raw_sendbuffer,\r
320     raw_size,\r
321     raw_special,\r
322     raw_get_specials,\r
323     raw_connected,\r
324     raw_exitcode,\r
325     raw_sendok,\r
326     raw_ldisc,\r
327     raw_provide_ldisc,\r
328     raw_provide_logctx,\r
329     raw_unthrottle,\r
330     raw_cfg_info,\r
331     NULL /* test_for_upstream */,\r
332     "raw",\r
333     PROT_RAW,\r
334     0\r
335 };\r