pterm.c now relies on backend `exitcode' functions returning <0 when
[u/mdw/putty] / 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, 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 }
41 if (error_msg) {
42 /* A socket error has occurred. */
43 logevent(raw->frontend, error_msg);
44 connection_fatal("%s", error_msg);
45 } /* Otherwise, the remote side closed the connection normally. */
46 return 0;
47 }
48
49 static int raw_receive(Plug plug, int urgent, char *data, int len)
50 {
51 Raw raw = (Raw) plug;
52 c_write(raw, data, len);
53 return 1;
54 }
55
56 static void raw_sent(Plug plug, int bufsize)
57 {
58 Raw raw = (Raw) plug;
59 raw->bufsize = bufsize;
60 }
61
62 /*
63 * Called to set up the raw connection.
64 *
65 * Returns an error message, or NULL on success.
66 *
67 * Also places the canonical host name into `realhost'. It must be
68 * freed by the caller.
69 */
70 static char *raw_init(void *frontend_handle, void **backend_handle,
71 Config *cfg,
72 char *host, int port, char **realhost, int nodelay)
73 {
74 static const struct plug_function_table fn_table = {
75 raw_closing,
76 raw_receive,
77 raw_sent
78 };
79 SockAddr addr;
80 char *err;
81 Raw raw;
82
83 raw = snew(struct raw_backend_data);
84 raw->fn = &fn_table;
85 raw->s = NULL;
86 *backend_handle = raw;
87
88 raw->frontend = frontend_handle;
89
90 /*
91 * Try to find host.
92 */
93 {
94 char *buf;
95 buf = dupprintf("Looking up host \"%s\"", host);
96 logevent(raw->frontend, buf);
97 sfree(buf);
98 }
99 addr = name_lookup(host, port, realhost, cfg);
100 if ((err = sk_addr_error(addr)) != NULL)
101 return err;
102
103 if (port < 0)
104 port = 23; /* default telnet port */
105
106 /*
107 * Open socket.
108 */
109 {
110 char *buf, addrbuf[100];
111 sk_getaddr(addr, addrbuf, 100);
112 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
113 logevent(raw->frontend, buf);
114 sfree(buf);
115 }
116 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay,
117 (Plug) raw, cfg);
118 if ((err = sk_socket_error(raw->s)) != NULL)
119 return err;
120
121 sk_addr_free(addr);
122
123 return NULL;
124 }
125
126 static void raw_free(void *handle)
127 {
128 Raw raw = (Raw) handle;
129
130 if (raw->s)
131 sk_close(raw->s);
132 sfree(raw);
133 }
134
135 /*
136 * Stub routine (we don't have any need to reconfigure this backend).
137 */
138 static void raw_reconfig(void *handle, Config *cfg)
139 {
140 }
141
142 /*
143 * Called to send data down the raw connection.
144 */
145 static int raw_send(void *handle, char *buf, int len)
146 {
147 Raw raw = (Raw) handle;
148
149 if (raw->s == NULL)
150 return 0;
151
152 raw->bufsize = sk_write(raw->s, buf, len);
153
154 return raw->bufsize;
155 }
156
157 /*
158 * Called to query the current socket sendability status.
159 */
160 static int raw_sendbuffer(void *handle)
161 {
162 Raw raw = (Raw) handle;
163 return raw->bufsize;
164 }
165
166 /*
167 * Called to set the size of the window
168 */
169 static void raw_size(void *handle, int width, int height)
170 {
171 /* Do nothing! */
172 return;
173 }
174
175 /*
176 * Send raw special codes.
177 */
178 static void raw_special(void *handle, Telnet_Special code)
179 {
180 /* Do nothing! */
181 return;
182 }
183
184 static Socket raw_socket(void *handle)
185 {
186 Raw raw = (Raw) handle;
187 return raw->s;
188 }
189
190 static int raw_sendok(void *handle)
191 {
192 return 1;
193 }
194
195 static void raw_unthrottle(void *handle, int backlog)
196 {
197 Raw raw = (Raw) handle;
198 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
199 }
200
201 static int raw_ldisc(void *handle, int option)
202 {
203 if (option == LD_EDIT || option == LD_ECHO)
204 return 1;
205 return 0;
206 }
207
208 static void raw_provide_ldisc(void *handle, void *ldisc)
209 {
210 /* This is a stub. */
211 }
212
213 static void raw_provide_logctx(void *handle, void *logctx)
214 {
215 /* This is a stub. */
216 }
217
218 static int raw_exitcode(void *handle)
219 {
220 Raw raw = (Raw) handle;
221 if (raw->s != NULL)
222 return -1; /* still connected */
223 else
224 /* Exit codes are a meaningless concept in the Raw protocol */
225 return 0;
226 }
227
228 Backend raw_backend = {
229 raw_init,
230 raw_free,
231 raw_reconfig,
232 raw_send,
233 raw_sendbuffer,
234 raw_size,
235 raw_special,
236 raw_socket,
237 raw_exitcode,
238 raw_sendok,
239 raw_ldisc,
240 raw_provide_ldisc,
241 raw_provide_logctx,
242 raw_unthrottle,
243 1
244 };