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