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