Prevent network errors from summarily closing the window when CoE is off
[u/mdw/putty] / raw.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <winsock.h>
5
6 #include "putty.h"
7
8 #ifndef FALSE
9 #define FALSE 0
10 #endif
11 #ifndef TRUE
12 #define TRUE 1
13 #endif
14
15 static SOCKET s = INVALID_SOCKET;
16
17 static void raw_size(void);
18
19 static int sb_opt, sb_len;
20 static char *sb_buf = NULL;
21 static int sb_size = 0;
22 #define SB_DELTA 1024
23
24 static void try_write (void) {
25 while (outbuf_head != outbuf_reap) {
26 int end = (outbuf_reap < outbuf_head ? outbuf_head : OUTBUF_SIZE);
27 int len = end - outbuf_reap;
28 int ret;
29
30 ret = send (s, outbuf+outbuf_reap, len, 0);
31 if (ret > 0)
32 outbuf_reap = (outbuf_reap + ret) & OUTBUF_MASK;
33 if (ret < len)
34 return;
35 }
36 }
37
38 static void s_write (void *buf, int len) {
39 unsigned char *p = buf;
40 while (len--) {
41 int new_head = (outbuf_head + 1) & OUTBUF_MASK;
42 if (new_head != outbuf_reap) {
43 outbuf[outbuf_head] = *p++;
44 outbuf_head = new_head;
45 }
46 }
47 try_write();
48 }
49
50 static void c_write (char *buf, int len) {
51 while (len--)
52 c_write1(*buf++);
53 }
54
55 /*
56 * Called to set up the raw connection. Will arrange for
57 * WM_NETEVENT messages to be passed to the specified window, whose
58 * window procedure should then call raw_msg().
59 *
60 * Returns an error message, or NULL on success.
61 *
62 * Also places the canonical host name into `realhost'.
63 */
64 static char *raw_init (HWND hwnd, char *host, int port, char **realhost) {
65 SOCKADDR_IN addr;
66 struct hostent *h;
67 unsigned long a;
68
69 /*
70 * Try to find host.
71 */
72 if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) {
73 if ( (h = gethostbyname(host)) == NULL)
74 switch (WSAGetLastError()) {
75 case WSAENETDOWN: return "Network is down";
76 case WSAHOST_NOT_FOUND: case WSANO_DATA:
77 return "Host does not exist";
78 case WSATRY_AGAIN: return "Host not found";
79 default: return "gethostbyname: unknown error";
80 }
81 memcpy (&a, h->h_addr, sizeof(a));
82 *realhost = h->h_name;
83 } else
84 *realhost = host;
85 a = ntohl(a);
86
87 if (port < 0)
88 port = 23; /* default telnet port */
89
90 /*
91 * Open socket.
92 */
93 s = socket(AF_INET, SOCK_STREAM, 0);
94 if (s == INVALID_SOCKET)
95 switch (WSAGetLastError()) {
96 case WSAENETDOWN: return "Network is down";
97 case WSAEAFNOSUPPORT: return "TCP/IP support not present";
98 default: return "socket(): unknown error";
99 }
100
101 /*
102 * Bind to local address.
103 */
104 addr.sin_family = AF_INET;
105 addr.sin_addr.s_addr = htonl(INADDR_ANY);
106 addr.sin_port = htons(0);
107 if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
108 switch (WSAGetLastError()) {
109 case WSAENETDOWN: return "Network is down";
110 default: return "bind(): unknown error";
111 }
112
113 /*
114 * Connect to remote address.
115 */
116 addr.sin_addr.s_addr = htonl(a);
117 addr.sin_port = htons((short)port);
118 if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
119 switch (WSAGetLastError()) {
120 case WSAENETDOWN: return "Network is down";
121 case WSAECONNREFUSED: return "Connection refused";
122 case WSAENETUNREACH: return "Network is unreachable";
123 case WSAEHOSTUNREACH: return "No route to host";
124 default: return "connect(): unknown error";
125 }
126
127 if (hwnd && WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ |
128 FD_WRITE | FD_OOB | FD_CLOSE) == SOCKET_ERROR)
129 switch (WSAGetLastError()) {
130 case WSAENETDOWN: return "Network is down";
131 default: return "WSAAsyncSelect(): unknown error";
132 }
133
134 return NULL;
135 }
136
137 /*
138 * Process a WM_NETEVENT message. Will return 0 if the connection
139 * has closed, or <0 for a socket error.
140 */
141 static int raw_msg (WPARAM wParam, LPARAM lParam) {
142 int ret;
143 char buf[256];
144
145 /*
146 * Because reading less than the whole of the available pending
147 * data can generate an FD_READ event, we need to allow for the
148 * possibility that FD_READ may arrive with FD_CLOSE already in
149 * the queue; so it's possible that we can get here even with s
150 * invalid. If so, we return 1 and don't worry about it.
151 */
152 if (s == INVALID_SOCKET) {
153 closesocket(s);
154 s = INVALID_SOCKET;
155 return 1;
156 }
157
158 if (WSAGETSELECTERROR(lParam) != 0)
159 return -WSAGETSELECTERROR(lParam);
160
161 switch (WSAGETSELECTEVENT(lParam)) {
162 case FD_READ:
163 case FD_CLOSE:
164 ret = recv(s, buf, sizeof(buf), 0);
165 if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
166 return 1;
167 if (ret < 0) { /* any _other_ error */
168 closesocket(s);
169 s = INVALID_SOCKET;
170 return -10000-WSAGetLastError();
171 }
172 if (ret == 0) {
173 s = INVALID_SOCKET;
174 return 0;
175 }
176 c_write( buf, ret );
177 return 1;
178 case FD_OOB:
179 do {
180 ret = recv(s, buf, sizeof(buf), 0);
181 c_write( buf, ret );
182 } while (ret > 0);
183 do {
184 ret = recv(s, buf, 1, MSG_OOB);
185 } while (ret > 0);
186 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
187 return -30000-WSAGetLastError();
188 return 1;
189 case FD_WRITE:
190 if (outbuf_head != outbuf_reap)
191 try_write();
192 return 1;
193 }
194 return 1; /* shouldn't happen, but WTF */
195 }
196
197 /*
198 * Called to send data down the raw connection.
199 */
200 static void raw_send (char *buf, int len) {
201
202 if (s == INVALID_SOCKET)
203 return;
204
205 s_write( buf, len );
206 }
207
208 /*
209 * Called to set the size of the window
210 */
211 static void raw_size(void) {
212 /* Do nothing! */
213 return;
214 }
215
216 /*
217 * Send raw special codes.
218 */
219 static void raw_special (Telnet_Special code) {
220 /* Do nothing! */
221 return;
222 }
223
224 static SOCKET raw_socket(void) { return s; }
225
226 static int raw_sendok(void) { return 1; }
227
228 Backend raw_backend = {
229 raw_init,
230 raw_msg,
231 raw_send,
232 raw_size,
233 raw_special,
234 raw_socket,
235 raw_sendok
236 };