Plug plug;
void *private_ptr;
bufchain output_data;
+ int connected;
int writable;
int frozen; /* this causes readability notifications to be ignored */
int frozen_readable; /* this means we missed at least one readability
return ret;
}
+void sk_getaddr(SockAddr addr, char *buf, int buflen)
+{
+#ifdef IPV6
+ if (addr->family == AF_INET) {
+#endif
+ struct in_addr a;
+ a.s_addr = htonl(addr->address);
+ strncpy(buf, inet_ntoa(a), buflen);
+#ifdef IPV6
+ } else {
+ FIXME; /* I don't know how to get a text form of an IPv6 address. */
+ }
+#endif
+}
+
void sk_addr_free(SockAddr addr)
{
sfree(addr);
ret->error = NULL;
ret->plug = plug;
bufchain_init(&ret->output_data);
- ret->writable = 1; /* to start with */
+ ret->connected = 0; /* to start with */
+ ret->writable = 0; /* to start with */
ret->sending_oob = 0;
ret->frozen = 0;
ret->frozen_readable = 0;
a.sin_addr.s_addr = htonl(addr->address);
a.sin_port = htons((short) port);
}
+
+ /* Set up a select mechanism. This could be an AsyncSelect on a
+ * window, or an EventSelect on an event object. */
+ errstr = do_select(s, 1);
+ if (errstr) {
+ ret->error = errstr;
+ return (Socket) ret;
+ }
+
if ((
#ifdef IPV6
connect(s, ((addr->family == AF_INET6) ?
#endif
) == SOCKET_ERROR) {
err = WSAGetLastError();
- ret->error = winsock_error_string(err);
- return (Socket) ret;
- }
-
- /* Set up a select mechanism. This could be an AsyncSelect on a
- * window, or an EventSelect on an event object. */
- errstr = do_select(s, 1);
- if (errstr) {
- ret->error = errstr;
- return (Socket) ret;
+ /*
+ * We expect a potential EWOULDBLOCK here, because the
+ * chances are the front end has done a select for
+ * FD_CONNECT, so that connect() will complete
+ * asynchronously.
+ */
+ if ( err != WSAEWOULDBLOCK ) {
+ ret->error = winsock_error_string(err);
+ return (Socket) ret;
+ }
+ } else {
+ /*
+ * If we _don't_ get EWOULDBLOCK, the connect has completed
+ * and we should set the socket as writable.
+ */
+ ret->writable = 1;
}
add234(sktree, ret);
return (Socket) ret;
}
-Socket sk_newlistenner(int port, Plug plug)
+Socket sk_newlistener(int port, Plug plug, int local_host_only)
{
static struct socket_function_table fn_table = {
sk_tcp_plug,
if (addr->family == AF_INET6) {
memset(&a6, 0, sizeof(a6));
a6.sin6_family = AF_INET6;
-/*a6.sin6_addr = in6addr_any; *//* == 0 */
+ if (local_host_only)
+ a6.sin6_addr = in6addr_loopback;
+ else
+ a6.sin6_addr = in6addr_any;
a6.sin6_port = htons(port);
} else
#endif
{
a.sin_family = AF_INET;
- a.sin_addr.s_addr = htonl(INADDR_ANY);
+ if (local_host_only)
+ a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ else
+ a.sin_addr.s_addr = htonl(INADDR_ANY);
a.sin_port = htons((short)port);
}
#ifdef IPV6
urgentflag = 0;
bufchain_prefix(&s->output_data, &data, &len);
}
-
nsent = send(s->s, data, len, urgentflag);
noise_ultralight(nsent);
if (nsent <= 0) {
err = (nsent < 0 ? WSAGetLastError() : 0);
- if ((err == 0 && nsent < 0) || err == WSAEWOULDBLOCK) {
+ if ((err < WSABASEERR && nsent < 0) || err == WSAEWOULDBLOCK) {
/*
* Perfectly normal: we've sent all we can for the moment.
*
- * (Apparently some WinSocks can return <0 but
- * leave no error indication - WSAGetLastError() is
- * called but returns zero - so we check that case
- * and treat it just like WSAEWOULDBLOCK.)
+ * (Some WinSock send() implementations can return
+ * <0 but leave no sensible error indication -
+ * WSAGetLastError() is called but returns zero or
+ * a small number - so we check that case and treat
+ * it just like WSAEWOULDBLOCK.)
*/
s->writable = FALSE;
return;
} else if (nsent == 0 ||
err == WSAECONNABORTED || err == WSAECONNRESET) {
/*
- * FIXME. This will have to be done better when we
- * start managing multiple sockets (e.g. SSH port
- * forwarding), because if we get CONNRESET while
- * trying to write a particular forwarded socket
- * then it isn't necessarily the end of the world.
- * Ideally I'd like to pass the error code back to
- * somewhere the next select_result() will see it,
- * but that might be hard. Perhaps I should pass it
- * back to be queued in the Windows front end bit.
+ * ASSUMPTION:
+ *
+ * I'm assuming here that if a TCP connection is
+ * reset or aborted once established, we will be
+ * notified by a select event rather than a
+ * CONNABORTED or CONNRESET from send(). In other
+ * words, I'm assuming CONNABORTED and CONNRESET
+ * don't come back from a _nonblocking_ send(),
+ * because the local side doesn't know they've
+ * happened until it waits for a response to its
+ * TCP segment - so the error will arrive
+ * asynchronously.
+ *
+ * If I'm wrong, this will be a really nasty case,
+ * because we can't necessarily call plug_closing()
+ * without having to make half the SSH code
+ * reentrant; so instead we'll have to schedule a
+ * call to plug_closing() for some suitable future
+ * time.
*/
- fatalbox(winsock_error_string(err));
+ fatalbox("SERIOUS NETWORK INTERNAL ERROR: %s\n"
+ "Please report this immediately to "
+ "<putty@projects.tartarus.org>.",
+ winsock_error_string(err));
} else {
fatalbox(winsock_error_string(err));
}
noise_ultralight(lParam);
switch (WSAGETSELECTEVENT(lParam)) {
+ case FD_CONNECT:
+ s->connected = s->writable = 1;
+ break;
case FD_READ:
/* In the case the socket is still frozen, we don't even bother */
if (s->frozen) {
return open;
case FD_ACCEPT:
{
- struct sockaddr isa;
- int addrlen = sizeof(struct sockaddr);
- SOCKET t; /* socket of connection */
-
- memset(&isa, 0, sizeof(struct sockaddr));
- err = 0;
- t = accept(s->s,&isa,&addrlen);
- if (t == INVALID_SOCKET)
- {
- err = WSAGetLastError();
- if (err == WSATRY_AGAIN)
- break;
- }
-
- if (plug_accepting(s->plug, &isa, (void*)t)) {
- closesocket(t); // denied or error
- }
+ struct sockaddr isa;
+ int addrlen = sizeof(struct sockaddr);
+ SOCKET t; /* socket of connection */
+
+ memset(&isa, 0, sizeof(struct sockaddr));
+ err = 0;
+ t = accept(s->s,&isa,&addrlen);
+ if (t == INVALID_SOCKET)
+ {
+ err = WSAGetLastError();
+ if (err == WSATRY_AGAIN)
+ break;
+ }
+
+ if (plug_accepting(s->plug, (void*)t)) {
+ closesocket(t); /* denied or error */
+ }
}
}