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