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