Add a configuration option for TCP keepalives (SO_KEEPALIVE), default off.
[u/mdw/putty] / raw.c
CommitLineData
5e1a8e27 1#include <stdio.h>
2#include <stdlib.h>
5e1a8e27 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
5471d09a 13#define RAW_MAX_BACKLOG 4096
14
51470298 15typedef struct raw_backend_data {
16 const struct plug_function_table *fn;
17 /* the above field _must_ be first in the structure */
5e1a8e27 18
51470298 19 Socket s;
20 int bufsize;
21 void *frontend;
22} *Raw;
5e1a8e27 23
51470298 24static void raw_size(void *handle, int width, int height);
25
26static void c_write(Raw raw, char *buf, int len)
32874aea 27{
51470298 28 int backlog = from_backend(raw->frontend, 0, buf, len);
29 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
5e1a8e27 30}
31
cbe2d68f 32static int raw_closing(Plug plug, const char *error_msg, int error_code,
32874aea 33 int calling_back)
34{
51470298 35 Raw raw = (Raw) plug;
36
37 if (raw->s) {
38 sk_close(raw->s);
39 raw->s = NULL;
f3ab576e 40 }
7e78000d 41 if (error_msg) {
32874aea 42 /* A socket error has occurred. */
a8327734 43 logevent(raw->frontend, error_msg);
971bcc0a 44 connection_fatal(raw->frontend, "%s", error_msg);
32874aea 45 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 46 return 0;
47}
48
32874aea 49static int raw_receive(Plug plug, int urgent, char *data, int len)
50{
51470298 51 Raw raw = (Raw) plug;
52 c_write(raw, data, len);
8df7a775 53 return 1;
5e1a8e27 54}
55
3ad9d396 56static void raw_sent(Plug plug, int bufsize)
57{
51470298 58 Raw raw = (Raw) plug;
59 raw->bufsize = bufsize;
3ad9d396 60}
61
5e1a8e27 62/*
8df7a775 63 * Called to set up the raw connection.
64 *
5e1a8e27 65 * Returns an error message, or NULL on success.
66 *
6e1ebb76 67 * Also places the canonical host name into `realhost'. It must be
68 * freed by the caller.
5e1a8e27 69 */
cbe2d68f 70static const char *raw_init(void *frontend_handle, void **backend_handle,
71 Config *cfg,
79bf227b 72 char *host, int port, char **realhost, int nodelay,
73 int keepalive)
32874aea 74{
51470298 75 static const struct plug_function_table fn_table = {
7e78000d 76 raw_closing,
3ad9d396 77 raw_receive,
78 raw_sent
51470298 79 };
8df7a775 80 SockAddr addr;
cbe2d68f 81 const char *err;
51470298 82 Raw raw;
5e1a8e27 83
3d88e64d 84 raw = snew(struct raw_backend_data);
51470298 85 raw->fn = &fn_table;
86 raw->s = NULL;
87 *backend_handle = raw;
88
89 raw->frontend = frontend_handle;
887035a5 90
5e1a8e27 91 /*
92 * Try to find host.
93 */
3ad9d396 94 {
57356d63 95 char *buf;
96 buf = dupprintf("Looking up host \"%s\"", host);
a8327734 97 logevent(raw->frontend, buf);
57356d63 98 sfree(buf);
3ad9d396 99 }
e8fa8f62 100 addr = name_lookup(host, port, realhost, cfg);
f85e6f6e 101 if ((err = sk_addr_error(addr)) != NULL) {
102 sk_addr_free(addr);
8df7a775 103 return err;
f85e6f6e 104 }
5e1a8e27 105
106 if (port < 0)
107 port = 23; /* default telnet port */
108
109 /*
110 * Open socket.
111 */
3ad9d396 112 {
57356d63 113 char *buf, addrbuf[100];
3ad9d396 114 sk_getaddr(addr, addrbuf, 100);
57356d63 115 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
a8327734 116 logevent(raw->frontend, buf);
57356d63 117 sfree(buf);
3ad9d396 118 }
79bf227b 119 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
e8fa8f62 120 (Plug) raw, cfg);
6f1e7b78 121 if ((err = sk_socket_error(raw->s)) != NULL)
8df7a775 122 return err;
5e1a8e27 123
5e1a8e27 124 return NULL;
125}
126
fabd1805 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
5e1a8e27 136/*
86916870 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/*
5e1a8e27 144 * Called to send data down the raw connection.
145 */
51470298 146static int raw_send(void *handle, char *buf, int len)
32874aea 147{
51470298 148 Raw raw = (Raw) handle;
149
150 if (raw->s == NULL)
b5a460cd 151 return 0;
5e1a8e27 152
51470298 153 raw->bufsize = sk_write(raw->s, buf, len);
5471d09a 154
51470298 155 return raw->bufsize;
5471d09a 156}
157
158/*
159 * Called to query the current socket sendability status.
160 */
51470298 161static int raw_sendbuffer(void *handle)
5471d09a 162{
51470298 163 Raw raw = (Raw) handle;
164 return raw->bufsize;
5e1a8e27 165}
166
167/*
168 * Called to set the size of the window
169 */
51470298 170static void raw_size(void *handle, int width, int height)
32874aea 171{
5e1a8e27 172 /* Do nothing! */
173 return;
174}
175
176/*
177 * Send raw special codes.
178 */
51470298 179static void raw_special(void *handle, Telnet_Special code)
32874aea 180{
5e1a8e27 181 /* Do nothing! */
182 return;
183}
184
125105d1 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
51470298 194static Socket raw_socket(void *handle)
32874aea 195{
51470298 196 Raw raw = (Raw) handle;
197 return raw->s;
32874aea 198}
8ccc75b0 199
51470298 200static int raw_sendok(void *handle)
32874aea 201{
202 return 1;
203}
4017be6d 204
51470298 205static void raw_unthrottle(void *handle, int backlog)
5471d09a 206{
51470298 207 Raw raw = (Raw) handle;
208 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
5471d09a 209}
210
51470298 211static int raw_ldisc(void *handle, int option)
32874aea 212{
0965bee0 213 if (option == LD_EDIT || option == LD_ECHO)
32874aea 214 return 1;
0965bee0 215 return 0;
216}
217
b9d7bcad 218static void raw_provide_ldisc(void *handle, void *ldisc)
219{
220 /* This is a stub. */
221}
222
a8327734 223static void raw_provide_logctx(void *handle, void *logctx)
224{
225 /* This is a stub. */
226}
227
51470298 228static int raw_exitcode(void *handle)
d8d6c7e5 229{
0da1a790 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;
d8d6c7e5 236}
237
5e1a8e27 238Backend raw_backend = {
239 raw_init,
fabd1805 240 raw_free,
86916870 241 raw_reconfig,
5e1a8e27 242 raw_send,
5471d09a 243 raw_sendbuffer,
5e1a8e27 244 raw_size,
4017be6d 245 raw_special,
125105d1 246 raw_get_specials,
8ccc75b0 247 raw_socket,
d8d6c7e5 248 raw_exitcode,
97db3be4 249 raw_sendok,
0965bee0 250 raw_ldisc,
b9d7bcad 251 raw_provide_ldisc,
a8327734 252 raw_provide_logctx,
5471d09a 253 raw_unthrottle,
97db3be4 254 1
5e1a8e27 255};