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