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