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