2 * Windows networking abstraction.
4 * Due to this clean abstraction it was possible
5 * to easily implement IPv6 support :)
7 * IPv6 patch 1 (27 October 2000) Jeroen Massar <jeroen@unfix.org>
8 * - Preliminary hacked IPv6 support.
9 * - Connecting to IPv6 address (eg fec0:4242:4242:100:2d0:b7ff:fe8f:5d42) works.
10 * - Connecting to IPv6 hostname (eg heaven.ipv6.unfix.org) works.
11 * - Compiles as either IPv4 or IPv6.
13 * IPv6 patch 2 (29 October 2000) Jeroen Massar <jeroen@unfix.org>
14 * - When compiled as IPv6 it also allows connecting to IPv4 hosts.
15 * - Added some more documentation.
17 * IPv6 patch 3 (18 November 2000) Jeroen Massar <jeroen@unfix.org>
18 * - It now supports dynamically loading the IPv6 resolver dll's.
19 * This way we should be able to distribute one (1) binary
20 * which supports both IPv4 and IPv6.
21 * - getaddrinfo() and getnameinfo() are loaded dynamicaly if possible.
22 * - in6addr_any is defined in this file so we don't need to link to wship6.lib
23 * - The patch is now more unified so that we can still
24 * remove all IPv6 support by undef'ing IPV6.
25 * But where it fallsback to IPv4 it uses the IPv4 code which is already in place...
26 * - Canonical name resolving works.
28 * IPv6 patch 4 (07 January 2001) Jeroen Massar <jeroen@unfix.org>
29 * - patch against CVS of today, will be submitted to the bugs list
30 * as a 'cvs diff -u' on Simon's request...
35 * Define IPV6 to have IPv6 on-the-fly-loading support.
36 * This means that one doesn't have to have an IPv6 stack to use it.
37 * But if an IPv6 stack is found it is used with a fallback to IPv4.
53 #define DEFINE_PLUG_METHOD_MACROS
59 struct socket_function_table
*fn
;
60 /* the above variable absolutely *must* be the first in this structure */
68 int frozen
; /* this causes readability notifications to be ignored */
69 int frozen_readable
; /* this means we missed at least one readability
70 * notification while we were frozen */
71 int localhost_only
; /* for listening sockets */
75 int pending_error
; /* in case send() returns error */
79 * We used to typedef struct Socket_tag *Socket.
81 * Since we have made the networking abstraction slightly more
82 * abstract, Socket no longer means a tcp socket (it could mean
83 * an ssl socket). So now we must use Actual_Socket when we know
84 * we are talking about a tcp socket.
86 typedef struct Socket_tag
*Actual_Socket
;
90 /* address family this belongs to, AF_INET for IPv4, AF_INET6 for IPv6. */
92 unsigned long address
; /* Address IPv4 style. */
94 struct addrinfo
*ai
; /* Address IPv6 style. */
98 static tree234
*sktree
;
100 static int cmpfortree(void *av
, void *bv
)
102 Actual_Socket a
= (Actual_Socket
) av
, b
= (Actual_Socket
) bv
;
103 unsigned long as
= (unsigned long) a
->s
, bs
= (unsigned long) b
->s
;
111 static int cmpforsearch(void *av
, void *bv
)
113 Actual_Socket b
= (Actual_Socket
) bv
;
114 unsigned long as
= (unsigned long) av
, bs
= (unsigned long) b
->s
;
124 sktree
= newtree234(cmpfortree
);
127 void sk_cleanup(void)
133 for (i
= 0; (s
= index234(sktree
, i
)) != NULL
; i
++) {
139 char *winsock_error_string(int error
)
143 return "Network error: Permission denied";
145 return "Network error: Address already in use";
146 case WSAEADDRNOTAVAIL
:
147 return "Network error: Cannot assign requested address";
148 case WSAEAFNOSUPPORT
:
150 "Network error: Address family not supported by protocol family";
152 return "Network error: Operation already in progress";
153 case WSAECONNABORTED
:
154 return "Network error: Software caused connection abort";
155 case WSAECONNREFUSED
:
156 return "Network error: Connection refused";
158 return "Network error: Connection reset by peer";
159 case WSAEDESTADDRREQ
:
160 return "Network error: Destination address required";
162 return "Network error: Bad address";
164 return "Network error: Host is down";
165 case WSAEHOSTUNREACH
:
166 return "Network error: No route to host";
168 return "Network error: Operation now in progress";
170 return "Network error: Interrupted function call";
172 return "Network error: Invalid argument";
174 return "Network error: Socket is already connected";
176 return "Network error: Too many open files";
178 return "Network error: Message too long";
180 return "Network error: Network is down";
182 return "Network error: Network dropped connection on reset";
184 return "Network error: Network is unreachable";
186 return "Network error: No buffer space available";
188 return "Network error: Bad protocol option";
190 return "Network error: Socket is not connected";
192 return "Network error: Socket operation on non-socket";
194 return "Network error: Operation not supported";
195 case WSAEPFNOSUPPORT
:
196 return "Network error: Protocol family not supported";
198 return "Network error: Too many processes";
199 case WSAEPROTONOSUPPORT
:
200 return "Network error: Protocol not supported";
202 return "Network error: Protocol wrong type for socket";
204 return "Network error: Cannot send after socket shutdown";
205 case WSAESOCKTNOSUPPORT
:
206 return "Network error: Socket type not supported";
208 return "Network error: Connection timed out";
210 return "Network error: Resource temporarily unavailable";
212 return "Network error: Graceful shutdown in progress";
214 return "Unknown network error";
218 SockAddr
sk_namelookup(char *host
, char **canonicalname
)
220 SockAddr ret
= smalloc(sizeof(struct SockAddr_tag
));
222 struct hostent
*h
= NULL
;
225 /* Clear the structure and default to IPv4. */
226 memset(ret
, 0, sizeof(struct SockAddr_tag
));
227 ret
->family
= 0; /* We set this one when we have resolved the host. */
230 if ((a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
233 /* Try to get the getaddrinfo() function from wship6.dll */
234 /* This way one doesn't need to have IPv6 dll's to use PuTTY and
235 * it will fallback to IPv4. */
236 typedef int (CALLBACK
* FGETADDRINFO
) (const char *nodename
,
237 const char *servname
,
238 const struct addrinfo
*
240 struct addrinfo
** res
);
241 FGETADDRINFO fGetAddrInfo
= NULL
;
243 HINSTANCE dllWSHIP6
= LoadLibrary("wship6.dll");
245 fGetAddrInfo
= (FGETADDRINFO
) GetProcAddress(dllWSHIP6
,
249 * Use fGetAddrInfo when it's available (which usually also
250 * means IPv6 is installed...)
253 /*debug(("Resolving \"%s\" with getaddrinfo() (IPv4+IPv6 capable)...\n", host)); */
254 if (fGetAddrInfo(host
, NULL
, NULL
, &ret
->ai
) == 0)
255 ret
->family
= ret
->ai
->ai_family
;
260 * Otherwise use the IPv4-only gethostbyname...
261 * (NOTE: we don't use gethostbyname as a
264 if (ret
->family
== 0) {
265 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
266 if ( (h
= gethostbyname(host
)) )
267 ret
->family
= AF_INET
;
270 /*debug(("Done resolving...(family is %d) AF_INET = %d, AF_INET6 = %d\n", ret->family, AF_INET, AF_INET6)); */
272 if (ret
->family
== 0) {
273 DWORD err
= WSAGetLastError();
274 ret
->error
= (err
== WSAENETDOWN ?
"Network is down" :
276 WSAHOST_NOT_FOUND ?
"Host does not exist" : err
277 == WSATRY_AGAIN ?
"Host not found" :
279 fGetAddrInfo ?
"getaddrinfo: unknown error" :
281 "gethostbyname: unknown error");
285 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
286 FORMAT_MESSAGE_FROM_SYSTEM
|
287 FORMAT_MESSAGE_IGNORE_INSERTS
, NULL
, err
,
288 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
289 (LPTSTR
) & lpMsgBuf
, 0, NULL
);
290 /*debug(("Error %ld: %s (h=%lx)\n", err, lpMsgBuf, h)); */
291 /* Free the buffer. */
299 /* If we got an address info use that... */
301 typedef int (CALLBACK
* FGETNAMEINFO
)
302 (const struct sockaddr FAR
* sa
, socklen_t salen
,
303 char FAR
* host
, size_t hostlen
, char FAR
* serv
,
304 size_t servlen
, int flags
);
305 FGETNAMEINFO fGetNameInfo
= NULL
;
307 /* Are we in IPv4 fallback mode? */
308 /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */
309 if (ret
->family
== AF_INET
)
311 (char *) &((SOCKADDR_IN
*) ret
->ai
->
312 ai_addr
)->sin_addr
, sizeof(a
));
314 /* Now let's find that canonicalname... */
317 (FGETNAMEINFO
) GetProcAddress(dllWSHIP6
,
320 ((struct sockaddr
*) ret
->ai
->ai_addr
,
322 AF_INET ?
sizeof(SOCKADDR_IN
) :
323 sizeof(SOCKADDR_IN6
), realhost
,
324 sizeof(realhost
), NULL
, 0, 0) != 0) {
325 strncpy(realhost
, host
, sizeof(realhost
));
329 /* We used the IPv4-only gethostbyname()... */
333 memcpy(&a
, h
->h_addr
, sizeof(a
));
334 /* This way we are always sure the h->h_name is valid :) */
335 strncpy(realhost
, h
->h_name
, sizeof(realhost
));
339 FreeLibrary(dllWSHIP6
);
343 * This must be a numeric IPv4 address because it caused a
344 * success return from inet_addr.
346 ret
->family
= AF_INET
;
347 strncpy(realhost
, host
, sizeof(realhost
));
349 ret
->address
= ntohl(a
);
350 realhost
[lenof(realhost
)-1] = '\0';
351 *canonicalname
= smalloc(1+strlen(realhost
));
352 strcpy(*canonicalname
, realhost
);
356 void sk_getaddr(SockAddr addr
, char *buf
, int buflen
)
359 if (addr
->family
== AF_INET
) {
362 a
.s_addr
= htonl(addr
->address
);
363 strncpy(buf
, inet_ntoa(a
), buflen
);
366 FIXME
; /* I don't know how to get a text form of an IPv6 address. */
371 void sk_addr_free(SockAddr addr
)
376 static Plug
sk_tcp_plug(Socket sock
, Plug p
)
378 Actual_Socket s
= (Actual_Socket
) sock
;
385 static void sk_tcp_flush(Socket s
)
388 * We send data to the socket as soon as we can anyway,
389 * so we don't need to do anything here. :-)
393 static void sk_tcp_close(Socket s
);
394 static int sk_tcp_write(Socket s
, char *data
, int len
);
395 static int sk_tcp_write_oob(Socket s
, char *data
, int len
);
396 static void sk_tcp_set_private_ptr(Socket s
, void *ptr
);
397 static void *sk_tcp_get_private_ptr(Socket s
);
398 static void sk_tcp_set_frozen(Socket s
, int is_frozen
);
399 static char *sk_tcp_socket_error(Socket s
);
401 extern char *do_select(SOCKET skt
, int startup
);
403 Socket
sk_register(void *sock
, Plug plug
)
405 static struct socket_function_table fn_table
= {
411 sk_tcp_set_private_ptr
,
412 sk_tcp_get_private_ptr
,
422 * Create Socket structure.
424 ret
= smalloc(sizeof(struct Socket_tag
));
428 bufchain_init(&ret
->output_data
);
429 ret
->writable
= 1; /* to start with */
430 ret
->sending_oob
= 0;
432 ret
->frozen_readable
= 0;
433 ret
->localhost_only
= 0; /* unused, but best init anyway */
434 ret
->pending_error
= 0;
436 ret
->s
= (SOCKET
)sock
;
438 if (ret
->s
== INVALID_SOCKET
) {
439 err
= WSAGetLastError();
440 ret
->error
= winsock_error_string(err
);
446 /* Set up a select mechanism. This could be an AsyncSelect on a
447 * window, or an EventSelect on an event object. */
448 errstr
= do_select(ret
->s
, 1);
459 Socket
sk_new(SockAddr addr
, int port
, int privport
, int oobinline
,
460 int nodelay
, Plug plug
)
462 static struct socket_function_table fn_table
= {
468 sk_tcp_set_private_ptr
,
469 sk_tcp_get_private_ptr
,
485 * Create Socket structure.
487 ret
= smalloc(sizeof(struct Socket_tag
));
491 bufchain_init(&ret
->output_data
);
492 ret
->connected
= 0; /* to start with */
493 ret
->writable
= 0; /* to start with */
494 ret
->sending_oob
= 0;
496 ret
->frozen_readable
= 0;
497 ret
->localhost_only
= 0; /* unused, but best init anyway */
498 ret
->pending_error
= 0;
503 s
= socket(addr
->family
, SOCK_STREAM
, 0);
506 if (s
== INVALID_SOCKET
) {
507 err
= WSAGetLastError();
508 ret
->error
= winsock_error_string(err
);
512 ret
->oobinline
= oobinline
;
515 setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, (void *) &b
, sizeof(b
));
520 setsockopt(s
, IPPROTO_TCP
, TCP_NODELAY
, (void *) &b
, sizeof(b
));
524 * Bind to local address.
527 localport
= 1023; /* count from 1023 downwards */
529 localport
= 0; /* just use port 0 (ie winsock picks) */
531 /* Loop round trying to bind */
536 if (addr
->family
== AF_INET6
) {
537 memset(&a6
, 0, sizeof(a6
));
538 a6
.sin6_family
= AF_INET6
;
539 /*a6.sin6_addr = in6addr_any; *//* == 0 */
540 a6
.sin6_port
= htons(localport
);
544 a
.sin_family
= AF_INET
;
545 a
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
546 a
.sin_port
= htons(localport
);
549 retcode
= bind(s
, (addr
->family
== AF_INET6 ?
550 (struct sockaddr
*) &a6
:
551 (struct sockaddr
*) &a
),
553 AF_INET6 ?
sizeof(a6
) : sizeof(a
)));
555 retcode
= bind(s
, (struct sockaddr
*) &a
, sizeof(a
));
557 if (retcode
!= SOCKET_ERROR
) {
561 err
= WSAGetLastError();
562 if (err
!= WSAEADDRINUSE
) /* failed, for a bad reason */
567 break; /* we're only looping once */
570 break; /* we might have got to the end */
574 ret
->error
= winsock_error_string(err
);
579 * Connect to remote address.
582 if (addr
->family
== AF_INET6
) {
583 memset(&a
, 0, sizeof(a
));
584 a6
.sin6_family
= AF_INET6
;
585 a6
.sin6_port
= htons((short) port
);
587 ((struct sockaddr_in6
*) addr
->ai
->ai_addr
)->sin6_addr
;
591 a
.sin_family
= AF_INET
;
592 a
.sin_addr
.s_addr
= htonl(addr
->address
);
593 a
.sin_port
= htons((short) port
);
596 /* Set up a select mechanism. This could be an AsyncSelect on a
597 * window, or an EventSelect on an event object. */
598 errstr
= do_select(s
, 1);
606 connect(s
, ((addr
->family
== AF_INET6
) ?
607 (struct sockaddr
*) &a6
: (struct sockaddr
*) &a
),
608 (addr
->family
== AF_INET6
) ?
sizeof(a6
) : sizeof(a
))
610 connect(s
, (struct sockaddr
*) &a
, sizeof(a
))
613 err
= WSAGetLastError();
615 * We expect a potential EWOULDBLOCK here, because the
616 * chances are the front end has done a select for
617 * FD_CONNECT, so that connect() will complete
620 if ( err
!= WSAEWOULDBLOCK
) {
621 ret
->error
= winsock_error_string(err
);
626 * If we _don't_ get EWOULDBLOCK, the connect has completed
627 * and we should set the socket as writable.
637 Socket
sk_newlistener(int port
, Plug plug
, int local_host_only
)
639 static struct socket_function_table fn_table
= {
645 sk_tcp_set_private_ptr
,
646 sk_tcp_get_private_ptr
,
663 * Create Socket structure.
665 ret
= smalloc(sizeof(struct Socket_tag
));
669 bufchain_init(&ret
->output_data
);
670 ret
->writable
= 0; /* to start with */
671 ret
->sending_oob
= 0;
673 ret
->frozen_readable
= 0;
674 ret
->localhost_only
= local_host_only
;
675 ret
->pending_error
= 0;
680 s
= socket(AF_INET
, SOCK_STREAM
, 0);
683 if (s
== INVALID_SOCKET
) {
684 err
= WSAGetLastError();
685 ret
->error
= winsock_error_string(err
);
692 setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&on
, sizeof(on
));
696 if (addr
->family
== AF_INET6
) {
697 memset(&a6
, 0, sizeof(a6
));
698 a6
.sin6_family
= AF_INET6
;
700 a6
.sin6_addr
= in6addr_loopback
;
702 a6
.sin6_addr
= in6addr_any
;
703 a6
.sin6_port
= htons(port
);
707 a
.sin_family
= AF_INET
;
709 a
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
711 a
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
712 a
.sin_port
= htons((short)port
);
715 retcode
= bind(s
, (addr
->family
== AF_INET6 ?
716 (struct sockaddr
*) &a6
:
717 (struct sockaddr
*) &a
),
719 AF_INET6 ?
sizeof(a6
) : sizeof(a
)));
721 retcode
= bind(s
, (struct sockaddr
*) &a
, sizeof(a
));
723 if (retcode
!= SOCKET_ERROR
) {
726 err
= WSAGetLastError();
730 ret
->error
= winsock_error_string(err
);
735 if (listen(s
, SOMAXCONN
) == SOCKET_ERROR
) {
737 ret
->error
= winsock_error_string(err
);
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(s
, 1);
754 static void sk_tcp_close(Socket sock
)
756 extern char *do_select(SOCKET skt
, int startup
);
757 Actual_Socket s
= (Actual_Socket
) sock
;
766 * The function which tries to send on a socket once it's deemed
769 void try_send(Actual_Socket s
)
771 while (s
->sending_oob
|| bufchain_size(&s
->output_data
) > 0) {
777 if (s
->sending_oob
) {
778 urgentflag
= MSG_OOB
;
779 len
= s
->sending_oob
;
783 bufchain_prefix(&s
->output_data
, &data
, &len
);
785 nsent
= send(s
->s
, data
, len
, urgentflag
);
786 noise_ultralight(nsent
);
788 err
= (nsent
< 0 ?
WSAGetLastError() : 0);
789 if ((err
< WSABASEERR
&& nsent
< 0) || err
== WSAEWOULDBLOCK
) {
791 * Perfectly normal: we've sent all we can for the moment.
793 * (Some WinSock send() implementations can return
794 * <0 but leave no sensible error indication -
795 * WSAGetLastError() is called but returns zero or
796 * a small number - so we check that case and treat
797 * it just like WSAEWOULDBLOCK.)
801 } else if (nsent
== 0 ||
802 err
== WSAECONNABORTED
|| err
== WSAECONNRESET
) {
804 * If send() returns CONNABORTED or CONNRESET, we
805 * unfortunately can't just call plug_closing(),
806 * because it's quite likely that we're currently
807 * _in_ a call from the code we'd be calling back
808 * to, so we'd have to make half the SSH code
809 * reentrant. Instead we flag a pending error on
810 * the socket, to be dealt with (by calling
811 * plug_closing()) at some suitable future moment.
813 s
->pending_error
= err
;
816 logevent(winsock_error_string(err
));
817 fatalbox("%s", winsock_error_string(err
));
820 if (s
->sending_oob
) {
822 memmove(s
->oobdata
, s
->oobdata
+nsent
, len
-nsent
);
823 s
->sending_oob
= len
- nsent
;
828 bufchain_consume(&s
->output_data
, nsent
);
834 static int sk_tcp_write(Socket sock
, char *buf
, int len
)
836 Actual_Socket s
= (Actual_Socket
) sock
;
839 * Add the data to the buffer list on the socket.
841 bufchain_add(&s
->output_data
, buf
, len
);
844 * Now try sending from the start of the buffer list.
849 return bufchain_size(&s
->output_data
);
852 static int sk_tcp_write_oob(Socket sock
, char *buf
, int len
)
854 Actual_Socket s
= (Actual_Socket
) sock
;
857 * Replace the buffer list on the socket with the data.
859 bufchain_clear(&s
->output_data
);
860 assert(len
<= sizeof(s
->oobdata
));
861 memcpy(s
->oobdata
, buf
, len
);
862 s
->sending_oob
= len
;
865 * Now try sending from the start of the buffer list.
870 return s
->sending_oob
;
873 int select_result(WPARAM wParam
, LPARAM lParam
)
877 char buf
[20480]; /* nice big buffer for plenty of speed */
881 /* wParam is the socket itself */
882 s
= find234(sktree
, (void *) wParam
, cmpforsearch
);
884 return 1; /* boggle */
886 if ((err
= WSAGETSELECTERROR(lParam
)) != 0) {
888 * An error has occurred on this socket. Pass it to the
891 return plug_closing(s
->plug
, winsock_error_string(err
), err
, 0);
894 noise_ultralight(lParam
);
896 switch (WSAGETSELECTEVENT(lParam
)) {
898 s
->connected
= s
->writable
= 1;
901 /* In the case the socket is still frozen, we don't even bother */
903 s
->frozen_readable
= 1;
908 * We have received data on the socket. For an oobinline
909 * socket, this might be data _before_ an urgent pointer,
910 * in which case we send it to the back end with type==1
911 * (data prior to urgent).
915 ioctlsocket(s
->s
, SIOCATMARK
, &atmark
);
917 * Avoid checking the return value from ioctlsocket(),
918 * on the grounds that some WinSock wrappers don't
919 * support it. If it does nothing, we get atmark==1,
920 * which is equivalent to `no OOB pending', so the
921 * effect will be to non-OOB-ify any OOB data.
926 ret
= recv(s
->s
, buf
, sizeof(buf
), 0);
927 noise_ultralight(ret
);
929 err
= WSAGetLastError();
930 if (err
== WSAEWOULDBLOCK
) {
935 return plug_closing(s
->plug
, winsock_error_string(err
), err
,
937 } else if (0 == ret
) {
938 return plug_closing(s
->plug
, NULL
, 0, 0);
940 return plug_receive(s
->plug
, atmark ?
0 : 1, buf
, ret
);
945 * This will only happen on a non-oobinline socket. It
946 * indicates that we can immediately perform an OOB read
947 * and get back OOB data, which we will send to the back
948 * end with type==2 (urgent data).
950 ret
= recv(s
->s
, buf
, sizeof(buf
), MSG_OOB
);
951 noise_ultralight(ret
);
953 char *str
= (ret
== 0 ?
"Internal networking trouble" :
954 winsock_error_string(WSAGetLastError()));
958 return plug_receive(s
->plug
, 2, buf
, ret
);
963 int bufsize_before
, bufsize_after
;
965 bufsize_before
= s
->sending_oob
+ bufchain_size(&s
->output_data
);
967 bufsize_after
= s
->sending_oob
+ bufchain_size(&s
->output_data
);
968 if (bufsize_after
< bufsize_before
)
969 plug_sent(s
->plug
, bufsize_after
);
973 /* Signal a close on the socket. First read any outstanding data. */
976 ret
= recv(s
->s
, buf
, sizeof(buf
), 0);
978 err
= WSAGetLastError();
979 if (err
== WSAEWOULDBLOCK
)
981 return plug_closing(s
->plug
, winsock_error_string(err
),
985 open
&= plug_receive(s
->plug
, 0, buf
, ret
);
987 open
&= plug_closing(s
->plug
, NULL
, 0, 0);
993 struct sockaddr_in isa
;
994 int addrlen
= sizeof(struct sockaddr_in
);
995 SOCKET t
; /* socket of connection */
997 memset(&isa
, 0, sizeof(struct sockaddr_in
));
999 t
= accept(s
->s
,(struct sockaddr
*)&isa
,&addrlen
);
1000 if (t
== INVALID_SOCKET
)
1002 err
= WSAGetLastError();
1003 if (err
== WSATRY_AGAIN
)
1007 if (s
->localhost_only
&&
1008 ntohl(isa
.sin_addr
.s_addr
) != INADDR_LOOPBACK
) {
1009 closesocket(t
); /* dodgy WinSock let nonlocal through */
1010 } else if (plug_accepting(s
->plug
, (void*)t
)) {
1011 closesocket(t
); /* denied or error */
1020 * Deal with socket errors detected in try_send().
1022 void net_pending_errors(void)
1028 * This might be a fiddly business, because it's just possible
1029 * that handling a pending error on one socket might cause
1030 * others to be closed. (I can't think of any reason this might
1031 * happen in current SSH implementation, but to maintain
1032 * generality of this network layer I'll assume the worst.)
1034 * So what we'll do is search the socket list for _one_ socket
1035 * with a pending error, and then handle it, and then search
1036 * the list again _from the beginning_. Repeat until we make a
1037 * pass with no socket errors present. That way we are
1038 * protected against the socket list changing under our feet.
1042 for (i
= 0; (s
= index234(sktree
, i
)) != NULL
; i
++) {
1043 if (s
->pending_error
) {
1045 * An error has occurred on this socket. Pass it to the
1048 plug_closing(s
->plug
,
1049 winsock_error_string(s
->pending_error
),
1050 s
->pending_error
, 0);
1058 * Each socket abstraction contains a `void *' private field in
1059 * which the client can keep state.
1061 static void sk_tcp_set_private_ptr(Socket sock
, void *ptr
)
1063 Actual_Socket s
= (Actual_Socket
) sock
;
1064 s
->private_ptr
= ptr
;
1067 static void *sk_tcp_get_private_ptr(Socket sock
)
1069 Actual_Socket s
= (Actual_Socket
) sock
;
1070 return s
->private_ptr
;
1074 * Special error values are returned from sk_namelookup and sk_new
1075 * if there's a problem. These functions extract an error message,
1076 * or return NULL if there's no problem.
1078 char *sk_addr_error(SockAddr addr
)
1082 static char *sk_tcp_socket_error(Socket sock
)
1084 Actual_Socket s
= (Actual_Socket
) sock
;
1088 static void sk_tcp_set_frozen(Socket sock
, int is_frozen
)
1090 Actual_Socket s
= (Actual_Socket
) sock
;
1091 if (s
->frozen
== is_frozen
)
1093 s
->frozen
= is_frozen
;
1094 if (!is_frozen
&& s
->frozen_readable
) {
1096 recv(s
->s
, &c
, 1, MSG_PEEK
);
1098 s
->frozen_readable
= 0;
1102 * For Plink: enumerate all sockets currently active.
1104 SOCKET
first_socket(int *state
)
1108 s
= index234(sktree
, (*state
)++);
1109 return s ? s
->s
: INVALID_SOCKET
;
1112 SOCKET
next_socket(int *state
)
1114 Actual_Socket s
= index234(sktree
, (*state
)++);
1115 return s ? s
->s
: INVALID_SOCKET
;