2f75bae1 |
1 | /* |
2 | * Windows networking abstraction. |
c4d8e107 |
3 | * |
4 | * Due to this clean abstraction it was possible |
5 | * to easily implement IPv6 support :) |
6 | * |
7 | * IPv6 patch 1 (27 October 2000) Jeroen Massar <jeroen@unfix.org> |
8 | * - Preliminary hacked IPv6 support. |
9 | * - Connecting to IPv6 address (eg fec0:4242:4242:100:2d0:b7ff:fe8f:5d42) works. |
10 | * - Connecting to IPv6 hostname (eg heaven.ipv6.unfix.org) works. |
11 | * - Compiles as either IPv4 or IPv6. |
12 | * |
13 | * IPv6 patch 2 (29 October 2000) Jeroen Massar <jeroen@unfix.org> |
14 | * - When compiled as IPv6 it also allows connecting to IPv4 hosts. |
15 | * - Added some more documentation. |
16 | * |
17 | * IPv6 patch 3 (18 November 2000) Jeroen Massar <jeroen@unfix.org> |
18 | * - It now supports dynamically loading the IPv6 resolver dll's. |
19 | * This way we should be able to distribute one (1) binary |
20 | * which supports both IPv4 and IPv6. |
21 | * - getaddrinfo() and getnameinfo() are loaded dynamicaly if possible. |
22 | * - in6addr_any is defined in this file so we don't need to link to wship6.lib |
23 | * - The patch is now more unified so that we can still |
24 | * remove all IPv6 support by undef'ing IPV6. |
25 | * But where it fallsback to IPv4 it uses the IPv4 code which is already in place... |
26 | * - Canonical name resolving works. |
27 | * |
28 | * IPv6 patch 4 (07 January 2001) Jeroen Massar <jeroen@unfix.org> |
29 | * - patch against CVS of today, will be submitted to the bugs list |
30 | * as a 'cvs diff -u' on Simon's request... |
31 | * |
2f75bae1 |
32 | */ |
33 | |
c4d8e107 |
34 | /* |
35 | * Define IPV6 to have IPv6 on-the-fly-loading support. |
36 | * This means that one doesn't have to have an IPv6 stack to use it. |
37 | * But if an IPv6 stack is found it is used with a fallback to IPv4. |
38 | */ |
39 | /* #define IPV6 1 */ |
40 | |
2f75bae1 |
41 | #include <stdio.h> |
49bad831 |
42 | #include <stdlib.h> |
5471d09a |
43 | #include <assert.h> |
2f75bae1 |
44 | |
7e78000d |
45 | #define DEFINE_PLUG_METHOD_MACROS |
2f75bae1 |
46 | #include "putty.h" |
47 | #include "network.h" |
48 | #include "tree234.h" |
49 | |
7440fd44 |
50 | #include <ws2tcpip.h> |
a143ea0a |
51 | #ifdef IPV6 |
7440fd44 |
52 | #include <tpipv6.h> |
53 | #endif |
54 | |
6ee9b735 |
55 | #define ipv4_is_loopback(addr) \ |
7440fd44 |
56 | ((p_ntohl(addr.s_addr) & 0xFF000000L) == 0x7F000000L) |
6ee9b735 |
57 | |
2f75bae1 |
58 | struct Socket_tag { |
c85623f9 |
59 | const struct socket_function_table *fn; |
7e78000d |
60 | /* the above variable absolutely *must* be the first in this structure */ |
2f75bae1 |
61 | char *error; |
62 | SOCKET s; |
7e78000d |
63 | Plug plug; |
2f75bae1 |
64 | void *private_ptr; |
5471d09a |
65 | bufchain output_data; |
3ad9d396 |
66 | int connected; |
2f75bae1 |
67 | int writable; |
5471d09a |
68 | int frozen; /* this causes readability notifications to be ignored */ |
69 | int frozen_readable; /* this means we missed at least one readability |
70 | * notification while we were frozen */ |
bc4802a1 |
71 | int localhost_only; /* for listening sockets */ |
5471d09a |
72 | char oobdata[1]; |
1ad4eb6f |
73 | int sending_oob; |
4b1e8acc |
74 | int oobinline; |
7732d38a |
75 | int pending_error; /* in case send() returns error */ |
2f75bae1 |
76 | }; |
77 | |
7e78000d |
78 | /* |
79 | * We used to typedef struct Socket_tag *Socket. |
80 | * |
81 | * Since we have made the networking abstraction slightly more |
82 | * abstract, Socket no longer means a tcp socket (it could mean |
83 | * an ssl socket). So now we must use Actual_Socket when we know |
84 | * we are talking about a tcp socket. |
85 | */ |
86 | typedef struct Socket_tag *Actual_Socket; |
87 | |
2f75bae1 |
88 | struct SockAddr_tag { |
89 | char *error; |
b7a189f3 |
90 | /* |
91 | * Which address family this address belongs to. AF_INET for |
92 | * IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name |
93 | * resolution has not been done and a simple host name is held |
94 | * in this SockAddr structure. |
95 | */ |
32874aea |
96 | int family; |
c4d8e107 |
97 | unsigned long address; /* Address IPv4 style. */ |
98 | #ifdef IPV6 |
99 | struct addrinfo *ai; /* Address IPv6 style. */ |
100 | #endif |
b7a189f3 |
101 | char hostname[512]; /* Store an unresolved host name. */ |
2f75bae1 |
102 | }; |
103 | |
2f75bae1 |
104 | static tree234 *sktree; |
105 | |
32874aea |
106 | static int cmpfortree(void *av, void *bv) |
107 | { |
108 | Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv; |
109 | unsigned long as = (unsigned long) a->s, bs = (unsigned long) b->s; |
110 | if (as < bs) |
111 | return -1; |
112 | if (as > bs) |
113 | return +1; |
2f75bae1 |
114 | return 0; |
115 | } |
116 | |
32874aea |
117 | static int cmpforsearch(void *av, void *bv) |
118 | { |
119 | Actual_Socket b = (Actual_Socket) bv; |
120 | unsigned long as = (unsigned long) av, bs = (unsigned long) b->s; |
121 | if (as < bs) |
122 | return -1; |
123 | if (as > bs) |
124 | return +1; |
2f75bae1 |
125 | return 0; |
126 | } |
127 | |
7440fd44 |
128 | #define NOTHING |
129 | #define DECL_WINSOCK_FUNCTION(linkage, rettype, name, params) \ |
130 | typedef rettype (WINAPI *t_##name) params; \ |
131 | linkage t_##name p_##name |
132 | #define GET_WINSOCK_FUNCTION(name) \ |
133 | p_##name = (t_##name) GetProcAddress(winsock_module, #name) |
134 | |
135 | DECL_WINSOCK_FUNCTION(NOTHING, int, WSAAsyncSelect, |
136 | (SOCKET, HWND, u_int, long)); |
137 | DECL_WINSOCK_FUNCTION(NOTHING, int, WSAEventSelect, (SOCKET, WSAEVENT, long)); |
138 | DECL_WINSOCK_FUNCTION(NOTHING, int, select, |
139 | (int, fd_set FAR *, fd_set FAR *, |
140 | fd_set FAR *, const struct timeval FAR *)); |
141 | DECL_WINSOCK_FUNCTION(NOTHING, int, WSAGetLastError, (void)); |
142 | DECL_WINSOCK_FUNCTION(NOTHING, int, WSAEnumNetworkEvents, |
143 | (SOCKET, WSAEVENT, LPWSANETWORKEVENTS)); |
144 | DECL_WINSOCK_FUNCTION(static, int, WSAStartup, (WORD, LPWSADATA)); |
145 | DECL_WINSOCK_FUNCTION(static, int, WSACleanup, (void)); |
146 | DECL_WINSOCK_FUNCTION(static, int, closesocket, (SOCKET)); |
147 | DECL_WINSOCK_FUNCTION(static, u_long, ntohl, (u_long)); |
148 | DECL_WINSOCK_FUNCTION(static, u_long, htonl, (u_long)); |
149 | DECL_WINSOCK_FUNCTION(static, u_short, htons, (u_short)); |
150 | DECL_WINSOCK_FUNCTION(static, u_short, ntohs, (u_short)); |
151 | DECL_WINSOCK_FUNCTION(static, struct hostent FAR *, gethostbyname, |
152 | (const char FAR *)); |
153 | DECL_WINSOCK_FUNCTION(static, struct servent FAR *, getservbyname, |
154 | (const char FAR *, const char FAR *)); |
155 | DECL_WINSOCK_FUNCTION(static, unsigned long, inet_addr, (const char FAR *)); |
156 | DECL_WINSOCK_FUNCTION(static, char FAR *, inet_ntoa, (struct in_addr)); |
157 | DECL_WINSOCK_FUNCTION(static, int, connect, |
158 | (SOCKET, const struct sockaddr FAR *, int)); |
159 | DECL_WINSOCK_FUNCTION(static, int, bind, |
160 | (SOCKET, const struct sockaddr FAR *, int)); |
161 | DECL_WINSOCK_FUNCTION(static, int, setsockopt, |
162 | (SOCKET, int, int, const char FAR *, int)); |
163 | DECL_WINSOCK_FUNCTION(static, SOCKET, socket, (int, int, int)); |
164 | DECL_WINSOCK_FUNCTION(static, int, listen, (SOCKET, int)); |
165 | DECL_WINSOCK_FUNCTION(static, int, send, (SOCKET, const char FAR *, int, int)); |
166 | DECL_WINSOCK_FUNCTION(static, int, ioctlsocket, |
167 | (SOCKET, long, u_long FAR *)); |
168 | DECL_WINSOCK_FUNCTION(static, SOCKET, accept, |
169 | (SOCKET, struct sockaddr FAR *, int FAR *)); |
170 | DECL_WINSOCK_FUNCTION(static, int, recv, (SOCKET, char FAR *, int, int)); |
a143ea0a |
171 | DECL_WINSOCK_FUNCTION(static, int, WSAIoctl, |
172 | (SOCKET, DWORD, LPVOID, DWORD, LPVOID, DWORD, |
173 | LPDWORD, LPWSAOVERLAPPED, |
174 | LPWSAOVERLAPPED_COMPLETION_ROUTINE)); |
7440fd44 |
175 | |
176 | static HMODULE winsock_module; |
177 | |
32874aea |
178 | void sk_init(void) |
179 | { |
7440fd44 |
180 | WORD winsock_ver; |
181 | WSADATA wsadata; |
182 | |
183 | winsock_ver = MAKEWORD(2, 0); |
184 | winsock_module = LoadLibrary("WS2_32.DLL"); |
185 | if (!winsock_module) { |
186 | winsock_module = LoadLibrary("WSOCK32.DLL"); |
187 | winsock_ver = MAKEWORD(1, 1); |
188 | } |
189 | if (!winsock_module) |
190 | fatalbox("Unable to load any WinSock library"); |
191 | |
192 | GET_WINSOCK_FUNCTION(WSAAsyncSelect); |
193 | GET_WINSOCK_FUNCTION(WSAEventSelect); |
194 | GET_WINSOCK_FUNCTION(select); |
195 | GET_WINSOCK_FUNCTION(WSAGetLastError); |
196 | GET_WINSOCK_FUNCTION(WSAEnumNetworkEvents); |
197 | GET_WINSOCK_FUNCTION(WSAStartup); |
198 | GET_WINSOCK_FUNCTION(WSACleanup); |
199 | GET_WINSOCK_FUNCTION(closesocket); |
200 | GET_WINSOCK_FUNCTION(ntohl); |
201 | GET_WINSOCK_FUNCTION(htonl); |
202 | GET_WINSOCK_FUNCTION(htons); |
203 | GET_WINSOCK_FUNCTION(ntohs); |
204 | GET_WINSOCK_FUNCTION(gethostbyname); |
205 | GET_WINSOCK_FUNCTION(getservbyname); |
206 | GET_WINSOCK_FUNCTION(inet_addr); |
207 | GET_WINSOCK_FUNCTION(inet_ntoa); |
208 | GET_WINSOCK_FUNCTION(connect); |
209 | GET_WINSOCK_FUNCTION(bind); |
210 | GET_WINSOCK_FUNCTION(setsockopt); |
211 | GET_WINSOCK_FUNCTION(socket); |
212 | GET_WINSOCK_FUNCTION(listen); |
213 | GET_WINSOCK_FUNCTION(send); |
214 | GET_WINSOCK_FUNCTION(ioctlsocket); |
215 | GET_WINSOCK_FUNCTION(accept); |
216 | GET_WINSOCK_FUNCTION(recv); |
a143ea0a |
217 | GET_WINSOCK_FUNCTION(WSAIoctl); |
7440fd44 |
218 | |
219 | if (p_WSAStartup(winsock_ver, &wsadata)) { |
220 | fatalbox("Unable to initialise WinSock"); |
221 | } |
222 | if (LOBYTE(wsadata.wVersion) != LOBYTE(winsock_ver)) { |
223 | p_WSACleanup(); |
224 | fatalbox("WinSock version is incompatible with %d.%d", |
225 | LOBYTE(winsock_ver), HIBYTE(winsock_ver)); |
226 | } |
227 | |
2f75bae1 |
228 | sktree = newtree234(cmpfortree); |
229 | } |
230 | |
93b581bd |
231 | void sk_cleanup(void) |
232 | { |
233 | Actual_Socket s; |
234 | int i; |
235 | |
236 | if (sktree) { |
237 | for (i = 0; (s = index234(sktree, i)) != NULL; i++) { |
7440fd44 |
238 | p_closesocket(s->s); |
93b581bd |
239 | } |
240 | } |
7440fd44 |
241 | |
242 | p_WSACleanup(); |
243 | if (winsock_module) |
244 | FreeLibrary(winsock_module); |
93b581bd |
245 | } |
246 | |
32874aea |
247 | char *winsock_error_string(int error) |
248 | { |
e74fbad9 |
249 | switch (error) { |
32874aea |
250 | case WSAEACCES: |
251 | return "Network error: Permission denied"; |
252 | case WSAEADDRINUSE: |
253 | return "Network error: Address already in use"; |
254 | case WSAEADDRNOTAVAIL: |
255 | return "Network error: Cannot assign requested address"; |
256 | case WSAEAFNOSUPPORT: |
257 | return |
258 | "Network error: Address family not supported by protocol family"; |
259 | case WSAEALREADY: |
260 | return "Network error: Operation already in progress"; |
261 | case WSAECONNABORTED: |
262 | return "Network error: Software caused connection abort"; |
263 | case WSAECONNREFUSED: |
264 | return "Network error: Connection refused"; |
265 | case WSAECONNRESET: |
266 | return "Network error: Connection reset by peer"; |
267 | case WSAEDESTADDRREQ: |
268 | return "Network error: Destination address required"; |
269 | case WSAEFAULT: |
270 | return "Network error: Bad address"; |
271 | case WSAEHOSTDOWN: |
272 | return "Network error: Host is down"; |
273 | case WSAEHOSTUNREACH: |
274 | return "Network error: No route to host"; |
275 | case WSAEINPROGRESS: |
276 | return "Network error: Operation now in progress"; |
277 | case WSAEINTR: |
278 | return "Network error: Interrupted function call"; |
279 | case WSAEINVAL: |
280 | return "Network error: Invalid argument"; |
281 | case WSAEISCONN: |
282 | return "Network error: Socket is already connected"; |
283 | case WSAEMFILE: |
284 | return "Network error: Too many open files"; |
285 | case WSAEMSGSIZE: |
286 | return "Network error: Message too long"; |
287 | case WSAENETDOWN: |
288 | return "Network error: Network is down"; |
289 | case WSAENETRESET: |
290 | return "Network error: Network dropped connection on reset"; |
291 | case WSAENETUNREACH: |
292 | return "Network error: Network is unreachable"; |
293 | case WSAENOBUFS: |
294 | return "Network error: No buffer space available"; |
295 | case WSAENOPROTOOPT: |
296 | return "Network error: Bad protocol option"; |
297 | case WSAENOTCONN: |
298 | return "Network error: Socket is not connected"; |
299 | case WSAENOTSOCK: |
300 | return "Network error: Socket operation on non-socket"; |
301 | case WSAEOPNOTSUPP: |
302 | return "Network error: Operation not supported"; |
303 | case WSAEPFNOSUPPORT: |
304 | return "Network error: Protocol family not supported"; |
305 | case WSAEPROCLIM: |
306 | return "Network error: Too many processes"; |
307 | case WSAEPROTONOSUPPORT: |
308 | return "Network error: Protocol not supported"; |
309 | case WSAEPROTOTYPE: |
310 | return "Network error: Protocol wrong type for socket"; |
311 | case WSAESHUTDOWN: |
312 | return "Network error: Cannot send after socket shutdown"; |
313 | case WSAESOCKTNOSUPPORT: |
314 | return "Network error: Socket type not supported"; |
315 | case WSAETIMEDOUT: |
316 | return "Network error: Connection timed out"; |
317 | case WSAEWOULDBLOCK: |
318 | return "Network error: Resource temporarily unavailable"; |
319 | case WSAEDISCON: |
320 | return "Network error: Graceful shutdown in progress"; |
321 | default: |
322 | return "Unknown network error"; |
e74fbad9 |
323 | } |
324 | } |
325 | |
e8fa8f62 |
326 | SockAddr sk_namelookup(const char *host, char **canonicalname) |
c4d8e107 |
327 | { |
3d88e64d |
328 | SockAddr ret = snew(struct SockAddr_tag); |
2f75bae1 |
329 | unsigned long a; |
c4d8e107 |
330 | struct hostent *h = NULL; |
6e1ebb76 |
331 | char realhost[8192]; |
2f75bae1 |
332 | |
c4d8e107 |
333 | /* Clear the structure and default to IPv4. */ |
334 | memset(ret, 0, sizeof(struct SockAddr_tag)); |
335 | ret->family = 0; /* We set this one when we have resolved the host. */ |
6e1ebb76 |
336 | *realhost = '\0'; |
c4d8e107 |
337 | |
7440fd44 |
338 | if ((a = p_inet_addr(host)) == (unsigned long) INADDR_NONE) { |
c4d8e107 |
339 | #ifdef IPV6 |
340 | |
341 | /* Try to get the getaddrinfo() function from wship6.dll */ |
342 | /* This way one doesn't need to have IPv6 dll's to use PuTTY and |
343 | * it will fallback to IPv4. */ |
32874aea |
344 | typedef int (CALLBACK * FGETADDRINFO) (const char *nodename, |
345 | const char *servname, |
346 | const struct addrinfo * |
347 | hints, |
348 | struct addrinfo ** res); |
c4d8e107 |
349 | FGETADDRINFO fGetAddrInfo = NULL; |
350 | |
351 | HINSTANCE dllWSHIP6 = LoadLibrary("wship6.dll"); |
352 | if (dllWSHIP6) |
32874aea |
353 | fGetAddrInfo = (FGETADDRINFO) GetProcAddress(dllWSHIP6, |
354 | "getaddrinfo"); |
c4d8e107 |
355 | |
356 | /* |
357 | * Use fGetAddrInfo when it's available (which usually also |
358 | * means IPv6 is installed...) |
359 | */ |
32874aea |
360 | if (fGetAddrInfo) { |
c4d8e107 |
361 | /*debug(("Resolving \"%s\" with getaddrinfo() (IPv4+IPv6 capable)...\n", host)); */ |
362 | if (fGetAddrInfo(host, NULL, NULL, &ret->ai) == 0) |
363 | ret->family = ret->ai->ai_family; |
32874aea |
364 | } else |
c4d8e107 |
365 | #endif |
32874aea |
366 | { |
c4d8e107 |
367 | /* |
368 | * Otherwise use the IPv4-only gethostbyname... |
369 | * (NOTE: we don't use gethostbyname as a |
370 | * fallback!) |
371 | */ |
32874aea |
372 | if (ret->family == 0) { |
373 | /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */ |
7440fd44 |
374 | if ( (h = p_gethostbyname(host)) ) |
32874aea |
375 | ret->family = AF_INET; |
376 | } |
c4d8e107 |
377 | } |
378 | /*debug(("Done resolving...(family is %d) AF_INET = %d, AF_INET6 = %d\n", ret->family, AF_INET, AF_INET6)); */ |
379 | |
32874aea |
380 | if (ret->family == 0) { |
7440fd44 |
381 | DWORD err = p_WSAGetLastError(); |
c4d8e107 |
382 | ret->error = (err == WSAENETDOWN ? "Network is down" : |
32874aea |
383 | err == |
384 | WSAHOST_NOT_FOUND ? "Host does not exist" : err |
385 | == WSATRY_AGAIN ? "Host not found" : |
c4d8e107 |
386 | #ifdef IPV6 |
387 | fGetAddrInfo ? "getaddrinfo: unknown error" : |
388 | #endif |
389 | "gethostbyname: unknown error"); |
390 | #ifdef DEBUG |
391 | { |
392 | LPVOID lpMsgBuf; |
32874aea |
393 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
394 | FORMAT_MESSAGE_FROM_SYSTEM | |
395 | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, |
396 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
397 | (LPTSTR) & lpMsgBuf, 0, NULL); |
398 | /*debug(("Error %ld: %s (h=%lx)\n", err, lpMsgBuf, h)); */ |
c4d8e107 |
399 | /* Free the buffer. */ |
400 | LocalFree(lpMsgBuf); |
401 | } |
402 | #endif |
32874aea |
403 | } else { |
c4d8e107 |
404 | ret->error = NULL; |
405 | |
406 | #ifdef IPV6 |
407 | /* If we got an address info use that... */ |
32874aea |
408 | if (ret->ai) { |
409 | typedef int (CALLBACK * FGETNAMEINFO) |
410 | (const struct sockaddr FAR * sa, socklen_t salen, |
411 | char FAR * host, size_t hostlen, char FAR * serv, |
412 | size_t servlen, int flags); |
c4d8e107 |
413 | FGETNAMEINFO fGetNameInfo = NULL; |
414 | |
415 | /* Are we in IPv4 fallback mode? */ |
416 | /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */ |
417 | if (ret->family == AF_INET) |
32874aea |
418 | memcpy(&a, |
419 | (char *) &((SOCKADDR_IN *) ret->ai-> |
420 | ai_addr)->sin_addr, sizeof(a)); |
c4d8e107 |
421 | |
422 | /* Now let's find that canonicalname... */ |
32874aea |
423 | if ((dllWSHIP6) |
424 | && (fGetNameInfo = |
425 | (FGETNAMEINFO) GetProcAddress(dllWSHIP6, |
426 | "getnameinfo"))) { |
427 | if (fGetNameInfo |
428 | ((struct sockaddr *) ret->ai->ai_addr, |
429 | ret->family == |
430 | AF_INET ? sizeof(SOCKADDR_IN) : |
6e1ebb76 |
431 | sizeof(SOCKADDR_IN6), realhost, |
432 | sizeof(realhost), NULL, 0, 0) != 0) { |
433 | strncpy(realhost, host, sizeof(realhost)); |
c4d8e107 |
434 | } |
435 | } |
436 | } |
437 | /* We used the IPv4-only gethostbyname()... */ |
438 | else |
c4d8e107 |
439 | #endif |
32874aea |
440 | { |
c4d8e107 |
441 | memcpy(&a, h->h_addr, sizeof(a)); |
442 | /* This way we are always sure the h->h_name is valid :) */ |
6e1ebb76 |
443 | strncpy(realhost, h->h_name, sizeof(realhost)); |
c4d8e107 |
444 | } |
c4d8e107 |
445 | } |
446 | #ifdef IPV6 |
447 | FreeLibrary(dllWSHIP6); |
448 | #endif |
32874aea |
449 | } else { |
87ed061f |
450 | /* |
451 | * This must be a numeric IPv4 address because it caused a |
452 | * success return from inet_addr. |
453 | */ |
32874aea |
454 | ret->family = AF_INET; |
6e1ebb76 |
455 | strncpy(realhost, host, sizeof(realhost)); |
2f75bae1 |
456 | } |
7440fd44 |
457 | ret->address = p_ntohl(a); |
6e1ebb76 |
458 | realhost[lenof(realhost)-1] = '\0'; |
3d88e64d |
459 | *canonicalname = snewn(1+strlen(realhost), char); |
6e1ebb76 |
460 | strcpy(*canonicalname, realhost); |
2f75bae1 |
461 | return ret; |
462 | } |
463 | |
e8fa8f62 |
464 | SockAddr sk_nonamelookup(const char *host) |
b7a189f3 |
465 | { |
3d88e64d |
466 | SockAddr ret = snew(struct SockAddr_tag); |
ab0873ab |
467 | ret->error = NULL; |
b7a189f3 |
468 | ret->family = AF_UNSPEC; |
469 | strncpy(ret->hostname, host, lenof(ret->hostname)); |
470 | ret->hostname[lenof(ret->hostname)-1] = '\0'; |
471 | return ret; |
472 | } |
473 | |
3ad9d396 |
474 | void sk_getaddr(SockAddr addr, char *buf, int buflen) |
475 | { |
476 | #ifdef IPV6 |
b7a189f3 |
477 | if (addr->family == AF_INET6) { |
478 | FIXME; /* I don't know how to get a text form of an IPv6 address. */ |
479 | } else |
3ad9d396 |
480 | #endif |
b7a189f3 |
481 | if (addr->family == AF_INET) { |
3ad9d396 |
482 | struct in_addr a; |
7440fd44 |
483 | a.s_addr = p_htonl(addr->address); |
484 | strncpy(buf, p_inet_ntoa(a), buflen); |
b7a189f3 |
485 | buf[buflen-1] = '\0'; |
3ad9d396 |
486 | } else { |
b7a189f3 |
487 | assert(addr->family == AF_UNSPEC); |
488 | strncpy(buf, addr->hostname, buflen); |
489 | buf[buflen-1] = '\0'; |
3ad9d396 |
490 | } |
3ad9d396 |
491 | } |
492 | |
b804e1e5 |
493 | int sk_hostname_is_local(char *name) |
494 | { |
495 | return !strcmp(name, "localhost"); |
496 | } |
497 | |
a143ea0a |
498 | static INTERFACE_INFO local_interfaces[16]; |
499 | static int n_local_interfaces; /* 0=not yet, -1=failed, >0=number */ |
500 | |
501 | static int ipv4_is_local_addr(struct in_addr addr) |
502 | { |
503 | if (ipv4_is_loopback(addr)) |
504 | return 1; /* loopback addresses are local */ |
505 | if (!n_local_interfaces) { |
506 | SOCKET s = p_socket(AF_INET, SOCK_DGRAM, 0); |
507 | DWORD retbytes; |
508 | |
509 | if (p_WSAIoctl && |
510 | p_WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0, |
511 | local_interfaces, sizeof(local_interfaces), |
512 | &retbytes, NULL, NULL) == 0) |
513 | n_local_interfaces = retbytes / sizeof(INTERFACE_INFO); |
514 | else |
515 | logevent(NULL, "Unable to get list of local IP addresses"); |
516 | } |
517 | if (n_local_interfaces > 0) { |
518 | int i; |
519 | for (i = 0; i < n_local_interfaces; i++) { |
520 | SOCKADDR_IN *address = |
521 | (SOCKADDR_IN *)&local_interfaces[i].iiAddress; |
522 | if (address->sin_addr.s_addr == addr.s_addr) |
523 | return 1; /* this address is local */ |
524 | } |
525 | } |
526 | return 0; /* this address is not local */ |
527 | } |
528 | |
b804e1e5 |
529 | int sk_address_is_local(SockAddr addr) |
530 | { |
531 | #ifdef IPV6 |
b7a189f3 |
532 | if (addr->family == AF_INET6) { |
533 | FIXME; /* someone who can compile for IPV6 had better do this bit */ |
534 | } else |
b804e1e5 |
535 | #endif |
b7a189f3 |
536 | if (addr->family == AF_INET) { |
b804e1e5 |
537 | struct in_addr a; |
7440fd44 |
538 | a.s_addr = p_htonl(addr->address); |
a143ea0a |
539 | return ipv4_is_local_addr(a); |
b804e1e5 |
540 | } else { |
b7a189f3 |
541 | assert(addr->family == AF_UNSPEC); |
542 | return 0; /* we don't know; assume not */ |
b804e1e5 |
543 | } |
b804e1e5 |
544 | } |
545 | |
6971bbe7 |
546 | int sk_addrtype(SockAddr addr) |
547 | { |
b7a189f3 |
548 | return (addr->family == AF_INET ? ADDRTYPE_IPV4 : |
549 | #ifdef IPV6 |
550 | addr->family == AF_INET6 ? ADDRTYPE_IPV6 : |
551 | #endif |
552 | ADDRTYPE_NAME); |
6971bbe7 |
553 | } |
554 | |
555 | void sk_addrcopy(SockAddr addr, char *buf) |
556 | { |
b7a189f3 |
557 | assert(addr->family != AF_UNSPEC); |
6971bbe7 |
558 | #ifdef IPV6 |
b7a189f3 |
559 | if (addr->family == AF_INET6) { |
560 | memcpy(buf, (char*) addr->ai, 16); |
561 | } else |
6971bbe7 |
562 | #endif |
b7a189f3 |
563 | if (addr->family == AF_INET) { |
6971bbe7 |
564 | struct in_addr a; |
7440fd44 |
565 | a.s_addr = p_htonl(addr->address); |
96d9eb89 |
566 | memcpy(buf, (char*) &a.s_addr, 4); |
6971bbe7 |
567 | } |
6971bbe7 |
568 | } |
569 | |
32874aea |
570 | void sk_addr_free(SockAddr addr) |
571 | { |
2f75bae1 |
572 | sfree(addr); |
573 | } |
574 | |
32874aea |
575 | static Plug sk_tcp_plug(Socket sock, Plug p) |
576 | { |
7e78000d |
577 | Actual_Socket s = (Actual_Socket) sock; |
578 | Plug ret = s->plug; |
32874aea |
579 | if (p) |
580 | s->plug = p; |
7e78000d |
581 | return ret; |
582 | } |
583 | |
32874aea |
584 | static void sk_tcp_flush(Socket s) |
585 | { |
7e78000d |
586 | /* |
587 | * We send data to the socket as soon as we can anyway, |
588 | * so we don't need to do anything here. :-) |
589 | */ |
590 | } |
591 | |
2d466ffd |
592 | static void sk_tcp_close(Socket s); |
e0e7dff8 |
593 | static int sk_tcp_write(Socket s, const char *data, int len); |
594 | static int sk_tcp_write_oob(Socket s, const char *data, int len); |
8eebd221 |
595 | static void sk_tcp_set_private_ptr(Socket s, void *ptr); |
596 | static void *sk_tcp_get_private_ptr(Socket s); |
597 | static void sk_tcp_set_frozen(Socket s, int is_frozen); |
cbe2d68f |
598 | static const char *sk_tcp_socket_error(Socket s); |
7e78000d |
599 | |
d74d141c |
600 | extern char *do_select(SOCKET skt, int startup); |
601 | |
602 | Socket sk_register(void *sock, Plug plug) |
603 | { |
c85623f9 |
604 | static const struct socket_function_table fn_table = { |
d74d141c |
605 | sk_tcp_plug, |
606 | sk_tcp_close, |
607 | sk_tcp_write, |
608 | sk_tcp_write_oob, |
609 | sk_tcp_flush, |
8eebd221 |
610 | sk_tcp_set_private_ptr, |
611 | sk_tcp_get_private_ptr, |
612 | sk_tcp_set_frozen, |
d74d141c |
613 | sk_tcp_socket_error |
614 | }; |
615 | |
616 | DWORD err; |
617 | char *errstr; |
618 | Actual_Socket ret; |
619 | |
620 | /* |
621 | * Create Socket structure. |
622 | */ |
3d88e64d |
623 | ret = snew(struct Socket_tag); |
d74d141c |
624 | ret->fn = &fn_table; |
625 | ret->error = NULL; |
626 | ret->plug = plug; |
5471d09a |
627 | bufchain_init(&ret->output_data); |
d74d141c |
628 | ret->writable = 1; /* to start with */ |
629 | ret->sending_oob = 0; |
630 | ret->frozen = 1; |
5471d09a |
631 | ret->frozen_readable = 0; |
bc4802a1 |
632 | ret->localhost_only = 0; /* unused, but best init anyway */ |
7732d38a |
633 | ret->pending_error = 0; |
d74d141c |
634 | |
635 | ret->s = (SOCKET)sock; |
636 | |
637 | if (ret->s == INVALID_SOCKET) { |
7440fd44 |
638 | err = p_WSAGetLastError(); |
d74d141c |
639 | ret->error = winsock_error_string(err); |
640 | return (Socket) ret; |
641 | } |
642 | |
643 | ret->oobinline = 0; |
644 | |
645 | /* Set up a select mechanism. This could be an AsyncSelect on a |
646 | * window, or an EventSelect on an event object. */ |
647 | errstr = do_select(ret->s, 1); |
648 | if (errstr) { |
649 | ret->error = errstr; |
650 | return (Socket) ret; |
651 | } |
652 | |
653 | add234(sktree, ret); |
654 | |
655 | return (Socket) ret; |
656 | } |
657 | |
4b1e8acc |
658 | Socket sk_new(SockAddr addr, int port, int privport, int oobinline, |
2184a5d9 |
659 | int nodelay, Plug plug) |
7e78000d |
660 | { |
c85623f9 |
661 | static const struct socket_function_table fn_table = { |
7e78000d |
662 | sk_tcp_plug, |
663 | sk_tcp_close, |
664 | sk_tcp_write, |
665 | sk_tcp_write_oob, |
666 | sk_tcp_flush, |
8eebd221 |
667 | sk_tcp_set_private_ptr, |
668 | sk_tcp_get_private_ptr, |
669 | sk_tcp_set_frozen, |
7e78000d |
670 | sk_tcp_socket_error |
671 | }; |
672 | |
2f75bae1 |
673 | SOCKET s; |
c4d8e107 |
674 | #ifdef IPV6 |
675 | SOCKADDR_IN6 a6; |
676 | #endif |
2f75bae1 |
677 | SOCKADDR_IN a; |
678 | DWORD err; |
679 | char *errstr; |
7e78000d |
680 | Actual_Socket ret; |
c91409da |
681 | short localport; |
2f75bae1 |
682 | |
683 | /* |
684 | * Create Socket structure. |
685 | */ |
3d88e64d |
686 | ret = snew(struct Socket_tag); |
7e78000d |
687 | ret->fn = &fn_table; |
2f75bae1 |
688 | ret->error = NULL; |
7e78000d |
689 | ret->plug = plug; |
5471d09a |
690 | bufchain_init(&ret->output_data); |
3ad9d396 |
691 | ret->connected = 0; /* to start with */ |
692 | ret->writable = 0; /* to start with */ |
33232c8f |
693 | ret->sending_oob = 0; |
d74d141c |
694 | ret->frozen = 0; |
5471d09a |
695 | ret->frozen_readable = 0; |
bc4802a1 |
696 | ret->localhost_only = 0; /* unused, but best init anyway */ |
7732d38a |
697 | ret->pending_error = 0; |
2f75bae1 |
698 | |
699 | /* |
700 | * Open socket. |
701 | */ |
b7a189f3 |
702 | assert(addr->family != AF_UNSPEC); |
7440fd44 |
703 | s = p_socket(addr->family, SOCK_STREAM, 0); |
2f75bae1 |
704 | ret->s = s; |
705 | |
706 | if (s == INVALID_SOCKET) { |
7440fd44 |
707 | err = p_WSAGetLastError(); |
32874aea |
708 | ret->error = winsock_error_string(err); |
7e78000d |
709 | return (Socket) ret; |
2f75bae1 |
710 | } |
4b1e8acc |
711 | |
712 | ret->oobinline = oobinline; |
713 | if (oobinline) { |
1ad4eb6f |
714 | BOOL b = TRUE; |
7440fd44 |
715 | p_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b)); |
1ad4eb6f |
716 | } |
2f75bae1 |
717 | |
2184a5d9 |
718 | if (nodelay) { |
719 | BOOL b = TRUE; |
7440fd44 |
720 | p_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b)); |
2184a5d9 |
721 | } |
722 | |
2f75bae1 |
723 | /* |
724 | * Bind to local address. |
725 | */ |
c91409da |
726 | if (privport) |
32874aea |
727 | localport = 1023; /* count from 1023 downwards */ |
c4d8e107 |
728 | else |
32874aea |
729 | localport = 0; /* just use port 0 (ie winsock picks) */ |
c91409da |
730 | |
731 | /* Loop round trying to bind */ |
732 | while (1) { |
32874aea |
733 | int retcode; |
c91409da |
734 | |
735 | #ifdef IPV6 |
32874aea |
736 | if (addr->family == AF_INET6) { |
737 | memset(&a6, 0, sizeof(a6)); |
738 | a6.sin6_family = AF_INET6; |
739 | /*a6.sin6_addr = in6addr_any; *//* == 0 */ |
7440fd44 |
740 | a6.sin6_port = p_htons(localport); |
32874aea |
741 | } else |
c4d8e107 |
742 | #endif |
32874aea |
743 | { |
744 | a.sin_family = AF_INET; |
7440fd44 |
745 | a.sin_addr.s_addr = p_htonl(INADDR_ANY); |
746 | a.sin_port = p_htons(localport); |
32874aea |
747 | } |
c4d8e107 |
748 | #ifdef IPV6 |
7440fd44 |
749 | retcode = p_bind(s, (addr->family == AF_INET6 ? |
32874aea |
750 | (struct sockaddr *) &a6 : |
751 | (struct sockaddr *) &a), |
752 | (addr->family == |
753 | AF_INET6 ? sizeof(a6) : sizeof(a))); |
c4d8e107 |
754 | #else |
7440fd44 |
755 | retcode = p_bind(s, (struct sockaddr *) &a, sizeof(a)); |
c4d8e107 |
756 | #endif |
32874aea |
757 | if (retcode != SOCKET_ERROR) { |
758 | err = 0; |
759 | break; /* done */ |
760 | } else { |
7440fd44 |
761 | err = p_WSAGetLastError(); |
32874aea |
762 | if (err != WSAEADDRINUSE) /* failed, for a bad reason */ |
763 | break; |
764 | } |
765 | |
766 | if (localport == 0) |
767 | break; /* we're only looping once */ |
768 | localport--; |
769 | if (localport == 0) |
770 | break; /* we might have got to the end */ |
c91409da |
771 | } |
772 | |
32874aea |
773 | if (err) { |
c4d8e107 |
774 | ret->error = winsock_error_string(err); |
7e78000d |
775 | return (Socket) ret; |
2f75bae1 |
776 | } |
777 | |
778 | /* |
779 | * Connect to remote address. |
780 | */ |
c4d8e107 |
781 | #ifdef IPV6 |
32874aea |
782 | if (addr->family == AF_INET6) { |
783 | memset(&a, 0, sizeof(a)); |
c4d8e107 |
784 | a6.sin6_family = AF_INET6; |
7440fd44 |
785 | a6.sin6_port = p_htons((short) port); |
32874aea |
786 | a6.sin6_addr = |
787 | ((struct sockaddr_in6 *) addr->ai->ai_addr)->sin6_addr; |
788 | } else |
c4d8e107 |
789 | #endif |
32874aea |
790 | { |
c4d8e107 |
791 | a.sin_family = AF_INET; |
7440fd44 |
792 | a.sin_addr.s_addr = p_htonl(addr->address); |
793 | a.sin_port = p_htons((short) port); |
c4d8e107 |
794 | } |
3ad9d396 |
795 | |
796 | /* Set up a select mechanism. This could be an AsyncSelect on a |
797 | * window, or an EventSelect on an event object. */ |
798 | errstr = do_select(s, 1); |
799 | if (errstr) { |
800 | ret->error = errstr; |
801 | return (Socket) ret; |
802 | } |
803 | |
32874aea |
804 | if (( |
805 | #ifdef IPV6 |
7440fd44 |
806 | p_connect(s, ((addr->family == AF_INET6) ? |
32874aea |
807 | (struct sockaddr *) &a6 : (struct sockaddr *) &a), |
808 | (addr->family == AF_INET6) ? sizeof(a6) : sizeof(a)) |
c4d8e107 |
809 | #else |
7440fd44 |
810 | p_connect(s, (struct sockaddr *) &a, sizeof(a)) |
c4d8e107 |
811 | #endif |
32874aea |
812 | ) == SOCKET_ERROR) { |
7440fd44 |
813 | err = p_WSAGetLastError(); |
3ad9d396 |
814 | /* |
815 | * We expect a potential EWOULDBLOCK here, because the |
816 | * chances are the front end has done a select for |
817 | * FD_CONNECT, so that connect() will complete |
818 | * asynchronously. |
819 | */ |
820 | if ( err != WSAEWOULDBLOCK ) { |
821 | ret->error = winsock_error_string(err); |
822 | return (Socket) ret; |
823 | } |
824 | } else { |
825 | /* |
826 | * If we _don't_ get EWOULDBLOCK, the connect has completed |
827 | * and we should set the socket as writable. |
828 | */ |
829 | ret->writable = 1; |
2f75bae1 |
830 | } |
831 | |
832 | add234(sktree, ret); |
833 | |
f85e6f6e |
834 | /* We're done with 'addr' now. */ |
835 | sk_addr_free(addr); |
836 | |
7e78000d |
837 | return (Socket) ret; |
2f75bae1 |
838 | } |
839 | |
6ee9b735 |
840 | Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only) |
d74d141c |
841 | { |
c85623f9 |
842 | static const struct socket_function_table fn_table = { |
d74d141c |
843 | sk_tcp_plug, |
844 | sk_tcp_close, |
845 | sk_tcp_write, |
846 | sk_tcp_write_oob, |
847 | sk_tcp_flush, |
8eebd221 |
848 | sk_tcp_set_private_ptr, |
849 | sk_tcp_get_private_ptr, |
850 | sk_tcp_set_frozen, |
d74d141c |
851 | sk_tcp_socket_error |
852 | }; |
853 | |
854 | SOCKET s; |
855 | #ifdef IPV6 |
856 | SOCKADDR_IN6 a6; |
857 | #endif |
858 | SOCKADDR_IN a; |
859 | DWORD err; |
860 | char *errstr; |
861 | Actual_Socket ret; |
862 | int retcode; |
863 | int on = 1; |
864 | |
865 | /* |
866 | * Create Socket structure. |
867 | */ |
3d88e64d |
868 | ret = snew(struct Socket_tag); |
d74d141c |
869 | ret->fn = &fn_table; |
870 | ret->error = NULL; |
871 | ret->plug = plug; |
5471d09a |
872 | bufchain_init(&ret->output_data); |
d74d141c |
873 | ret->writable = 0; /* to start with */ |
874 | ret->sending_oob = 0; |
875 | ret->frozen = 0; |
5471d09a |
876 | ret->frozen_readable = 0; |
bc4802a1 |
877 | ret->localhost_only = local_host_only; |
7732d38a |
878 | ret->pending_error = 0; |
d74d141c |
879 | |
880 | /* |
881 | * Open socket. |
882 | */ |
7440fd44 |
883 | s = p_socket(AF_INET, SOCK_STREAM, 0); |
d74d141c |
884 | ret->s = s; |
885 | |
886 | if (s == INVALID_SOCKET) { |
7440fd44 |
887 | err = p_WSAGetLastError(); |
d74d141c |
888 | ret->error = winsock_error_string(err); |
889 | return (Socket) ret; |
890 | } |
891 | |
892 | ret->oobinline = 0; |
893 | |
7440fd44 |
894 | p_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)); |
d74d141c |
895 | |
d74d141c |
896 | #ifdef IPV6 |
897 | if (addr->family == AF_INET6) { |
898 | memset(&a6, 0, sizeof(a6)); |
899 | a6.sin6_family = AF_INET6; |
6ee9b735 |
900 | /* FIXME: srcaddr is ignored for IPv6, because I (SGT) don't |
901 | * know how to do it. :-) */ |
bcce45ed |
902 | if (local_host_only) |
903 | a6.sin6_addr = in6addr_loopback; |
904 | else |
905 | a6.sin6_addr = in6addr_any; |
7440fd44 |
906 | a6.sin6_port = p_htons(port); |
d74d141c |
907 | } else |
908 | #endif |
909 | { |
6ee9b735 |
910 | int got_addr = 0; |
d74d141c |
911 | a.sin_family = AF_INET; |
6ee9b735 |
912 | |
913 | /* |
914 | * Bind to source address. First try an explicitly |
915 | * specified one... |
916 | */ |
917 | if (srcaddr) { |
7440fd44 |
918 | a.sin_addr.s_addr = p_inet_addr(srcaddr); |
6ee9b735 |
919 | if (a.sin_addr.s_addr != INADDR_NONE) { |
920 | /* Override localhost_only with specified listen addr. */ |
921 | ret->localhost_only = ipv4_is_loopback(a.sin_addr); |
922 | got_addr = 1; |
923 | } |
924 | } |
925 | |
926 | /* |
927 | * ... and failing that, go with one of the standard ones. |
928 | */ |
929 | if (!got_addr) { |
930 | if (local_host_only) |
7440fd44 |
931 | a.sin_addr.s_addr = p_htonl(INADDR_LOOPBACK); |
6ee9b735 |
932 | else |
7440fd44 |
933 | a.sin_addr.s_addr = p_htonl(INADDR_ANY); |
6ee9b735 |
934 | } |
935 | |
7440fd44 |
936 | a.sin_port = p_htons((short)port); |
d74d141c |
937 | } |
938 | #ifdef IPV6 |
7440fd44 |
939 | retcode = p_bind(s, (addr->family == AF_INET6 ? |
d74d141c |
940 | (struct sockaddr *) &a6 : |
941 | (struct sockaddr *) &a), |
942 | (addr->family == |
943 | AF_INET6 ? sizeof(a6) : sizeof(a))); |
944 | #else |
7440fd44 |
945 | retcode = p_bind(s, (struct sockaddr *) &a, sizeof(a)); |
d74d141c |
946 | #endif |
947 | if (retcode != SOCKET_ERROR) { |
948 | err = 0; |
949 | } else { |
7440fd44 |
950 | err = p_WSAGetLastError(); |
d74d141c |
951 | } |
952 | |
953 | if (err) { |
954 | ret->error = winsock_error_string(err); |
955 | return (Socket) ret; |
956 | } |
957 | |
958 | |
7440fd44 |
959 | if (p_listen(s, SOMAXCONN) == SOCKET_ERROR) { |
960 | p_closesocket(s); |
d74d141c |
961 | ret->error = winsock_error_string(err); |
962 | return (Socket) ret; |
963 | } |
964 | |
965 | /* Set up a select mechanism. This could be an AsyncSelect on a |
966 | * window, or an EventSelect on an event object. */ |
967 | errstr = do_select(s, 1); |
968 | if (errstr) { |
969 | ret->error = errstr; |
970 | return (Socket) ret; |
971 | } |
972 | |
973 | add234(sktree, ret); |
974 | |
975 | return (Socket) ret; |
976 | } |
977 | |
32874aea |
978 | static void sk_tcp_close(Socket sock) |
979 | { |
9c964e85 |
980 | extern char *do_select(SOCKET skt, int startup); |
7e78000d |
981 | Actual_Socket s = (Actual_Socket) sock; |
9c964e85 |
982 | |
2f75bae1 |
983 | del234(sktree, s); |
984 | do_select(s->s, 0); |
7440fd44 |
985 | p_closesocket(s->s); |
dcbde236 |
986 | sfree(s); |
2f75bae1 |
987 | } |
988 | |
2f75bae1 |
989 | /* |
990 | * The function which tries to send on a socket once it's deemed |
991 | * writable. |
992 | */ |
32874aea |
993 | void try_send(Actual_Socket s) |
994 | { |
5471d09a |
995 | while (s->sending_oob || bufchain_size(&s->output_data) > 0) { |
2f75bae1 |
996 | int nsent; |
997 | DWORD err; |
5471d09a |
998 | void *data; |
32874aea |
999 | int len, urgentflag; |
1000 | |
1001 | if (s->sending_oob) { |
1002 | urgentflag = MSG_OOB; |
1003 | len = s->sending_oob; |
5471d09a |
1004 | data = &s->oobdata; |
32874aea |
1005 | } else { |
1006 | urgentflag = 0; |
5471d09a |
1007 | bufchain_prefix(&s->output_data, &data, &len); |
32874aea |
1008 | } |
7440fd44 |
1009 | nsent = p_send(s->s, data, len, urgentflag); |
32874aea |
1010 | noise_ultralight(nsent); |
2f75bae1 |
1011 | if (nsent <= 0) { |
7440fd44 |
1012 | err = (nsent < 0 ? p_WSAGetLastError() : 0); |
e5eb3a1c |
1013 | if ((err < WSABASEERR && nsent < 0) || err == WSAEWOULDBLOCK) { |
d40a94b9 |
1014 | /* |
1015 | * Perfectly normal: we've sent all we can for the moment. |
1016 | * |
e5eb3a1c |
1017 | * (Some WinSock send() implementations can return |
1018 | * <0 but leave no sensible error indication - |
1019 | * WSAGetLastError() is called but returns zero or |
1020 | * a small number - so we check that case and treat |
1021 | * it just like WSAEWOULDBLOCK.) |
d40a94b9 |
1022 | */ |
2f75bae1 |
1023 | s->writable = FALSE; |
32874aea |
1024 | return; |
2f75bae1 |
1025 | } else if (nsent == 0 || |
32874aea |
1026 | err == WSAECONNABORTED || err == WSAECONNRESET) { |
1027 | /* |
7732d38a |
1028 | * If send() returns CONNABORTED or CONNRESET, we |
1029 | * unfortunately can't just call plug_closing(), |
1030 | * because it's quite likely that we're currently |
1031 | * _in_ a call from the code we'd be calling back |
1032 | * to, so we'd have to make half the SSH code |
1033 | * reentrant. Instead we flag a pending error on |
1034 | * the socket, to be dealt with (by calling |
1035 | * plug_closing()) at some suitable future moment. |
32874aea |
1036 | */ |
7732d38a |
1037 | s->pending_error = err; |
1038 | return; |
2f75bae1 |
1039 | } else { |
a8327734 |
1040 | /* We're inside the Windows frontend here, so we know |
1041 | * that the frontend handle is unnecessary. */ |
1042 | logevent(NULL, winsock_error_string(err)); |
247308b5 |
1043 | fatalbox("%s", winsock_error_string(err)); |
2f75bae1 |
1044 | } |
1045 | } else { |
5471d09a |
1046 | if (s->sending_oob) { |
1047 | if (nsent < len) { |
1048 | memmove(s->oobdata, s->oobdata+nsent, len-nsent); |
1049 | s->sending_oob = len - nsent; |
1050 | } else { |
1051 | s->sending_oob = 0; |
1052 | } |
1053 | } else { |
1054 | bufchain_consume(&s->output_data, nsent); |
2f75bae1 |
1055 | } |
1056 | } |
1057 | } |
1058 | } |
1059 | |
e0e7dff8 |
1060 | static int sk_tcp_write(Socket sock, const char *buf, int len) |
32874aea |
1061 | { |
7e78000d |
1062 | Actual_Socket s = (Actual_Socket) sock; |
1063 | |
2f75bae1 |
1064 | /* |
1065 | * Add the data to the buffer list on the socket. |
1066 | */ |
5471d09a |
1067 | bufchain_add(&s->output_data, buf, len); |
2f75bae1 |
1068 | |
1069 | /* |
1070 | * Now try sending from the start of the buffer list. |
1071 | */ |
1072 | if (s->writable) |
1073 | try_send(s); |
5471d09a |
1074 | |
1075 | return bufchain_size(&s->output_data); |
2f75bae1 |
1076 | } |
1077 | |
e0e7dff8 |
1078 | static int sk_tcp_write_oob(Socket sock, const char *buf, int len) |
32874aea |
1079 | { |
7e78000d |
1080 | Actual_Socket s = (Actual_Socket) sock; |
1081 | |
2f75bae1 |
1082 | /* |
1083 | * Replace the buffer list on the socket with the data. |
1084 | */ |
5471d09a |
1085 | bufchain_clear(&s->output_data); |
1086 | assert(len <= sizeof(s->oobdata)); |
1087 | memcpy(s->oobdata, buf, len); |
2f75bae1 |
1088 | s->sending_oob = len; |
1089 | |
1090 | /* |
1091 | * Now try sending from the start of the buffer list. |
1092 | */ |
1093 | if (s->writable) |
1094 | try_send(s); |
5471d09a |
1095 | |
1096 | return s->sending_oob; |
2f75bae1 |
1097 | } |
1098 | |
32874aea |
1099 | int select_result(WPARAM wParam, LPARAM lParam) |
1100 | { |
f9d3f227 |
1101 | int ret, open; |
2f75bae1 |
1102 | DWORD err; |
b675c612 |
1103 | char buf[20480]; /* nice big buffer for plenty of speed */ |
7e78000d |
1104 | Actual_Socket s; |
49bad831 |
1105 | u_long atmark; |
2f75bae1 |
1106 | |
1107 | /* wParam is the socket itself */ |
cc8b0ee9 |
1108 | |
ce6fcfa5 |
1109 | if (wParam == 0) |
1110 | return 1; /* boggle */ |
cc8b0ee9 |
1111 | |
32874aea |
1112 | s = find234(sktree, (void *) wParam, cmpforsearch); |
2f75bae1 |
1113 | if (!s) |
1114 | return 1; /* boggle */ |
1115 | |
1116 | if ((err = WSAGETSELECTERROR(lParam)) != 0) { |
32874aea |
1117 | /* |
1118 | * An error has occurred on this socket. Pass it to the |
1119 | * plug. |
1120 | */ |
1121 | return plug_closing(s->plug, winsock_error_string(err), err, 0); |
2f75bae1 |
1122 | } |
1123 | |
7d6ee6ff |
1124 | noise_ultralight(lParam); |
1125 | |
2f75bae1 |
1126 | switch (WSAGETSELECTEVENT(lParam)) { |
3ad9d396 |
1127 | case FD_CONNECT: |
1128 | s->connected = s->writable = 1; |
1129 | break; |
2f75bae1 |
1130 | case FD_READ: |
d74d141c |
1131 | /* In the case the socket is still frozen, we don't even bother */ |
5471d09a |
1132 | if (s->frozen) { |
1133 | s->frozen_readable = 1; |
d74d141c |
1134 | break; |
5471d09a |
1135 | } |
d74d141c |
1136 | |
32874aea |
1137 | /* |
1138 | * We have received data on the socket. For an oobinline |
1139 | * socket, this might be data _before_ an urgent pointer, |
1140 | * in which case we send it to the back end with type==1 |
1141 | * (data prior to urgent). |
1142 | */ |
1143 | if (s->oobinline) { |
1144 | atmark = 1; |
7440fd44 |
1145 | p_ioctlsocket(s->s, SIOCATMARK, &atmark); |
32874aea |
1146 | /* |
1147 | * Avoid checking the return value from ioctlsocket(), |
1148 | * on the grounds that some WinSock wrappers don't |
1149 | * support it. If it does nothing, we get atmark==1, |
1150 | * which is equivalent to `no OOB pending', so the |
1151 | * effect will be to non-OOB-ify any OOB data. |
1152 | */ |
1153 | } else |
1154 | atmark = 1; |
4b1e8acc |
1155 | |
7440fd44 |
1156 | ret = p_recv(s->s, buf, sizeof(buf), 0); |
32874aea |
1157 | noise_ultralight(ret); |
2f75bae1 |
1158 | if (ret < 0) { |
7440fd44 |
1159 | err = p_WSAGetLastError(); |
2f75bae1 |
1160 | if (err == WSAEWOULDBLOCK) { |
1161 | break; |
1162 | } |
1163 | } |
1164 | if (ret < 0) { |
32874aea |
1165 | return plug_closing(s->plug, winsock_error_string(err), err, |
1166 | 0); |
7e78000d |
1167 | } else if (0 == ret) { |
32874aea |
1168 | return plug_closing(s->plug, NULL, 0, 0); |
2f75bae1 |
1169 | } else { |
32874aea |
1170 | return plug_receive(s->plug, atmark ? 0 : 1, buf, ret); |
2f75bae1 |
1171 | } |
1172 | break; |
1173 | case FD_OOB: |
32874aea |
1174 | /* |
1175 | * This will only happen on a non-oobinline socket. It |
1176 | * indicates that we can immediately perform an OOB read |
1177 | * and get back OOB data, which we will send to the back |
1178 | * end with type==2 (urgent data). |
1179 | */ |
7440fd44 |
1180 | ret = p_recv(s->s, buf, sizeof(buf), MSG_OOB); |
32874aea |
1181 | noise_ultralight(ret); |
1182 | if (ret <= 0) { |
247308b5 |
1183 | char *str = (ret == 0 ? "Internal networking trouble" : |
7440fd44 |
1184 | winsock_error_string(p_WSAGetLastError())); |
a8327734 |
1185 | /* We're inside the Windows frontend here, so we know |
1186 | * that the frontend handle is unnecessary. */ |
1187 | logevent(NULL, str); |
247308b5 |
1188 | fatalbox("%s", str); |
32874aea |
1189 | } else { |
1190 | return plug_receive(s->plug, 2, buf, ret); |
1191 | } |
1192 | break; |
2f75bae1 |
1193 | case FD_WRITE: |
5471d09a |
1194 | { |
1195 | int bufsize_before, bufsize_after; |
1196 | s->writable = 1; |
1197 | bufsize_before = s->sending_oob + bufchain_size(&s->output_data); |
1198 | try_send(s); |
1199 | bufsize_after = s->sending_oob + bufchain_size(&s->output_data); |
1200 | if (bufsize_after < bufsize_before) |
1201 | plug_sent(s->plug, bufsize_after); |
1202 | } |
2f75bae1 |
1203 | break; |
1204 | case FD_CLOSE: |
f9d3f227 |
1205 | /* Signal a close on the socket. First read any outstanding data. */ |
32874aea |
1206 | open = 1; |
1207 | do { |
7440fd44 |
1208 | ret = p_recv(s->s, buf, sizeof(buf), 0); |
32874aea |
1209 | if (ret < 0) { |
7440fd44 |
1210 | err = p_WSAGetLastError(); |
32874aea |
1211 | if (err == WSAEWOULDBLOCK) |
1212 | break; |
1213 | return plug_closing(s->plug, winsock_error_string(err), |
1214 | err, 0); |
1215 | } else { |
1216 | if (ret) |
1217 | open &= plug_receive(s->plug, 0, buf, ret); |
1218 | else |
1219 | open &= plug_closing(s->plug, NULL, 0, 0); |
7e78000d |
1220 | } |
f9d3f227 |
1221 | } while (ret > 0); |
32874aea |
1222 | return open; |
d74d141c |
1223 | case FD_ACCEPT: |
1224 | { |
bc4802a1 |
1225 | struct sockaddr_in isa; |
1226 | int addrlen = sizeof(struct sockaddr_in); |
bcce45ed |
1227 | SOCKET t; /* socket of connection */ |
1228 | |
bc4802a1 |
1229 | memset(&isa, 0, sizeof(struct sockaddr_in)); |
bcce45ed |
1230 | err = 0; |
7440fd44 |
1231 | t = p_accept(s->s,(struct sockaddr *)&isa,&addrlen); |
bcce45ed |
1232 | if (t == INVALID_SOCKET) |
1233 | { |
7440fd44 |
1234 | err = p_WSAGetLastError(); |
bcce45ed |
1235 | if (err == WSATRY_AGAIN) |
1236 | break; |
1237 | } |
1238 | |
a143ea0a |
1239 | if (s->localhost_only && !ipv4_is_local_addr(isa.sin_addr)) { |
7440fd44 |
1240 | p_closesocket(t); /* dodgy WinSock let nonlocal through */ |
bc4802a1 |
1241 | } else if (plug_accepting(s->plug, (void*)t)) { |
7440fd44 |
1242 | p_closesocket(t); /* denied or error */ |
bcce45ed |
1243 | } |
d74d141c |
1244 | } |
2f75bae1 |
1245 | } |
1246 | |
1247 | return 1; |
1248 | } |
1249 | |
1250 | /* |
7732d38a |
1251 | * Deal with socket errors detected in try_send(). |
1252 | */ |
1253 | void net_pending_errors(void) |
1254 | { |
1255 | int i; |
1256 | Actual_Socket s; |
1257 | |
1258 | /* |
1259 | * This might be a fiddly business, because it's just possible |
1260 | * that handling a pending error on one socket might cause |
1261 | * others to be closed. (I can't think of any reason this might |
1262 | * happen in current SSH implementation, but to maintain |
1263 | * generality of this network layer I'll assume the worst.) |
1264 | * |
1265 | * So what we'll do is search the socket list for _one_ socket |
1266 | * with a pending error, and then handle it, and then search |
1267 | * the list again _from the beginning_. Repeat until we make a |
1268 | * pass with no socket errors present. That way we are |
1269 | * protected against the socket list changing under our feet. |
1270 | */ |
1271 | |
1272 | do { |
1273 | for (i = 0; (s = index234(sktree, i)) != NULL; i++) { |
1274 | if (s->pending_error) { |
1275 | /* |
1276 | * An error has occurred on this socket. Pass it to the |
1277 | * plug. |
1278 | */ |
1279 | plug_closing(s->plug, |
1280 | winsock_error_string(s->pending_error), |
1281 | s->pending_error, 0); |
1282 | break; |
1283 | } |
1284 | } |
1285 | } while (s); |
1286 | } |
1287 | |
1288 | /* |
2f75bae1 |
1289 | * Each socket abstraction contains a `void *' private field in |
1290 | * which the client can keep state. |
1291 | */ |
8eebd221 |
1292 | static void sk_tcp_set_private_ptr(Socket sock, void *ptr) |
32874aea |
1293 | { |
7e78000d |
1294 | Actual_Socket s = (Actual_Socket) sock; |
2f75bae1 |
1295 | s->private_ptr = ptr; |
1296 | } |
32874aea |
1297 | |
8eebd221 |
1298 | static void *sk_tcp_get_private_ptr(Socket sock) |
32874aea |
1299 | { |
7e78000d |
1300 | Actual_Socket s = (Actual_Socket) sock; |
2f75bae1 |
1301 | return s->private_ptr; |
1302 | } |
1303 | |
1304 | /* |
1305 | * Special error values are returned from sk_namelookup and sk_new |
1306 | * if there's a problem. These functions extract an error message, |
1307 | * or return NULL if there's no problem. |
1308 | */ |
cbe2d68f |
1309 | const char *sk_addr_error(SockAddr addr) |
32874aea |
1310 | { |
2f75bae1 |
1311 | return addr->error; |
1312 | } |
cbe2d68f |
1313 | static const char *sk_tcp_socket_error(Socket sock) |
32874aea |
1314 | { |
7e78000d |
1315 | Actual_Socket s = (Actual_Socket) sock; |
2f75bae1 |
1316 | return s->error; |
1317 | } |
1318 | |
8eebd221 |
1319 | static void sk_tcp_set_frozen(Socket sock, int is_frozen) |
d74d141c |
1320 | { |
1321 | Actual_Socket s = (Actual_Socket) sock; |
5471d09a |
1322 | if (s->frozen == is_frozen) |
1323 | return; |
d74d141c |
1324 | s->frozen = is_frozen; |
5471d09a |
1325 | if (!is_frozen && s->frozen_readable) { |
d74d141c |
1326 | char c; |
7440fd44 |
1327 | p_recv(s->s, &c, 1, MSG_PEEK); |
d74d141c |
1328 | } |
5471d09a |
1329 | s->frozen_readable = 0; |
d74d141c |
1330 | } |
1331 | |
2f75bae1 |
1332 | /* |
1333 | * For Plink: enumerate all sockets currently active. |
1334 | */ |
32874aea |
1335 | SOCKET first_socket(int *state) |
1336 | { |
d2371c81 |
1337 | Actual_Socket s; |
1338 | *state = 0; |
1339 | s = index234(sktree, (*state)++); |
2f75bae1 |
1340 | return s ? s->s : INVALID_SOCKET; |
1341 | } |
32874aea |
1342 | |
1343 | SOCKET next_socket(int *state) |
1344 | { |
d2371c81 |
1345 | Actual_Socket s = index234(sktree, (*state)++); |
2f75bae1 |
1346 | return s ? s->s : INVALID_SOCKET; |
1347 | } |
68a49acb |
1348 | |
1349 | int net_service_lookup(char *service) |
1350 | { |
1351 | struct servent *se; |
7440fd44 |
1352 | se = p_getservbyname(service, NULL); |
68a49acb |
1353 | if (se != NULL) |
7440fd44 |
1354 | return p_ntohs(se->s_port); |
68a49acb |
1355 | else |
1356 | return 0; |
1357 | } |