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