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 char *winsock_error_string(int error
)
131 return "Network error: Permission denied";
133 return "Network error: Address already in use";
134 case WSAEADDRNOTAVAIL
:
135 return "Network error: Cannot assign requested address";
136 case WSAEAFNOSUPPORT
:
138 "Network error: Address family not supported by protocol family";
140 return "Network error: Operation already in progress";
141 case WSAECONNABORTED
:
142 return "Network error: Software caused connection abort";
143 case WSAECONNREFUSED
:
144 return "Network error: Connection refused";
146 return "Network error: Connection reset by peer";
147 case WSAEDESTADDRREQ
:
148 return "Network error: Destination address required";
150 return "Network error: Bad address";
152 return "Network error: Host is down";
153 case WSAEHOSTUNREACH
:
154 return "Network error: No route to host";
156 return "Network error: Operation now in progress";
158 return "Network error: Interrupted function call";
160 return "Network error: Invalid argument";
162 return "Network error: Socket is already connected";
164 return "Network error: Too many open files";
166 return "Network error: Message too long";
168 return "Network error: Network is down";
170 return "Network error: Network dropped connection on reset";
172 return "Network error: Network is unreachable";
174 return "Network error: No buffer space available";
176 return "Network error: Bad protocol option";
178 return "Network error: Socket is not connected";
180 return "Network error: Socket operation on non-socket";
182 return "Network error: Operation not supported";
183 case WSAEPFNOSUPPORT
:
184 return "Network error: Protocol family not supported";
186 return "Network error: Too many processes";
187 case WSAEPROTONOSUPPORT
:
188 return "Network error: Protocol not supported";
190 return "Network error: Protocol wrong type for socket";
192 return "Network error: Cannot send after socket shutdown";
193 case WSAESOCKTNOSUPPORT
:
194 return "Network error: Socket type not supported";
196 return "Network error: Connection timed out";
198 return "Network error: Resource temporarily unavailable";
200 return "Network error: Graceful shutdown in progress";
202 return "Unknown network error";
206 SockAddr
sk_namelookup(char *host
, char **canonicalname
)
208 SockAddr ret
= smalloc(sizeof(struct SockAddr_tag
));
210 struct hostent
*h
= NULL
;
213 /* Clear the structure and default to IPv4. */
214 memset(ret
, 0, sizeof(struct SockAddr_tag
));
215 ret
->family
= 0; /* We set this one when we have resolved the host. */
218 if ((a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
221 /* Try to get the getaddrinfo() function from wship6.dll */
222 /* This way one doesn't need to have IPv6 dll's to use PuTTY and
223 * it will fallback to IPv4. */
224 typedef int (CALLBACK
* FGETADDRINFO
) (const char *nodename
,
225 const char *servname
,
226 const struct addrinfo
*
228 struct addrinfo
** res
);
229 FGETADDRINFO fGetAddrInfo
= NULL
;
231 HINSTANCE dllWSHIP6
= LoadLibrary("wship6.dll");
233 fGetAddrInfo
= (FGETADDRINFO
) GetProcAddress(dllWSHIP6
,
237 * Use fGetAddrInfo when it's available (which usually also
238 * means IPv6 is installed...)
241 /*debug(("Resolving \"%s\" with getaddrinfo() (IPv4+IPv6 capable)...\n", host)); */
242 if (fGetAddrInfo(host
, NULL
, NULL
, &ret
->ai
) == 0)
243 ret
->family
= ret
->ai
->ai_family
;
248 * Otherwise use the IPv4-only gethostbyname...
249 * (NOTE: we don't use gethostbyname as a
252 if (ret
->family
== 0) {
253 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
254 if ( (h
= gethostbyname(host
)) )
255 ret
->family
= AF_INET
;
258 /*debug(("Done resolving...(family is %d) AF_INET = %d, AF_INET6 = %d\n", ret->family, AF_INET, AF_INET6)); */
260 if (ret
->family
== 0) {
261 DWORD err
= WSAGetLastError();
262 ret
->error
= (err
== WSAENETDOWN ?
"Network is down" :
264 WSAHOST_NOT_FOUND ?
"Host does not exist" : err
265 == WSATRY_AGAIN ?
"Host not found" :
267 fGetAddrInfo ?
"getaddrinfo: unknown error" :
269 "gethostbyname: unknown error");
273 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
274 FORMAT_MESSAGE_FROM_SYSTEM
|
275 FORMAT_MESSAGE_IGNORE_INSERTS
, NULL
, err
,
276 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
277 (LPTSTR
) & lpMsgBuf
, 0, NULL
);
278 /*debug(("Error %ld: %s (h=%lx)\n", err, lpMsgBuf, h)); */
279 /* Free the buffer. */
287 /* If we got an address info use that... */
289 typedef int (CALLBACK
* FGETNAMEINFO
)
290 (const struct sockaddr FAR
* sa
, socklen_t salen
,
291 char FAR
* host
, size_t hostlen
, char FAR
* serv
,
292 size_t servlen
, int flags
);
293 FGETNAMEINFO fGetNameInfo
= NULL
;
295 /* Are we in IPv4 fallback mode? */
296 /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */
297 if (ret
->family
== AF_INET
)
299 (char *) &((SOCKADDR_IN
*) ret
->ai
->
300 ai_addr
)->sin_addr
, sizeof(a
));
302 /* Now let's find that canonicalname... */
305 (FGETNAMEINFO
) GetProcAddress(dllWSHIP6
,
308 ((struct sockaddr
*) ret
->ai
->ai_addr
,
310 AF_INET ?
sizeof(SOCKADDR_IN
) :
311 sizeof(SOCKADDR_IN6
), realhost
,
312 sizeof(realhost
), NULL
, 0, 0) != 0) {
313 strncpy(realhost
, host
, sizeof(realhost
));
317 /* We used the IPv4-only gethostbyname()... */
321 memcpy(&a
, h
->h_addr
, sizeof(a
));
322 /* This way we are always sure the h->h_name is valid :) */
323 strncpy(realhost
, h
->h_name
, sizeof(realhost
));
327 FreeLibrary(dllWSHIP6
);
331 * This must be a numeric IPv4 address because it caused a
332 * success return from inet_addr.
334 ret
->family
= AF_INET
;
335 strncpy(realhost
, host
, sizeof(realhost
));
337 ret
->address
= ntohl(a
);
338 realhost
[lenof(realhost
)-1] = '\0';
339 *canonicalname
= smalloc(1+strlen(realhost
));
340 strcpy(*canonicalname
, realhost
);
344 void sk_getaddr(SockAddr addr
, char *buf
, int buflen
)
347 if (addr
->family
== AF_INET
) {
350 a
.s_addr
= htonl(addr
->address
);
351 strncpy(buf
, inet_ntoa(a
), buflen
);
354 FIXME
; /* I don't know how to get a text form of an IPv6 address. */
359 void sk_addr_free(SockAddr addr
)
364 static Plug
sk_tcp_plug(Socket sock
, Plug p
)
366 Actual_Socket s
= (Actual_Socket
) sock
;
373 static void sk_tcp_flush(Socket s
)
376 * We send data to the socket as soon as we can anyway,
377 * so we don't need to do anything here. :-)
381 static void sk_tcp_close(Socket s
);
382 static int sk_tcp_write(Socket s
, char *data
, int len
);
383 static int sk_tcp_write_oob(Socket s
, char *data
, int len
);
384 static char *sk_tcp_socket_error(Socket s
);
386 extern char *do_select(SOCKET skt
, int startup
);
388 Socket
sk_register(void *sock
, Plug plug
)
390 static struct socket_function_table fn_table
= {
404 * Create Socket structure.
406 ret
= smalloc(sizeof(struct Socket_tag
));
410 bufchain_init(&ret
->output_data
);
411 ret
->writable
= 1; /* to start with */
412 ret
->sending_oob
= 0;
414 ret
->frozen_readable
= 0;
415 ret
->localhost_only
= 0; /* unused, but best init anyway */
416 ret
->pending_error
= 0;
418 ret
->s
= (SOCKET
)sock
;
420 if (ret
->s
== INVALID_SOCKET
) {
421 err
= WSAGetLastError();
422 ret
->error
= winsock_error_string(err
);
428 /* Set up a select mechanism. This could be an AsyncSelect on a
429 * window, or an EventSelect on an event object. */
430 errstr
= do_select(ret
->s
, 1);
441 Socket
sk_new(SockAddr addr
, int port
, int privport
, int oobinline
,
442 int nodelay
, Plug plug
)
444 static struct socket_function_table fn_table
= {
464 * Create Socket structure.
466 ret
= smalloc(sizeof(struct Socket_tag
));
470 bufchain_init(&ret
->output_data
);
471 ret
->connected
= 0; /* to start with */
472 ret
->writable
= 0; /* to start with */
473 ret
->sending_oob
= 0;
475 ret
->frozen_readable
= 0;
476 ret
->localhost_only
= 0; /* unused, but best init anyway */
477 ret
->pending_error
= 0;
482 s
= socket(addr
->family
, SOCK_STREAM
, 0);
485 if (s
== INVALID_SOCKET
) {
486 err
= WSAGetLastError();
487 ret
->error
= winsock_error_string(err
);
491 ret
->oobinline
= oobinline
;
494 setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, (void *) &b
, sizeof(b
));
499 setsockopt(s
, IPPROTO_TCP
, TCP_NODELAY
, (void *) &b
, sizeof(b
));
503 * Bind to local address.
506 localport
= 1023; /* count from 1023 downwards */
508 localport
= 0; /* just use port 0 (ie winsock picks) */
510 /* Loop round trying to bind */
515 if (addr
->family
== AF_INET6
) {
516 memset(&a6
, 0, sizeof(a6
));
517 a6
.sin6_family
= AF_INET6
;
518 /*a6.sin6_addr = in6addr_any; *//* == 0 */
519 a6
.sin6_port
= htons(localport
);
523 a
.sin_family
= AF_INET
;
524 a
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
525 a
.sin_port
= htons(localport
);
528 retcode
= bind(s
, (addr
->family
== AF_INET6 ?
529 (struct sockaddr
*) &a6
:
530 (struct sockaddr
*) &a
),
532 AF_INET6 ?
sizeof(a6
) : sizeof(a
)));
534 retcode
= bind(s
, (struct sockaddr
*) &a
, sizeof(a
));
536 if (retcode
!= SOCKET_ERROR
) {
540 err
= WSAGetLastError();
541 if (err
!= WSAEADDRINUSE
) /* failed, for a bad reason */
546 break; /* we're only looping once */
549 break; /* we might have got to the end */
553 ret
->error
= winsock_error_string(err
);
558 * Connect to remote address.
561 if (addr
->family
== AF_INET6
) {
562 memset(&a
, 0, sizeof(a
));
563 a6
.sin6_family
= AF_INET6
;
564 a6
.sin6_port
= htons((short) port
);
566 ((struct sockaddr_in6
*) addr
->ai
->ai_addr
)->sin6_addr
;
570 a
.sin_family
= AF_INET
;
571 a
.sin_addr
.s_addr
= htonl(addr
->address
);
572 a
.sin_port
= htons((short) port
);
575 /* Set up a select mechanism. This could be an AsyncSelect on a
576 * window, or an EventSelect on an event object. */
577 errstr
= do_select(s
, 1);
585 connect(s
, ((addr
->family
== AF_INET6
) ?
586 (struct sockaddr
*) &a6
: (struct sockaddr
*) &a
),
587 (addr
->family
== AF_INET6
) ?
sizeof(a6
) : sizeof(a
))
589 connect(s
, (struct sockaddr
*) &a
, sizeof(a
))
592 err
= WSAGetLastError();
594 * We expect a potential EWOULDBLOCK here, because the
595 * chances are the front end has done a select for
596 * FD_CONNECT, so that connect() will complete
599 if ( err
!= WSAEWOULDBLOCK
) {
600 ret
->error
= winsock_error_string(err
);
605 * If we _don't_ get EWOULDBLOCK, the connect has completed
606 * and we should set the socket as writable.
616 Socket
sk_newlistener(int port
, Plug plug
, int local_host_only
)
618 static struct socket_function_table fn_table
= {
639 * Create Socket structure.
641 ret
= smalloc(sizeof(struct Socket_tag
));
645 bufchain_init(&ret
->output_data
);
646 ret
->writable
= 0; /* to start with */
647 ret
->sending_oob
= 0;
649 ret
->frozen_readable
= 0;
650 ret
->localhost_only
= local_host_only
;
651 ret
->pending_error
= 0;
656 s
= socket(AF_INET
, SOCK_STREAM
, 0);
659 if (s
== INVALID_SOCKET
) {
660 err
= WSAGetLastError();
661 ret
->error
= winsock_error_string(err
);
668 setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&on
, sizeof(on
));
672 if (addr
->family
== AF_INET6
) {
673 memset(&a6
, 0, sizeof(a6
));
674 a6
.sin6_family
= AF_INET6
;
676 a6
.sin6_addr
= in6addr_loopback
;
678 a6
.sin6_addr
= in6addr_any
;
679 a6
.sin6_port
= htons(port
);
683 a
.sin_family
= AF_INET
;
685 a
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
687 a
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
688 a
.sin_port
= htons((short)port
);
691 retcode
= bind(s
, (addr
->family
== AF_INET6 ?
692 (struct sockaddr
*) &a6
:
693 (struct sockaddr
*) &a
),
695 AF_INET6 ?
sizeof(a6
) : sizeof(a
)));
697 retcode
= bind(s
, (struct sockaddr
*) &a
, sizeof(a
));
699 if (retcode
!= SOCKET_ERROR
) {
702 err
= WSAGetLastError();
706 ret
->error
= winsock_error_string(err
);
711 if (listen(s
, SOMAXCONN
) == SOCKET_ERROR
) {
713 ret
->error
= winsock_error_string(err
);
717 /* Set up a select mechanism. This could be an AsyncSelect on a
718 * window, or an EventSelect on an event object. */
719 errstr
= do_select(s
, 1);
730 static void sk_tcp_close(Socket sock
)
732 extern char *do_select(SOCKET skt
, int startup
);
733 Actual_Socket s
= (Actual_Socket
) sock
;
742 * The function which tries to send on a socket once it's deemed
745 void try_send(Actual_Socket s
)
747 while (s
->sending_oob
|| bufchain_size(&s
->output_data
) > 0) {
753 if (s
->sending_oob
) {
754 urgentflag
= MSG_OOB
;
755 len
= s
->sending_oob
;
759 bufchain_prefix(&s
->output_data
, &data
, &len
);
761 nsent
= send(s
->s
, data
, len
, urgentflag
);
762 noise_ultralight(nsent
);
764 err
= (nsent
< 0 ?
WSAGetLastError() : 0);
765 if ((err
< WSABASEERR
&& nsent
< 0) || err
== WSAEWOULDBLOCK
) {
767 * Perfectly normal: we've sent all we can for the moment.
769 * (Some WinSock send() implementations can return
770 * <0 but leave no sensible error indication -
771 * WSAGetLastError() is called but returns zero or
772 * a small number - so we check that case and treat
773 * it just like WSAEWOULDBLOCK.)
777 } else if (nsent
== 0 ||
778 err
== WSAECONNABORTED
|| err
== WSAECONNRESET
) {
780 * If send() returns CONNABORTED or CONNRESET, we
781 * unfortunately can't just call plug_closing(),
782 * because it's quite likely that we're currently
783 * _in_ a call from the code we'd be calling back
784 * to, so we'd have to make half the SSH code
785 * reentrant. Instead we flag a pending error on
786 * the socket, to be dealt with (by calling
787 * plug_closing()) at some suitable future moment.
789 s
->pending_error
= err
;
792 fatalbox(winsock_error_string(err
));
795 if (s
->sending_oob
) {
797 memmove(s
->oobdata
, s
->oobdata
+nsent
, len
-nsent
);
798 s
->sending_oob
= len
- nsent
;
803 bufchain_consume(&s
->output_data
, nsent
);
809 static int sk_tcp_write(Socket sock
, char *buf
, int len
)
811 Actual_Socket s
= (Actual_Socket
) sock
;
814 * Add the data to the buffer list on the socket.
816 bufchain_add(&s
->output_data
, buf
, len
);
819 * Now try sending from the start of the buffer list.
824 return bufchain_size(&s
->output_data
);
827 static int sk_tcp_write_oob(Socket sock
, char *buf
, int len
)
829 Actual_Socket s
= (Actual_Socket
) sock
;
832 * Replace the buffer list on the socket with the data.
834 bufchain_clear(&s
->output_data
);
835 assert(len
<= sizeof(s
->oobdata
));
836 memcpy(s
->oobdata
, buf
, len
);
837 s
->sending_oob
= len
;
840 * Now try sending from the start of the buffer list.
845 return s
->sending_oob
;
848 int select_result(WPARAM wParam
, LPARAM lParam
)
852 char buf
[20480]; /* nice big buffer for plenty of speed */
856 /* wParam is the socket itself */
857 s
= find234(sktree
, (void *) wParam
, cmpforsearch
);
859 return 1; /* boggle */
861 if ((err
= WSAGETSELECTERROR(lParam
)) != 0) {
863 * An error has occurred on this socket. Pass it to the
866 return plug_closing(s
->plug
, winsock_error_string(err
), err
, 0);
869 noise_ultralight(lParam
);
871 switch (WSAGETSELECTEVENT(lParam
)) {
873 s
->connected
= s
->writable
= 1;
876 /* In the case the socket is still frozen, we don't even bother */
878 s
->frozen_readable
= 1;
883 * We have received data on the socket. For an oobinline
884 * socket, this might be data _before_ an urgent pointer,
885 * in which case we send it to the back end with type==1
886 * (data prior to urgent).
890 ioctlsocket(s
->s
, SIOCATMARK
, &atmark
);
892 * Avoid checking the return value from ioctlsocket(),
893 * on the grounds that some WinSock wrappers don't
894 * support it. If it does nothing, we get atmark==1,
895 * which is equivalent to `no OOB pending', so the
896 * effect will be to non-OOB-ify any OOB data.
901 ret
= recv(s
->s
, buf
, sizeof(buf
), 0);
902 noise_ultralight(ret
);
904 err
= WSAGetLastError();
905 if (err
== WSAEWOULDBLOCK
) {
910 return plug_closing(s
->plug
, winsock_error_string(err
), err
,
912 } else if (0 == ret
) {
913 return plug_closing(s
->plug
, NULL
, 0, 0);
915 return plug_receive(s
->plug
, atmark ?
0 : 1, buf
, ret
);
920 * This will only happen on a non-oobinline socket. It
921 * indicates that we can immediately perform an OOB read
922 * and get back OOB data, which we will send to the back
923 * end with type==2 (urgent data).
925 ret
= recv(s
->s
, buf
, sizeof(buf
), MSG_OOB
);
926 noise_ultralight(ret
);
928 fatalbox(ret
== 0 ?
"Internal networking trouble" :
929 winsock_error_string(WSAGetLastError()));
931 return plug_receive(s
->plug
, 2, buf
, ret
);
936 int bufsize_before
, bufsize_after
;
938 bufsize_before
= s
->sending_oob
+ bufchain_size(&s
->output_data
);
940 bufsize_after
= s
->sending_oob
+ bufchain_size(&s
->output_data
);
941 if (bufsize_after
< bufsize_before
)
942 plug_sent(s
->plug
, bufsize_after
);
946 /* Signal a close on the socket. First read any outstanding data. */
949 ret
= recv(s
->s
, buf
, sizeof(buf
), 0);
951 err
= WSAGetLastError();
952 if (err
== WSAEWOULDBLOCK
)
954 return plug_closing(s
->plug
, winsock_error_string(err
),
958 open
&= plug_receive(s
->plug
, 0, buf
, ret
);
960 open
&= plug_closing(s
->plug
, NULL
, 0, 0);
966 struct sockaddr_in isa
;
967 int addrlen
= sizeof(struct sockaddr_in
);
968 SOCKET t
; /* socket of connection */
970 memset(&isa
, 0, sizeof(struct sockaddr_in
));
972 t
= accept(s
->s
,(struct sockaddr
*)&isa
,&addrlen
);
973 if (t
== INVALID_SOCKET
)
975 err
= WSAGetLastError();
976 if (err
== WSATRY_AGAIN
)
980 if (s
->localhost_only
&&
981 ntohl(isa
.sin_addr
.s_addr
) != INADDR_LOOPBACK
) {
982 closesocket(t
); /* dodgy WinSock let nonlocal through */
983 } else if (plug_accepting(s
->plug
, (void*)t
)) {
984 closesocket(t
); /* denied or error */
993 * Deal with socket errors detected in try_send().
995 void net_pending_errors(void)
1001 * This might be a fiddly business, because it's just possible
1002 * that handling a pending error on one socket might cause
1003 * others to be closed. (I can't think of any reason this might
1004 * happen in current SSH implementation, but to maintain
1005 * generality of this network layer I'll assume the worst.)
1007 * So what we'll do is search the socket list for _one_ socket
1008 * with a pending error, and then handle it, and then search
1009 * the list again _from the beginning_. Repeat until we make a
1010 * pass with no socket errors present. That way we are
1011 * protected against the socket list changing under our feet.
1015 for (i
= 0; (s
= index234(sktree
, i
)) != NULL
; i
++) {
1016 if (s
->pending_error
) {
1018 * An error has occurred on this socket. Pass it to the
1021 plug_closing(s
->plug
,
1022 winsock_error_string(s
->pending_error
),
1023 s
->pending_error
, 0);
1031 * Each socket abstraction contains a `void *' private field in
1032 * which the client can keep state.
1034 void sk_set_private_ptr(Socket sock
, void *ptr
)
1036 Actual_Socket s
= (Actual_Socket
) sock
;
1037 s
->private_ptr
= ptr
;
1040 void *sk_get_private_ptr(Socket sock
)
1042 Actual_Socket s
= (Actual_Socket
) sock
;
1043 return s
->private_ptr
;
1047 * Special error values are returned from sk_namelookup and sk_new
1048 * if there's a problem. These functions extract an error message,
1049 * or return NULL if there's no problem.
1051 char *sk_addr_error(SockAddr addr
)
1055 static char *sk_tcp_socket_error(Socket sock
)
1057 Actual_Socket s
= (Actual_Socket
) sock
;
1061 void sk_set_frozen(Socket sock
, int is_frozen
)
1063 Actual_Socket s
= (Actual_Socket
) sock
;
1064 if (s
->frozen
== is_frozen
)
1066 s
->frozen
= is_frozen
;
1067 if (!is_frozen
&& s
->frozen_readable
) {
1069 recv(s
->s
, &c
, 1, MSG_PEEK
);
1071 s
->frozen_readable
= 0;
1075 * For Plink: enumerate all sockets currently active.
1077 SOCKET
first_socket(int *state
)
1081 s
= index234(sktree
, (*state
)++);
1082 return s ? s
->s
: INVALID_SOCKET
;
1085 SOCKET
next_socket(int *state
)
1087 Actual_Socket s
= index234(sktree
, (*state
)++);
1088 return s ? s
->s
: INVALID_SOCKET
;