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