5e1a8e27 |
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 | int c = (unsigned char) *buf; |
56 | if (new_head != inbuf_reap) { |
57 | inbuf[inbuf_head] = *buf++; |
58 | inbuf_head = new_head; |
cabfd08c |
59 | } else { |
60 | term_out(); |
61 | if( inbuf_head == inbuf_reap ) len++; else break; |
5e1a8e27 |
62 | } |
63 | } |
64 | } |
65 | |
66 | /* |
67 | * Called to set up the raw connection. Will arrange for |
68 | * WM_NETEVENT messages to be passed to the specified window, whose |
69 | * window procedure should then call raw_msg(). |
70 | * |
71 | * Returns an error message, or NULL on success. |
72 | * |
73 | * Also places the canonical host name into `realhost'. |
74 | */ |
75 | static char *raw_init (HWND hwnd, char *host, int port, char **realhost) { |
76 | SOCKADDR_IN addr; |
77 | struct hostent *h; |
78 | unsigned long a; |
79 | |
80 | /* |
81 | * Try to find host. |
82 | */ |
83 | if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) { |
84 | if ( (h = gethostbyname(host)) == NULL) |
85 | switch (WSAGetLastError()) { |
86 | case WSAENETDOWN: return "Network is down"; |
87 | case WSAHOST_NOT_FOUND: case WSANO_DATA: |
88 | return "Host does not exist"; |
89 | case WSATRY_AGAIN: return "Host not found"; |
90 | default: return "gethostbyname: unknown error"; |
91 | } |
92 | memcpy (&a, h->h_addr, sizeof(a)); |
93 | *realhost = h->h_name; |
94 | } else |
95 | *realhost = host; |
96 | a = ntohl(a); |
97 | |
98 | if (port < 0) |
99 | port = 23; /* default telnet port */ |
100 | |
101 | /* |
102 | * Open socket. |
103 | */ |
104 | s = socket(AF_INET, SOCK_STREAM, 0); |
105 | if (s == INVALID_SOCKET) |
106 | switch (WSAGetLastError()) { |
107 | case WSAENETDOWN: return "Network is down"; |
108 | case WSAEAFNOSUPPORT: return "TCP/IP support not present"; |
109 | default: return "socket(): unknown error"; |
110 | } |
111 | |
112 | /* |
113 | * Bind to local address. |
114 | */ |
115 | addr.sin_family = AF_INET; |
116 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
117 | addr.sin_port = htons(0); |
118 | if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
119 | switch (WSAGetLastError()) { |
120 | case WSAENETDOWN: return "Network is down"; |
121 | default: return "bind(): unknown error"; |
122 | } |
123 | |
124 | /* |
125 | * Connect to remote address. |
126 | */ |
127 | addr.sin_addr.s_addr = htonl(a); |
128 | addr.sin_port = htons((short)port); |
129 | if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
130 | switch (WSAGetLastError()) { |
131 | case WSAENETDOWN: return "Network is down"; |
132 | case WSAECONNREFUSED: return "Connection refused"; |
133 | case WSAENETUNREACH: return "Network is unreachable"; |
134 | case WSAEHOSTUNREACH: return "No route to host"; |
135 | default: return "connect(): unknown error"; |
136 | } |
137 | |
138 | if (WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ | |
139 | FD_WRITE | FD_OOB | FD_CLOSE) == SOCKET_ERROR) |
140 | switch (WSAGetLastError()) { |
141 | case WSAENETDOWN: return "Network is down"; |
142 | default: return "WSAAsyncSelect(): unknown error"; |
143 | } |
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 | |
8ce72d2c |
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 | return 1; |
5e1a8e27 |
165 | |
166 | if (WSAGETSELECTERROR(lParam) != 0) |
167 | return -WSAGETSELECTERROR(lParam); |
168 | |
169 | switch (WSAGETSELECTEVENT(lParam)) { |
170 | case FD_READ: |
8ce72d2c |
171 | case FD_CLOSE: |
5e1a8e27 |
172 | ret = recv(s, buf, sizeof(buf), 0); |
173 | if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK) |
174 | return 1; |
175 | if (ret < 0) /* any _other_ error */ |
176 | return -10000-WSAGetLastError(); |
177 | if (ret == 0) { |
178 | s = INVALID_SOCKET; |
8ce72d2c |
179 | return 0; |
5e1a8e27 |
180 | } |
181 | c_write( buf, ret ); |
182 | return 1; |
183 | case FD_OOB: |
184 | do { |
185 | ret = recv(s, buf, sizeof(buf), 0); |
186 | c_write( buf, ret ); |
187 | } while (ret > 0); |
188 | do { |
189 | ret = recv(s, buf, 1, MSG_OOB); |
190 | } while (ret > 0); |
191 | if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) |
192 | return -30000-WSAGetLastError(); |
193 | return 1; |
194 | case FD_WRITE: |
195 | if (outbuf_head != outbuf_reap) |
196 | try_write(); |
197 | return 1; |
5e1a8e27 |
198 | } |
199 | return 1; /* shouldn't happen, but WTF */ |
200 | } |
201 | |
202 | /* |
203 | * Called to send data down the raw connection. |
204 | */ |
205 | static void raw_send (char *buf, int len) { |
206 | |
207 | if (s == INVALID_SOCKET) |
208 | return; |
209 | |
210 | s_write( buf, len ); |
211 | } |
212 | |
213 | /* |
214 | * Called to set the size of the window |
215 | */ |
216 | static void raw_size(void) { |
217 | /* Do nothing! */ |
218 | return; |
219 | } |
220 | |
221 | /* |
222 | * Send raw special codes. |
223 | */ |
224 | static void raw_special (Telnet_Special code) { |
225 | /* Do nothing! */ |
226 | return; |
227 | } |
228 | |
229 | Backend raw_backend = { |
230 | raw_init, |
231 | raw_msg, |
232 | raw_send, |
233 | raw_size, |
234 | raw_special |
235 | }; |