Make our SSH2 maximum packet size into a constant, since it's used in several
[sgt/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 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
50static 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
57static 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 */
71static 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\"%s", host,
98 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
99 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
100 "")));
101 logevent(raw->frontend, buf);
102 sfree(buf);
103 }
104 addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily);
105 if ((err = sk_addr_error(addr)) != NULL) {
106 sk_addr_free(addr);
107 return err;
108 }
109
110 if (port < 0)
111 port = 23; /* default telnet port */
112
113 /*
114 * Open socket.
115 */
116 {
117 char *buf, addrbuf[100];
118 sk_getaddr(addr, addrbuf, 100);
119 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
120 logevent(raw->frontend, buf);
121 sfree(buf);
122 }
123 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
124 (Plug) raw, cfg);
125 if ((err = sk_socket_error(raw->s)) != NULL)
126 return err;
127
128 return NULL;
129}
130
131static void raw_free(void *handle)
132{
133 Raw raw = (Raw) handle;
134
135 if (raw->s)
136 sk_close(raw->s);
137 sfree(raw);
138}
139
140/*
141 * Stub routine (we don't have any need to reconfigure this backend).
142 */
143static void raw_reconfig(void *handle, Config *cfg)
144{
145}
146
147/*
148 * Called to send data down the raw connection.
149 */
150static int raw_send(void *handle, char *buf, int len)
151{
152 Raw raw = (Raw) handle;
153
154 if (raw->s == NULL)
155 return 0;
156
157 raw->bufsize = sk_write(raw->s, buf, len);
158
159 return raw->bufsize;
160}
161
162/*
163 * Called to query the current socket sendability status.
164 */
165static int raw_sendbuffer(void *handle)
166{
167 Raw raw = (Raw) handle;
168 return raw->bufsize;
169}
170
171/*
172 * Called to set the size of the window
173 */
174static void raw_size(void *handle, int width, int height)
175{
176 /* Do nothing! */
177 return;
178}
179
180/*
181 * Send raw special codes.
182 */
183static void raw_special(void *handle, Telnet_Special code)
184{
185 /* Do nothing! */
186 return;
187}
188
189/*
190 * Return a list of the special codes that make sense in this
191 * protocol.
192 */
193static const struct telnet_special *raw_get_specials(void *handle)
194{
195 return NULL;
196}
197
198static Socket raw_socket(void *handle)
199{
200 Raw raw = (Raw) handle;
201 return raw->s;
202}
203
204static int raw_sendok(void *handle)
205{
206 return 1;
207}
208
209static void raw_unthrottle(void *handle, int backlog)
210{
211 Raw raw = (Raw) handle;
212 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
213}
214
215static int raw_ldisc(void *handle, int option)
216{
217 if (option == LD_EDIT || option == LD_ECHO)
218 return 1;
219 return 0;
220}
221
222static void raw_provide_ldisc(void *handle, void *ldisc)
223{
224 /* This is a stub. */
225}
226
227static void raw_provide_logctx(void *handle, void *logctx)
228{
229 /* This is a stub. */
230}
231
232static int raw_exitcode(void *handle)
233{
234 Raw raw = (Raw) handle;
235 if (raw->s != NULL)
236 return -1; /* still connected */
237 else
238 /* Exit codes are a meaningless concept in the Raw protocol */
239 return 0;
240}
241
242/*
243 * cfg_info for Raw does nothing at all.
244 */
245static int raw_cfg_info(void *handle)
246{
247 return 0;
248}
249
250Backend raw_backend = {
251 raw_init,
252 raw_free,
253 raw_reconfig,
254 raw_send,
255 raw_sendbuffer,
256 raw_size,
257 raw_special,
258 raw_get_specials,
259 raw_socket,
260 raw_exitcode,
261 raw_sendok,
262 raw_ldisc,
263 raw_provide_ldisc,
264 raw_provide_logctx,
265 raw_unthrottle,
266 raw_cfg_info,
267 1
268};