Support for doing DNS at the proxy end. I've invented a new type of
[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 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;
94 buf = dupprintf("Looking up host \"%s\"", host);
95 logevent(raw->frontend, buf);
96 sfree(buf);
97 }
98 addr = name_lookup(host, port, realhost);
99 if ((err = sk_addr_error(addr)))
100 return err;
101
102 if (port < 0)
103 port = 23; /* default telnet port */
104
105 /*
106 * Open socket.
107 */
108 {
109 char *buf, addrbuf[100];
110 sk_getaddr(addr, addrbuf, 100);
111 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
112 logevent(raw->frontend, buf);
113 sfree(buf);
114 }
115 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, (Plug) raw);
116 if ((err = sk_socket_error(raw->s)))
117 return err;
118
119 sk_addr_free(addr);
120
121 return NULL;
122 }
123
124 /*
125 * Called to send data down the raw connection.
126 */
127 static int raw_send(void *handle, char *buf, int len)
128 {
129 Raw raw = (Raw) handle;
130
131 if (raw->s == NULL)
132 return 0;
133
134 raw->bufsize = sk_write(raw->s, buf, len);
135
136 return raw->bufsize;
137 }
138
139 /*
140 * Called to query the current socket sendability status.
141 */
142 static int raw_sendbuffer(void *handle)
143 {
144 Raw raw = (Raw) handle;
145 return raw->bufsize;
146 }
147
148 /*
149 * Called to set the size of the window
150 */
151 static void raw_size(void *handle, int width, int height)
152 {
153 /* Do nothing! */
154 return;
155 }
156
157 /*
158 * Send raw special codes.
159 */
160 static void raw_special(void *handle, Telnet_Special code)
161 {
162 /* Do nothing! */
163 return;
164 }
165
166 static Socket raw_socket(void *handle)
167 {
168 Raw raw = (Raw) handle;
169 return raw->s;
170 }
171
172 static int raw_sendok(void *handle)
173 {
174 return 1;
175 }
176
177 static void raw_unthrottle(void *handle, int backlog)
178 {
179 Raw raw = (Raw) handle;
180 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
181 }
182
183 static int raw_ldisc(void *handle, int option)
184 {
185 if (option == LD_EDIT || option == LD_ECHO)
186 return 1;
187 return 0;
188 }
189
190 static void raw_provide_ldisc(void *handle, void *ldisc)
191 {
192 /* This is a stub. */
193 }
194
195 static void raw_provide_logctx(void *handle, void *logctx)
196 {
197 /* This is a stub. */
198 }
199
200 static int raw_exitcode(void *handle)
201 {
202 /* Exit codes are a meaningless concept in the Raw protocol */
203 return 0;
204 }
205
206 Backend raw_backend = {
207 raw_init,
208 raw_send,
209 raw_sendbuffer,
210 raw_size,
211 raw_special,
212 raw_socket,
213 raw_exitcode,
214 raw_sendok,
215 raw_ldisc,
216 raw_provide_ldisc,
217 raw_provide_logctx,
218 raw_unthrottle,
219 1
220 };