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