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 |
15 | typedef 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 |
24 | static void raw_size(void *handle, int width, int height); |
25 | |
26 | static 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 | |
32874aea |
32 | static int raw_closing(Plug plug, char *error_msg, int error_code, |
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); |
247308b5 |
44 | connection_fatal("%s", error_msg); |
32874aea |
45 | } /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
46 | return 0; |
47 | } |
48 | |
32874aea |
49 | static 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 |
56 | static 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 | */ |
51470298 |
70 | static char *raw_init(void *frontend_handle, void **backend_handle, |
86916870 |
71 | Config *cfg, |
51470298 |
72 | char *host, int port, char **realhost, int nodelay) |
32874aea |
73 | { |
51470298 |
74 | static const struct plug_function_table fn_table = { |
7e78000d |
75 | raw_closing, |
3ad9d396 |
76 | raw_receive, |
77 | raw_sent |
51470298 |
78 | }; |
8df7a775 |
79 | SockAddr addr; |
80 | char *err; |
51470298 |
81 | Raw raw; |
5e1a8e27 |
82 | |
51470298 |
83 | raw = smalloc(sizeof(*raw)); |
84 | raw->fn = &fn_table; |
85 | raw->s = NULL; |
86 | *backend_handle = raw; |
87 | |
88 | raw->frontend = frontend_handle; |
887035a5 |
89 | |
5e1a8e27 |
90 | /* |
91 | * Try to find host. |
92 | */ |
3ad9d396 |
93 | { |
57356d63 |
94 | char *buf; |
95 | buf = dupprintf("Looking up host \"%s\"", host); |
a8327734 |
96 | logevent(raw->frontend, buf); |
57356d63 |
97 | sfree(buf); |
3ad9d396 |
98 | } |
b7a189f3 |
99 | addr = name_lookup(host, port, realhost); |
6f1e7b78 |
100 | if ((err = sk_addr_error(addr)) != NULL) |
8df7a775 |
101 | return err; |
5e1a8e27 |
102 | |
103 | if (port < 0) |
104 | port = 23; /* default telnet port */ |
105 | |
106 | /* |
107 | * Open socket. |
108 | */ |
3ad9d396 |
109 | { |
57356d63 |
110 | char *buf, addrbuf[100]; |
3ad9d396 |
111 | sk_getaddr(addr, addrbuf, 100); |
57356d63 |
112 | buf = dupprintf("Connecting to %s port %d", addrbuf, port); |
a8327734 |
113 | logevent(raw->frontend, buf); |
57356d63 |
114 | sfree(buf); |
3ad9d396 |
115 | } |
51470298 |
116 | raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, (Plug) raw); |
6f1e7b78 |
117 | if ((err = sk_socket_error(raw->s)) != NULL) |
8df7a775 |
118 | return err; |
5e1a8e27 |
119 | |
8df7a775 |
120 | sk_addr_free(addr); |
5e1a8e27 |
121 | |
122 | return NULL; |
123 | } |
124 | |
125 | /* |
86916870 |
126 | * Stub routine (we don't have any need to reconfigure this backend). |
127 | */ |
128 | static void raw_reconfig(void *handle, Config *cfg) |
129 | { |
130 | } |
131 | |
132 | /* |
5e1a8e27 |
133 | * Called to send data down the raw connection. |
134 | */ |
51470298 |
135 | static int raw_send(void *handle, char *buf, int len) |
32874aea |
136 | { |
51470298 |
137 | Raw raw = (Raw) handle; |
138 | |
139 | if (raw->s == NULL) |
b5a460cd |
140 | return 0; |
5e1a8e27 |
141 | |
51470298 |
142 | raw->bufsize = sk_write(raw->s, buf, len); |
5471d09a |
143 | |
51470298 |
144 | return raw->bufsize; |
5471d09a |
145 | } |
146 | |
147 | /* |
148 | * Called to query the current socket sendability status. |
149 | */ |
51470298 |
150 | static int raw_sendbuffer(void *handle) |
5471d09a |
151 | { |
51470298 |
152 | Raw raw = (Raw) handle; |
153 | return raw->bufsize; |
5e1a8e27 |
154 | } |
155 | |
156 | /* |
157 | * Called to set the size of the window |
158 | */ |
51470298 |
159 | static void raw_size(void *handle, int width, int height) |
32874aea |
160 | { |
5e1a8e27 |
161 | /* Do nothing! */ |
162 | return; |
163 | } |
164 | |
165 | /* |
166 | * Send raw special codes. |
167 | */ |
51470298 |
168 | static void raw_special(void *handle, Telnet_Special code) |
32874aea |
169 | { |
5e1a8e27 |
170 | /* Do nothing! */ |
171 | return; |
172 | } |
173 | |
51470298 |
174 | static Socket raw_socket(void *handle) |
32874aea |
175 | { |
51470298 |
176 | Raw raw = (Raw) handle; |
177 | return raw->s; |
32874aea |
178 | } |
8ccc75b0 |
179 | |
51470298 |
180 | static int raw_sendok(void *handle) |
32874aea |
181 | { |
182 | return 1; |
183 | } |
4017be6d |
184 | |
51470298 |
185 | static void raw_unthrottle(void *handle, int backlog) |
5471d09a |
186 | { |
51470298 |
187 | Raw raw = (Raw) handle; |
188 | sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG); |
5471d09a |
189 | } |
190 | |
51470298 |
191 | static int raw_ldisc(void *handle, int option) |
32874aea |
192 | { |
0965bee0 |
193 | if (option == LD_EDIT || option == LD_ECHO) |
32874aea |
194 | return 1; |
0965bee0 |
195 | return 0; |
196 | } |
197 | |
b9d7bcad |
198 | static void raw_provide_ldisc(void *handle, void *ldisc) |
199 | { |
200 | /* This is a stub. */ |
201 | } |
202 | |
a8327734 |
203 | static void raw_provide_logctx(void *handle, void *logctx) |
204 | { |
205 | /* This is a stub. */ |
206 | } |
207 | |
51470298 |
208 | static int raw_exitcode(void *handle) |
d8d6c7e5 |
209 | { |
210 | /* Exit codes are a meaningless concept in the Raw protocol */ |
211 | return 0; |
212 | } |
213 | |
5e1a8e27 |
214 | Backend raw_backend = { |
215 | raw_init, |
86916870 |
216 | raw_reconfig, |
5e1a8e27 |
217 | raw_send, |
5471d09a |
218 | raw_sendbuffer, |
5e1a8e27 |
219 | raw_size, |
4017be6d |
220 | raw_special, |
8ccc75b0 |
221 | raw_socket, |
d8d6c7e5 |
222 | raw_exitcode, |
97db3be4 |
223 | raw_sendok, |
0965bee0 |
224 | raw_ldisc, |
b9d7bcad |
225 | raw_provide_ldisc, |
a8327734 |
226 | raw_provide_logctx, |
5471d09a |
227 | raw_unthrottle, |
97db3be4 |
228 | 1 |
5e1a8e27 |
229 | }; |