Fixes for (Backend)->size() changes -- internal declarations didn't include
[u/mdw/putty] / raw.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "putty.h"
6
7 #ifndef FALSE
8 #define FALSE 0
9 #endif
10 #ifndef TRUE
11 #define TRUE 1
12 #endif
13
14 #define RAW_MAX_BACKLOG 4096
15
16 static Socket s = NULL;
17 static int raw_bufsize;
18 static void *frontend;
19
20 static void raw_size(int width, int height);
21
22 static void c_write(char *buf, int len)
23 {
24 int backlog = from_backend(frontend, 0, buf, len);
25 sk_set_frozen(s, backlog > RAW_MAX_BACKLOG);
26 }
27
28 static int raw_closing(Plug plug, char *error_msg, int error_code,
29 int calling_back)
30 {
31 if (s) {
32 sk_close(s);
33 s = NULL;
34 }
35 if (error_msg) {
36 /* A socket error has occurred. */
37 logevent(error_msg);
38 connection_fatal("%s", error_msg);
39 } /* Otherwise, the remote side closed the connection normally. */
40 return 0;
41 }
42
43 static int raw_receive(Plug plug, int urgent, char *data, int len)
44 {
45 c_write(data, len);
46 return 1;
47 }
48
49 static void raw_sent(Plug plug, int bufsize)
50 {
51 raw_bufsize = bufsize;
52 }
53
54 /*
55 * Called to set up the raw connection.
56 *
57 * Returns an error message, or NULL on success.
58 *
59 * Also places the canonical host name into `realhost'. It must be
60 * freed by the caller.
61 */
62 static char *raw_init(void *frontend_handle, char *host, int port,
63 char **realhost, int nodelay)
64 {
65 static struct plug_function_table fn_table = {
66 raw_closing,
67 raw_receive,
68 raw_sent
69 }, *fn_table_ptr = &fn_table;
70
71 SockAddr addr;
72 char *err;
73
74 frontend = frontend_handle;
75
76 /*
77 * Try to find host.
78 */
79 {
80 char buf[200];
81 sprintf(buf, "Looking up host \"%.170s\"", host);
82 logevent(buf);
83 }
84 addr = sk_namelookup(host, realhost);
85 if ((err = sk_addr_error(addr)))
86 return err;
87
88 if (port < 0)
89 port = 23; /* default telnet port */
90
91 /*
92 * Open socket.
93 */
94 {
95 char buf[200], addrbuf[100];
96 sk_getaddr(addr, addrbuf, 100);
97 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
98 logevent(buf);
99 }
100 s = new_connection(addr, *realhost, port, 0, 1, nodelay, &fn_table_ptr);
101 if ((err = sk_socket_error(s)))
102 return err;
103
104 sk_addr_free(addr);
105
106 return NULL;
107 }
108
109 /*
110 * Called to send data down the raw connection.
111 */
112 static int raw_send(char *buf, int len)
113 {
114 if (s == NULL)
115 return 0;
116
117 raw_bufsize = sk_write(s, buf, len);
118
119 return raw_bufsize;
120 }
121
122 /*
123 * Called to query the current socket sendability status.
124 */
125 static int raw_sendbuffer(void)
126 {
127 return raw_bufsize;
128 }
129
130 /*
131 * Called to set the size of the window
132 */
133 static void raw_size(int width, int height)
134 {
135 /* Do nothing! */
136 return;
137 }
138
139 /*
140 * Send raw special codes.
141 */
142 static void raw_special(Telnet_Special code)
143 {
144 /* Do nothing! */
145 return;
146 }
147
148 static Socket raw_socket(void)
149 {
150 return s;
151 }
152
153 static int raw_sendok(void)
154 {
155 return 1;
156 }
157
158 static void raw_unthrottle(int backlog)
159 {
160 sk_set_frozen(s, backlog > RAW_MAX_BACKLOG);
161 }
162
163 static int raw_ldisc(int option)
164 {
165 if (option == LD_EDIT || option == LD_ECHO)
166 return 1;
167 return 0;
168 }
169
170 static int raw_exitcode(void)
171 {
172 /* Exit codes are a meaningless concept in the Raw protocol */
173 return 0;
174 }
175
176 Backend raw_backend = {
177 raw_init,
178 raw_send,
179 raw_sendbuffer,
180 raw_size,
181 raw_special,
182 raw_socket,
183 raw_exitcode,
184 raw_sendok,
185 raw_ldisc,
186 raw_unthrottle,
187 1
188 };