More preparatory work: remove the <windows.h> include from lots of
[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, 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
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 char *raw_init(void *frontend_handle, void **backend_handle,
71 char *host, int port, char **realhost, int nodelay)
72{
73 static const struct plug_function_table fn_table = {
74 raw_closing,
75 raw_receive,
76 raw_sent
77 };
78 SockAddr addr;
79 char *err;
80 Raw raw;
81
82 raw = smalloc(sizeof(*raw));
83 raw->fn = &fn_table;
84 raw->s = NULL;
85 *backend_handle = raw;
86
87 raw->frontend = frontend_handle;
88
89 /*
90 * Try to find host.
91 */
92 {
93 char buf[200];
94 sprintf(buf, "Looking up host \"%.170s\"", host);
95 logevent(raw->frontend, buf);
96 }
97 addr = sk_namelookup(host, realhost);
98 if ((err = sk_addr_error(addr)))
99 return err;
100
101 if (port < 0)
102 port = 23; /* default telnet port */
103
104 /*
105 * Open socket.
106 */
107 {
108 char buf[200], addrbuf[100];
109 sk_getaddr(addr, addrbuf, 100);
110 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
111 logevent(raw->frontend, buf);
112 }
113 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, (Plug) raw);
114 if ((err = sk_socket_error(raw->s)))
115 return err;
116
117 sk_addr_free(addr);
118
119 return NULL;
120}
121
122/*
123 * Called to send data down the raw connection.
124 */
125static int raw_send(void *handle, char *buf, int len)
126{
127 Raw raw = (Raw) handle;
128
129 if (raw->s == NULL)
130 return 0;
131
132 raw->bufsize = sk_write(raw->s, buf, len);
133
134 return raw->bufsize;
135}
136
137/*
138 * Called to query the current socket sendability status.
139 */
140static int raw_sendbuffer(void *handle)
141{
142 Raw raw = (Raw) handle;
143 return raw->bufsize;
144}
145
146/*
147 * Called to set the size of the window
148 */
149static void raw_size(void *handle, int width, int height)
150{
151 /* Do nothing! */
152 return;
153}
154
155/*
156 * Send raw special codes.
157 */
158static void raw_special(void *handle, Telnet_Special code)
159{
160 /* Do nothing! */
161 return;
162}
163
164static Socket raw_socket(void *handle)
165{
166 Raw raw = (Raw) handle;
167 return raw->s;
168}
169
170static int raw_sendok(void *handle)
171{
172 return 1;
173}
174
175static void raw_unthrottle(void *handle, int backlog)
176{
177 Raw raw = (Raw) handle;
178 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
179}
180
181static int raw_ldisc(void *handle, int option)
182{
183 if (option == LD_EDIT || option == LD_ECHO)
184 return 1;
185 return 0;
186}
187
188static void raw_provide_ldisc(void *handle, void *ldisc)
189{
190 /* This is a stub. */
191}
192
193static void raw_provide_logctx(void *handle, void *logctx)
194{
195 /* This is a stub. */
196}
197
198static int raw_exitcode(void *handle)
199{
200 /* Exit codes are a meaningless concept in the Raw protocol */
201 return 0;
202}
203
204Backend raw_backend = {
205 raw_init,
206 raw_send,
207 raw_sendbuffer,
208 raw_size,
209 raw_special,
210 raw_socket,
211 raw_exitcode,
212 raw_sendok,
213 raw_ldisc,
214 raw_provide_ldisc,
215 raw_provide_logctx,
216 raw_unthrottle,
217 1
218};