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