]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - raw.c
8b1b1e9b15e9e843e9854f775c95d358f6ab8ab1
[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 int raw_closing(Plug plug, const char *error_msg, int error_code,
33                        int calling_back)
34 {
35     Raw raw = (Raw) plug;
36
37     if (raw->s) {
38         sk_close(raw->s);
39         raw->s = NULL;
40         notify_remote_exit(raw->frontend);
41     }
42     if (error_msg) {
43         /* A socket error has occurred. */
44         logevent(raw->frontend, error_msg);
45         connection_fatal(raw->frontend, "%s", error_msg);
46     }                                  /* Otherwise, the remote side closed the connection normally. */
47     return 0;
48 }
49
50 static int raw_receive(Plug plug, int urgent, char *data, int len)
51 {
52     Raw raw = (Raw) plug;
53     c_write(raw, data, len);
54     return 1;
55 }
56
57 static void raw_sent(Plug plug, int bufsize)
58 {
59     Raw raw = (Raw) plug;
60     raw->bufsize = bufsize;
61 }
62
63 /*
64  * Called to set up the raw connection.
65  * 
66  * Returns an error message, or NULL on success.
67  *
68  * Also places the canonical host name into `realhost'. It must be
69  * freed by the caller.
70  */
71 static const char *raw_init(void *frontend_handle, void **backend_handle,
72                             Config *cfg,
73                             char *host, int port, char **realhost, int nodelay,
74                             int keepalive)
75 {
76     static const struct plug_function_table fn_table = {
77         raw_closing,
78         raw_receive,
79         raw_sent
80     };
81     SockAddr addr;
82     const char *err;
83     Raw raw;
84
85     raw = snew(struct raw_backend_data);
86     raw->fn = &fn_table;
87     raw->s = NULL;
88     *backend_handle = raw;
89
90     raw->frontend = frontend_handle;
91
92     /*
93      * Try to find host.
94      */
95     {
96         char *buf;
97         buf = dupprintf("Looking up host \"%s\"", host);
98         logevent(raw->frontend, buf);
99         sfree(buf);
100     }
101     addr = name_lookup(host, port, realhost, cfg);
102     if ((err = sk_addr_error(addr)) != NULL) {
103         sk_addr_free(addr);
104         return err;
105     }
106
107     if (port < 0)
108         port = 23;                     /* default telnet port */
109
110     /*
111      * Open socket.
112      */
113     {
114         char *buf, addrbuf[100];
115         sk_getaddr(addr, addrbuf, 100);
116         buf = dupprintf("Connecting to %s port %d", addrbuf, port);
117         logevent(raw->frontend, buf);
118         sfree(buf);
119     }
120     raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
121                             (Plug) raw, cfg);
122     if ((err = sk_socket_error(raw->s)) != NULL)
123         return err;
124
125     return NULL;
126 }
127
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
137 /*
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 /*
145  * Called to send data down the raw connection.
146  */
147 static int raw_send(void *handle, char *buf, int len)
148 {
149     Raw raw = (Raw) handle;
150
151     if (raw->s == NULL)
152         return 0;
153
154     raw->bufsize = sk_write(raw->s, buf, len);
155
156     return raw->bufsize;
157 }
158
159 /*
160  * Called to query the current socket sendability status.
161  */
162 static int raw_sendbuffer(void *handle)
163 {
164     Raw raw = (Raw) handle;
165     return raw->bufsize;
166 }
167
168 /*
169  * Called to set the size of the window
170  */
171 static void raw_size(void *handle, int width, int height)
172 {
173     /* Do nothing! */
174     return;
175 }
176
177 /*
178  * Send raw special codes.
179  */
180 static void raw_special(void *handle, Telnet_Special code)
181 {
182     /* Do nothing! */
183     return;
184 }
185
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
195 static Socket raw_socket(void *handle)
196 {
197     Raw raw = (Raw) handle;
198     return raw->s;
199 }
200
201 static int raw_sendok(void *handle)
202 {
203     return 1;
204 }
205
206 static void raw_unthrottle(void *handle, int backlog)
207 {
208     Raw raw = (Raw) handle;
209     sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
210 }
211
212 static int raw_ldisc(void *handle, int option)
213 {
214     if (option == LD_EDIT || option == LD_ECHO)
215         return 1;
216     return 0;
217 }
218
219 static void raw_provide_ldisc(void *handle, void *ldisc)
220 {
221     /* This is a stub. */
222 }
223
224 static void raw_provide_logctx(void *handle, void *logctx)
225 {
226     /* This is a stub. */
227 }
228
229 static int raw_exitcode(void *handle)
230 {
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;
237 }
238
239 Backend raw_backend = {
240     raw_init,
241     raw_free,
242     raw_reconfig,
243     raw_send,
244     raw_sendbuffer,
245     raw_size,
246     raw_special,
247     raw_get_specials,
248     raw_socket,
249     raw_exitcode,
250     raw_sendok,
251     raw_ldisc,
252     raw_provide_ldisc,
253     raw_provide_logctx,
254     raw_unthrottle,
255     1
256 };